码迷,mamicode.com
首页 > 其他好文 > 详细

Coins POJ - 1742

时间:2017-08-05 00:11:57      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:sam   ++   amp   and   with   class   pen   std   cout   

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

Sample Output

8
4

题解:dp[i+1][j]:=用前i种数加和得到j时第i种数最多能剩余多少个(不能加和得到i的情况下为-1)(摘自《挑战》,话说个人对这种定义方式很懵逼)按照上述定义递推关系,这样如果前i-1
个数能加和得到j的话,第i个数就可以留下Mi个。此外,前i种数加和出j-Ai时第i种数还剩下K(K>0)的话,用这i种数加和j时第i种数就剩下K-1个。
复杂度O(nK)
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn=100005;
 8 
 9 int n,m;
10 int va[105],vo[105];
11 int dp[maxn];
12 
13 void solve()
14 {   memset(dp,-1,sizeof(dp));
15     dp[0]=0;
16     for(int i=1;i<=n;i++){
17         for(int j=0;j<=m;j++){
18             if(dp[j]>=0) dp[j]=vo[i];
19             else if(j<va[i]||dp[j-va[i]]<=0) dp[j]=-1;
20             else dp[j]=dp[j-va[i]]-1;
21         }
22     }
23     int ans=0;
24     for(int i=1;i<=m;i++) if(dp[i]>=0) ans++;
25     cout<<ans<<endl;
26 }
27 
28 int main()
29 {   while(~scanf("%d%d",&n,&m)){
30         if(n==0&&m==0) break;
31         for(int i=1;i<=n;i++) scanf("%d",&va[i]);
32         for(int i=1;i<=n;i++) scanf("%d",&vo[i]);
33         
34         solve();
35     }
36     return 0;
37 } 

 

Coins POJ - 1742

标签:sam   ++   amp   and   with   class   pen   std   cout   

原文地址:http://www.cnblogs.com/zgglj-com/p/7287416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!