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. 2017.7.18 linux下ELK环境搭建

    参考来自:Linux日志分析ELK环境搭建  另一篇博文:2017.7.18 windows下ELK环境搭建   0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1 ...

  2. 一起学Netty(一)之 Hello Netty

    一起学Netty(一)之 Hello Netty 学习了:https://blog.csdn.net/linuu/article/details/51306480

  3. SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错

    这个问题之前就发现过,最近有几个哥们一直在问我这个怎么搞,我手上在做另一个项目,cas就暂时搁浅了几周.现在我们来一起改一下你的应用(client2/3)的web.xml来解决这个2b问题,首先看下错 ...

  4. zoj 3888 Twelves Monkeys 二分+线段树维护次小值

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=3888 Twelves Monkeys Time Limit: 5 ...

  5. 底部菜单实现(Dialog方案)

    项目中经常会要实现在屏幕底部弹出一个窗口,比如一个分享窗口: 下面详解实现步骤: 1.定义布局 <?xml version="1.0" encoding="utf- ...

  6. eclipse黄色警告(finally block does not complete normally) ,不建议在finally中使用return语句

    在eclipse中编写例如以下的代码,eclipse会给出黄色告警:finally block does not complete normally. public class Test { publ ...

  7. 【IOS】mac终端运行.sh文件总是提示permission denied

    如果我目录jni有一个list.sh文件 我直接 nxgametekiMacBook-Air:jni luonan$  ./list.sh ../../Classes 提示 permission de ...

  8. 杭电 HDU 2717 Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. cpp学习笔记 1一个简单的小程序以及一些的知识点

    今天买的cpp到了从今天開始又一次学习cpp如今发现学校发的书真的不怎莫样. <em>#include<stdio.h>//预处理命令 int main()/*第一个被调用的函 ...

  10. Python--多进程--01

    multiprocess import multiprocessing import time def worker_1(interval): print(' i am worker1') n=5 w ...