前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ-------2844Coins

HDUOJ-------2844Coins

作者头像
Gxjun
发布2018-03-22 12:27:03
5020
发布2018-03-22 12:27:03
举报
文章被收录于专栏:ml

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5936    Accepted Submission(s): 2458

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.

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

 代码: 简单的多重背包,但是恶心的地方是m居然可以为负数....是不是很伤不起呀...哈哈

代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 const int maxn=105;
 5 struct price
 6 {
 7     int val;
 8     int num;
 9 };
10 price sta[maxn];
11 int dp[100010];
12 int main()
13  {
14     int n,m,i,j,ans;
15     while(scanf("%d%d",&n,&m),n+m)
16     {
17         
18         for(i=0;i<n;i++)
19             scanf("%d",&sta[i].val);
20         for(ans=i=0;i<n ;i++)
21         {
22           scanf("%d",&sta[i].num);
23             ans+=sta[i].val*sta[i].num ;
24         }
25         int tem=ans<m?ans:m;
26         if(tem<0)
27         {
28             printf("0\n");
29         continue;
30         }
31         memset(dp,-1,sizeof(dp[0])*(tem+1));
32         dp[0]=0;
33         for(i=0;i<n;i++)
34         {
35             if(sta[i].num*sta[i].val>=tem)  //完全背包
36             {
37                 for(j=sta[i].val ;j<=tem;j++)
38                 {
39                     if(dp[j-sta[i].val]>-1&&dp[j]<dp[j-sta[i].val]+sta[i].num)
40                         dp[j]=dp[j-sta[i].val]+sta[i].num;
41                 }
42             }
43             else
44             {
45                 int k=1;
46                 while(sta[i].num>=k)
47                 {
48                     for(j=tem ;j>=k*sta[i].val ; j--)
49                     {
50                       if(dp[j-k*sta[i].val]>-1&&dp[j]<dp[j-k*sta[i].val]+k)
51                         dp[j]=dp[j-k*sta[i].val]+k;
52                     }
53                     sta[i].num-=k;
54                     k<<=1;
55                 }
56                    
57                  for(j=tem ;j>=sta[i].num ;j--)
58                  {
59                     if(j<sta[i].num*sta[i].val) break;
60                     if(dp[j-sta[i].num*sta[i].val]>-1&&dp[j]<dp[j-sta[i].num*sta[i].val]+sta[i].num)
61                         dp[j]=dp[j-sta[i].num*sta[i].val]+sta[i].num;
62                  }
63 
64             }
65         }
66         ans=0;
67         for(i=1;i<=tem;i++)
68         {
69             if(dp[i]!=-1)ans++;
70         }
71         printf("%d\n",ans);
72     }
73     return 0;
74  }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-03-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Coins
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档