Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5281 Accepted Submission(s): 2171
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.
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
Recommend
gaojie
这个题目是多重背包的经典题目,这个是一个模板,其实如果不是数据太大的话,我们是可以直接转化成01背包完成的,但是数据实在太大了,我们必须引用成完全背包,就是说当数据相当,价值*数量的值大于背包容量的时候,我们只会从数量中取出几件而已,所以转化成 完全背包了,我是很佩服第一个产生这种思想的人,我一般是想不到直接看别人的了,惭愧啊!!!!!
还有一个地方就是对于dp 的初始化赋值,这是去找到最后一个值,举个例子来说,我们要求dp[5],就必须要求到dp[0],但是假如没有找到最后一个值,就只能成为一个负值,所以在最后遍历是大于等于0的就是了,不过我觉得大于0 应该也是能AC的
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int i,j,k;
int n,m;
int dp[100044];
int va[100004];
int num[100004];
while(scanf("%d%d",&n,&m)!=EOF)
{
if((n==0)&&(m==0))
break;
for(i=1;i<=n;i++)
scanf("%d",&va[i]);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
for(i=0;i<100000;i++)
dp[i]=-9999999;
//dp[0]=1;
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=1;i<=n;i++)
{
if(va[i]*num[i]>=m)
{
for(j=va[i];j<=m;j++)
dp[j]=max(dp[j],dp[j-va[i]]+va[i]);
}
else
{
for(k=1;k<=num[i];k=k*2)
{
for(j=m;j>=va[i]*k;j--)
{
dp[j]=max(dp[j],dp[j-va[i]*k]+va[i]*k);
}
num[i]=num[i]-k;
}
if(va[i]>0)
{
for(j=m;j>=va[i]*num[i];j--)
dp[j]=max(dp[j],dp[j-va[i]*num[i]]+va[i]*num[i]);
}
}
}
int count=0;
for(j=1;j<=m;j++)
{
if(dp[j]>=0)
count++;
}
printf("%d\n",count);
}
return 0;
}
另外附上另一种传统的多重背包代码做法
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>
#define MAX_VOLUMN 100001
#define MAX_N 11
#define max(a,b) ((a)>(b) ? (a) : (b))
using namespace std;
int vol, n; // 背包容量 ;数量
int cv[MAX_VOLUMN], num[MAX_VOLUMN], f[MAX_VOLUMN];
void ZeroOnePack(int cost, int value) //(01)体积、质量、费用;价值
{
int v;
for(v=vol; v>=cost; v--)
f[v] = max(f[v], f[v-cost]+value);
}
void CompletePack(int cost, int value) //(无限)体积、质量、费用;价值
{
int v;
for(v=cost; v<=vol; v++)
f[v] = max(f[v], f[v-cost]+value);
}
void MultiplePack(int cost, int value, int amount) //(多重)体积、质量、费用;价值;数量
{
int k = 1;
if(cost*amount >= vol)
CompletePack(cost, value);
else
{
while((amount-k) >= 0)
{
ZeroOnePack(cost*k, value*k);
amount -= k;
k = k*2;
}
ZeroOnePack(cost*amount, value*amount);
}
}
void zpack( int v, int w )
{
for( int i=vol; i >= w; -- i )
f[i]=max( f[i], f[i-w]+v );
}
void cpack( int v, int w )
{
for( int i=w; i <= vol; ++ i )
f[i]=max( f[i], f[i-w]+v );
}
void dp()
{
int i;
for(i=1; i<=n; i++)
MultiplePack(cv[i], cv[i], num[i]);
}
int main(int argc, char **argv)
{
int i,k,j;
while(scanf("%d %d", &n, &vol),n+vol)
{
memset(f, 0, sizeof(f));
for(i=1; i<=n; i++)
scanf("%d",&cv[i]);
for(i=1; i<=n; i++)
scanf("%d",&num[i]);
dp();
/* for( i=1; i <= n; ++ i )
{
if( num[i]*cv[i]>=vol )
cpack( cv[i], cv[i] );
else
{
k=1;
while( k <= num[i] )
{
zpack( k*cv[i], k*cv[i] );
num[i]-=k;
k <<= 1;
}
zpack( num[i]*cv[i], num[i]*cv[i] );
}
} */
int sum=0;
for( i=1; i <= vol; i++ )
{
if( f[i] == i )
sum++;
}
printf("%d\n", sum); //最大数
}
return 0;
}