POJ2506 Tiling

Tiling

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.

 

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

 

题目大意:

本题问我们,用2*1和2*2的图形拼成2*n的图形有几种方法。

解题思路:

首先我们用f(n)表示2*n情况下的方法数。

这是一道递推问题,仔细分析可知f(n)只和f(n-1)以及f(n-2)有关。

  1. f(n)由f(n-1)的基础上加一块2*1得来,因此有1*f(n-1)种方法。
  2. f(n)由f(n-2)的基础上加两块2*1得来或者一块2*2得来,但由于其中一种2*1的方法与上面有重复因此有2*f(n-1)种方法。

最终递推公式:f(n) = f(n-1)+2*f(n-2)

接下来需要注意的是用数组处理大数。

代码:

#include<iostream>
#include<stdio.h>
using namespace std;

struct Arr{
    int num[10000];
}arr[260];


//f(n) = f(n-1)+2*f(n-2)
void BigDigit()          //大数处理,初始化结构体Arr
{
    for(int i = 2;i<260;i++)
    {
        int c = 0;  //进位位
        for(int j = 0;j<10000;j++)
        {
            arr[i].num[j] = arr[i-1].num[j]+2*arr[i-2].num[j]+c;
            c = arr[i].num[j]/10;
            arr[i].num[j]%=10;
        }
    }
}

int main()
{
    arr[0].num[0] = 1;
    arr[1].num[0] = 1;
    BigDigit();
    int n;
    while(~scanf("%d",&n))
    {
        int j;
        for(j = 10000-1;arr[n].num[j]==0;j--);
        for(j;j>=0;j--)
            printf("%d",arr[n].num[j]);
        cout<<endl;
    }
    return 0;
}

 

 

posted @ 2019-09-07 10:59  Hu_YaYa  阅读(37)  评论(0)    收藏  举报