ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]

传送门

C. Om Nom and Candies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?

One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.

Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.

Input

The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).

Output

Print a single integer — the maximum number of joy units that Om Nom can get.

Sample test(s)
Input
10 3 5 2 3
Output
16
Note

In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.

 

哎,做过一次,知道了最大公约数化简,最后枚举的时候忘了用大的枚举。。。

 

10596551                2015-04-05 05:09:39    njczy2010                    C - Om Nom and Candies                         GNU C++    Accepted31 ms0 KB

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <stack>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <map>
 8 
 9 #define ll long long
10 int const N = 2500;
11 int const M = 205;
12 int const INF = 0x3f3f3f3f;
13 ll const mod = 1000000007;
14 
15 using namespace std;
16 
17 ll c,hr,hb,wr,wb;
18 ll g;
19 ll ans;
20 ll ma;
21 ll lcm;
22 
23 ll gcd(ll a,ll b)
24 {
25     if(b==0)
26         return a;
27     return gcd(b,a%b);
28 }
29 
30 void ini()
31 {
32     g=gcd(wr,wb);
33     if(wr<wb){
34         swap(wr,wb);
35         swap(hr,hb);
36     }
37     lcm=wr*wb/g;
38     ans=0;
39     ma=0;
40 }
41 
42 void solve()
43 {
44     if(c%lcm==0){
45         ans=max(c/wr*hr,c/wb*hb);
46         return;
47     }
48     ll shang=c/lcm;
49     if(shang>=2){
50         c-=lcm*(shang-1);
51         ans=(shang-1)*(max(lcm/wr*hr,lcm/wb*hb));
52     }
53     ll i;
54     for(i=0;i*wr<=c;i++){
55         ma=max(ma,i*hr+(c-i*wr)/wb*hb);
56     }
57     ans+=ma;
58 }
59 
60 void out()
61 {
62     printf("%I64d\n",ans);
63 }
64 
65 int main()
66 {
67     //freopen("data.in","r",stdin);
68     //scanf("%d",&T);
69     //for(int cnt=1;cnt<=T;cnt++)
70     //while(T--)
71     while(scanf("%I64d%I64d%I64d%I64d%I64d",&c,&hr,&hb,&wr,&wb)!=EOF)
72     {
73         ini();
74         solve();
75         out();
76     }
77 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4393700.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值