概述
Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11791 Accepted Submission(s): 4702
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
Source
2009 Multi-University Training Contest 3 - Host by WHU
Recommend
gaojie | We have carefully selected several similar problems for you: 2159 1203 2845 2870 2577
题意:
给你n和m分别代表n种银币,要买的东西不高于m元
然后给你2*n个数据,前n个是价值,后n个是个数
然后问你,重新组合这些硬币,能组合出多少组不大于m的组合?
就是1到m有几种情况。
这道题,考虑到有两个情况:1、物品可拿有n件 2、物品可拿数量有上限
所以我们考虑使用多重背包。
这里又要考虑使用多重背包的二进制优化,否则三个循环,会超时。
Problem : 2844 ( Coins ) Judge Status : Accepted
RunId : 17217268 Language : G++ Author : 1136242673
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
RunId : 17217268 Language : G++ Author : 1136242673
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<map> #define max(a,b)(a>b?a:b) #define min(a,b)(a<b?a:b) typedef long long ll; using namespace std; #define N 100010 int dp[N]; int v[N],num[N]; int m,n; void ZeroOnePack(int weight,int value) { for(int i=m;i>=weight;i--) dp[i]=max(dp[i],dp[i-weight]+value); } void CompletePack(int weight,int value) { for(int i=weight;i<=m;i++) dp[i]=max(dp[i],dp[i-weight]+value); } void MulPack(int weight,int value,int cut) { if(weight*cut>=m) CompletePack(weight,value); else { int k=1; while(k<=cut) { ZeroOnePack(k*weight,k*value); cut=cut-k; k=k*2; } ZeroOnePack(cut*weight,cut*value); } } int main() { int i; while(scanf("%d %d",&n,&m),m+n) { memset(dp,0,sizeof(dp)); for(i=0;i<n;i++) scanf("%d",&v[i]); for(i=0;i<n;i++) scanf("%d",&num[i]); for(i=0;i<n;i++) MulPack(v[i],v[i],num[i]); int ans=0; for(i=1;i<=m;i++) { if(dp[i]==i) ans++; } printf("%dn",ans); } return 0; }
[ Copy to Clipboard ]
[ Save to File]
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; bool dp[120003]; int w[101],c[101]; int main() { int n, m, i, j; while(~scanf("%d%d",&n,&m)) { if(!m&&!n)break; for(i=1;i<=n;i++) scanf("%d",&w[i]); for(i=1;i<=n;i++) scanf("%d",&c[i]); memset(dp,0,sizeof(dp)); dp[0]=1; //多重背包 for(i=1;i<=n;i++) { if(c[i]*w[i]>=m) { for(j=w[i];j<=m;j++) if(dp[j-w[i]])dp[j]=1; } else { int k=1; while(k<c[i]) { for(j=m;j>=w[i]*k;j--) if(dp[j-w[i]*k])dp[j]=1; c[i] -= k; k*=2; } for(j=m;j>=w[i]*c[i];j--) if(dp[j-w[i]*c[i]])dp[j]=1; } } int num=0; for(i=1;i<=m;i++) if(dp[i])num++; printf("%dn",num); } return 0; }
最后
以上就是紧张饼干为你收集整理的hdu 2844 多重背包 (模版题)Coins的全部内容,希望文章能够帮你解决hdu 2844 多重背包 (模版题)Coins所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复