POJ训练计划2506_Tiling(递推)

来源:互联网 发布:知乎 四个意识 编辑:程序博客网 时间:2024/05/17 07:38
Tiling
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7352 Accepted: 3584

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

2812100200

Sample Output

317127318451004001521529343311354702511071292029505993517027974728227441735014801995855195223534251

Source

The UofA Local 2000.10.14
解题报告
推n的方式是先推出n-1,然后加一个2*1的矩形,或者推出n-2,然后考虑2*2的矩形有3种情况,其中一种会和前面的重复,去掉就行。
其实想知道为什么F[0]=1而不是0;
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char f[301][10000]= {"1","1","3"};int main(){    int i,j,a[10000],b[10000];    for(i=3; i<=250; i++)    {        int k=0;        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        int l1=strlen(f[i-1]);        int l2=strlen(f[i-2]);        for(j=0; j<l1; j++)            a[j]=f[i-1][l1-j-1]-'0';        for(j=0; j<l2; j++)            b[j]=f[i-2][l2-j-1]-'0';        for(j=0; j<max(l1,l2); j++)        {            a[j]=2*b[j]+a[j];            if(a[j]>=10)            {                a[j+1]+=a[j]/10;                a[j]%=10;            }        }        for(j=9000; j>=0; j--)            if(a[j]!=0)break;        for(; j>=0; j--)            f[i][k++]=a[j]+'0';        f[i][k]=0;    }    int n;    while(cin>>n)    {        cout<<f[n]<<endl;    }    return 0;}


0 0