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
思路:先输入输入每种硬币的价值,再输入每种硬币的个数求在m以内能组成几种价值
#include<iostream>
#include<stdio.h>
#include<algorithm>
int n, m;
int dp[100001];
int num[105];
int money[105];
using namespace std;
void ZeroOnePack(int w, int v) {
int j;
for (j = m; j >= w; j--) {
dp[j] = max(dp[j], dp[j - w] + v);
}
}
void CompletePack(int w, int v) {
int j;
for (j = w; j <= m; j++) {
dp[j] = max(dp[j], dp[j - w] + v);
}
}
void MultiplePack(int w, int v, int amount) {
if (amount*w >= m) {
CompletePack(w, v);
return;
}
for (int k = 1; k < amount; k <<= 1) {//用了2进制的思想,2的次方的数字能表达任何数字例如7可以由1+2+4表达
ZeroOnePack(k*w, k*v);
amount -= k;
}
ZeroOnePack(amount*w, amount*v);
}
int main() {
while (scanf("%d %d", &n, &m) != EOF) {
int ans = 0;
memset(dp, 0, sizeof(dp));
if (n == 0 && m == 0) {
break;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &money[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%dd", &num[i]);
}
for (int i = 1; i <= n; i++) {
MultiplePack(money[i], money[i], num[i]);
}
for (int i = 1; i <= m; i++) {
if (dp[i] == i) {
ans = ans + 1;
}
}
printf("%d\n", ans);
}
return 0;
}