Coins

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

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<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
typedef long long ll;
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 100005
int a[maxn],c[maxn],dp[maxn];
int main(){
int n,m;
while(cin >> n >> m){
if(!n && !m){
break;
}
for(int i=;i<n;i++){
cin >> a[i];//a[i]既是物体的体积,又是物体的价值
}
for(int i=;i<n;i++){
cin >> c[i]; //c[i]是物体的数量
}
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
if(a[i]*c[i]>=m){//体积乘以数量大于总体积,说明不能完全装完,相当于有无穷件,用完全背包
for(int j=a[i];j<=m;j++){
dp[j] = max(dp[j],dp[j-a[i]]+a[i]);
}
}
else{//可以装完,用01背包
int k = ;
while(k<c[i]){//二进制优化
for(int j=m;j>=a[i]*k;j--){
dp[j] = max(dp[j],dp[j-a[i]*k]+a[i]*k);
}
c[i] -= k;
k *= ;
}
for(int j=m;j>=a[i]*c[i];j--){
dp[j] = max(dp[j],dp[j-a[i]*c[i]]+a[i]*c[i]);
}
}
}
int count = ;//计数
for(int i=;i<=m;i++){
if(dp[i] == i){
count++;//可以组合且不用找钱
}
}
cout << count << endl;
}
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 Coin 多重背包

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

  7. 背包系列练习及总结(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 ...

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

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

  9. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

随机推荐

  1. 关于FFT分析音频的学习

    本文部分知识从以下文章学习: https://zhuanlan.zhihu.com/p/19763358 傅里叶变换的知识 https://www.cnblogs.com/RabbitHu/p/FFT ...

  2. 优雅的对象转换解决方案-MapStruct及其入门(一)

    第一次看到 MapStruct 的时候, 我个人非常的开心. 因为其跟我内心里面的想法不谋而合. 1 MapStruct 是什么? 1.1 JavaBean 的困扰 对于代码中 JavaBean之间的 ...

  3. 使用 OpenSSL 为 Nginx 创建自签名证书 并开启客户端身份验证

    本文章默认读者了解Openssl,CA,网站证书相关知识,直接实战!配置完成后,浏览器会显示"安全的HTTPS"连接.不会像其他文章那样,是红色警告的证书提示. 准备环境 笔者使用 ...

  4. APPCAN   版本控制SVN

      1.检出代码 checkout 常规的操作     appcan 中,在官网新建一个项目后,就会有一项目的svn 地址,而且已经是主干分支了,这个是项目的位移目录,不能再trunk目录同级创建分子 ...

  5. 【C/C++】随机数的生成

    C/C++:rand()函数 rand()函数的头文件:#include<stdlib.h> 该函数产生的随机数随机性差,速度慢,周期小(0-32767) 用法如下所示: #include ...

  6. Duilib的圆环形 进度条 实现(网易云信版本)

    /** @file CircleProgress.h* @brief 圆环型进度条控件,圆环中间可以有文本(如85%)* @copyright (c) 2019-2022, NetEase Inc. ...

  7. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. Visual Studio 2015&2017 key

    Visual Studio 2015 key Key : HMGNV-WCYXV-X7G9W-YCX63-B98R2 Visual Studio Enterprise 2015 Key :HM6NR- ...

  9. MySQL--单表查询、多表查询简单概述

    表的前期准备: create table emp( id int not null unique auto_increment, name ) not null, sex enum('male','f ...

  10. [FJOI2015]火星商店问题(线段树分治,可持久化,Trie树)

    [FJOI2015]火星商店问题 前天考了到线段树分治模板题,全场都切了,就我不会QAQ 于是切题无数的Tyher巨巨就告诉我:"你可以去看看火星商店问题,看了你就会了." 第一道 ...