hdu 2844 Coins
来源:互联网 发布:淘宝创立于哪一年 编辑:程序博客网 时间:2024/06/16 14:41
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
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
题意:
给你硬币的种类和数量,问你这些硬币在不超过m的情况下,共有多少种组合方案
这是一道多重背包的模板题
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[100005], a[105], c[105];int n, m;void zeroonepack(int cost, int val){for (int i = m;i >= cost;i--){dp[i] = max(dp[i], dp[i - cost] + val);}}void completepack(int cost, int val){for (int i = cost;i <= m;i++){dp[i] = max(dp[i], dp[i - cost] + val);}}void multiplepack(int cost, int val, int num){if (num*cost >= m){completepack(cost, val);return;}int k = 1;while (k < num){zeroonepack(k*cost, k*val);num -= k;k = 2 * k;}zeroonepack(num*cost, num*val);}int main(){int i, j;while (cin >> n >> m){if (n == 0 && m == 0) break;for (i = 1;i <= n;i++)cin >> a[i];for (i = 1;i <= n;i++)cin >> c[i];memset(dp, 0, sizeof(dp));for (i = 1;i <= n;i++){multiplepack(a[i], a[i], c[i]);}int ans = 0;for (i = 1;i <= m;i++)if (dp[i] == i)ans++;cout << ans << endl;}return 0;}
0 0
- HDU 2844 - Coins
- hdu 2844 Coins
- hdu 2844 Coins 背包
- hdu 2844 coins
- Hdu 2844 Coins
- HDU 2844 Coins
- hdu 2844 Coins
- hdu 2844 Coins
- HDU 2844 coins
- hdu 2844 Coins
- hdu 2844 coins
- HDU-2844-Coins
- hdu 2844 Coins
- hdu 2844 Coins
- HDU - 2844 Coins
- hdu 2844 Coins dp
- HDU 2844 Coins
- hdu 2844 Coins
- SpringMVC整合Shiro
- 笔记:Centos 6安装过程
- 多线程开发2 -GCD
- 图像简介
- 对于好书APP的总结
- hdu 2844 Coins
- Web Service有关术语的解释
- kmp算法
- 判断其是否为一个回文串,java实现
- c++中使用struct,struct中有指针
- 用java代码实现二叉树的遍历算法
- java设计模式之命令模式
- 汉语拼音工具包相关实用方法
- Android项目编码的批量修改(GBK转UTF-8)