题目链接:传送门 题面:
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 <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
#define
#define
#define
using namespace std;
int n, m, V, w[A], f[A], p[A];
int main() {
while (cin >> n >> V) {
if (!n and !V) break;
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i <= n; i++) cin >> p[i];
memset(f, -0x3f, sizeof f); f[0] = 0;
for (int i = 1; i <= n; i++) {
int xx = p[i], k = 1;
while (k < xx) {
for (int j = V; j >= k * w[i]; j--)
f[j] = max(f[j], f[j - k * w[i]] + k);
xx -= k; k <<= 1;
}
for (int j = V; j >= xx * w[i]; j--)
f[j] = max(f[j], f[j - xx * w[i]] + xx);
}
int ans = 0;
for (int i = 1; i <= V; i++)
if (f[i] > 0) ans++;
cout << ans << endl;
}
}