【ybtoj 高效进阶 1.1】【递推】 平铺方案
题目
解题思路
设f[n]为长是n时的方案数
n=1时
n==2时
当n>=3时
- 最后的一个竖着放2*1的,有f[n-1]种方案
- 最后一个是2*2的,有f[n-2]种方案
- 最后一个是横着放2个2*1的,有f[n-2]种方案
代码
#include<iostream>
#include<cstdio>
using namespace std;
struct lzf{
int l,a[120];
}f[300];
int n;
void gj(int x)
{
int t=0;
for (int i=1;i<=f[x-2].l;i++)
{
f[x].a[i]=f[x-2].a[i]*2+t;
t=f[x].a[i]/10;
f[x].a[i]=f[x].a[i]%10;
}
if (t>0)
f[x].l=f[x-2].l+1,f[x].a[f[x].l]=t;
t=0;
f[x].l=max(f[x].l,f[x-1].l);
for (int i=1;i<=f[x].l;i++)
{
f[x].a[i]=f[x].a[i]+t+f[x-1].a[i];
t=f[x].a[i]/10;
f[x].a[i]=f[x].a[i]%10;
}
if (t)
f[x].l++,f[x].a[f[x].l]=t;
}
int main()
{
f[1].l=1;
f[1].a[1]=1;
f[2].l=1;
f[2].a[1]=3;
for (int i=3;i<=250;i++)
gj(i);
while (scanf("%d",&n)>0)
{
for (int i=f[n].l;i>0;i--)
printf("%d",f[n].a[i]);
printf("\n");
}
return 0;
}