Tiling
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 7487 |
|
Accepted: 3661 |
Description
In what 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 Rectan Gle.
Sample Input
2812100200
Sample Output
317127318451004001521529343311354702511071292029505993517027974728227441735014801995855195223534251
Source
The UofA Local 2000.10.14
Problem Solving Ideas:
There are two kinds of rectangles, 1*2 (length 1. Height 2), 2*2. Given a large rectangle of n*2, ask two sizes of rectangles to fill it up, and how many ways together.
If the number of methods with length n-1 has been obtained. Then, to fill N, there is only one way. With 1*2
If the number of methods with length n-2 has been obtained, then fill N. There are three ways
But the third one will be repeated and removed with the number of methods that have been obtained for length n-1.
So the recursive equation obtained is f [0] =1 f[1] = 1 F[2] =3 f[n]= f[n-2]*2 + f[n-1]
It is important to note that a large number is used in the subject.
Code:
#include <iostream> #include <string.h>using namespace std;string s1,s2;int a[1000],b[1000],c[1000];//a, B Save two strings to get a large number. C Save the large number of string f[300];string Add (string s1,string s2)//To add the large number S1,S2. and returns the result of the string type {memset (a,0,sizeof (a)); Memset (b,0,sizeof (b)); Memset (C,0,sizeof (c)); string result; int Lena=s1.length (); int Lenb=s2.length (); int k=0; for (int i=lena-1;i>=0;i--) a[k++]=s1[i]-' 0 '; k=0; for (int j=lenb-1;j>=0;j--) b[k++]=s2[j]-' 0 '; int Len=lena>lenb?Lena:lenb; for (int i=0;i<len;i++) {c[i]+=a[i]+b[i];//Note is + =, also consider rounding if (c[i]>=10) {c[i+1]++; c[i]-=10; }} int i; for (i=999;i>=0;i--) if (c[i]!=0) break; for (; i>=0;i--) result+= (char) (c[i]+ ' 0 '); return result;} void Get () {f[0]= "1"; f[1]= "1"; f[2]= "3"; f[3]= "5"; for (int i=4;i<=250;i++) {f[i]=add (f[i-2],f[i-2]); F[i]=add (F[i],f[i-1]); }}int Main () {get (); int n; while (cin>>n) {cout<<f[n]<<endl; } return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
[ACM] POJ 2506 Tiling (recursive, eyelid plate)