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. [iOS 高级] iOS远程推送与本地推送大致流程

    本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...

  2. 对Linux文件权限的理解

    755,775,777,ugoa 等分别代表什么含义?这些数字是如何得到的? 1.常用的linux文件权限: 444 -r--r--r-- 600 -rw------- 644 -rw-r--r-- ...

  3. 【重点突破】—— 百度地图在React单页面应用中的使用

    前言:百度地图是网页中使用地图的常用第三方工具,这里结合React项目中学到的应用场景总结一些使用要点. 一.在网页中嵌入百度地图 搜百度地图开放平台,注册百度开发者账号 控制台:查看应用.创建应用( ...

  4. sql取随机结果集

    应用场景: 某日,接产品姐姐需求,网站搜索页在搜索特定的内容时候,会现实搜索不到结果!如衣服网站,搜索鞋子等.为了不直接呈现一个赤裸裸的无此商品页面,so,需要在搜索商品件数小于3时,在下面随机推荐本 ...

  5. ARM指令中的函数调用

    1. 重要寄存器 SP 栈指针,  每一种异常模式都有其自己独立的r13,它通常指向异常模式所专用的堆栈,也就是说五种异常模式.非异常模式(用户模式和系统模式),都有各自独立的堆栈,用不同的堆栈指针来 ...

  6. Python&lt;1&gt;List

    list里的元素以逗号隔开,以[]包围,当中元素的类型随意 官方一点的说:list列表是一个随意类型的对象的位置相关的有序集合. 它没有固定的大小(1).通过对偏移量 (2)进行赋值以及其它各种列表的 ...

  7. 基于Saltstatck实现页面实时显示tomcat启动日志(17)

    一.相关文件 master端: /srv/salt/tomcat/start.sls               #tomcat启动服务state.sls,须要自己创建 /srv/salt/tomca ...

  8. MySQL几个性能指标

    近期参加了一个DBA MySQL的分享,主要从MySQL的性能指标分析.同步及运维三个方面分享一些经验,其中,一些经验值还是值得记录下来的: 对于一个MySQL实例,CRUD上限经验值如下: Quer ...

  9. windows超过最大连接数解决命令

    query user /server:218.57.146.175 logoff  1 /server:218.57.146.175

  10. 我攻克了oom

    BitmapFactory.Options options=new BitmapFactory.Options();     options.inJustDecodeBounds = false;   ...