diff --git a/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/README.md b/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/README.md new file mode 100644 index 000000000..750baca56 --- /dev/null +++ b/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/README.md @@ -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 diff --git a/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution.go b/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution.go index d115ccf5e..ec1a08a22 100755 --- a/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution.go +++ b/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution.go @@ -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) } diff --git a/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution_test.go b/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution_test.go index 14ff50eb4..19a93e4cc 100755 --- a/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution_test.go +++ b/leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution_test.go @@ -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() { }