递推POJ2663

来源:互联网 发布:暴力营销软件 编辑:程序博客网 时间:2024/05/16 17:05
Tri Tiling
Time Limit: 1000MS Memory Limit: 65536KTotal 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. 

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

2812-1

Sample Output

31532131
    这个题目开始以为跟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。
0 0
原创粉丝点击