Ways to Express an Integer as Sum of Powers - Problem
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 = n1^x + n2^x + ... + nk^x.
Since the result can be very large, return it modulo 10^9 + 7.
Example: If n = 160 and x = 3, one way to express n is n = 2^3 + 3^3 + 5^3.
Input & Output
Example 1 — Basic Case
$
Input:
n = 10, x = 2
›
Output:
1
💡 Note:
Only one way: 10 = 1² + 3² = 1 + 9. We use unique positive integers 1 and 3.
Example 2 — Multiple Ways
$
Input:
n = 4, x = 1
›
Output:
5
💡 Note:
Five ways to express 4 as sum of unique positive integers: {4}, {1,3}, {1,2,1} is invalid (1 repeated), so we have {4}, {1,3} only. Wait, let me recalculate: {4}, {1,3} are the only valid ways using unique integers. Actually: 4¹=4, or 1¹+3¹=4. So 2 ways.
Example 3 — Larger Input
$
Input:
n = 160, x = 3
›
Output:
5
💡 Note:
Multiple ways to express 160 as sum of unique cubes. One example: 2³ + 3³ + 5³ = 8 + 27 + 125 = 160.
Constraints
- 1 ≤ n ≤ 300
- 1 ≤ x ≤ 5
Visualization
Tap to expand
Understanding the Visualization
1
Input
n=10, x=2 (find ways to express 10 as sum of unique squares)
2
Consider Powers
Available: 1²=1, 2²=4, 3²=9, 4²=16, ...
3
Find Combinations
Only valid: 1² + 3² = 1 + 9 = 10
Key Takeaway
🎯 Key Insight: This is a subset sum problem with power constraints - use DP to systematically count valid unique combinations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code