Ways to Express an Integer as Sum of Powers
Time complexity: $O(n^{1+\frac{1}{x}})$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <iostream>
#include <cmath>
const int M = 1'000'000'007;
class Solution {
public:
int numberOfWays(int n, int x) {
long long dp[301] = { 0 };
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
int b = std::pow(i, x);
if (b > n) {
break;
}
for (int c = n; c >= b; --c) {
dp[c] += dp[c - b];
}
}
return dp[n] % M;
}
};
|
