H - Coins

H - Coins

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

//题意是 第一行两个整数 n 和 m (1 <= n <= 100)(m <= 100000) 然后第二行是 n 个硬币的价值,再是 n 个硬币的数量,问这些硬币能组成多少个小于等于 m 的价值。多重背包,有一点难吧,要用到二进制优化,直接当 0 1 背包处理要超时

hud 340ms 另一个oj 187 ms

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5
 6 int a[105],c[105],d[105];
 7 bool f[100005];
 8
 9 int max(int a,int b)
10 {
11     return a>b?a:b;
12 }
13
14 int main()
15 {
16     int i,j,k,m,n;
17     while(scanf("%d%d",&n,&m)&&n+m)
18     {
19         for(i=1;i<=n;i++)
20           scanf("%d",&a[i]);
21         for(i=1;i<=n;i++)
22             scanf("%d",&c[i]);
23
24         memset(f,0,sizeof(f));
25         f[0]=1;
26
27         for(i=1;i<=n;i++)
28         {
29             if(a[i]*c[i]>=m) //如果该货币总价值大于等于 m
30             {
31                 for(j=a[i];j<=m;j++)
32                     if(!f[j])          //只需记得能不能达到即可
33                     f[j]=f[j-a[i]];
34             }
35             else             //总价值小于 m
36             {
37                 if(c[i]==0)
38                     continue;
39
40                 int num=c[i];
41                 int p=1;
42                 int t=1;
43
44                 while(p<num)    //二进制优化的 0 1 背包 1 . 2 . 4 . 8
45                 {
46                     d[t++]=a[i]*p;
47                     num-=p;
48                     p*=2;
49                 }
50
51                 d[t++]=a[i]*num;
52
53                 for(j=1;j<t;j++)
54                 for(k=m;k>=d[j];k--)//f[k]=max(f[k],f[k-d[j]]);
55                 {
56                     if(!f[k])
57                     f[k]=f[k-d[j]];
58                 }
59             }
60         }
61
62         j=0;
63         for(i=1;i<=m;i++)
64         {
65             if(f[i]==1)
66             j++;
67         }
68         printf("%d\n",j);
69     }
70     return 0;
71 }


 
				
时间: 2024-08-28 12:56:12

H - Coins的相关文章

H - Gold Coins(2.4.1)

H - Gold Coins(2.4.1) Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【背包专题】 D - Coins hdu2844【多重背包】

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 kn

F - Coins

Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1742 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 fou

POJ-1742 Coins

Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 37959 Accepted: 12882 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 coi

Lucky Coins Sequence

Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35 Accepted Submission(s): 29   Problem Description As we all know,every coin has two sides,with one side facing up and another

HDU1398 Square Coins

Description People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 28

hdu 2844 poj 1742 Coins

hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正好为时限3000ms....太慢了,hdu直接TLE(时限1s); 之 后发现其实并不是算法的问题,而是库函数的效率没有关注到.我是使用fill()按量初始化的,但是由于memset()可能是系统底层使用了四个字节拷 贝的函数(远比循环初始化快),效率要高得多..这就是为什么一直TLE的原因,fil

hdu 1398 Square Coins(母函数|完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1398 题意:有价值为1^2,2^2....7^2的硬币共17种,每种硬币都有无限个.问用这些硬币能够组成价值为n的钱数共有几种方案数. 母函数: #include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack> #include <vector>