Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20860 Accepted Submission(s): 8198
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 <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n,m,a[],c[],dp[];
void comdp(int w,int v)
{
int i;
for(i=w; i<=m; i++)
dp[i]=max(dp[i],dp[i-w]+v);
}
void zeroone(int w,int v)
{
int i;
for(i=m; i>=w; i--)
dp[i]=max(dp[i],dp[i-w]+v);
}
void multidp(int w,int v,int cnt)//此时开始多重背包,dp[i]表示背包中重量为i时所包含的最大价值
{
if(cnt*w>=m)//此时相当于物品数量无限进行完全背包
{
comdp(w,v);
return;
}
int k=;//否则进行01背包转化,具体由代码下数学定理可得
while(k<=cnt)
{
zeroone(k*w,k*v);
cnt-=k;
k*=;
}
zeroone(cnt*w,cnt*v);
return ;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)break;
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)scanf("%d",&c[i]);
memset(dp,,sizeof(dp));
int ans=;
for(int i=;i<=n;i++)
{
multidp(a[i],a[i],c[i]);//重量和价值都设为a[i],硬币的价值
}
for(int i=;i<=m;i++)//因为重量和价值都是a[i],所以dp[i]就表示在重量为i时的价值,这题的答案就是数一下有多少个价值是在1到m之间的
{
if(dp[i]==i)ans++; }
printf("%d\n",ans);
}
return ;
}