HDU2506 Tiling (高精度+递推)

来源:互联网 发布:ipad上不能看淘宝卖家 编辑:程序博客网 时间:2024/04/29 17:30

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
分析: 手写可以写出前三项 分别为 1,3,5;
从第四项开始 我们可以发现 
假设已经求得了 长度为n-1 的方法数,那么要铺满n,只有一种方法,用1*2
假设已经求得了 长度为n-2 的方法数,那么要铺满n,有三种方法,用1*2,1*1(2);
由于n-1的时候已经包括了n-2的时候 所以
和起来的公式为: a[n]=a[n-1]+2*a[n-2];
数据比较大,需要用高精度进行处理
代码如下:
#include <iostream>#include <cstring>using namespace std;void Bigadd(char a[],char b[],char c[]){    int n=strlen(a);    int m=strlen(b);    char temp;    int i,j,e,d;    for(i=0; i<m/2; i++)    {        temp=b[i];        b[i]=b[m-1-i];        b[m-1-i]=temp;    }    for(i=0; i<n/2; i++)    {        temp=a[i];        a[i]=a[n-1-i];        a[n-1-i]=temp;    }        d=0;        e=0;    for(i=0;i<n&&i<m;i++)    {        d=a[i]-'0'+b[i]-'0'+e;        c[i]=d%10+'0';        e=d/10;    }    if(i==m)    {        for(;i<n;i++)        {            d=a[i]-'0'+e;            c[i]=d%10+'0';            e=d/10;        }    }    if(i==n)    {        for(;i<m;i++)        {            d=b[i]-'0'+e;            c[i]=d%10+'0';            e=d/10;        }    }    if(e)        c[i++]=e+'0';    int t=i;    for(i=0; i<t/2; i++)    {        temp=c[i];        c[i]=c[t-1-i];        c[t-1-i]=temp;    }    for(i=0; i<m/2; i++)    {        temp=b[i];        b[i]=b[m-1-i];        b[m-1-i]=temp;    }    for(i=0; i<n/2; i++)    {        temp=a[i];        a[i]=a[n-1-i];        a[n-1-i]=temp;    }}int main(){    int n;    while(cin>>n){        char a1[3000]="1";        char a2[3000]="3";        char a3[3000]="5";        char tmp1[3000],tmp3[3000],tmp[3000];        for(int i=4;i<=n;i++){            memset(tmp1,0,sizeof(tmp1));            memset(tmp3,0,sizeof(tmp3));            strcpy(tmp,a2);            Bigadd(a2,tmp,tmp1);            Bigadd(a3,tmp1,tmp3);            strcpy(a2,a3);            strcpy(a3,tmp3);        }        if(n==1||n==0){            cout<<"1"<<endl;            continue;        }        if(n==2){            cout<<"3"<<endl;            continue;        }        cout<<a3<<endl;    }    return 0;}


0 0