HDU 2844 Coins (多重背包)



题意:有n种面值的硬币a[i],每种硬币有c[i]个,问能组成不大于m面值(1~m)的个数。


多重背包模板:by背包九讲。



#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define maxn 100001
#define mol 1000000007
//procedure MultiplePack(cost,weight,amount)
//     if cost*amount>=V
//         CompletePack(cost,weight)
//         return
//     integer k=1
//     while k<amount
//         ZeroOnePack(k*cost,k*weight)
//         amount=amount-k
//         k=k*2
//     ZeroOnePack(amount*cost,amount*weight)
int n,m,a[110],c[110];
int dp[maxn];
void CompletePack(int cost,int weight)//完全背包
{
	for(int v=cost;v<=m;v++)
		dp[v]=max(dp[v],dp[v-cost]+weight);
}
void ZeroOnePack(int cost,int weight)//01背包
{
	for(int v=m;v>=cost;v--)
			dp[v]=max(dp[v],dp[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount)
{
	if(cost*amount>=m)
		 CompletePack(cost,weight);
	else 
	{
		int  k=1;
		while( k<amount)//二进制拆分
		{
			 ZeroOnePack(k*cost,k*weight);
			 amount=amount-k;
			 k=k*2;
		}
		ZeroOnePack(amount*cost,amount*weight);
	}
}
int main()
{
	
	while(scanf("%d%d",&n,&m))
	{
		if(n==0&&m==0) break;
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
			scanf("%d",&c[i]);
		for(int i=1;i<=m;i++) dp[i]=0;
		for(int i=1;i<=n;i++)
			MultiplePack(a[i],a[i],c[i]);
		int ans=0;
		for(int i=1;i<=m;i++)
			if(dp[i]==i) 
				ans++;
		printf("%d\n",ans);

	}
	return 0;
}
/*
3 10
1 2 4 2 1 1
2 5
1 4 2 1
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值