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
 
题目就是让你用所给的 种类一定,数目一定的硬币,看能组成的数字有那先(当然,询问范围是1 ~ m)
还是列出已知条件,硬币的种类,每类的个数,查询范围(1 ~ m)
在多重背包里,我们用到的条件有:背包容量,物品种类,物品每类的数量, 物品每类所用的体积大小
抽象这道题目,我们直观的知道,硬币的种类,硬币每类的数量,每类硬币的面值。题目里只有这 3 个条件, 做背包问题一定会涉及“物品占用体积的大小”,而这道题完全没有提 ,因为我们最后求的是所能组成面值的总数。 这里提一下,我们的面值,既可以当作物品的重量,又可以当作物品占的体积
 
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
const int max_size = + ;
const int MAX = ;
int dp[max_size];
bool vis[max_size]; int main()
{
//1.将问题的模型抽象出来
int cnt, vol;
int val[MAX];
int num[MAX];
while(scanf("%d %d", &cnt, &vol) != EOF)
{
memset(dp, , sizeof(dp));
memset(vis, false, sizeof(vis));
if(cnt == && vol == )
break;
for(int i = ; i < cnt; i++)
scanf("%d", val+i);
for(int i = ; i < cnt; i++)
scanf("%d", num+i); for(int i = ; i < cnt; i++)
{
if(val[i] * num[i] >= vol)
{
//CompletePack(val[i], val[i]); //那么多的价值,那么多的占用? for(int j = val[i]; j <= vol; j++) ///多重背包这里错了两次了,要注意,昨天找了一晚上
{
dp[j] = max(dp[j], dp[j - val[i]] + val[i]);
vis[dp[j]] = true;
}
continue;
}
int k = ;
while(k < num[i])
{
//ZeroOnePack(k*val[i], k*val[i]);
for(int j = vol; j - k * val[i] >= ; j--)
{
dp[j] = max(dp[j], dp[j-k*val[i]] + k*val[i]);
vis[dp[j]] = true;
}
num[i] -= k;
k *= ;
}
//ZeroOnePack(num[i]*val[i], num[i]*val[i]);
for(int j = vol; j - num[i]*val[i] >= ; j--)
{
dp[j] = max(dp[j], dp[j - num[i]*val[i]] + num[i]*val[i]);
vis[dp[j]] = true;
}
} int ans = ;
for(int i = ; i <= vol; i++)
{
ans += (vis[i] == true) ? : ;
}
printf("%d\n", ans);
}
return ;
}

HDU-2844 Coins(多重背包)的更多相关文章

  1. hdu 2844 Coins (多重背包+二进制优化)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...

  2. HDu -2844 Coins多重背包

    这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...

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

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

  4. HDU 2844 Coins (多重背包计数 空间换时间)

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

  5. hdu 2844 coins(多重背包 二进制拆分法)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  6. hdu 2844 Coins 多重背包(模板) *

    Coins                                                                             Time Limit: 2000/1 ...

  7. HDU 2844 Coin 多重背包

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

  8. 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)

    作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...

  9. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  10. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

随机推荐

  1. Struts2拦截器之FileUploadInterceptor

    一.它能做什么? 借助于这个拦截器我们可以实现文件的上传和下载功能. 理论部分: struts2的文件上传下载功能也要依赖于Apache commons-fileupload和Apache commo ...

  2. hdu 2546饭卡

    用5块钱去买最贵的物品,用剩下的m-5块去买尽量多的物品 #include<stdio.h> #include<math.h> #include<vector> # ...

  3. MVC – 4.mvc初体验(2)

    5.显示学员列表 效果 数据表 5.1 首先,在文件夹Models新建一个新建项(W),选择ADO.NET 实体数据模型 (SingleTest.edmx) 5.2 建一个控制器,StudentsCo ...

  4. Faster-rnnlm代码分析2 - HSTree的构造

    也就是构造一棵Huffman Tree,输入是按照词汇频次由高到低排序的 采用层次SoftMax的做法,是为了使得训练和预测时候的softmax输出加速,原有multinomal softmax,是和 ...

  5. python中如何用dis模块来查看py的汇编代码?

    之前测试不成功,用导入dis的方式. 但如何在命令行里加入 -m dis,就会OK啦. python -m dis test.py #coding: utf8 x = [1, 2, 3] for i ...

  6. 攻城狮在路上(壹) Hibernate(十六)--- Hibernate声明数据库事务

    一.数据库事务的概念: 数据库的ACID特征:Atomic.Consistency.Isolation.Durability.原子性.一致性.隔离性.持久性.不同的隔离级别引发的不同问题. 事务的AC ...

  7. hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***

    新模板 /* HDU 4273 Rescue 给一个三维凸包,求重心到表面的最短距离 模板题:三维凸包+多边形重心+点面距离 */ #include<stdio.h> #include&l ...

  8. linux下搭建属于自己的博客(WordPress安装)

    转自:http://www.cnblogs.com/xiaofengkang/archive/2011/11/16/2251608.html WordPress简介 WordPress 是一种使用 P ...

  9. 有了iscsi存储怎么让主机识别以及使用创建lvm

    1.查找安装包:rpm -ivh iscsi-initiator-utils去sf.net下载iscsitarget包make kernel,usr,install开启服务 (0)查看iscsi发现记 ...

  10. 《大话》之 装饰模式 Vs 建造者模式

    一.简介: 装饰模式:     背景:小菜要见美女娇娇,感慨自己不会着装,怕给娇娇留下坏印象               内容:动态的给一个对象添加一些额外职责               图文并茂: ...