Coins
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 22094 | Accepted: 7513 |
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.
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
我去上课了。。方法1不大会刚学的。。这种方法完全脱离了常规的多重背包模版 ACM牛人真是太多了。。。方法2还有一个问题就是 multiplepack(A[i],A[i],C[i]); 这个地方不理解。。
代码1(新方法)
#include<stdio.h>
#include<string.h>
#define maxn 105
#define maxm 100005
bool f[maxm];
int used[maxm], num[maxn], value[maxn];
int n, m;
void input() {
int i;
for (i = 0; i < n; i++)
scanf("%d", &value[i]);
for (i = 0; i < n; i++)
scanf("%d", &num[i]);
}
void work() {
memset(f, 0, sizeof(f));
f[0] = true;
int sum = 0;
for (int i = 0; i < n; i++) {
memset(used, 0, sizeof(used));//初始化存放使用次数
for (int j = value[i]; j <= m; j++)
if (!f[j] && f[j - value[i]] && used[j - value[i]] < num[i])//使用次数不可超过num
{
f[j] = true;
used[j] = used[j - value[i]] + 1;//该物品使用次数增加
sum++;//边背包边累计结果
}
}
printf("%d\n", sum);
}
int main() {
while (scanf("%d %d", &n, &m), n | m) {
input();
work();
}
return 0;
}
代码2(居然不会超时)
#include<stdio.h>
#include<string.h>
#define maxn 110
#define maxncash 1000010
int n, m, f[maxncash], A[maxn], C[maxn];
void zero(int cost, int weight) {
int i;
for (i = m; i >= cost; i--) //逆续
if (f[i] < f[i - cost] + weight)
f[i] = f[i - cost] + weight;
}
void complepack(int cost, int weight) {
int i;
for (i = cost; i <= m; i++)
if (f[i] < f[i - cost] + weight)//顺序
f[i] = f[i - cost] + weight;
}
void multiplepack(int cost, int weight, int account) {
int k;
if (account * weight >= m)
complepack(cost, weight);
else {
k = 1;
while (k < account) {
zero(k * cost, k * weight);
account -= k;
k *= 2;
}
zero(account * cost, account * weight);
}
}
int main() {
while (scanf("%d %d", &n, &m) != EOF && (n != 0 || m != 0)) {
int i;
int ans = 0;
for (i = 1; i <= n; i++)
scanf("%d", &A[i]);//输入硬币的价值;
for (i = 1; i <= n; i++)
scanf("%d", &C[i]);//输入对应硬币的价值;
memset(f, 0, sizeof(f));//数组清零;
for (i = 1; i <= n; i++)
multiplepack(A[i], A[i], C[i]);
ans = 0;
for (i = 1; i <= m; i++)
if (f[i] == i)
ans++;
printf("%d\n", ans);
}
return 0;
}