Ctrl K

Sums of Distinct Powers

Medium
You get positive integers total and power. Count the ways to write total as a sum of power-th powers of **distinct** positive integers, that is, the number of sets of distinct numbers {a₁, a₂, …, aₖ} with a₁^power + a₂^power + … + aₖ^power = total. Order does not matter: sets made of the same numbers count as one way. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
total=13, power=2 → 1

13 = 2² + 3² is the only way: no other set of distinct squares from 1, 4, 9 adds up to 13.

Example 2
total=6, power=1 → 4

With power = 1 we count splits into distinct parts: 6, 5 + 1, 4 + 2 and 3 + 2 + 1, so 4 in total.

Example 3
total=35, power=3 → 1
Constraints
  • 1 ≤ total ≤ 300
  • 1 ≤ power ≤ 5
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumAll Balanced Bracket Strings MediumFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell Medium
loading Python…
Input
total=13, power=2
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.