Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:
1 @ US$3 + 1 @ US$2Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).
1 @ US$3 + 2 @ US$1
1 @ US$2 + 3 @ US$1
2 @ US$2 + 1 @ US$1
5 @ US$1
Input
A single line with two space-separated integers: N and K.
Output
A single line with a single integer that is the number of unique ways FJ can spend his money.
Sample Input
5 3
Sample Output
5
完全背包+高精度
高精度也可以是把数分成前半部分和后半部分,分别算。注意,要先算前半部分,否则后半部分取完mod再除mod会一直是0。
同时,容易知道(dp1[j]+dp1[j-price[i]])/mod ≤ 1 ,故先算前半部分不用担心错误
提一点,前半部分的计算也是状态的转移。

#include<iostream> #include<cstdio> using namespace std; const long long mod=1e18; int price[105]; long long dp1[1005],dp2[1005]; int main() { int a,k; cin>>a>>k; for(int i=1;i<=k;i++) price[i]=i; dp1[0]=1; for(int i=1;i<=k;i++) for(int j=price[i];j<=a;j++) { dp2[j]=(dp1[j]+dp1[j-price[i]])/mod+dp2[j]+dp2[j-price[i]]; dp1[j]=(dp1[j]+dp1[j-price[i]])%mod; } if(dp2[a]) cout<<dp2[a]; cout<<dp1[a]; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现