您的位置:首页 > 其它

HDU 2844 Coins 多重背包 (重量和价值相同)

2018-02-02 12:11 495 查看
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

题意:给出硬币的面值和数量,问1-m的价格里面有多少种价格可以用已经有的硬币支付

思路:完全背包  背包的重量是价格,重量和价值都是硬币的面值,数量即为硬币的数量。

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
const int N = 100005;
using namespace std;
typedef long long ll;
int m,n;
int w
,v
,num
;
int dp
;
void ZeroOnePack(int w,int v)
{
for(int i=m;i>=w;i--)
dp[i]=max(dp[i],dp[i-w]+v);
}
void  CompletePack(int w,int v)
{
for(int i=w;i<=m;i++)
dp[i]=max(dp[i],dp[i-w]+v);
}
void  MultiplePack(int w,int v,int cnt)
{
if(cnt*w>=m) //物品无限的完全背包
{
CompletePack(w,v);
return ;
}
else{
int k=1; //否则进行0-1背包转化
while(k<=cnt){
ZeroOnePack(k*w,k*v);
cnt-=k;
k=k*2;
}
ZeroOnePack(cnt*w,cnt*v);
return ;
}
}//完全背包模板
int main()
{
while(scanf("%d%d",&n,&m)==2){
if(n==0&&m==0)
break;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
MultiplePack(w[i],w[i],num[i]);
int cnt=0;
for(int i=1;i<=m;i++)
if(dp[i]==i)
cnt++;
printf("%d\n",cnt);
}
}

因为此题目是在判断是否可以到达某一个状态,所以可以用0 1或运算表示,需要注意的是dp[0]=1

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
const int N = 100005;
using namespace std;
typedef long long ll;
int m,n;
int w
,v
,num
;
int dp
;
void ZeroOnePack(int w,int v)
{
for(int i=m;i>=w;i--)
dp[i]=dp[i-w]|dp[i];
}
void  CompletePack(int w,int v)
{
for(int i=w;i<=m;i++)
dp[i]=dp[i-w]|dp[i];
}
void  MultiplePack(int w,int v,int cnt)
{
if(cnt*w>=m) //物品无限的完全背包
{
CompletePack(w,v);
return ;
}
else{
int k=1; //否则进行0-1背包转化
while(k<=cnt){
ZeroOnePack(k*w,k*v);
cnt-=k;
k=k*2;
}
ZeroOnePack(cnt*w,cnt*v);
return ;
}
}//完全背包模板
int main()
{
while(scanf("%d%d",&n,&m)==2){
if(n==0&&m==0)
break;
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
MultiplePack(w[i],w[i],num[i]);
int cnt=0;
for(int i=1;i<=m;i++)
cnt+=dp[i];
printf("%d\n",cnt);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: