Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20860    Accepted Submission(s): 8198

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.

 
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

多重背包的模板题

 #include <iostream>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n,m,a[],c[],dp[];
void comdp(int w,int v)
{
int i;
for(i=w; i<=m; i++)
dp[i]=max(dp[i],dp[i-w]+v);
}
void zeroone(int w,int v)
{
int i;
for(i=m; i>=w; i--)
dp[i]=max(dp[i],dp[i-w]+v);
}
void multidp(int w,int v,int cnt)//此时开始多重背包,dp[i]表示背包中重量为i时所包含的最大价值
{
if(cnt*w>=m)//此时相当于物品数量无限进行完全背包
{
comdp(w,v);
return;
}
int k=;//否则进行01背包转化,具体由代码下数学定理可得
while(k<=cnt)
{
zeroone(k*w,k*v);
cnt-=k;
k*=;
}
zeroone(cnt*w,cnt*v);
return ;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)break;
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)scanf("%d",&c[i]);
memset(dp,,sizeof(dp));
int ans=;
for(int i=;i<=n;i++)
{
multidp(a[i],a[i],c[i]);//重量和价值都设为a[i],硬币的价值
}
for(int i=;i<=m;i++)//因为重量和价值都是a[i],所以dp[i]就表示在重量为i时的价值,这题的答案就是数一下有多少个价值是在1到m之间的
{
if(dp[i]==i)ans++; }
printf("%d\n",ans);
}
return ;
}

hdu2844Coins(多重背包模板)的更多相关文章

  1. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

  2. 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活--hdu2191(多重背包模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 标准的多重背包 题目 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是 ...

  3. 解题报告:hdu2191汶川地震 - 多重背包模板

    2017-09-03 17:01:36 writer:pprp 这是一道多重背包裸题 - 记得是从右向左进行,还有几点需要注意啊,都在代码中表示出来了 代码如下: /* @theme:hdu2191 ...

  4. 【多重背包模板】poj 1014

    #include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...

  5. [51nod]多重背包模板

    https://www.51nod.com/tutorial/course.html#!courseId=11 题目大意: 有$N$种物品和一个容量为$W$的背包.第$i$种物品最多有$c[i]$件可 ...

  6. 多重背包模板 51Nod 1086

    有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...

  7. hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)

    题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活                                   ...

  8. 01背包模板、全然背包 and 多重背包(模板)

    转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...

  9. Coins(HDU 2844):一个会超时的多重背包

    Coins  HDU 2844 不能用最基础的多重背包模板:会超时的!!! 之后看了二进制优化了的多重背包. 就是把多重转变成01背包: 具体思路见:http://www.cnblogs.com/tt ...

随机推荐

  1. PHP最全防止sql注入方法

    (1)mysql_real_escape_string -- 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集 使用方法如下: $sql = "select count ...

  2. laravel 资料

    1.http://maxoffsky.com/maxoffsky-blog/building-a-shop-with-laravel-tutorial-series-announcement/  一篇 ...

  3. 用 Visual Studio Code 调试运行在 homestead 环境中的 laravel 程序

    由于之前做 .net 开发比较熟悉 visualstudio,所以自 visualstudio code 发布后就一直在不同场合使用 vscode ,比如前端.node等等.最近在做 laravel ...

  4. 【转载】MySQl 数据库插入加锁分析

    http://yeshaoting.cn/article/database/mysql%20insert%E9%94%81%E6%9C%BA%E5%88%B6/

  5. 用python解析word文件(一):paragraph

    太长了,我决定还是拆开三篇写.   (一)段落篇(paragraph)(本篇) (二)表格篇(table) (三)样式篇(style) 选你所需即可.下面开始正文. 最近公司的项目,需要在页面上显示w ...

  6. NSProxy应用例子

    动态代理模式的应用很多,特别是在不能修改被代理类的前提下,要对执行某些方法时需要打log或者捕捉异常等处理时,是一个非常方便的方法.只需要少量修改客户端(场景类)代码和添加一个代理类就可以实现,这个符 ...

  7. ABAP知识点提纲

    编号 课程名称 课程内容 预计课时 10.1.1~10.1.2 SAP系统与产品集 1. 了解SAP常见产品 ,了解SAP系统架构 1 10.1.3~10.1.4 导航界面与用户界面 1. 了解SAP ...

  8. Python - 格式化字符串的用法

    0. 摘要 Python支持多种格式化字符串的方法,包括%-fromatting.str.format().f-strings三种,f-strings是Python3.6以后出现的一种新方法,相比其他 ...

  9. 创建JDBCUtils工具类

    JDBCUtils工具类 私有化构造函数,外界无法直接创建对象 提供公共的,静态的,getConnection 方法,用来给外界提供数据库连接 提供公共的,静态的,close方法,用来释放资源 pac ...

  10. ORP计

    ORP计 ORP计通过测量铂或金电极与参比电极之间的电位差,转换成氧化还原电位信号.氧化还原电位测量用电极可与转换放大器组合,转换放大器部分与pH测量用相同.ORP计可用于排水处理(氰基处理.铬酸处理 ...