Coins (01背包)
2017-08-18 21:00
120 查看
##**coins**##
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
代码:
#include<stdio.h> #include<string.h> #include<math.h> int max(int x,int y) { return x>y?x:y; } int main() { int n,m; int num[105]; int a[105]; int val[100005]; int dp[100005]; while(scanf("%d%d",&n,&m),n&&m) { memset(a,0,sizeof(a)); memset(num,0,sizeof(num)); memset(val,0,sizeof(val)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { scanf("%d",&num[i]); } int w=1; for(int i=1;i<=n;i++) { int sum=0; for(int k=0;pow(2.0,k)<=num[i]-sum;k++) { val[w++]=a[i]*pow(2.0,k); sum=sum+pow(2.0,k); } if(sum<num[i]) val[w++]=a[i]*(num[i]-sum); } for(int i=1;i<w;i++) for(int j=m;j>=val[i];j--) dp[j]=max(dp[j],dp[j-val[i]]+val[i]); long long nub=0; for(int i=1;i<=m;i++) if(dp[i]==i) nub++; printf("%lld\n",nub); } return 0; }
相关文章推荐
- UVA - 10306 e-Coins 二维的01背包问题
- HDU 2844 Coins(多重背包 + 二进制优化 + 01背包)
- UVA 562 Dividing coins 01背包 Commencel
- uvaoj 562 - Dividing coins 动态规划-01背包
- 【转载】HDU 2844.Coins(多重背包转01背包)
- HDU ACM 2844 Coins (多重背包)----------------01背包,完全背包,多重背包模板
- POJ 3260 The Fewest Coins(完全背包+多重背包(转化为01背包))
- GYM 101102 A.Coins(01背包)
- UVA 562 Dividing coins --01背包的变形
- UVA 562 Dividing coins 01背包
- 1068. Find More Coins (30) 01背包
- UVA - 562 - Dividing coins (01背包)
- hdu 2844 Coins 多重背包 二进制优化 算是否能组成的背包
- 背包问题——01背包&01背包装箱
- hdu 2602 Bone Collector 01背包
- bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草【01背包】
- poj2184 Cow Exhibition 01背包 变形
- POJ3624Charm Bracelet(01背包)
- 求01背包的第k大解
- NYOJ 49 开心的小明(01背包)