Skip to content

Commit 3ae2f9c

Browse files
authored
update: 添加问题“2787.将一个数字表示成幂的和的方案数”的代码和题解 (#1077)
* words: jp - 这个部分好多生词啊 (#1074) * 2787: init (#1074) * 2787: WA.cpp (#1074) - 要的是互不相同整数 这种方法每个正数可以被复用 * 2787: WA.cpp (#1074) * 2787: MLE.cpp (#1074) - x=1时死循环 * 2787: WA.cpp (#1074) - 是t^x而不是x^t * 2787: WA.cpp (#1074) input: 213 1 should: 55852583 output: 1055852590 这咋还都有个558525 * 2787: WA.cpp (#1074) - 看来不是pow精度的问题 * 2787: AC.cpp (#1074) - 原来是忘取模了 cpp - AC,46.82%,36.36% TODO: 不再单独创建个pows数组了 晚上整整转正材料复习(学习)下背包吧 * 2787: AC.cpp+WA.py (#1074) - 不预先创建pow数组 cpp - AC,82.74%,95.33% * 2787: AC.py+WA.go (#1074) py - AC,75.72%,50.00% * 2787: WA.go (#1074) - 忘记取模了 * 2787: AC.go+CE.java (#1074) go - AC,83.10%,90.14% * 2787: AC.java+rust (#1074) - rust编译过就AC了,safe(bushi) java - AC,98.08%,42.58% rust - AC,57.14%,71.43% 刚刚: 1. ArrayList<>(n + 1)只是预分配空间,还需要n+1次.add(0) 2. dp.get(xxa) = xxb不可,需要dp.set(xxa, xxb) 3. long完要int回去 4. pow完也要强制转int * fix: Mac上debug cpp后会newSolution.py读文件夹的“文本内容”失败 close #1075 * fix: Mac上debug cpp后会newSolution.py读文件夹的“文本内容”失败 (#1074) close #1075 * update: 添加问题“2787.将一个数字表示成幂的和的方案数”的代码和题解 (#1077) close #1074 Signed-off-by: LetMeFly666 <[email protected]> * 2787: clean.java (#1074) #1077 (comment) --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 76a903a commit 3ae2f9c

14 files changed

+389
-4
lines changed

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
close #1074

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"request": "launch",
3939
"program": "${fileDirname}/${fileBasenameNoExtension}",
4040
"args": [],
41-
"stopAtEntry": true,
41+
"stopAtEntry": false,
4242
"cwd": "${fileDirname}",
4343
"environment": [],
4444
"externalConsole": true,

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@
9292
"queue": "cpp",
9393
"span": "cpp",
9494
"__locale": "cpp",
95-
"ios": "cpp"
95+
"ios": "cpp",
96+
"__config": "cpp",
97+
"__bit_reference": "cpp"
9698
},
9799
"editor.mouseWheelZoom": true,
98100
"workbench.tree.enableStickyScroll": true,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-12 09:48:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-12 21:33:24
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// dp[i]: 和为i的方案数
12+
class Solution {
13+
private:
14+
static const long long MOD = 1e9 + 7;
15+
16+
int pow(int a, int b) {
17+
long long ans = 1;
18+
while (b) {
19+
if (b & 1) {
20+
ans = ans * a;
21+
}
22+
a = a * a;
23+
b >>= 1;
24+
}
25+
return ans;
26+
}
27+
public:
28+
int numberOfWays(int n, int x) {
29+
vector<int> dp(n + 1);
30+
dp[0] = 1;
31+
for (int th = 1; ; th++) {
32+
int p = pow(th, x);
33+
if (p > n) {
34+
break;
35+
}
36+
for (int i = n; i >= p; i--) {
37+
dp[i] = (dp[i] + dp[i - p]) % MOD;
38+
}
39+
}
40+
return dp.back();
41+
}
42+
};
43+
44+
#if defined(_WIN32) || defined(__APPLE__)
45+
/*
46+
10 2
47+
48+
1
49+
*/
50+
51+
/*
52+
4 1
53+
54+
55+
*/
56+
int main() {
57+
int a, b;
58+
while (cin >> a >> b) {
59+
Solution sol;
60+
cout << sol.numberOfWays(a, b) << endl;
61+
}
62+
return 0;
63+
}
64+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-12 09:48:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-12 21:42:16
6+
*/
7+
package main
8+
9+
import "math"
10+
11+
func pow2787(a, b int) int {
12+
return int(math.Pow(float64(a), float64(b)))
13+
}
14+
15+
func numberOfWays(n int, x int) int {
16+
dp := make([]int, n + 1)
17+
dp[0] = 1
18+
for th := 1; ; th++ {
19+
p := pow2787(th, x)
20+
if p > n {
21+
break
22+
}
23+
for i := n; i >= p; i-- {
24+
dp[i] += dp[i - p]
25+
}
26+
}
27+
return dp[n] % 1000000007
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-12 09:48:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-12 22:58:59
6+
*/
7+
class Solution {
8+
public int numberOfWays(int n, int x) {
9+
long[] dp = new long[n + 1];
10+
dp[0] = 1;
11+
for (int th = 1; ; th++) {
12+
int p = (int)Math.pow(th, x);
13+
if (p > n) {
14+
break;
15+
}
16+
for (int i = n; i >= p; i--) {
17+
dp[i] += dp[i - p];
18+
}
19+
}
20+
return (int)(dp[n] % 1000000007);
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-08-12 09:48:56
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-12 21:37:06
6+
'''
7+
class Solution:
8+
def numberOfWays(self, n: int, x: int) -> int:
9+
dp = [1] + [0] * n
10+
th = 1
11+
while True:
12+
p = th ** x
13+
if p > n:
14+
break
15+
th += 1
16+
for i in range(n, p - 1, -1):
17+
dp[i] += dp[i - p]
18+
return dp[-1] % 1000000007
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-12 09:48:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-12 22:00:07
6+
*/
7+
impl Solution {
8+
pub fn number_of_ways(n: i32, x: i32) -> i32 {
9+
let n: usize = n as usize;
10+
let mut dp: Vec<i32> = vec![0; n + 1];
11+
dp[0] = 1;
12+
for th in 1usize.. {
13+
let p: usize = th.pow(x as u32);
14+
if p > n {
15+
break;
16+
}
17+
for i in (p..=n).rev() {
18+
dp[i] = ((dp[i] as i64 + dp[i - p] as i64) % (1000000007 as i64)) as i32
19+
}
20+
}
21+
dp[n]
22+
}
23+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-08-11 22:07:08
66
*/
77
pub struct Solution;
8-
include!("0869-reordered-power-of-2_20250810.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("2787-ways-to-express-an-integer-as-sum-of-powers.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@
870870
|2769.找出最大的可达成数字|简单|<a href="https://leetcode.cn/problems/find-the-maximum-achievable-number/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/21/LeetCode%202769.%E6%89%BE%E5%87%BA%E6%9C%80%E5%A4%A7%E7%9A%84%E5%8F%AF%E8%BE%BE%E6%88%90%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139101760" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-maximum-achievable-number/solutions/2785705/letmefly-2769zhao-chu-zui-da-de-ke-da-ch-sb7w/" target="_blank">LeetCode题解</a>|
871871
|2779.数组的最大美丽值|中等|<a href="https://leetcode.cn/problems/maximum-beauty-of-an-array-after-applying-operation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/15/LeetCode%202779.%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E7%BE%8E%E4%B8%BD%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139707025" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-beauty-of-an-array-after-applying-operation/solutions/2811818/letmefly-2779shu-zu-de-zui-da-mei-li-zhi-9ywc/" target="_blank">LeetCode题解</a>|
872872
|2786.访问数组中的位置使分数最大|中等|<a href="https://leetcode.cn/problems/visit-array-positions-to-maximize-score/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/14/LeetCode%202786.%E8%AE%BF%E9%97%AE%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E4%BD%8D%E7%BD%AE%E4%BD%BF%E5%88%86%E6%95%B0%E6%9C%80%E5%A4%A7/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139688753" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/visit-array-positions-to-maximize-score/solutions/2810990/letmefly-2786fang-wen-shu-zu-zhong-de-we-71a5/" target="_blank">LeetCode题解</a>|
873+
|2787.将一个数字表示成幂的和的方案数|中等|<a href="https://leetcode.cn/problems/ways-to-express-an-integer-as-sum-of-powers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/12/LeetCode%202787.%E5%B0%86%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%E8%A1%A8%E7%A4%BA%E6%88%90%E5%B9%82%E7%9A%84%E5%92%8C%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150296585" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/ways-to-express-an-integer-as-sum-of-powers/solutions/3751207/letmefly-2787jiang-yi-ge-shu-zi-biao-shi-no5x/" target="_blank">LeetCode题解</a>|
873874
|2788.按分隔符拆分字符串|简单|<a href="https://leetcode.cn/problems/split-strings-by-separator/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/20/LeetCode%202788.%E6%8C%89%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%86%E5%88%86%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135724019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-strings-by-separator/solutions/2612832/letmefly-2788an-fen-ge-fu-chai-fen-zi-fu-s1dn/" target="_blank">LeetCode题解</a>|
874875
|2789.合并后数组中的最大元素|中等|<a href="https://leetcode.cn/problems/largest-element-in-an-array-after-merge-operations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/14/LeetCode%202789.%E5%90%88%E5%B9%B6%E5%90%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136698122" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-element-in-an-array-after-merge-operations/solutions/2685943/letmefly-2789he-bing-hou-shu-zu-zhong-de-jrx8/" target="_blank">LeetCode题解</a>|
875876
|2798.满足目标工作时长的员工数目|简单|<a href="https://leetcode.cn/problems/number-of-employees-who-met-the-target/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/30/LeetCode%202798.%E6%BB%A1%E8%B6%B3%E7%9B%AE%E6%A0%87%E5%B7%A5%E4%BD%9C%E6%97%B6%E9%95%BF%E7%9A%84%E5%91%98%E5%B7%A5%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138352241" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-employees-who-met-the-target/solutions/2762052/letmefly-2798man-zu-mu-biao-gong-zuo-shi-2sb8/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)