Description
Given two positive integers n and x.
Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx.
Since the result can be very large, return it modulo 109 + 7.
For example, if n = 160 and x = 3, one way to express n is n = 23 + 33 + 53.
Example 1:
Input: n = 10, x = 2
Output: 1
Explanation: We can express n as the following: n = 32 + 12 = 10.
It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers.
Example 2:
Input: n = 4, x = 1
Output: 2
Explanation: We can express n in the following ways:
- n = 41 = 4.
- n = 31 + 11 = 4.
Constraints:
1 <= n <= 300
1 <= x <= 5
Solutions
Solution 1: Dynamic Programming
We define f[i][j] as the number of ways to select some numbers from the first i positive integers such that the sum of their x-th powers equals j. Initially, f[0][0] = 1, and all others are 0. The answer is f[n][n].
For each positive integer i, we can choose to either include it or not:
- Not include it: the number of ways is f[i-1][j];
- Include it: the number of ways is f[i-1][j-ix] (provided that j ≥ ix).
Therefore, the state transition equation is:
f[i][j] = f[i-1][j] + (j ≥ ix ? f[i-1][j-ix] : 0)
Note that the answer can be very large, so we need to take modulo 109 + 7.
The time complexity is O(n2), and the space complexity is O(n2), where n is the given integer in the
PythonJavaC++GoTypeScriptRustJavaScriptC#
class Solution:
def numberOfWays(self, n: int, x: int) -> int:
mod = 10**9 + 7
f = [[0] * (n + 1) for _ in range(n + 1)]
f[0][0] = 1
for i in range(1, n + 1):
k = pow(i, x)
for j in range(n + 1):
f[i][j] = f[i - 1][j]
if k <= j:
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod
return f[n][n](code-box)
class Solution {
public int numberOfWays(int n, int x) {
final int mod = (int) 1e9 + 7;
int[][] f = new int[n + 1][n + 1];
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
long k = (long) Math.pow(i, x);
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (k <= j) {
f[i][j] = (f[i][j] + f[i - 1][j - (int) k]) % mod;
}
}
}
return f[n][n];
}
}(code-box)
class Solution {
public:
int numberOfWays(int n, int x) {
const int mod = 1e9 + 7;
int f[n + 1][n + 1];
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
long long k = (long long) pow(i, x);
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (k <= j) {
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
}
}
}
return f[n][n];
}
};(code-box)
func numberOfWays(n int, x int) int {
const mod int = 1e9 + 7
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, n+1)
}
f[0][0] = 1
for i := 1; i <= n; i++ {
k := int(math.Pow(float64(i), float64(x)))
for j := 0; j <= n; j++ {
f[i][j] = f[i-1][j]
if k <= j {
f[i][j] = (f[i][j] + f[i-1][j-k]) % mod
}
}
}
return f[n][n]
}(code-box)
function numberOfWays(n: number, x: number): number {
const mod = 10 ** 9 + 7;
const f = Array.from({ length: n + 1 }, () => Array(n + 1).fill(0));
f[0][0] = 1;
for (let i = 1; i <= n; ++i) {
const k = Math.pow(i, x);
for (let j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (k <= j) {
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
}
}
}
return f[n][n];
}(code-box)
impl Solution {
pub fn number_of_ways(n: i32, x: i32) -> i32 {
const MOD: i64 = 1_000_000_007;
let n = n as usize;
let x = x as u32;
let mut f = vec![vec![0; n + 1]; n + 1];
f[0][0] = 1;
for i in 1..=n {
let k = (i as i64).pow(x);
for j in 0..=n {
f[i][j] = f[i - 1][j];
if j >= k as usize {
f[i][j] = (f[i][j] + f[i - 1][j - k as usize]) % MOD;
}
}
}
f[n][n] as i32
}
}(code-box)
/**
* @param {number} n
* @param {number} x
* @return {number}
*/
var numberOfWays = function (n, x) {
const mod = 10 ** 9 + 7;
const f = Array.from({ length: n + 1 }, () => Array(n + 1).fill(0));
f[0][0] = 1;
for (let i = 1; i <= n; ++i) {
const k = Math.pow(i, x);
for (let j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (k <= j) {
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
}
}
}
return f[n][n];
};(code-box)
public class Solution {
public int NumberOfWays(int n, int x) {
const int mod = 1000000007;
int[,] f = new int[n + 1, n + 1];
f[0, 0] = 1;
for (int i = 1; i <= n; ++i) {
long k = (long)Math.Pow(i, x);
for (int j = 0; j <= n; ++j) {
f[i, j] = f[i - 1, j];
if (k <= j) {
f[i, j] = (f[i, j] + f[i - 1, j - (int)k]) % mod;
}
}
}
return f[n, n];
}
}(code-box)