解题思路
我们设 f ( i ) f(i) f(i)表示 2 ∗ i 2*i 2∗i矩形的方案数,我们考虑三种情况:
- 在第i列上竖着放置1个 2 ∗ 1 2*1 2∗1的瓦片,方案数为 f ( i − 1 ) f(i-1) f(i−1)
- 在第i列上放置1个 2 ∗ 2 2*2 2∗2的瓦片,方案数为 f ( i − 2 ) f(i-2) f(i−2)
- 在第i列上横着放置2个 2 ∗ 1 2*1 2∗1的瓦片,方案数为 f ( i − 2 ) f(i-2) f(i−2)
故可得递推式: f ( i ) = 2 ∗ f ( i − 2 ) + f ( i − 1 ) f(i)=2*f(i-2)+f(i-1) f(i)=2∗f(i−2)+f(i−1)
代码
#include<iostream>
#include<cstdio>
using namespace std;
int n,nn,f[300][200],c[300];
void ch(int x) {
int g=0,w=0,s=0,t[200];
for(int i=1; i<=c[x-2]; i++) {
s=f[x-2][i]*2+g;
g=s/10;
f[x][++w]=s%10;
}
if(g!=0)
f[x][++w]=g;
c[x]=w;
}
void add(int x) {
int g=0,w=0,s=0,t[200];
c[x]=max(c[x-1],c[x]);
for(int i=1; i<=max(c[x-1],c[x]); i++) {
s=f[x-1][i]+f[x][i]+g;
g=s/10;
f[x][i]=s%10;
}
if(g!=0)
f[x][++c[x]]=g;
}
int main() {
f[1][1]=1;
c[1]=1;
f[2][1]=3;
c[2]=1;
nn=3;
while(scanf("%d",&n)!=EOF) {
for(int j=nn; j<=n; j++) {
ch(j);
add(j);
}
for(int j=c[n]; j>0; j--)
printf("%d",f[n][j]);
printf("\n");
nn=n+1;
}
}