Tri Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10549 | Accepted: 5347 |
Description
In how many ways can you tile a 3xn rectangle with 2x1 dominoes?
Here is a sample tiling of a 3x12 rectangle.
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
这个题目开始以为跟POJ2506一样,首先,n等于奇数的时候3n也是奇数,所以是0种排法。然后就考虑把格子分成3×2+3×(n-2)、3×4+3×(n-4)来看,3×2有三种摆法,3×4有两种摆法,然后就得到了dp方程,F(n)=3F(n-2)+2F(n-4).结果写出来发现结果不对,然后我又多画了几组,然后就发现n>=4后又会多出小单位组成的大单位(6、9、12.块等)处也有衔接,然后就把它们加入了dp方程中,就成了F(n)=3F(n-2)+2(F(n-4)+F(n-6)+……+F(2)),然后再写一步F(n-2)=3F(n-4)+2(F(n-6)+F(n-8)+……+F(2)),两个式子相减就可以得到关系式F(n)=4×F(n-2)-F(n-4)。以下是我的代码
#include<stdio.h>
int main ()
{
int T;
long long arr[35];
arr[2]=3;
arr[4]=11;
for(int i=6;i<35;i+=2)
arr[i]=4*arr[i-2]-arr[i-4];
while(~scanf("%d",&T)&&T!=-1)
{
if(T%2!=0) printf("0\n");
else printf("%d\n",arr[T]);
}
return 0;
}
一次AC。