当前位置: 首页 > news >正文

LeetCode 2787.将一个数字表示成幂的和的方案数:经典01背包

【LetMeFly】2787.将一个数字表示成幂的和的方案数:经典01背包

力扣题目链接:https://leetcode.cn/problems/ways-to-express-an-integer-as-sum-of-powers/

给你两个  整数 n 和 x 。

请你返回将 n 表示成一些 互不相同 正整数的 x 次幂之和的方案数。换句话说,你需要返回互不相同整数 [n1, n2, ..., nk] 的集合数目,满足 n = n1x + n2x + ... + nkx 。

由于答案可能非常大,请你将它对 109 + 7 取余后返回。

比方说,n = 160 且 x = 3 ,一个表示 n 的方法是 n = 23 + 33 + 53 

 

示例 1:

输入:n = 10, x = 2
输出:1
解释:我们可以将 n 表示为:n = 32 + 12 = 10 。
这是唯一将 10 表达成不同整数 2 次方之和的方案。

示例 2:

输入:n = 4, x = 1
输出:2
解释:我们可以将 n 按以下方案表示:
- n = 41 = 4 。
- n = 31 + 11 = 4 。

 

提示:

  • 1 <= n <= 300
  • 1 <= x <= 5

解题方法:动态规划(01背包)

这道题可以翻译为:txt^xtx组成的数组中选择一些和为nnn的数有多少种方案。

可以使用dp[i]dp[i]dp[i]代表和为iii的方案数,初始值dp[0]=1dp[0]=1dp[0]=1dp[1..n]=0dp[1..n]=0dp[1..n]=0

第一层循环从数组中取数,第二层循环倒序遍历dp数组且有状态转移方程dp[i]+=dp[i−p]dp[i] += dp[i - p]dp[i]+=dp[ip],其中ppp是数组中的一个txt^xtx。倒序是为了得到dp[i]dp[i]dp[i]时保证dp[i−p]dp[i-p]dp[ip]在第二层循环中还未被更改过。

  • 时间复杂度O(n×nx)O(n\times \sqrt[x]{n})O(n×xn)
  • 空间复杂度O(n)O(n)O(n)

AC代码

C++

C++代码使用了快速幂,因数据范围很小所以浮点数pow也不会出现精度误差,若不想写快速幂也可以直接删掉int pow()函数。

/** @Author: LetMeFly* @Date: 2025-08-12 09:48:56* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-08-12 21:33:24*/
// dp[i]: 和为i的方案数
class Solution {
private:static const long long MOD = 1e9 + 7;int pow(int a, int b) {long long ans = 1;while (b) {if (b & 1) {ans = ans * a;}a = a * a;b >>= 1;}return ans;}
public:int numberOfWays(int n, int x) {vector<int> dp(n + 1);dp[0] = 1;for (int th = 1; ; th++) {int p = pow(th, x);if (p > n) {break;}for (int i = n; i >= p; i--) {dp[i] = (dp[i] + dp[i - p]) % MOD;}}return dp.back();}
};
Python
'''
Author: LetMeFly
Date: 2025-08-12 09:48:56
LastEditors: LetMeFly.xyz
LastEditTime: 2025-08-12 21:37:06
'''
class Solution:def numberOfWays(self, n: int, x: int) -> int:dp = [1] + [0] * nth = 1while True:p = th ** xif p > n:breakth += 1for i in range(n, p - 1, -1):dp[i] += dp[i - p]return dp[-1] % 1000000007
Java
/** @Author: LetMeFly* @Date: 2025-08-12 09:48:56* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-08-12 21:50:02*/
import java.util.List;
import java.util.ArrayList;class Solution {public int numberOfWays(int n, int x) {long[] dp = new long[n + 1];dp[0] = 1;for (int th = 1; ; th++) {int p = (int)Math.pow(th, x);if (p > n) {break;}for (int i = n; i >= p; i--) {dp[i] += dp[i - p];}}return (int)(dp[n] % 1000000007);}
}
Go
/** @Author: LetMeFly* @Date: 2025-08-12 09:48:56* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-08-12 21:42:16*/
package mainimport "math"func pow2787(a, b int) int {return int(math.Pow(float64(a), float64(b)))
}func numberOfWays(n int, x int) int {dp := make([]int, n + 1)dp[0] = 1for th := 1; ; th++ {p := pow2787(th, x)if p > n {break}for i := n; i >= p; i-- {dp[i] += dp[i - p]}}return dp[n] % 1000000007
}
Rust
/** @Author: LetMeFly* @Date: 2025-08-12 09:48:56* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-08-12 22:00:07*/
impl Solution {pub fn number_of_ways(n: i32, x: i32) -> i32 {let n: usize = n as usize;let mut dp: Vec<i32> = vec![0; n + 1];dp[0] = 1;for th in 1usize.. {let p: usize = th.pow(x as u32);if p > n {break;}for i in (p..=n).rev() {dp[i] = ((dp[i] as i64 + dp[i - p] as i64) % (1000000007 as i64)) as i32}}dp[n]}
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

http://www.lryc.cn/news/618265.html

相关文章:

  • 小红书笔记信息获取_实在智能RPA源码解读
  • 使用 NetBird 创建安全的私有网络,简化远程连接!
  • 完整多端口 Nginx Docker部署 + GitLab Runner注册及标签使用指南
  • 从原理到实践:一文掌握Kafka的消息生产与消费
  • Unity:GUI笔记(一)——文本、按钮、多选框和单选框、输入框和拖动条、图片绘制和框绘制
  • 从零部署Nacos:替代Eureka的服务注册与服务发现基础教程
  • WPS文字和Word:不只是表格,段落也可以排序
  • 文字转语音 edge_tts
  • 微内核与插件化设计思想及其在前端项目中的应用
  • PostgreSQL 范围、空间唯一性约束
  • 用 Apache Iceberg 与 Apache Spark 在 Google Cloud 打造高性能、可扩展的数据湖仓
  • Flink运行时的实现细节
  • SQL 语言分类
  • Spark 运行流程核心组件(一)作业提交
  • 数据量暴涨时,抓取架构该如何应对?
  • 开发npm包【详细教程】
  • Bevy渲染引擎核心技术深度解析:架构、体积雾与Meshlet渲染
  • C++Linux八股
  • 08--深入解析C++ list:高效操作与实现原理
  • K8S 节点初始化一键脚本(禁用 SELinux + 关闭 swap + 开启 ipvs 亲测实用)
  • 微前端架构:原理、场景与实践案例
  • 前端JS处理时间,适用于聊天、操作记录等(包含刚刚、x分钟前、x小时前、x天前)
  • Windows已经安装了一个MySQL8,通过修改配置文件的端口号跑2个或多个Mysql服务方法,并注册为系统服务
  • lesson36:MySQL从入门到精通:全面掌握数据库操作与核心原理
  • 嵌入式系统学习Day17(文件编程)
  • 项目实战2——LAMP_LNMP实践
  • 智能化评估体系:数据生产、在线化与自动化的三重奏
  • 解锁 Appium Inspector:移动端 UI 自动化定位的利器
  • 【论文阅读】一种基于经典机器学习的肌电下肢意图检测方法,用于人机交互系统
  • Secure CRT做代理转发