递推POJ2663

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. 

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。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值