原题:
In how many ways can you tile a 3 × n rectangle with 2 × 1 dominoes? Here is a sample tiling of a 3 × 12 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
中文:
给你n×3的长条,让你用1×2的平铺。有多少种方法?
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[31];
int main()
{
ios::sync_with_stdio(false);
memset(f,0,sizeof(f));
f[0]=1;
for(int i=2;i<=30;i++)
{
if(i%2)
continue;
ll tmp=3*f[i-2];
for(int j=i-4;j>=0;j-=2)
tmp+=f[j]*2;
f[i]=tmp;
}
int n;
while(cin>>n)
{
if(n==-1)
break;
cout<<f[n]<<endl;
}
return 0;
}
解答:
递推的题目,奇数的肯定不行,没有办法铺满,答案是0.考虑偶数
f[n]=3×f[n-2]+2×Σf[n-i] i等于4,6,8….
f[n-2]的情况如下有3种
另外加上腾出4×2,6×2个或者8×2个等等的空间可以这样放置有两种