码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2506 (Tiling)

时间:2014-11-10 21:38:02      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

                                     Tiling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7755   Accepted: 3770

Description

In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle.
bubuko.com,布布扣

Input

Input is a sequence of lines, each line containing an integer number 0 <= n <= 250.

Output

For each line of input, output one integer number in a separate line giving the number of possible tilings of a 2xn rectangle.

Sample Input

2
8
12
100
200

Sample Output

3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251

Source

 
刚拿到这个题的时候,觉得并不是非常难,就是简单的递推,公式很快的就找出来了. p = a[i - 2][j] * 2 + a[i - 1][j] + k.
难点有两个,大数处理 和 n=0时,输出的是1 !!!!! (至今不知道为什么!!!)
代码:
#include <stdio.h>
#include <string.h>
int a[310][310];
int main()
{
    int n, i, j, s, k, p;
    while (scanf("%d", &n) != EOF)
    {
        memset(a, 0, sizeof(a));
        a[0][0] = 1;//初始化
        a[1][0] = 1;
        a[2][0] = 3;
        if (n <= 2)
        {
            printf("%d\n", a[n][0]);
        }
        else
        {
            s = 1;
            for (i = 3; i <= n; ++i)
            {
                k = 0; //记录是否超过十
                p = 0;
                for (j = 0; j < s; ++j)
                {
                    p = a[i - 2][j] * 2 + a[i - 1][j] + k;
                    a[i][j] = p % 10;
                    k = p / 10;
                }
                if (k)//位数增加一位时的处理
                {
                    a[i][s] = k;
                    s ++;
                }
            }
            for (i = s - 1; i >= 0; --i)
            {
                printf("%d", a[n][i]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

POJ2506 (Tiling)

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/ikids/p/4087992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!