Tri Tiling
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 2118 Accepted Submission (s): 1211
Problem DescriptionIn How many ways can do tile a 3xn rectangle with 2x1 dominoes? Here is a sample tiling of a 3x12 rectangle.
Inputinput consists of several test cases followed by a line containing-1. Each test case was a line containing an integer 0≤n≤30.
Outputfor each test case, output one integer number giving the number of possible tilings.
Sample Input
2812-1
Sample Output
31532131
Sourceuniversity of Waterloo Local Contest 2005.09.24
Recommendeddy | We have carefully selected several similar problems for you:1133 1267 1207 1249 1284
Go::, assuming N is odd, there must be no solution.
When n is an even number, a more intuitive idea is to cut the large rectangle with a vertical bar to the left part, and then solve it recursively, just like uva_10359, directly consider how the leftmost small rectangle is formed and we can recursively get the F (n) recursion.
Although the topic appears to be a small rectangle at first glance, it seems to be a lot more spelling, and it is not intuitive to spell a small rectangle that can no longer be sliced with a vertical line. But suppose we draw more on paper, for a small rectangle that can be segmented with a horizontal edge length of x that is not allowed to be separated by a vertical line, assuming X is 2, there are obviously 3 spellings, assuming X is more than 2 even, then there are only 2 spellings.
As for assuming x>2 why there are only two spellings, we'd better actually spell it out. First the first column must have only two cases, the first is a horizontal in the top, then a vertical in the lower left, the other is a horizontal in the following, and then a vertical in the upper left, because of the two symmetry, we only discuss the first case.
Now already spelled two, assuming that the following horizontal and then put a vertical, then obviously this becomes a x=2 small rectangle, then the final spell out of what we said before the bar can not be used to cut the characteristics of the vertical, so the following is vertical to the right, only can put two horizontal, After putting the two horizontal, we find that there is a small square area on the top of the two horizontal, this area can only wedge into a horizontal, and so on after this horizontal painting, mygod, we will find an astonishing fact, today's structure is the same as we first put a horizontal vertical of the situation of the structure is the same! So, assuming that we want to continue to the right to spell a rectangle that cannot be sliced by a vertical line, then it is only possible to repeat the previous operation, and the resulting rectangle with the last transverse edge of x is only determined.
The first time we did this, we only took one of the two cases of symmetry, so suppose x>2, there are only two ways to spell a rectangle that cannot be separated by a vertical line.
Then the recursive formula naturally has, F (n) =3*f (n-2) +2*f (n-4) +...+2*f (0), and then write out the recursion of F (n-2) after the two-type difference can be obtained f (n) =4*f (n-2)-F (n-4), the recursive boundary is F (0) =1,f (2) = 4.
Code: 0MS
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h>using namespaceStd;#define MintF[M]={1,3}; int Main () { int I,N; for (I=2;I<M;I++)F[I]=4*F[I-1]-F[I-2]; while (Cin>>N ) { if( N<0 ) break; if (N%2==0)cout<<F[N/2]<<Endl; Else cout<<0<<Endl; } return 0;}
HDU 1143 Tri Tiling (Recursive)