Tiling
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:7452 |
|
Accepted:3639 |
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
The question is there are two types of floor, 2x1 and 2X2, now we want to fight 2 x n shape, there are many cases.
Recursive thinking:
If we have already laid 2 x (n-1), we can only use 2x1 floors if we want to lay 2 x n.
Suppose we have paved 2 x (n-2), then to spread to 2 x N, you can choose 1 2x2 or two 2x1, there may be three paving methods
It should be noted that the third will repeat the situation where 2 x (n-1) is paved, so it is not advisable to obtain the recursive formula.
A [I] = 2 * A [I-2] + A [I-1];
It is also a big number problem .. It is particularly refreshing to use Java .. Therefore, this question is also written in Java.
Import Java. math. biginteger; import Java. util. extends; public class main {public static void main (string [] ARGs) {using CIN = new using (system. in); int N; biginteger [] A = new biginteger [300]; A [0] = biginteger. one; A [1] = biginteger. one; A [2] = biginteger. valueof (3); For (INT I = 2; I <. length; I ++) {A [I] = A [I-1]. add (A [I-2]. multiply (biginteger. valueof (2);} while (CIN. hasnext () {n = cin. nextint (); system. out. println (A [n]) ;}}