Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4428 Accepted Submission(s): 1781
Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the
price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed
by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
Sample Output
8 4多重背包+二进制优化AC代码:#include <iostream> #include <cstring> using namespace std; int dp[100005],a[105],c[105],n,m,cnt; void zeroonepack(int v) { for(int j=m;j>=v;j--) { if(dp[j]==0&&dp[j-v]!=0) { cnt++; dp[j]=1; } } } int main() { while(cin>>n>>m) { if(!n&&!m) break; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) cin>>c[i]; memset(dp,0,sizeof(dp)); cnt=0; dp[0]=1; for(int i=1;i<=n;i++) { int k=1; while(k<c[i]) { zeroonepack(k*a[i]); c[i]-=k; k*=2; } zeroonepack(c[i]*a[i]); } cout<<cnt<<endl; } }

本文介绍了一种解决多重背包问题的方法,并通过一个示例程序详细展示了如何使用二进制优化技术来计算在给定硬币数量和价值的情况下,能够支付的不同价格的数量。问题背景为一位顾客尝试使用不同面额的硬币购买商品,且已知硬币数量。
5万+

被折叠的 条评论
为什么被折叠?



