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. JavaWeb项目实现文件下载

    File file = new File(path);// path是根据日志路径和文件名拼接出来的 String filename = file.getName();// 获取日志文件名称 Inpu ...

  2. 2017.7.31 ELK+logback+redis的使用

    参考来自:spring mvc+ELK从头开始搭建日志平台 0 前提 ELK安装成功 redis安装成功 使用logback的项目运行成功 1 配置文件 1.1 pom.xml 为了使用logback ...

  3. Node.js 网页爬虫再进阶,cheerio助力

    任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...

  4. scss使用后的简单入门总结

    端午节第一天 将之前做的一个小demo的css样式改为了scss 好吧 改完了 赶紧由小兵 升级到中尉了 什么是scss? 我的理解是scss 就是css 的预处理器,使css变得更加富有逻辑. 有什 ...

  5. setTimeout应用 && 自动播放——幻灯片效果&& 自动改变方向——幻灯片效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 不是书评 :《我是一只IT小小鸟》

    本文转自刘未鹏 博客,写的非常的好 就转回来了 设计你自己的进度条 进度条的设计是一个很多人都知道的故事:同样的耗时,如果不给任何进度提示,只是在完成之后才弹出一个完成消息,中间没有任何动态变化,那么 ...

  7. 《Java设计模式》之调停者模式(Mediator)

    调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从而使它们能够较松散地耦合.当这些对象中的某些对象之间的相互作用发生改变时,不会马上影响到其它的一些 ...

  8. html中插入php的方法

    .html文件是不可以被读取为php文件的必须修改后缀名为.php这个时候如果你在浏览器中调用此页面所有的HTML代码都会被正确显示 这个时候你可以在文件的任意为止插入<?php ?>作为 ...

  9. android中依据不同分辨率dp和px的相互转算

    public class PxAndDp { /** * 依据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context ...

  10. jetty学习小结

    1.什么是jetty? 开源HTTP服务器和Servlet引擎,是web应用的容器,同tomcat类似.由于其轻量灵活的特性,很多知名产品也应用了它,如maven.eclipse.hadoop.spa ...