H - Coins

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 //题意是 第一行两个整数 n 和 m (1 <= n <= 100)(m <= 100000) 然后第二行是 n 个硬币的价值,再是 n 个硬币的数量,
问这些硬币能组成多少个小于等于 m 的价值。
多重背包,有一点难吧,要用到二进制优化,直接当 0 1 背包处理要超时 一维多重背包二进制优化的思考:先看这个问题,100=1+2+4+8+16+32+37,观察可以得出100以内任何一个数都可以由以上7个数选择组合得到,所以对物品数目就不是从0都100遍历,而是0,1,2,4,5,16,32,37遍历,时间大大优化。 hud 340ms 另一个oj 187 ms
 #include <stdio.h>
#include <string.h> int a[],c[],b[];
bool dp[]; int main()
{
int n,m,i,j;
while (scanf("%d%d",&n,&m)&&n+m)
{
memset(dp,,sizeof(dp));
dp[]=;
for (i=;i<=n;i++)
scanf("%d",&a[i]);
for (i=;i<=n;i++)
scanf("%d",&c[i]);
for (i=;i<=n;i++)
{
if (a[i]*c[i]>m)
{
for (j=a[i];j<=m;j++)
if (!dp[j])
dp[j]=dp[j-a[i]];
}
else
{
if (c[i]==)
continue; int num=c[i];
int p=,t=;
while (p<num)//二进制优化成 01 背包问题
{
num-=p;
b[t++]=p*a[i];
p*=;
}
b[t++]=num*a[i]; for (p=;p<t;p++)
for (j=m;j>=b[p];j--)
if (!dp[j])
dp[j]=dp[j-b[p]];
}
}
j=;
for (i=;i<=m;i++)
if (dp[i])
j++;
printf("%d\n",j);
}
return ;
}

 


H - Coins的更多相关文章

  1. 2016huasacm暑假集训训练五 H - Coins

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...

  2. 2012-2014 三年浙江 acm 省赛 题目 分类

    The 9th Zhejiang Provincial Collegiate Programming Contest A    Taxi Fare    25.57% (166/649)     (水 ...

  3. 2019 Multi-University Training Contest 10

    目录 Contest Info Solutions C - Valentine's Day D - Play Games with Rounddog E - Welcome Party G - Clo ...

  4. H - Gold Coins(2.4.1)

    H - Gold Coins(2.4.1) Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:3000 ...

  5. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  6. Coins

    Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hi ...

  7. hdu 1398 Square Coins (母函数)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. Coins(HDU 2844):一个会超时的多重背包

    Coins  HDU 2844 不能用最基础的多重背包模板:会超时的!!! 之后看了二进制优化了的多重背包. 就是把多重转变成01背包: 具体思路见:http://www.cnblogs.com/tt ...

  9. hdu 2844 多重背包coins

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意: 有n个硬币,知道其价值A1.....An.数量C1...Cn.问在1到m价值之间,最多能组成多少种 ...

随机推荐

  1. python 读取CSV文件 中文乱码

    今天读取一个CSV文件,打印出来,中文显示乱码,原因是编码的缘故,CSV保存是编码格式ANSI,解决办法是以记事本方式打开CSV文件,然后另存为时编码选择UTF-8进行保存即可.

  2. 【机器学习】Mahout算法集

    在Mahout实现的机器学习算法见下表 算法类 算法名 中文名 分类算法 Logistic Regression 逻辑回归 Bayesian 贝叶斯 SVM 支持向量机 Perceptron 感知器算 ...

  3. 文本文件打印类库(C#)

    我写了一个打印文本文件的类库,功能包含:打印预览.打印.打印时能够选择打印机.能够指定页码范围. 调用方法很easy: TextFilePrinter p = new TextFilePrinter( ...

  4. Node.js 使用jQuery取得Nodejs http服务端返回的JSON对象示例

    server.js代码: // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http" ...

  5. POJ 1040 Transportation

    链接:http://poj.org/problem?id=1040 Transportation Time Limit: 1000MS Memory Limit: 10000K Total Submi ...

  6. 使用 react-native-tab-navigator 创建 TabBar 组件

    1.首先安装好ReactNative的运行环境,安装组件依赖库 使用npm install react-native-tab-navigator --save安装所依赖的第三方库 2.导入 impor ...

  7. python第一个web程序

    例一: import web urls= ('/(.*)','index') app= web.application(urls,globals()) class index: def GET(sel ...

  8. Android:使用ZXing生成二维码(支持加入Logo图案)

    ZXing是谷歌的一个开源库.能够用来生成二维码.扫描二维码.本文所介绍的是第一部分. 首先上效果图: ZXing相关各种文件官方下载地址:https://github.com/zxing/zxing ...

  9. lvs+keepalived+bind实现负载均衡高可用智能dns

    整体架构: 1.IP地址规划: Dns1:172.28.0.54 Dns2:172.28.0.55 Dr服务器主:172.28.0.57 Dr服务器从:172.28.0.67 Vip:172.28.0 ...

  10. 征信报告页面的input验证收集

    https://ipcrs.pbccrc.org.cn/ function checkLoginName() { var loginName = $.trim($("#loginname&q ...