POJ - 1742 Coins

Coins

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 42608   Accepted: 14398

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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

有n种面额的硬币,面额个数分别为A_i、C_i,求最多能搭配出几种不超过m的金额?

//c1,c2,c3,..cn 代表 a1,a2,a2...an前的数量  数量*价值加起来 <=m 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int n,m,dp[100005],sum[100005],cnt;
int a[205],c[205];
int solve()
{
	 cnt=0;
	 for(int i=0;i<n;i++)
     {
       memset(sum,0,sizeof(sum));//sum[j]表示当前这种面值为j时用了a[i]多少次,它在使用时限制a[i]被使用的数量,否则就可以用无限个了
       for(int j=a[i];j<=m;j++)
       {
       	 int t=j-a[i];/// 用dp[j]表示能组成这价值为j的钱和不能(0与1),dp[j-a[i]] 表示之前能组成j-a[i]这么多钱 
         if(sum[t]<c[i]&&!dp[j]&&dp[t])//!dp[j]表示之前没有组成j这么多的钱
         {
           sum[j]=sum[t]+1;// 组成j这么多钱还需要用多一次a[i]这样的币值
           dp[j]=1;//dp[j]=1就表示已经组成过j那么多的钱了,下次同样能构成j那么多的钱就不要再重复算了
           cnt++;
         }
       }
    }
}
int main()
{
	while(cin>>n>>m)
	{
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		for(int i=0;i<n;i++)
		{
			cin>>c[i];
		}	
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		solve();
		cout<<cnt<<endl;
	}return 0; 
 } 

Sample Output

8
4

Source

猜你喜欢

转载自blog.csdn.net/qq_40046426/article/details/81084550