永夜初晗凝碧天

本博客现已全部转移到新地址,欲获取更多精彩文章,请访问http://acshiryu.github.io/

导航

POJ 2506 高精度+递推

Tiling
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5694 Accepted: 2768

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
题目大意就是有2×1和2×2两种规格的地板,现要拼2×n的形状,共有多少种情况,首先要做这道题目要先对递推有一定的了解。
假设我们已经铺好了2×(n-1)的情形,则要铺到2×n则只能用2×1的地板
假设我们已经铺好了2×(n-2)的情形,则要铺到2×n则可以选择1个2×2或两个2×1,故可能有下列三种铺法

    

其中要注意到第三个会与铺好2×(n-1)的情况重复,故不可取,故可以得到递推式

a[i]=2*a[i-2]+a[i-1];

然后就是高精度部分,可直接用高精度的模板

直接套用模板就1A了,只是简单的递推题,算是练习套模板能力或验证模板的正确性吧!

参考代码:

 1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<string>
5 #include<algorithm>
6 #include<cmath>
7 #include <deque>
8 #include <vector>
9 using namespace std;
10
11 const int Base=1000000000;
12 const int Capacity=100;
13 typedef long long huge;
14
15 struct BigInt{
16 int Len;
17 int Data[Capacity];
18 BigInt() : Len(0) {}
19 BigInt (const BigInt &V) : Len(V.Len) { memcpy (Data, V.Data, Len*sizeof*Data);}
20 BigInt(int V) : Len(0) {for(;V>0;V/=Base) Data[Len++]=V%Base;}
21 BigInt &operator=(const BigInt &V) {Len=V.Len; memcpy(Data, V.Data, Len*sizeof*Data); return *this;}
22 int &operator[] (int Index) {return Data[Index];}
23 int operator[] (int Index) const {return Data[Index];}
24 };
25
26 BigInt operator+(const BigInt &A,const BigInt &B){
27 int i,Carry(0);
28 BigInt R;
29 for(i=0;i<A.Len||i<B.Len||Carry>0;i++){
30 if(i<A.Len) Carry+=A[i];
31 if(i<B.Len) Carry+=B[i];
32 R[i]=Carry%Base;
33 Carry/=Base;
34 }
35 R.Len=i;
36 return R;
37 }
38
39
40
41
42 ostream &operator<<(ostream &Out,const BigInt &V){
43 int i;
44 Out<<(V.Len==0 ? 0:V[V.Len-1]);
45 for(i=V.Len-2;i>=0;i--) for(int j=Base/10;j>0;j/=10) Out<<V[i]/j%10;
46 return Out;
47 }
48
49 BigInt ans[300];
50
51 int main()
52 {
53 ans[0]=1;
54 ans[1]=1;
55 int i;
56 for ( i = 2 ; i <= 250 ; i ++ )
57 {
58 ans[i]=ans[i-2]+ans[i-1]+ans[i-2];
59 }
60 int n ;
61 while ( cin >> n )
62 cout<<ans[n]<<endl;
63 return 0;
64 }

  

posted on 2011-08-06 15:15  ACShiryu  阅读(1847)  评论(0编辑  收藏  举报