Tri Tiling (hdu1143)

Source: Internet
Author: User

Tri Tiling (hdu1143)
Tri Tiling

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 2731 Accepted Submission (s): 1547


 

Problem Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sample tiling of a 3x12 rectangle.



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
2812-1

Sample Output
31532131

Source University of Waterloo Local Contest 2005.09.24
Ideas: 1. tagging and concepts

F (n): where n is the length of the rectangle in the question, and the height of the positioning 3, that is, the n, f (n) in the 3xn in the question) it indicates the number of all placement methods when the length is n. Split line: a vertical line. This line passes through the rectangle in the question and splits the rectangle into two parts. This line cannot pass through the middle of the brick, that is, only when the edge of the brick is aligned, to pass through.

2. Solutions

2.1 for the placement of each type of Brick, there may be multiple split lines mentioned above, but for each case, we only need one of the rightmost of all split lines, which is marked as L. That is to say, the right part of L is inseparable, but the left part may still be separated. For the left side of L, we can continue to use function f, while the right side is the main part of our study, because function f cannot be applied on the right side.

2.2 The reason that function f cannot be applied is that the right side is not severable. There are three ways to place an inseparable rectangle with a length of 2. There are two ways to place an inseparable rectangle with a length greater than 2. You may need to pick up a pen and draw a picture on the paper.

2.3 At the same time, consider where such L may be located? It may be in the position where the length from the right is 2, or in the position where the length is 4 ,......, It may also be at the position where the length is n. Of course, it is only possible that,

Therefore, the following results are displayed:

F (n) = f (n-2) * 3 + f (n-4) * 2 +... + f (2) * 2 + f (0) * 2 ---- expression 1

Then, replace the above formula with a N-2: f (n-2) = f (n-4) * 3 + f (n-6) * 2 +... + f (2) * 2 + f (0) * 2 ---- expression 2

Expression 1 minus expression 2 get: f (n) = 4 * f (n-2)-f (n-4) 2.4 when using the above recursive formula, we need two recursive egresses, that is, f (0) = 1, f (2) = 3. the recursive formula above also knows that when n is an odd number, it is zero directly when n is an odd number. When n is an odd number, the area of the rectangle is odd. However, no matter how many bricks are used, the total area of the bricks must be an even number, so there is no placement form.
 
 

I think it is a bit difficult here. That is, F [0] = 1; why is it 1 when n = 0 ???

 

#include
 
  #define LL __int64LL ans[35];void init(){    ans[0]=1;    ans[1]=ans[3]=0;    ans[2]=3;ans[4]=11;    for(int i=5;i<=30;i++)    {        if(i&1) ans[i]=0;        else ans[i]=ans[i-2]*4-ans[i-4];    }}int main(){    int n;    init();    while(scanf(%d,&n)!=EOF)    {        if(n==-1) break;        printf(%I64d,ans[n]);    }    return 0;}
 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.