题目

Coins

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

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

题意:Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[i]要买的物品价值不超过m

输入:第一行输入n和m,第二行输入n个硬币的面值和n个硬币的数量,输入0 0结束

输出:1到m之间有多少价格Tony可以支付

分析

一道多重背包

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define re register int
#define fp(i,a,b) for(re i=a,I=b;i<=I;++i)
#define fd(i,a,b) for(re i=a,I=b;i>=I;--i)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
using namespace std;
const int INF=0x7fffff;
inline int read() {
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-48,ch=getchar();
return x*w;
}
int a[100+10],c[100+10],f[10000]; void Obag(int m,int v,int w) { //0-1背包 m背包的总容量、v物品的体积、w物品的价值
for(int i=m; i>=v; i--)
f[i]=max(f[i],f[i-v]+w);
} void Cbag(int m,int v,int w) { //完全背包 m背包的总容量、v物品的体积、w物品的价值
for(int i=v; i<=m; i++)
f[i]=max(f[i],f[i-v]+w);
} void Mbag(int m,int v,int w,int num) { //多重背包 m背包的总容量、v物品的体积、w物品的价值、num物品的数量
if(v*num>=m) {
Cbag(m,v,w);
return ;
}
int k=1;
for(k=1; k<=num; k<<=1) {
Obag(m,k*v,k*w);
num=num-k;
}
if(num)
Obag(m,num*v,num*w);
} int main() {
//file("s");
int n,m;
n=read();
m=read();
while(n||m) {
fp(i,1,n) a[i]=read();
fp(i,1,n) c[i]=read();
fp(i,1,m) f[i]=-INF;
f[0]=0;
fp(i,1,n) {
Mbag(m,a[i],a[i],c[i]);
}
fp(i,1;m) if(f[i]>0) sum++;
cout<<sum<<endl;
}
return 0;
}

【题解】coin HDU2884 多重背包的更多相关文章

  1. 【bzoj1531】[POI2005]Bank notes 多重背包dp

    题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值 ...

  2. HDU 2844 Coin 多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. Lightoj 1231 - Coin Change (I) (裸裸的多重背包)

    题目链接: Lightoj  1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...

  4. hdu2844 &amp; poj1742 Coin ---多重背包--两种方法

    意甲冠军:你有N种硬币,每个价格值A[i],每个号码C[i],要求. 在不超过M如果是,我们用这些硬币,有多少种付款的情况下,.那是,:1,2,3,4,5,....,M这么多的情况下,,你可以用你的硬 ...

  5. 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)

    Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...

  6. 题解报告:hdu 1171 Big Event in HDU(多重背包)

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  7. 题解报告:hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

    Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品, ...

  8. 洛谷P1776 宝物筛选 题解 多重背包

    题目链接:https://www.luogu.com.cn/problem/P1776 题目大意: 这道题目是一道 多重背包 的模板题. 首先告诉你 n 件物品和背包的容量 V ,然后分别告诉你 n ...

  9. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

随机推荐

  1. 使用TK框架中updateByPrimaryKey与updateByPrimaryKeySelective区别

    int updateByPrimaryKey(T var1); int updateByPrimaryKeySelective(T var1); updateByPrimaryKeySelective ...

  2. DWVA--File Inclusion

    文件包含漏洞 先来了解一下什么是文件包含 因为程序开放人员通常会把可重复使用的函数写到单个文件中,在需要使用到这些函数时候,就可以 直接调用这个文件,这种对文件的调用过程就被称为文件包含. 文件包含漏 ...

  3. 缓冲流以及JAVA路径相关问题

    缓冲流 缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO 次数,从而提高读写的效率. 字节缓冲流 按字节处理 字符缓冲流 按字符处理 实例练习:文 ...

  4. 『居善地』接口测试 — 3、Requests库介绍

    目录 1.Requests库 2.Requests库文档 3.Requests库安装 4.Requests库的使用 (1)使用步骤 (2)示例练习 5.补充:Json数据和Python对象互相转化 1 ...

  5. Python数模笔记-(1)NetworkX 图的操作

    1.NetworkX 图论与网络工具包 NetworkX 是基于 Python 语言的图论与复杂网络工具包,用于创建.操作和研究复杂网络的结构.动力学和功能. NetworkX 可以以标准和非标准的数 ...

  6. 初识Vue2(一):表单输入绑定(附Demo)

    在线演示 http://demo.xiongze.net/ 下载地址 https://gitee.com/xiongze/Vue2.git js引用 <!--这里可以自己下载下来引用,也可以使用 ...

  7. C++知识概要

    static的用法和作用 在全局变量前加上关键字 static,全局变量就定义成一个全局静态变量.存储在静态存储区,在整个程序运行期间一直存在.同时全局静态变量在声明他的文件之外是不可见的 在局部变量 ...

  8. powercli创建虚拟机步骤及批量创建脚本

    https://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FSet-OSCust ...

  9. http协议工作原理及工作流程

    什么是url ? url = 协议 + 域名 + 资源路径 比如: https://www.baidu.com/index.html http : 超文本传输协议 https: 安全套接字协议 HTT ...

  10. kvm虚拟化网络管理(5)

    一.Linux Bridge网桥管理 网络虚拟化是虚拟化技术中最复杂的部分,也是非常重要的资源. 第一节中我们创建了一个名为br0的linux-bridge网桥,如果在此网桥上新建一台vm,如下图: ...