Coins(poj2844)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2844

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

题目大意就是你有n种类型的硬币,要买一个手表,这个手表不会超过m.

第一行是n,m

第二行是n种硬币

第三行是每种硬币的个数

让你输出可以支付多少价格.

由此可知这是一个多重背包问题.如果不了解多重背包,请查看https://blog.csdn.net/qq_38984851/article/details/81133840

#include<iostream>
#include<algorithm>
#include<string.h>
#define INF 0x3f3f3f3f
using namespace std;
int m;
int v[1005];
int num[1005];
int dp[100005];
void Zero(int cost){
	for(int i=m;i>=cost;i--)
		dp[i]=max(dp[i],dp[i-cost]+cost);
}
void Com(int cost){
	for(int i=cost;i<=m;i++)
		dp[i]=max(dp[i],dp[i-cost]+cost);
}
void mul(int v,int num){
	if(v*num>=m)
		Com(v);
	else{
		int k=1;
		while(k<=num){
			Zero(k*v);
			num-=k;
			k*=2;
		}
		Zero(v*num);
	}
}
int main(){
	int n;
	while(cin>>n>>m,(n||m)){
		for(int i=1;i<=n;i++)
			cin>>v[i];
		for(int i=1;i<=n;i++)
			cin>>num[i];
		memset(dp,0,sizeof(dp));
		for(int i=0;i<=m;i++)
			dp[i]=-INF;   
		dp[0]=0;
		for(int i=1;i<=n;i++)
			mul(v[i],num[i]);
		int ans=0;
		for(int i=1;i<=m;i++)
			if(dp[i]>0)  
				ans++;
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38984851/article/details/81190829