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. 安全预警-防范新型勒索软件“BlackRouter”

    近期,出现一种新型勒索软件“BlackRouter”,开发者将其与正常软件恶意捆绑在一起,借助正常软件的下载和安装实现病毒传播,并以此躲避安全软件的查杀.目前,已知的被利用软件有AnyDesk工具(一 ...

  2. SOAR平台初探(一)

    1.前言 Security Orchestration, Automation and Response(SOAR)安全编排和自动化响应,是Gartner2017年提出的新概念.Gartner预计到2 ...

  3. 10-19文献阅读之DCB

    ---恢复内容开始--- <一种精确估计区域北斗接收机硬件延迟的方法>-2016-,很有价值 估计北斗接收机硬件延迟,不需要传统复杂的电离层模型.已知一个参考站接收机硬件延迟的条件下,利用 ...

  4. java内部类案例

    实现键值对的存储输出 import java.util.Arrays; public class EntryDemoTest { //实现键值对的存储 public static void main( ...

  5. CURL的学习和应用

    curl安装: xp下面的安装 :修改php.ini文件的设置,找到php_curl.dll //取消下在的注释extension=php_curl.dll linux下面安装: # wget htt ...

  6. programming-languages学习笔记--第10部分

    programming-languages学习笔记–第10部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.sr ...

  7. Beautiful Report 异步并发测试html报告

    version_ :python3.7 下载BeautifulReport   https://github.com/TesterlifeRaymond/BeautifulReport/archive ...

  8. 4D产品(DLG、DEM、DOM、DRG)介绍及区别

    4D产品(DLG.DEM.DOM.DRG)是什么? 4D产品是指DRG(数字栅格地图).DLG(数字线化图) .DEM(数字高程模型).DOM(数字正射影像图).4D 复合产品是将4D产品中的任意两种 ...

  9. 使用Python编写简单网络爬虫抓取视频下载资源

    我第一次接触爬虫这东西是在今年的5月份,当时写了一个博客搜索引擎.所用到的爬虫也挺智能的,起码比电影来了这个站用到的爬虫水平高多了! 回到用Python写爬虫的话题. Python一直是我主要使用的脚 ...

  10. docker~dockertoolbox的加速器

    对于在win10以下的操作系统上跑docker,我们可以安装docker toolbox工具,下载安装后第一次启动它会从远程github上下载最新版的boot2docker镜像文件,40多兆,但下载非 ...