Coins

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

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
 
Source
 
 
题意:有不同面值的 相应数量不同n种硬币  问1~m元的价格有哪些 可以由硬币组成 f[i]==i
 
题解: 多重背包    二进制优化
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int n,m;
struct node
{
int a,c;
} N[];
int f[];
int ans;
int value[],size[];
int count;
void slove(int q)
{
count=;
for(int i=;i<=q;i++)
{
int c=N[i].c,v=N[i].a;
for (int k=; k<=c; k<<=)
{
value[count] = k*v;
size[count++] = k*v;
c -= k;
}
if (c > )
{
value[count] = c*v;
size[count++] = c*v;
}
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==&&m==)
break;
for(int i=; i <= m; i++)
f[i] = ;
ans=;
for(int i=;i<=n;i++)
scanf("%d",&N[i].a);
for(int i=;i<=n;i++)
scanf("%d",&N[i].c);
slove(n);
for(int i=;i<count;i++)
for(int gg=m;gg>=size[i];gg--)
f[gg]=max(f[gg],f[gg-size[i]]+value[i]);
for(int i=;i<=m;i++)
if(f[i]==i)
ans++;
cout<<ans<<endl;
}
return ;
}
 

HDU 2844 二进制优化的多重背包的更多相关文章

  1. HDU 3591 (完全背包+二进制优化的多重背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...

  2. HDU 3732 Ahui Writes Word(多重背包)

    HDU 3732 Ahui Writes Word(多重背包) http://acm.hdu.edu.cn/showproblem.php? pid=3732 题意: 初始有N个物品, 每一个物品有c ...

  3. HDU 5445 Food Problem(多重背包+二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5445 题意:现在你要为运动会提供食物,总共需要提供P能量的食物,现在有n种食物,每种食物能提供 t 能量,体积为 ...

  4. 多重背包!!!(二进制优化的01背包)hdoj-2844

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  5. HDU 3732 Ahui Writes Word 多重背包优化01背包

    题目大意:有n个单词,m的耐心,每个单词有一定的价值,以及学习这个单词所消耗的耐心,耐心消耗完则,无法学习.问能学到的单词的最大价值为多少. 题目思路:很明显的01背包,但如果按常规的方法解决时间复杂 ...

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

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

  7. HDU 1059 Dividing 分配(多重背包,母函数)

    题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...

  8. HDU 5445——Food Problem——————【多重背包】

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  9. ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

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

随机推荐

  1. STM32CubeMx配置正交编码器遇到的问题

    配置时参考了这个哥们的方法: http://www.eemaker.com/stm32cubemx-encoder.html 然后我的配置是这样的 配置是没有问题. 调用时出现了问题. 由于配置完了, ...

  2. 洛谷(P1006 传纸条)

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个mm行nn列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运 ...

  3. 实现一个带有指纹加密功能的笔记本(Android)第一部分

    自己经常会忘记一些密码什么的,想把这些密码保存下来,但是别人做的软件总有一点不安全的感觉,所以自己动手做了一个带有指纹加密的笔记本. 以下是本工程用到的一些第三方包 compile 'org.gree ...

  4. Angularjs+bootstrap 实现横向滑屏

    本地环境: AngularJS v1.3.2 angular-ui-bootstrap Version: 0.12.0 - 2014-11-16 实现代码:Html部分 <div ng-cont ...

  5. 什么是 Cookie

    什么是 Cookie? Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递.Cookie 包含每次用户访问站点时 Web 应用程序都可以读取的信息. 例如,如果在用 ...

  6. linux里面的fork函数创建一个新进程

    由fork创建的新进程成为子进程,fork函数被调用一次,但是返回两次,

  7. 关于 SSH Server 的整体设定

    # . 关于 SSH Server 的整体设定,包含使用的 port 啦,以及使用的密码演算方式 Port # SSH 预设使用 这个 port,您也可以使用多的 port ! # 亦即重复使用 po ...

  8. 复制MySQL数据库A到另外一个MySQL数据库B(仅仅针对innodb数据库引擎)

    方案一:(不用太大的变化my.ini文件) copy 原数据库A中的   数据库(database)  ib_logfile1  ib_logfile0   ibdata1: 关闭目的数据库B: 备份 ...

  9. Android4.0系统以上程序不出现菜单键的问题解决

    去掉targetSdkVersion 或改为targetSdkVersion =13或更小.. 不改targetSdkVersion的办法:在onCreate() 里setContentView()之 ...

  10. Ngix配置,让所有请求指向同一个文件

    统一入口使所有请求在同一个文件先验证处理,Ngix添加如下代码: location / { try_files '' /index.php; }