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 ;
}

 


最新文章

  1. 【转】sublime配置默认浏览器+多浏览器快捷键
  2. Linux init
  3. c#之结构体
  4. 一个巧妙的实现悬浮的tableViewHeader的方法
  5. JavaScript设计模式 - 迭代器模式
  6. 详解Android Handler的使用-别说你不懂handler(转)
  7. 程序员求职之道(《程序员面试笔试宝典》)之求职有用网站及QQ群一览表
  8. Oracle ACL(Access Control List)
  9. touch命令功能
  10. 二叉搜索树的平衡--AVL树和树的旋转(图解)
  11. sublime安装AngularJS插件
  12. 性能测试 查看Android APP 帧数FPS的方法
  13. .10-浅析webpack源码之graceful-fs模块
  14. 部署Redis(脚本安装)
  15. Devexpress中Gridcontrol查找分组
  16. 多线程UI
  17. Mac下Android绘制点9格式png以及解决IllegalArgumentException: Unknown image type 0报错
  18. EF的表左连接方法Include和Join
  19. XmlHttpRequest对象 ajax核心之一
  20. 使用jmeter工具测试上传接口

热门文章

  1. 机器学习第2课:单变量线性回归(Linear Regression with One Variable)
  2. 【Hadoop】Hadoop HA 部署 详细过程(架构、机器规划、配置文件、部署步骤)
  3. python解析json文件报错No JSON object could be decoded
  4. Nginx include和Nginx指令的使用
  5. .net 真实代理和透明代理的交互
  6. ARM指令中的函数调用
  7. 【Scala-ML】怎样利用Scala构建并行机器学习系统
  8. UE4射击小游戏原型
  9. ASP.NET综合管理ERP系统100%源代码+所有开发文档
  10. ubuntu 安装时出错 sudo apt-get update Reading package lists… Error