Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8319 | Accepted: 4012 |
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
题目大意:计算要铺一个2xn的地板,只能用2x1,2x2的瓷砖,问有几种方法铺这个地板。
考查点:高精度加法。
核心思想:通过枚举一些情况,我们可以得到一个规律,2xn=2x(n-1)+2倍的 2x(n-2);接下来我们就可以用一个二维数组存储n从0到250每一种情况的答案。
代码如下:
#include<stdio.h>
#include<string.h>
int a[260][1000];
int main()
{
int n,m,p,i,j,l;
memset(a,0,sizeof(a));
a[0][0]=1;a[1][0]=1;
a[2][0]=3;a[3][0]=5;p=0;
int k=1;
for(i=4;i<=250;i++)
{
p=0;//p表示进位
l=k;
for(j=0;j<l;j++)
{
a[i][j]=2*(a[i-2][j])+a[i-1][j]+p;
p=a[i][j]/10;
a[i][j]=a[i][j]%10;
}
while(p)
{
a[i][j]=p%10;
p=p/10;
j++;
}
k=j;
}
while(scanf("%d",&n)!=EOF)
{
for(i=80;i>=0;i--)
{
if(a[n][i]!=0)break;
}
for(j=i;j>=0;j--)
{
printf("%d",a[n][j]);
}
printf("\n");
}
return 0;
}