原题链接:UVa580
解析:设f[n]为答案,g[n]为前n个没有3个盒子放在一起的情况。
代码实例:
#include<iostream>
using namespace std;
int f[31], g[31];
int main() {
f[0] = f[1] = f[2] = 0;
g[0] = 1; g[1] = 2; g[2] = 4;
for(int n = 3; n <= 30; n++) {
f[n] = 1 << (n-3);
for(int i = 2; i <= n-2; i++)
f[n] += g[i-2] * (1 << (n-i-2));
g[n] = (1<<n) - f[n];
}
int n;
while(cin >> n && n)
cout << f[n] << "
";
return 0;
}