pku 1742 Coins(多重背包dp)
2013-06-30 17:37
176 查看
Coins
Description
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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
Sample Output
Source
LouTiancheng@POJ
题意:给n种一定数量一定面值的硬币,问可以得出多少种不同面值的组合
题解:多重背包当完全背包做,然后计算多少个dp值不为0即可,本人感觉二进制优化比开一个cou数组计算已用数目慢,所以这里的AC代码不是二进制优化的多重背包
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 25161 | Accepted: 8509 |
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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
LouTiancheng@POJ
题意:给n种一定数量一定面值的硬币,问可以得出多少种不同面值的组合
题解:多重背包当完全背包做,然后计算多少个dp值不为0即可,本人感觉二进制优化比开一个cou数组计算已用数目慢,所以这里的AC代码不是二进制优化的多重背包
#include<stdio.h> int val[105],num[105]; int cou[100005],dp[100005]; int main() { int n,m,i,j,sum; while(scanf("%d%d",&n,&m),n!=0&&m!=0) { for(i=0;i<n;i++) scanf("%d",val+i); for(i=0;i<n;i++) scanf("%d",num+i); for(i=0;i<=m;i++) dp[i]=0; for(dp[0]=i=0;i<n;i++) { for(j=0;j<=m;j++) cou[j]=0; for(j=val[i];j<=m;j++) if(cou[j-val[i]]<num[i]&&dp[j-val[i]]+val[i]>dp[j]) dp[j]=dp[j-val[i]]+val[i],cou[j]=cou[j-val[i]]+1; } for(sum=i=0;i<=m;i++) cou[i]=0; for(i=1;i<=m;i++) { if(cou[dp[i]]||!dp[i]) continue; cou[dp[i]]=1,sum++; } printf("%d\n",sum); } return 0; }
相关文章推荐
- poj 1742 Coins(dp之多重背包+多次优化)
- poj 1742 Coins 背包dp
- POJ 1742 - Coins (dp 多重背包)
- POJ 1742-Coins [dp 多重背包问题] 《挑战程序设计竞赛》2.3
- POJ 1742 Coins 【多重背包DP】
- pku 1742 Coins(DP,母函数)
- 【DP|多重背包可行性】POJ-1742 Coins
- POJ 1742 Coins(多重背包) DP
- PKU1157 01背包 DP
- POJ 1742 Coins 多重背包题解
- UVALive5583 UVA562 Dividing coins【0/1背包+DP】
- Poj 1742 Coins(多重背包)
- poj 1742Coins(混合背包)
- 多重部分和(DP):POJ1742--Coins
- POJ 1742Coins 多重背包转完全背包
- POJ1742 Coins[多重背包可行性]
- POJ3260_The Fewest Coins_deque优化dp解多重背包和完全背包
- POJ 1742 Coins——不要套单调队列优化多重背包的模板
- POJ 1742 Coins 多重背包入门题
- poj-1742 Coins(多重背包优化)