Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8999    Accepted Submission(s): 3623

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
 

Source

2009 Multi-University Training Contest 3
- Host by WHU




题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844



题目大意:n种数,每一个数的值为ai,有ci个,求能组成多少个不大于m的不同的数字
 

题目分析:这题的数据量实在感人。三层循环(枚举种类,枚举数值。枚举个数)肯定超时,要想办法减去一层循环,细致分析能够发现个数的那层循环能够省掉,代价是耗费很多其他的空间。开一个cnt数组记录当前第i种使用了多少个,其他两层循环不变,然后就是普通的背包计数问题

#include <cstdio>
#include <cstring>
int const MAXM = 1e5 + 5;
int const MAXN = 1e2 + 5;
bool dp[MAXM];
int cnt[MAXM];
int a[MAXN], c[MAXN]; int main()
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF && (n + m))
{
memset(dp, false, sizeof(dp));
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++)
scanf("%d", &c[i]);
dp[0] = true;
int ans = 0;
for(int i = 1; i <= n; i ++)
{
memset(cnt, 0, sizeof(cnt));
for(int s = a[i]; s <= m; s ++)
{
if(!dp[s] && dp[s - a[i]] && cnt[s - a[i]] < c[i])
{
dp[s] = true;
ans ++;
cnt[s] = cnt[s - a[i]] + 1;
}
}
}
printf("%d\n", ans);
}
}

HDU 2844 Coins (多重背包计数 空间换时间)的更多相关文章

  1. hdu 2844 Coins (多重背包+二进制优化)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...

  2. HDu -2844 Coins多重背包

    这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...

  3. HDU - 2844 Coins(多重背包+完全背包)

    题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...

  4. hdu 2844 Coins 多重背包(模板) *

    Coins                                                                             Time Limit: 2000/1 ...

  5. hdu 2844 coins(多重背包 二进制拆分法)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  6. HDU 2844 Coin 多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)

    作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...

  8. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  9. 计数排序(O(n+k)的排序算法,空间换时间)

    计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...

随机推荐

  1. KendoUi中KendoDropDownList控件的使用——三级级联模块的实现

    1. 应用需求 在权限系统开发中除了以上数据表关系的设计之外.比較麻烦的地方是级联模块在页面的展示,因为设计中最多是控制到三级,因此三级级联模块的展示.编辑等页面操作是须要解决的问题,这里採用Kend ...

  2. 在oracle存储过程中创建暂时表

    在oracle的存储过程中,不能直接使用DDL语句,比方create.alter.drop.truncate等. 那假设我们想在存储过程中建立一张暂时表就仅仅能使用动态sql语句了: create o ...

  3. vim基础学习之自动补全功能

    本章我们学习自动补全功能1.自动补全优先从当前的编辑区获得补全列表例如:我们写下如下内容 aaaaa aabbb aaab 当我们再次输入aa,然后我们按下Tab的时候,会弹出一个包含 aaaaa a ...

  4. 考满分软件测试工程师(实习)面试&软达启航面试

    考满分软件测试工程师(实习)面试 从这学期秋季开学的时候开始准备找工作,一边学习看书,一边完善简历海投:九月下旬的时候在年级实习群里看到考满分发的宣传海报马上就加了hr的微信,hr要了我的简历,并给技 ...

  5. 简单说一下 JSON和JSONP

    JSON和JSONP,但从缩写看,可能会以为他们是很相似的两个名词,但他们除了缩写相似外,他们是两种类型的概念. 首先: JSON(JavaScript Object Notation)即JavaSc ...

  6. 交换工资 SQL

  7. CodeForcesGym 100502H Clock Pictures

    Clock Pictures Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGy ...

  8. Method and apparatus for transitioning between instruction sets in a processor

    A data processor (104) is described. The data processor (104) is capable of decoding and executing a ...

  9. 将bat批处理命令文件固定到任务栏

    将bat批处理命令文件固定到任务栏第一种方法:使用链接工具http://www.xstui.com/read/3451.在任务栏点击右键,移动到工具栏,勾选链接工具2.你会在通知栏左侧看到链接字样,将 ...

  10. home.pl 正在促销,一些域名免费(终止于2017.4.4)

    home.pl 正在促销,一些域名免费(终止于2017.4.4) home.pl 成立于1997年,是波兰顶尖的互联网服务公司.专注于域名登记,托管网站,保持电子邮件帐户等.  home.pl 正在促 ...