Skip to content

Add solution and test-cases for problem 2787 #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [2787.Ways to Express an Integer as Sum of Powers][title]

## Description
Given two **positive** integers `n` and `n`.

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 `10^9 + 7`.

For example, if `n = 160` and `x = 3`, one way to express `n` is `n = 2^3 + 3^3 + 5^3`.

**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.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/ways-to-express-an-integer-as-sum-of-powers
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package Solution

func Solution(x bool) bool {
return x
func power(base, n int) int {
result := 1
for i := 0; i < n; i++ {
result *= base
}
return result
}

const mod = 1000000007

func Solution(n int, x int) int {
candidates := []int{}
i := 1
for ; ; i++ {
p := power(i, x)
if p > n {
break
}
candidates = append(candidates, p)
}

var dfs func(index, target int) int
cache := make(map[[2]int]int)

dfs = func(index, target int) int {
if target == 0 {
return 1
}
if target < 0 || index == len(candidates) {
return 0
}
key := [2]int{index, target}
if val, ok := cache[key]; ok {
return val
}
res := dfs(index+1, target-candidates[index]) % mod
res = (res + dfs(index+1, target)) % mod
cache[key] = res
return res
}

return dfs(0, n)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n, x int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 10, 2, 1},
{"TestCase2", 4, 1, 2},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.x)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.x)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading