首页 > 代码库 > 【UVA】11137-Ingenuous Cubrency

【UVA】11137-Ingenuous Cubrency

DP问题,须要打表。

dp[i][j]代表利用大小不超过i的数字组成j的方法。

状态方程是 dp[i][j] = d[i - 1][j] + sum{dp[i - 1][j - k * i * i *i]};

1432770511137Ingenuous CubrencyAcceptedC++0.0492014-10-09 10:20:48

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 11111;
const int maxd = 10000;
LL dp[40][maxn];
void List(){
    memset(dp,0,sizeof(dp));
    for(int i = 1 ; i <= maxd ; i++) dp[1][i] = 1;
    for(int i = 2 ; i * i * i <= maxd ; i ++){
        for(int j = 1; j <= maxd ; j ++){
            dp[i][j] += dp[i - 1][j];
            for(int k = 1; ; k ++){
                if(j - k * i * i * i > 0)
                    dp[i][j] += dp[i - 1][j - k * i * i * i];
                else if(j - k * i * i * i == 0)
                    dp[i][j] ++;
                else
                    break;
            }
        }
    }
}
int main(){
    List();
    int n,k;
    for(k = 1; k * k * k <= maxd ; k ++);
    while(scanf("%d",&n) != EOF){
        printf("%lld\n",dp[k - 1][n]);
    }
    return 0;
}

【UVA】11137-Ingenuous Cubrency