1. 程式人生 > >【HDU2606】Renovation Problem(遞推)

【HDU2606】Renovation Problem(遞推)

題目連結

Renovation Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 754    Accepted Submission(s): 263


 

Problem Description

The New Year’s Day is comming ! Every family want to make theiir house looks more beautiful , so does Teddy’s . To make his own house looks different from other's , he went to the shop to buy four kinds of tiles . each kind of tiles has different size : 1*1 , 2*2 , 3*3 and 4 * 4, respectively .


The problem is , how many ways there are to tile his floor which has a area of 4*N using these four kinds of tiles ? You can assume that the number of each kind of tiles is enough.

 

 

Input

The first line contain a T. followed by T lines ,each line contain a integer N.(1<=N <=100).

 

 

Output

For each case, output the ans % 19890907.

 

 

Sample Input

 

2 1 2

 

 

Sample Output

 

1 5

 

【題意】

用1*1,2*2,3*3,4*4的磚塊去鋪4*n的地,輸出方案數。

【解題思路】

參考:https://blog.csdn.net/acm_cxq/article/details/51189527

f[i]表示鋪滿前i列的方案數。f[1]=1,f[2]=5。

1.因為剩餘4*1只能用1*1去填,所以f[n]=f[n-1](這種情況就包括了整個地板都用1*1去填的方案,下面肯定就不能再重複計算這個方案)

2.剩餘4*2的時候,我們用2*2和1*1的方塊去填可以有四種方案(注意不能不用2*2,不然就包含了全1了)。所以f[n]+=4*f[n-2]

3.剩餘4*3的時候,我們用3*3和2*2和1*1(跟上面一個道理不能不使用3*3,因為使用了3*3就不能用2*2了,所以這裡實際上只能用3*3和1*1),有兩種方案,所以f[n]+=f[n-3]*2

4.剩餘4*4的時候,我們用4*4和3*3和2*2和1*1(這裡必須用4*4,其實也只能用4*4)。有一種方案,所以f[n]+=f[n-4]*1

但此時的答案還不是完整的,因為當i=3時有以下兩種情況是多出來的,因為交叉,所以這兩種情況在i>=3時是可以不斷往前平移的。

【程式碼】

#include<bits/stdc++.h>
using namespace std;
const int mod=19890907;
const int maxn=105;
int f[maxn];
int main()
{
    f[0]=1;
    f[1]=1;
    f[2]=5;
    f[3]=f[2]+f[1]*4+f[0]*2+2;
    for(int i=4;i<=100;i++)
    {
        f[i]=(f[i]+f[i-1])%mod;
        f[i]=(f[i]+f[i-2]*4)%mod;
        f[i]=(f[i]+f[i-3]*2)%mod;
        f[i]=(f[i]+f[i-4])%mod;
        for(int j=3;j<=i;j++)
            f[i]=(f[i]+f[i-j]*2)%mod;
    }
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        printf("%d\n",f[n]);
    }
    return 0;
}