Coins
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 91 Accepted Submission(s) : 36
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
2009 Multi-University Training Contest 3 - Host by WHU
题目的意思是在1到m,有多少面值可以被已有的硬币凑成。
比如例2,拥有2种硬币,1面值的2个,4面值的1个,在1到5内可以凑的有1,2,4,5共4个数字,所以输出4。
思路是用前一状态推后一个状态,比如要凑j(j<=m),如果j-v[i]可以被凑成,那么加上当前的v[i],j也是可以凑成,但是要注意当前凑j所用的 当前硬币个数为 凑j-v[i]的硬币个数+1。凑j所用的 当前硬币个数<=c[i]
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int n,m;
int v[105],c[105];
int dp[100005];
int cnt[100005];//记录当前已用的硬币个数
while(cin>>n>>m)
{
if(m==0&&n==0) break;
int i,j,k;
memset(dp,0,sizeof(dp));
dp[0]=1;//因为,比如,硬币面值为2,此时dp[2]!=0&&dp[2-2]==1
for(i=1;i<=n;i++)
{
cin>>v[i];
}
for(i=1;i<=n;i++)
{
cin>>c[i];
}
int cnts=0;
for(i=1;i<=n;i++)//每一种硬币遍历
{
memset(cnt,0,sizeof(cnt));//因为只针对当前硬币
for(j=v[i];j<=m;j++)
{
if(dp[j]!=1&&dp[j-v[i]]==1&&cnt[j-v[i]]<=c[i]-1)
{//价值j还没能被凑成,但j-v[i]可以凑成,且当前所用硬币个数+1<=c[i]
dp[j]=1;
cnt[j]=cnt[j-v[i]]+1;//在凑成j-v[i]的基础上多用了一个当前的硬币
//如果凑成j-v[i]用了1个当前硬币,j就要用2个
cnts++;//种数++
}
}
}
cout<<cnts<<endl;
}
return 0;
}