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. 转:js点击事件在ios中失效的3种解决方案

    ios中不允许将点击事件绑定在document或者body上,如果绑定上的话将会失效.解决方案: 例如:  $(document).on('click', '#generate', function ...

  2. Python按行输出文件内容具体解释及延伸

    下面两端測试代码分别为笔者所写,第一段为错误版本号.后者为正确版本号: #! /usr/bin/python2.7 try:     filename = raw_input('please inpu ...

  3. [计算机-好软推荐]证件照制作的利器,不会PS也没有关系

    多年前发现了这一款软件,后来不管电脑是从xp升级到win7,还是从win7升级到win8,我都收藏了它. 我用它主要是制作大头照,然后通过咔嚓鱼冲印,比起一般的冲印店要便宜些. 这个软件是台湾的朋友开 ...

  4. HDU5768Lucky7

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. clojure学习记录

    take 从列表中获取子列表 into a b  把b conj 到a中 (defn count-a-seq [lat]  (reduce (fn [x y] (+ x 1)) 0 lat)) red ...

  6. socket listen backlog

    http://stackoverflow.com/questions/4253454/question-about-listening-and-backlog-for-sockets The list ...

  7. ubutu14.04无法使用sudo,也无法切换到root用户去解决问题怎么办?

    一不小心,修改了/etc/sudoers文件. 惨了. 无法使用sudo了,啥都干不成了. 最最关键的是,也无法用root登录. 本想着要重装系统了. 后来发现了神奇的ubuntu安全模式. 1.重启 ...

  8. 手推FP-growth (频繁模式增长)算法------挖掘频繁项集

    一.频繁项集挖掘为什么会出现FP-growth呢? 原因:这得从Apriori算法的原理说起,Apriori会产生大量候选项集(就是连接后产生的),在剪枝时,需要扫描整个数据库(就是给出的数据),通过 ...

  9. argis地图

  10. [App Store Connect帮助]三、管理 App 和版本(7)移除 App

    若要在“我的 App”主视图中移除 App,要先将此 App 从 App Store 中移除,并将所有与之关联的 App 内购买项目下架. 此外,仅当 App 的所有版本均处于下列状态之一时方可移除: ...