首页 > 其他 > 详细

poj_2506_Tiling_201407211555

时间:2014-07-22 00:09:03      阅读:291      评论:0      收藏:0      [点我收藏+]
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. 
bubuko.com,布布扣

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

 
bubuko.com,布布扣
 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("\n");                
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(暂时没搞明白为什么,请大神指教)。

poj_2506_Tiling_201407211555,布布扣,bubuko.com

poj_2506_Tiling_201407211555

原文:http://www.cnblogs.com/xl1027515989/p/3858740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!