Tiling
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 6314 Accepted: 3370
Description
In how many ways can you tile a 3xn rectangle with 2x1 dominoes?
Here is a sample tiling of a 3x12 rectangle.
Input
Input consists of several test cases followed by a line containing-1. Each test case is a line containing an integer 0 <= n <= 30.
Output
For each test case, output one integer number giving the number of possible tilings.
Sample Input
2
8
12
-1
Sample Output
3
153
2131
Source
Waterloo local 2005.09.24
Exercise thinking questions.
If n is an odd number, it must be 0, and n is an even number. Two columns are added each time. We can take the two columns as one column, if this column is separated from the front, there are only three methods, namely 3 * a [N-2]. If this column is not separated from the front, so there are only two kinds of non-decomposed rectangle, SO 2 * (a [n-4] + a [n-6] + ...... A [0])
Simplification is a [n] = 4 * a [N-2]-a [n-4]
[Cpp]
# Include <iostream>
# Include <cstdlib>
# Include <stdio. h>
# Define ll long
Using namespace std;
Ll a [31];
Int main ()
{
Ll n;
A [0] = 1; a [2] = 3;
For (int I = 4; I <= 30; I + = 2)
A [I] = 4 * a [I-2]-a [I-4];
While (scanf ("% lld", & n ))
{
If (n =-1) break;
Printf ("% lld \ n", a [n]);
}
}