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.
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 }
作者:ACShiryu
出处:http://www.cnblogs.com/ACShiryu/
若非注明,本博客文章均为原创,版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
该文章也同步发布在我的新浪微博中-ACShiryu's weibo,欢迎收听。