首页 > 代码库 > POJ-1742 Coins
POJ-1742 Coins
Coins
Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 37959 Accepted: 12882 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 0Sample Output
8 4
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 using namespace std; 5 int dp[100005]; 6 int main() 7 { 8 int n,m; 9 while(cin>>n>>m) 10 { 11 if(n==0&&m==0) break; 12 memset(dp,0,sizeof(dp)); 13 int val[1005],num[1005]; 14 for(int i=1;i<=n;i++) 15 cin>>val[i]; 16 for(int i=1;i<=n;i++) 17 cin>>num[i]; 18 dp[0]=1; 19 int sum[100005],ans=0; 20 for(int i=1;i<=n;i++) 21 { 22 memset(sum,0,sizeof(sum));//sum数组是个关键,第i件物品出现的次数 23 for(int j=val[i];j<=m;j++) 24 { 25 if(!dp[j]&&dp[j-val[i]]&&sum[j-val[i]]<num[i]) 26 { 27 dp[j]=1; 28 sum[j]=sum[j-val[i]]+1; 29 ans++; 30 } 31 } 32 } 33 cout<<ans<<endl; 34 } 35 return 0; 36 }
POJ-1742 Coins
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。