Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13453    Accepted Submission(s): 5382

Problem 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
 
一看题,以为普普通通一道模版题,结果卡时间卡到哭。。。
 
注意:当val[i]*num[i]>m时,使用完全背包更快,最开始每晚i注意到这点,通通使用二进制优化,结果超时超哭,然后发现完全背包比转化为二进制优化的01背包更快。
 
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int val[],num[];
int dp[],m; int main()
{
int n;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
memset(dp,,sizeof(dp));
//dp[0]=1;
int i;
for(i=; i<n; i++)
scanf("%d",&val[i]);
for(i=; i<n; i++)
scanf("%d",&num[i]);
for( i=; i<n; i++)
{
int j;
if(val[i]*num[i]>=m)
{
for( j=val[i];j<=m;j++)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
continue;
}
int tmp=;
while(num[i]>=tmp)
{
for( j=m;j>=val[i]*tmp;j--)
dp[j]=max(dp[j],dp[j-val[i]*tmp]+val[i]*tmp);
num[i]-=tmp;
tmp=tmp<<;
}
for( j=m;j>=val[i]*num[i];j--)
dp[j]=max(dp[j],dp[j-val[i]*num[i]]+val[i]*num[i]);
}
int cnt=;
for(i=; i<=m; i++)
if(dp[i]==i)
cnt++;
printf("%d\n",cnt);
}
return ;
}

HDU_2844_(多重背包)的更多相关文章

  1. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  2. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  3. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  4. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  5. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  6. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  7. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  8. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  9. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

随机推荐

  1. 一个Navi过程下多个DocumentCompleted事件问题的解决的方法

    7.16 Marked to Write.... 七月份马克的一篇文章了,今天才想起来把他写完,呵呵. 原本是七月份用来做微博爬虫的,后来发现新浪对机器人的检測不好绕过,连简单地訪问都会被检測出来,后 ...

  2. 【智能路由器】让MT7620固件openwrt支持USB

    [智能路由器]系列文章连接 http://blog.csdn.net/u012819339/article/category/5803489 首先确定硬件有USB,这个得检查板子是否引出了usb口,一 ...

  3. Java总结之网络

    [网络基础概念] 什么是计算机网络: 把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大.功能强的网络系统,从而使众多的计算机能够方便的互相传递信息,共享硬件.软件.数据信息等资源 ...

  4. Linux操作系统改动PATH的方法

    1. 暂时改动: 使用export.比如#export PATH=$PATH:/etc/apache/bin 2. 针对用户的改动: vi ~/.bash_profile 增加:export PATH ...

  5. MySQL测试代码

    MySQL测试代码 # 注释内容 -- 注释内容 -- 创建maizi数据库 CREATE DATABASE IF NOT EXISTS `maizi` DEFAULT CHARACTER SET ' ...

  6. Eclipse Import别人的源代码,出错解决过程

    1.从APKBus上下载了一个源码,想研究一下,结果出错了,经过各种百度,实验,终于解决了,现在记录一下.. File-Import 导入源文件.

  7. Keil和IAR——使用笔记

    1. 几个宏的定义 Keil和IAR均用到以下三种宏定义,分别是:USE_STDPERIPH_DRIVER——表示使用ST提供的标准外设固件库:STM32F40_41xxx——作为芯片的判断:VECT ...

  8. 玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp

    http://www.ifrog.cc/acm/problem/1028 题解处:http://www.ifrog.cc/acm/solution/4 #include <cstdio> ...

  9. WPF获取原始控件样式

    要获取WPF控件的原始样式,需要我们安装Blend for Visual Studio. 然后,我们打开Blend for Visual Studio,创建一个WPF项目. 然后,我们向页面拖动一个B ...

  10. POJ1743 Musical Theme 最长重复子串 利用后缀数组

    POJ1743 题目意思是求不重叠的最长相同变化的子串,输出该长度 比如1 2 3 4 5 6 7 8 9 10,最长长度为5,因为子串1 2 3 4 5 和 6 7 8 9 10变化都一样的 思路: ...