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
解题思路:这题用二进制解法在poj却超时了,而杭电却不会(单调队列就别想了-->TLE);考虑多重部分和问题,时间复杂度是O(nW)。
hdu 2844 #AC代码(343ms):有坑,测试数据种W有为负数的,这就是一直RE的原因=_=...简单判断一下即可。
 #include<algorithm>
#include<string.h>
#include<cstdio>
#include<iostream>
using namespace std;
int n,W,val[],cnt[],dp[];
int main(){
while(~scanf("%d%d",&n,&W)&&(n|W)){
for(int i=;i<=n;++i)scanf("%d",&val[i]);
for(int i=;i<=n;++i)scanf("%d",&cnt[i]);
if(W<=){puts("");continue;}
memset(dp,-,sizeof(dp));dp[]=;//注意初始化dp[0]为0
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=cnt[i];
else if(j<val[i]||dp[j-val[i]]<=)dp[j]=-;//面额太大或者或者在配更小的数时数量已经用光了
else dp[j]=dp[j-val[i]]-;
}
}
int ans=count_if(dp+,dp+W+,bind2nd(greater_equal<int>(),));//统计不小于0的个数
printf("%d\n",ans);
}
return ;
}

poj 1742 #AC代码(1813ms):中规中矩,W不会出现负数。

 #include<algorithm>
#include<string.h>
#include<cstdio>
#include<iostream>
using namespace std;
int n,W,val[],cnt[],dp[];
int main(){
while(~scanf("%d%d",&n,&W)&&(n|W)){
memset(dp,-,sizeof(dp));dp[]=;
for(int i=;i<=n;++i)scanf("%d",&val[i]);
for(int i=;i<=n;++i)scanf("%d",&cnt[i]);
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=cnt[i];
else if(j<val[i]||dp[j-val[i]]<=)dp[j]=-;
else dp[j]=dp[j-val[i]]-;
}
}
int ans=count_if(dp+,dp+W+,bind2nd(greater_equal<int>(),));
printf("%d\n",ans);
}
return ;
}

题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)的更多相关文章

  1. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  2. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  3. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  4. POJ 1742 Coins (多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Descriptio ...

  5. poj 1742 Coins(dp之多重背包+多次优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  6. [POJ 1742] Coins 【DP】

    题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 D ...

  7. poj 1742 Coins (多重背包)

    http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...

  8. Poj 1742 Coins(多重背包)

    一.Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dolla ...

  9. poj 1742 Coins(二进制拆分+bitset优化多重背包)

    \(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...

随机推荐

  1. 【Scrapy】Selectors

    Constructing selectors For convenience,response objects exposes a selector on .selector attribute,it ...

  2. LUA协程复用

    -----协程复用根函数 local function routine(fun, args) while (fun) do fun, args = coroutine.yield(fun(table. ...

  3. C#调用国家气象局天气预报接口

    原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...

  4. InfoQ中文站特供稿件:Rust编程语言的核心部件

    本文为InfoQ中文站特供稿件.首发地址为: http://www.infoq.com/cn/articles/rust-core-components .如需转载.请与InfoQ中文站联系. 原文发 ...

  5. struts2多图片上传实例【转】

    原文地址:http://blog.csdn.net/java_cxrs/article/details/6004144 描述: 通过struts2实现多图片上传. 我使用的版本是2.2.1,使用的包有 ...

  6. XML简单介绍及举例

    可扩展标记语言(eXtensibleMarkup Language,简称XML).是一种标记语言.标记指计算机所能理解的信息符号.通过此种标记,计算机之间能够处理包括各种信息的文章等. 怎样定义这些标 ...

  7. java定时器2-spring实现

    spring定时器(基于xml) spring定时器(基于注解) quartz定时器 1.使用基于xml配置的spring定时器 首先编写定时任务类Mytask public class Mytask ...

  8. YTU 2844: 改错题A-看电影

    2844: 改错题A-看电影 时间限制: 1 Sec  内存限制: 128 MB 提交: 69  解决: 47 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 小平家长为 ...

  9. java实现io读取数据

    ServletInputStream inputStream = request.getInputStream(); BufferedReader br = new BufferedReader(ne ...

  10. 一步一步学Silverlight 2系列(2):基本控件

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...