Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8305 | Accepted: 4004 |
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.
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
代码:
#include<cstdio> #include<cstring> #include<cstdlib> int n; int f[300][100]; void fib() { int i,j,k=0; f[0][1]=0; f[1][1]=1; f[2][1]=3; f[3][1]=5; for(i=4;i<=250;i++)//大数相加的精华部分,基本上所有的大数相加都要用到这一步。 { for(j=1;j<=100;j++) { k+=f[i-1][j]+2*f[i-2][j]; f[i][j]=k%10; k=k/10; } } } int main() { while(scanf("%d",&n)!=EOF) { int i,j; memset(f,0,sizeof(f)); fib(); if(n<=0||n>250) { printf("1\n"); continue; } for( i=100;i>=1;i--) if(f[n][i]!=0) break; for(j=i;j>=1;j--) printf("%d",f[n][j]); printf("\n"); } return 0; }