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.
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
这道题目是一道多重背包的题目,只要还是根据具体情况转换为01背包和完全背包。因为数据规模有点大,所以使用scanf函数。另外这道题还帮我理解了这几种背包问题以及程序。(因为完全背包的函数问题让我TLE可好几次)
<pre name="code" class="cpp"><span style="color:#ff0000;">//</span>///
// HDU 2844 dp // //
// Created by 吴尔立 //
// Copyright (c) 2015年 吴尔立. All rights reserved. //
/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
#define ll long long;
#define INF 1<<31
#define cir(i,a,b) for (int i=a;i<=b;i++)
#define CIR(j,a,b) for (int j=a;j>=b;j--)
#define CLR(x) memset(x,0,sizeof(x))
using namespace std;
#define maxn 100005
int n,c[maxn]; //n是硬币种数 c是硬币数量
long long m,a[maxn],d[maxn]; //m是价格种数 a是硬币面值
void ZeroOnePack(long long cost,long long weight) //0-1背包
{
for (long long v=m;v>=cost;v--)
d[v]=max(d[v],d[v-cost]+weight);
return;
}
void CompletePack(long long cost,long long weight) //完全背包
{
for (long long v=cost;v<=m;v++)
d[v]=max(d[v],d[v-cost]+weight);
return ;
}
void MultiplePack(long long cost,long long weight,long long amount) //多重背包
{
if (cost*amount>=m)
{
CompletePack(cost,weight);
return;
}
int k=1;
while (k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount=amount-k;
k*=2;
}
ZeroOnePack(amount*cost,amount*weight);
}
int main()
{
while (scanf("%d%lld",&n,&m))
{
if (n==0)
if (m==0) return 0;
CLR(d);
cir(i,1,n)
scanf("%lld",&a[i]);
cir(i,1,n)
scanf("%lld",&c[i]);
cir(i,1,n)
MultiplePack(a[i],a[i],c[i]);
long long ans=0;
for (long long i=1;i<=m;i++)
if(d[i]==i)
ans++;
printf("%lld\n",ans);
}
return 0;
}