Tiling
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*n的棋盘,用三种方块(2*2,1*2,2*1)将其铺满,为有多少种可能性。
解题思路:显然f(n)=f(n-1)+f(n-2)*2简单递推就可以。。 但是看样例就知道longlong也存不开。用string型来进行大数相加
Code:
1 #include<cstdio> 2 #include<string> 3 #include<iostream> 4 using namespace std; 5 string a[300]; 6 string Add(string s1,string s2) 7 { 8 if (s1.length()<s2.length()) 9 swap(s1,s2); 10 int i,j; 11 for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) 12 { 13 s1[i]=s1[i]+(j>=0?s2[j]-'0':0); 14 if(s1[i]-'0'>=10) 15 { 16 s1[i]=(s1[i]-'0')%10+'0'; 17 if(i) s1[i-1]++; 18 else s1='1'+s1; 19 } 20 } 21 return s1; 22 } 23 int main() 24 { 25 int i,n; 26 a[0]="1",a[1]="1",a[2]="3"; 27 for (i=3; i<=250; i++) 28 a[i]=Add(Add(a[i-1],a[i-2]),a[i-2]); 29 while (cin>>n) 30 cout<<a[n]<<endl; 31 return 0; 32 }