Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17216 Accepted Submission(s): 6809
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
Recommend
gaojie | We have carefully selected several similar problems for you: 2159 1203 1171 2845 2191
题意:
- 多组输入。每组数据先输入一个 n , m 。
- n : 有 n 种硬币; m : 所选硬币总和不超过 m ;
- 接下来输入 A1 , A2 , A3…An。代表第 i 种硬币的面值。
- 接着再输入 C1 , C2 , C3…Cn。代表第 i 种硬币的数量。
- n == 0 && m == 0 时结束。
问:在1 ~ m 种,有多少面值恰好能由所给的硬币组成?
参考博客:
http://blog.csdn.net/u012762625/article/details/43485973
http://blog.csdn.net/tinyguyyy/article/details/51203935
解题代码①:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
bool dp[100005];//dp[j] : 前 i 种物品是否能组成 j 。
int a[105],c[105],cnt[100005];//cnt[j] : 当组成 j 时,这时第 i 种物品已经用的个数。
int main(){
int n,m,sum;
while(~scanf("%d %d",&n,&m)){
memset(dp,0,sizeof(dp));
if(n == 0 && m == 0) break;
for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
for(int i = 1;i <= n;i++) scanf("%d",&c[i]);
dp[0] = 1,sum = 0;
for(int i = 1;i <= n;i++){
memset(cnt,0,sizeof(cnt));
for(int j = a[i];j <= m;j++){
if(!dp[j] && cnt[j - a[i]] < c[i] && dp[j - a[i]]){
dp[j] = 1;
sum++;
cnt[j] = cnt[j - a[i]] + 1;
}
}
}
printf("%d\n",sum);
}
return 0;
}
解题代码②:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
bool dp[100005];//dp[j]: 前 i 种物品是否能组成 j 。
int a[105],c[105];
int n,m,sum;
void ZeroOnePack(int cost){
for(int i = m;i >= cost;i--){
dp[i] = max(dp[i],dp[i - cost]);
}
}
void CompletePack(int cost){
for(int i = cost;i <= m;i++){
dp[i] = max(dp[i],dp[i - cost]);
}
}
void MultiPack(int cost,int num){
if(num * cost > m){
CompletePack(cost);
}
else{
int k = 1;//二进制思想
while(k < num){
ZeroOnePack(cost * k);
num -= k;
k <<= 1;
}
ZeroOnePack(cost * num);
}
}
int main(){
while(~scanf("%d %d",&n,&m)){
if(n == 0 && m == 0) break;
for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
for(int i = 1;i <= n;i++) scanf("%d",&c[i]);
memset(dp,0,sizeof(dp));
dp[0] = 1,sum = 0;
for(int i = 1;i <= n;i++){
MultiPack(a[i],c[i]);
}
for(int i = 1;i <= m;i++){
if(dp[i]) sum++;
}
printf("%d\n",sum);
}
}
本文介绍了一道经典的硬币组合问题,通过动态规划方法解决如何使用有限数量的不同面额硬币来组合出特定范围内的所有可能金额的问题。提供了两种解题代码实现方式,并附带详细注释。
&spm=1001.2101.3001.5002&articleId=79156853&d=1&t=3&u=dbca68f4c9b447e7b9be7dac6962c26c)
1430

被折叠的 条评论
为什么被折叠?



