UVA 580(p331)----Critical Mass

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=30+5;
int f[maxn],g[maxn];
void prepare()
{
    f[0]=0;f[1]=0;
    f[2]=0;f[3]=1;
    g[0]=1;g[1]=2;
    g[2]=4;g[3]=7;
    for(int i=3;i<=30;i++)
    {
        int tmp=0;
        for(int k=2;k<=i-2;k++)
            tmp+=g[k-2]*pow(2,i-k-2);
        f[i]=pow(2,i-3)+tmp;
        g[i]=pow(2,i)-f[i];
    }
}
int main()
{
    int n;
    prepare();
    while(scanf("%d",&n)==1&&n)
        printf("%d\n",f[n]);
    return 0;
}
题目地址: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=521

你可能感兴趣的:(UVA 580(p331)----Critical Mass)