POJ

来源:互联网 发布:java中类与对象的关系 编辑:程序博客网 时间:2024/06/05 02:39

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

a[n]=2*a[n-2]+a[n-1];


#include <algorithm>#include <string.h>#include <iostream>#include <stdio.h>#include <string>#include <vector>#include <queue>#include <map>#include <set>using namespace std;typedef long long LL;const int N = 2010;int a[300][300];int main(){    memset(a,0,sizeof(a));    a[0][0]=1, a[1][0]=1,a[2][0]=3;    int n;    while(scanf("%d", &n)!=EOF)    {        if(n==2) cout<<3<<endl;        else        {            int digit=1;            for(int i=3; i<=n; i++)            {                int sum=0;                for(int j=0; j<digit; j++)                {                    a[i][j]=(sum+a[i-1][j]+2*a[i-2][j])%10000;                    sum=(sum+a[i-1][j]+2*a[i-2][j])/10000;                }                if(sum!=0)                {                    a[i][digit++]=sum;                }            }            cout<<a[n][digit-1];            for(int i=digit-2; i>=0; i--) printf("%04d",a[n][i]);            cout<<endl;        }    }    return 0;}




0 0
原创粉丝点击