题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2844

Coins

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)

问题描述

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.

输入

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.

输出

For each test case output the answer on a single line.

样例输入

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

样例输出

8
4

题解

裸的多重背包,这里提供两种思路:
1、开个cntv数组维护一下第i个背包用了几次,限制一些非法的转移,像完全背包那一做一遍就ok。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printftypedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=101010;int cntv[maxn];
bool dp[maxn];
int v[maxn],c[maxn];int main() {int n,m;while(scf("%d%d",&n,&m)==2&&n){clr(dp,0);for(int i=1;i<=n;i++) scf("%d",&v[i]);for(int i=1;i<=n;i++) scf("%d",&c[i]);clr(dp,0);dp[0]=1;for(int i=1;i<=n;i++){clr(cntv,0);for(int j=v[i];j<=m;j++){if(!dp[j]&&dp[j-v[i]]&&cntv[j-v[i]]<c[i]){dp[j]=1;cntv[j]=cntv[j-v[i]]+1;}}}int ans=0;for(int i=1;i<=m;i++) ans+=dp[i];prf("%d\n",ans);}return 0;
}//end-----------------------------------------------------------------------

2、用二进制拆分转化成01背包。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printftypedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=101010;int cntv[maxn];
bool dp[maxn];
int v[maxn],c[maxn];int main() {int n,m;while(scf("%d%d",&n,&m)==2&&n){clr(dp,0);for(int i=1;i<=n;i++) scf("%d",&v[i]);for(int i=1;i<=n;i++) scf("%d",&c[i]);VI arr;for(int i=1;i<=n;i++){int j=1;//二进制拆分while(c[i]>0){if(c[i]>=j){arr.pb(v[i]*j);c[i]-=j;}else{arr.pb(v[i]*c[i]);c[i]=0;}j<<=1;}}clr(dp,0);dp[0]=1;for(int i=0;i<arr.sz();i++){for(int j=m;j>=arr[i];j--){dp[j]=dp[j]|dp[j-arr[i]];}}int ans=0;for(int i=1;i<=m;i++) ans+=dp[i];prf("%d\n",ans);}return 0;
}//end-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5917304.html

HDU 2844 Coins 多重背包相关推荐

  1. HDU - 2844 Coins(多重背包+完全背包)

    题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...

  2. HDU 2844 Coins (多重背包)

    题目链接 题意:Tony想要买一个东西,他只有n种硬币,每种硬币的面值为a[i],每种硬币的数量为c[i],要买的物品价值不超过m,输出1-m中有多少种价格Tony可以用硬币组合出来. 题解:多重背包 ...

  3. hdu 1171 dp(多重背包)

    View Code //hdu 1171 dp(多重背包)//题意:把所有物品的价值尽量分为相等的两份,不能等分的话 //后面那份可以稍小于前面的 //求出价值总和后,令价值的一半为背包容量,让背包尽 ...

  4. Coins (多重背包)模板题

    模板请看上一篇博客 Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibi ...

  5. (step3.3) hdu 1059(Dividing——多重背包)

    题目大意:分别给出价值为1~6的石头的数量.问能否将这些石头等价值平分... 解题思路:多重背包 1)多重背包的典型描述是这样的:给出n种物品,背包的容量为V.每种物品的可用数量为num[i],所占体 ...

  6. 【POJ3260】The Fewest Coins 多重背包+完全背包

    A来B处买东西,价值M元,有N种钱,每种钱A有一定数量,而B有无限数量. 求最少用多少张钞票可以满足交易,比如样例,A出50+25,B找5,即可满足,需要3张. A用多重背包转移状态,B用完全背包. ...

  7. HDU 1059 Dividing 多重背包

    这题想了想用母函数应该也可以做,不过得考虑一个细节,就是加起来的总和是否为奇数,我找了好久一直没找出错误,原来是没考虑奇数..  fuck  这种问题我怎么能不考虑奇数呢.....  啊 啊 啊啊 啊 ...

  8. Piggy-Bank HDU - 1114(多重背包)

    在 ACM 能够开展之前,必须准备预算,并获得必要的财力支持.该活动的主要收入来自于 Irreversibly Bound Money (IBM).思路很简单.任何时候,某位 ACM 会员有少量的钱时 ...

  9. HDU 2844 (多重背包)

    多重背包(二进制优化)模板 题意: 有n个硬币,每一个硬币有自己的数值Vi,其个数为Ci.不同硬币的不同组合能买到不同物品,问在1~V的区间内最多能买到多少不同种类的物品. 思路: 其实题意也就是求硬 ...

最新文章

  1. 算法周记(一)直接插入排序
  2. html文字绕图文字置顶,如何实现html文字绕排
  3. angular 控件css_Angular 4 设置组件样式的几种方式
  4. 在线阅读!!机器学习数学精华:线性代数
  5. 微软面向初学者的机器学习课程:1.2-机器学习的历史
  6. 行为设计模式:中介者
  7. 选择分集matlab程序,瑞利衰落信道下采用MRC分集误码性能Matlab程序
  8. (篇十)用结构体数组处理学生成绩、结构体类型函数求平均值
  9. 使用代码调用Attachments(附件)
  10. 项目中将orl改为pgsql函数
  11. python print文本和数字混合_详解Python中的文本处理
  12. 按摩椅控制板的开发让按摩椅变得简约智能
  13. 春晚红包花落拼多多 巨头们为何前赴后继抢春晚?
  14. Unity pc端内嵌网页插件Embedded Browser基本使用流程
  15. 【美团技术团队】2014年-2022年后端文章精选篇
  16. 从零开始的Unity萌导书#1:Hello,Unity! 1
  17. 音频降噪的软件有哪些?快来看看这些软件
  18. progress元素的使用
  19. 安搭Share:三星接班人李在镕或成韩方最富股东,持有票市值近百亿美元
  20. 有效衡量App多渠道推广效果

热门文章

  1. mysql产生大量数据_mysql语句批量产生大量测试数据
  2. java根据周数获取日期_java获取日期的周数和所属年份
  3. pythonlocust使用方法_python locust 性能测试:locust安装和一些参数介绍
  4. 安装eclipse的android adt 插件,eclipse安装ADT插件
  5. 两个分数化简比怎么化_怎么化行最简形矩阵?
  6. mysql like html_mysql - MySQL RLIKE查找,然后替换打开和关闭HTML标记之间的所有字符 - 堆栈内存溢出...
  7. Git基础操作及常见命令——详解
  8. 机器学习笔记——逻辑回归(Logistic Regression)
  9. python中list的切片和range函数
  10. python记录(5)- find() 与 rfind()