Tiling
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:7487 |
|
Accepted:3661 |
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
2812100200
Sample output
317127318451004001521529343311354702511071292029505993517027974728227441735014801995855195223534251
Source
The uofa local 2000.10.14
Solution:
There are two kinds of rectangles, 1*2 (length 1, height 2), 2*2. given a n * 2 large rectangle, I would like to ask how many methods are there to fill it with two types of rectangles.
Suppose we have obtained the number of methods with a length of N-1, so we need to overwrite n. There is only one method, with 1*2
Assuming that the number of methods whose length is N-2 has been obtained, there are three methods to overwrite n.
However, the third method will repeat the number of methods that have obtained the length of N-1 and remove it.
So the recursive equation obtained is f [0] = 1 f [1] = 1 f [2] = 3 F [N] = f [N-2] * 2 + F [n-1].
Note that large numbers are used in this question.
Code:
# Include <iostream> # include <string. h> using namespace STD; string S1, S2; int A [1000], B [1000], C [1000]; // A and B Save the large numbers of the two strings, c. Save the large numbers string f [300] After adding A and B; string add (string S1, string S2) // Add the large numbers S1 and S2, returns the string type results {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 that it is + =. Also consider carrying 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 ;}