Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total 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.
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
Source
解题报告
推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;
}