hdu 2844 Coins

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5517    Accepted Submission(s): 2280


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
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2845 2870 2577 2830 1505 
 
 1 //593MS    628K    1102 B    C++
 2 /*
 3     
 4     题意:
 5         给出n种硬币的面值和数量,再给出一个值m,求
 6     硬币可以组合成多少个 1~m 中的值
 7     
 8     多重背包:
 9         这是一道模板题,不过要用优化算法 
10          
11 */
12 #include<stdio.h>
13 #include<string.h>
14 int dp[100005];
15 int w[105],c[105];
16 int n,m;
17 int Max(int a,int b)
18 {
19     return a>b?a:b;
20 }
21 void zero_pack(int cost,int weight)
22 {
23     for(int i=m;i>=cost;i--)
24         dp[i]=Max(dp[i],dp[i-cost]+weight);
25 }
26 void com_pack(int cost,int weight)
27 {
28     for(int i=cost;i<=m;i++)
29         dp[i]=Max(dp[i],dp[i-cost]+weight);
30 }
31 void mul_pack(int cost,int weight,int number)
32 {
33     if(cost*number>=m){
34         com_pack(cost,weight);
35         return;
36     } 
37     int k=1;
38     while(k<number){
39         zero_pack(cost*k,weight*k);
40         number-=k;
41         k*=2;
42     }
43     zero_pack(cost*number,weight*number);
44 }
45 int main(void)
46 {
47     while(scanf("%d%d",&n,&m),n+m)
48     {
49         memset(dp,0,sizeof(dp));
50         for(int i=0;i<n;i++)
51             scanf("%d",&w[i]);
52         for(int i=0;i<n;i++)
53             scanf("%d",&c[i]);
54         for(int i=0;i<n;i++){
55             mul_pack(w[i],w[i],c[i]);
56         }
57         int cnt=0;
58         for(int i=1;i<=m;i++)
59             if(dp[i]==i) cnt++;
60         printf("%d\n",cnt);
61     }
62     return 0;
63 }

 

posted @ 2013-11-07 14:37  heaventouch  阅读(172)  评论(0编辑  收藏  举报