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