• + 0 comments

    knapsack, O(X * X^(1/N))

    int powerSum(int X, int N) {
        int L = pow(X, 1.0/N);
        vector<vector<int>> cache(X+1, vector<int>(L+1));
        for (int j=0; j <= L; j++) cache[0][j] = 1;
        for (int i=1; i <= X; i++) {
            for (int j=1; j <= L; j++) {
                if (i < pow(j, N)) cache[i][j] = cache[i][j-1];
                else cache[i][j] = cache[i][j-1] + cache[i - pow(j, N)][j-1];
            }
        }
        return cache[X][L];
    }