This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE
2787. Ways to Express an Integer as Sum of Powers
Difficulty: Medium
Topics: Dynamic Programming
, Biweekly Contest 109
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
Hint:
- You can use dynamic programming, where
dp[k][j]
represents the number of ways to express k as the sum of the xth power of unique positive integers such that the biggest possible number we use isj
. - To calculate
dp[k][j]
, you can iterate over the numbers smaller thanj
and try to use each one as a power ofx
to make our sumk
.
Solution:
We need to count the number of ways a given positive integer n
can be expressed as the sum of unique positive integers each raised to the x
-th power. The solution involves dynamic programming to efficiently compute the number of valid combinations without repetition.
Approach
-
Problem Analysis: The problem requires finding all sets of unique positive integers such that the sum of each integer raised to the
x
-th power equalsn
. The solution must avoid duplicate sets (e.g.,{1, 2}
and{2, 1}
are considered the same set) and each integer can be used at most once. -
Dynamic Programming Setup: We use a dynamic programming array
dp
wheredp[j]
represents the number of ways to achieve the sumj
using the integers processed so far. Initializedp[0]
to 1 (the base case representing the empty set). -
Determine Maximum Integer: Calculate the largest integer
maxi
such thatmaxix
is less than or equal ton
. This bounds the integers we need to consider. -
DP Array Update: For each integer
i
from 1 tomaxi
, computev = ix
. Update thedp
array in reverse order (fromn
down tov
) to ensure each integer is used only once in each combination. For eachj
, updatedp[j]
by addingdp[j - v]
, which represents the number of ways to formj
by includingi
. - Modulo Operation: Since the result can be large, all updates are performed modulo 109 + 7.
Let’s implement this solution in PHP: 2787. Ways to Express an Integer as Sum of Powers
<?php
/**
* @param Integer $n
* @param Integer $x
* @return Integer
*/
function numberOfWays($n, $x) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo numberOfWays(10, 2) . "\n"; // Output: 1
echo numberOfWays(4, 1) . "\n"; // Output: 2
?>
Explanation:
-
Initialization: The
dp
array is initialized with zeros, except fordp[0]
which is set to 1. This array will store the number of ways to achieve each sum from 0 ton
. -
Finding
maxi
: The loop calculates the largest integermaxi
such thatmaxix
does not exceedn
. This determines the range of integers we need to consider for the sum. -
Dynamic Programming Update: For each integer
i
from 1 tomaxi
:- Compute
v = ix
. - Update the
dp
array fromn
down tov
to ensure each integer is only used once per combination. For eachj
, the valuedp[j]
is incremented bydp[j - v]
, representing the inclusion ofi
in the sum.
- Compute
-
Result Extraction: The result is found in
dp[n]
, which gives the number of valid combinations modulo 109 + 7.
This approach efficiently counts all unique combinations of integers raised to the x
-th power that sum to n
using dynamic programming, ensuring optimal performance even for the upper constraint limits.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks . Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE