Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory
Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9826    Accepted Submission(s): 3916

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

题意:有n中面值的硬币每种面值的硬币数量已知问这些硬币能组成1-m中的几种多重背包已m作为背包容量在1-m中当有物品的价值等
于背包容量时即能组成该类型。




解题心得:
1、就是一个多重背包的二进制优化问题,题目要求的是一个最后表价格可能的个数。
2、关于多重背包的二进制优化问题(多重背包的二进制优化
#include<bits/stdc++.h>
using namespace std;
const int maxn1 = 1e5+100;
const int maxn2 = 110;
int n,m;
bool dp[maxn1];
struct Coin
{
int num;
int va;
} c[maxn2]; int pre_coin()
{
int sum = 0;
for(int i=1; i<=n; i++)
scanf("%d",&c[i].va);
for(int i=1; i<=n; i++)
{
scanf("%d",&c[i].num);
sum += c[i].num * c[i].va;
}
}
int main()
{
while(scanf("%d%d",&n,&m) && n+m)
{
memset(dp,0,sizeof(dp));
dp[0] = true;
pre_coin();
for(int i=1; i<=n; i++)
{
int cnt = 0;
for(int k=1; k<=c[i].num; k*=2)//k*=2二进制优化
{
cnt = k*c[i].va;
for(int j=m; j>=cnt; j--)
if(dp[j-cnt])
dp[j] = true;
c[i].num -= k;
} if(cnt)
{
cnt = c[i].num * c[i].va;
for(int j=m; j>=cnt; j--)
{
if(dp[j-cnt])
dp[j] = true;
}
}
} int Sum = 0;//记录可能是手表价格的个数
for(int i=m; i>=1; i--)
if(dp[i])
Sum ++;
printf("%d\n",Sum);
}
}



动态规划:HDU2844-Coins(多重背包的二进制优化)的更多相关文章

  1. HDU-2844 Coins 多重背包 物品数量二进制优化

    题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...

  2. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. hdu2844 Coins -----多重背包+二进制优化

    题目意思:给出你n种硬币的面额和数量,询问它能够组合成1~m元中的几种情况. 这题如果直接按照完全背包来写的话,会因为每一种硬币的数目1 ≤ Ci ≤ 1000而超时,所以这里需要运用二进制优化来解决 ...

  4. luogu||P1776||宝物筛选||多重背包||dp||二进制优化

    题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...

  5. CodeForces922E DP//多重背包的二进制优化

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

  6. 动态规划:HDU1059-Dividing(多重背包问题的二进制优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. dp之多重背包(二进制优化)

    void solve(int v,int w,int c){    int count=0;    for(int k=1;k<=c;k<<=1)    {        val[c ...

  8. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  9. HDU-2844 Coins(多重背包)

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

随机推荐

  1. mysql连接error,Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection .....

    完整error Establishing SSL connection without server's identity verification is not recommended. Accor ...

  2. C#数据库(MSSQL)帮助类

    /// <summary> /// 数据库帮助类 /// <author>Devin</author> /// </summary> public se ...

  3. Bootstrap学习笔记(三)

    三.组件 1.图标字体 图标字体本质是文字,而不是图形!可以无限放大或修改颜色. 使用方式:<span class="glyphicon glyphicon-XXX"> ...

  4. mysql-作业

    一.表关系 请创建如下表,并创建相关约束                 班级表:class       学生表:student       cid caption grade_id   sid sn ...

  5. CF1152C Neko does Maths

    思路: 假设a <= b,lcm(a + k, b + k) = (a + k) * (b + k) / gcd(a + k, b + k) = (a + k) * (b + k) / gcd( ...

  6. JavaScript常用数组操作方法

    数组是用于储存多个相同类型数据的集合,平时在数据的处理中用到最多,JavaScript 中常用的操作方法 1.concat()concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅 ...

  7. ECLIPSE 取消自动更新

    经常遇到一开eclipse 时,一直很卡的问题,发现是它一直尝试联网更新东西 ,如maven 所以解决办法  , eclipse 取消自动更新的方法: 1. window --> prefere ...

  8. FTP添加虚拟目录(图)

    设置 访问效果 可以看到有这个文件:

  9. 推荐一个VS2015 插件 Favorite Documents

    随着解决方案越来越庞大,查找某个文件变的非常费神,考眼力 有了这个工具我们可以将常用的几个文件或文件夹添加到收藏夹中,随时展开双击即可到达收藏位置 从 视图>其他窗口中打开     安装 在Vi ...

  10. 无效的 JSON 基元 解决办法

    在AJAX中进行如下修改: 加入: dataType: "json", 移除: contentType: 'application/json', 然后检查参数名称,类型是否符合后台 ...