题解:UVA11310 Delivery Debacle

· · 题解

一道练习递推的好题。

题目大意

你能做两种规格的蛋糕,一种是占一格的小蛋糕,一种是占三格的大蛋糕,问你能用多少种不同的方法把蛋糕装进特定大小的盒子里,盒子宽为二,长为 n。

思路

本题多组数据,通过枚举前几项我们可以得到递推公式:a_i = a_{i-3} \times 2 + 4 \times a_{i-2} + a_{i-1}。

然后设好前两项的值,递推就可以了。

优化

容易发现,如果多组数据用的是同一个值, 同样的过程要计算五次,这样未免太浪费。

有什么办法能解决这个问题?

答案是:记忆化

最后放一个记忆化递推的代码: ```cpp #include<bits/stdc++.h> using namespace std; const int N=41; int a[45]; int main() { a[1]=1; a[2]=5; for(int i=3; i<=N; i++) { a[i]=a[i-3]*2+4*a[i-2]+a[i-1]; } int t; cin>>t; while(t--) { int n; cin>>n; cout<<a[n]; } return 0; } ``` [看完就回去做题叭](https://www.luogu.com.cn/problem/UVA11310)