题目
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 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,A1,A2,A3...An 分别代表每种硬币的价值,C2,C3...Cn 代表每种硬币的数量。求一共有多少种可能的价格。就是个背包问题。
代码
#include <stdio.h>
#include <iostream>
#include <bitset>
using namespace std;
int main(int argc, const char * argv[]) {
int n, m;
// 每种硬币的价值
int value[105];
// 每种硬币的数量
int number[105];
// 价格的可能性,比如价格 i,如果 bitset 第 i 位为 true,则代表此价格可行,反之则不可行。
bitset<100005> resultBitSet;
while (true) {
// 处理数据的输入
scanf("%d %d", &n, &m);
if (n == 0 && m == 0) {
return 0;
}
for (int i = 0; i < n; i++) {
scanf("%d", &value[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &number[i]);
}
// 重置 resultBitSet
resultBitSet.reset();
// 从存钱罐中挨个取硬币处理
for (int i = 0; i < n; i++) {
for (int t = 1; t <= number[i] && t*value[i] <= m; t++) {
// 最关键的两行代码,<< 计算的是新增的可能价值,|= 是在原有可能价值的基础上累加新增的可能价值
resultBitSet |= (resultBitSet << value[i]);
// 这行其实也挺关键的,做初始化,如果这行代码顺序出错也是得不出正确结果的
resultBitSet.set(t*value[i]);
}
}
// 以上的 for 循环在移位时会计算进部分超过 m 价值的数据,这里通过移位把这些数据移掉
resultBitSet <<= (100005 - (m + 1));
printf("%lu\n", resultBitSet.count());
}
}
其实一直想找这样一个可以直接移位的内存块,这样处理起来就很方便了。不过说来惭愧,虽然 13 年左右写过一年的 C++,但是不知道是当时就不知道 bitset 还是后来这几年不写给忘了,反正就是没想起来,所以我一开始写的代码是如下的(Time Limit Exceeded
):
#include <stdio.h>
#include<iostream>
#include <cstring>
int value[105];
int number[105];
bool result[100005];
// 处理输入
bool processInput(int &n, int &m) {
scanf("%d %d", &n, &m);
if (n == 0 && m == 0) {
return false;
}
for (int i = 0; i < n; i++) {
scanf("%d", &value[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &number[i]);
}
return true;
}
// 计算结果
void calculate(int n, int m) {
memset(result, 0, sizeof(result));
for (int i = 0; i < n; i++) {
if (number[i] <= m) {
for (int k = m; k >= 1; k--) {
if (result[k] && k + value[i] <= m) {
for (int t = 1; t <= number[i] && k + t * value[i] <= m; t++) {
result[k + t * value[i]] = true;
}
}
}
for (int t = 1; t <= number[i] && t*value[i] <= m; t++) {
result[t*value[i]] = true;
}
}
}
}
int main(int argc, const char * argv[]) {
int n, m;
while (processInput(n, m)) {
calculate(n, m);
int total = 0;
for (int i = 1; i <= m; i++) {
if (result[i]) {
total++;
}
}
printf("%d\n", total);
}
return 0;
}