poj_2506_Tiling_201407211555

Tiling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7509   Accepted: 3672

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

2
8
12
100
200

Sample Output

3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251

Source

 
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAX 1000
 4 int s[300][MAX];
 5 int main()
 6 {
 7     int i,j,k,t,n,len=0;
 8     memset(s,0,sizeof(s));
 9     s[0][0]=1;
10     s[1][0]=1;
11     s[2][0]=3;
12     for(i=3;i<=300;i++)
13     {
14         t = 0;
15         for(j=0;j<=len;j++)
16         {
17             s[i][j]=s[i-1][j]+2*s[i-2][j]+t;
18             if(s[i][j]>9)
19             {
20                 t=s[i][j]/10;
21                 s[i][j]%=10;                
22             }
23             else
24             t = 0;
25             if(j==len&&t==1)
26             len++;
27         }
28     }
29     while(scanf("%d",&n)!=EOF)
30     {
31         for(i=MAX-1;i>0&&(s[n][i]==0);i--);
32         for(;i>=0;i--)
33         printf("%d",s[n][i]);
34         printf("
");                
35     }
36     return 0;
37 }
View Code
首先,无论任何一种方案,最左边的要么是一根竖的,要么是两根横的,要么是一个方的,对吧?所以:
当是一根竖时剩下的方案数是f[i-1]
当是两根横时剩下的方案数是f[i-2]
当是一个方时剩下的方案数是f[i-2]
故f[i]=f[i-1]+2*f[i-2]

//递推+大数

注意:0的时候输出1(暂时没搞明白为什么,请大神指教)。
原文地址:https://www.cnblogs.com/xl1027515989/p/3858740.html