ICPC训练联盟2021寒假冬令营 Dollar Dayz
作者:互联网
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; }dp
标签:Dayz,冬令营,int,Dollar,US,long,tools,spend,mod 来源: https://www.cnblogs.com/nightfury/p/14341826.html