CodeForces 625A Guest From the Past

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output


Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated.

Kolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plastic liter bottle, that costs a rubles, or in glass liter bottle, that costs b rubles. Also, you may return empty glass bottle and get c (c < b) rubles back, but you cannot return plastic bottles.

Kolya has n rubles and he is really hungry, so he wants to drink as much kefir as possible. There were no plastic bottles in his 1984, so Kolya doesn’t know how to act optimally and asks for your help.

Input
First line of the input contains a single integer n (1 ≤ n ≤ 10^18) — the number of rubles Kolya has at the beginning.

Then follow three lines containing integers a, b and c (1 ≤ a ≤ 10^18, 1 ≤ c < b ≤ 10^18) — the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by returning an empty glass bottle, respectively.

Output
Print the only integer — maximum number of liters of kefir, that Kolya can drink.

Examples

input

10
11
9
8

output

2

input

10
5
6
1

output

2

Note
In the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir.

In the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he will drink two liters of kefir.

思路

本题题目意思很简单,就是那给定的钱币n去购买最多可能的开菲尔酒。一种是塑料瓶装,价格为a,一种是玻璃瓶装,价格为b。玻璃瓶装喝完后可以卖给店家换取一定的钱币c。分为以下两种情况:
1、当a<=(b-c)时,那我们就拿所有的钱去买塑料瓶装的开菲尔酒
2、当a>(b-c)时,先去购买玻璃瓶装的酒,直到剩下的钱加上喝完酒的酒瓶换来的钱都不够买玻璃瓶装的酒时,去买尽可能多的塑料瓶装的酒(这个时候有人可能会想,这种情况不是玻璃瓶装的酒更便宜吗? 不是的,我们比较的是b-c的差值,a可能比b要小,只是买玻璃瓶可以将瓶子卖掉后可获得c钱币)

这样分析后,我们就可以分这两种情况来写。对于第二种情况:数值范围是10^18,所以不能直接循环比较,这样会超时。当n>b时,我们先留下买一瓶酒的钱b,拿n-b的钱去买价格为b-c的酒,因为此时剩下的钱肯定比b要多,所以肯定可以买。这样处理后就把循环次数大大降低。

代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f

int main()
{
    __int64 i,j,t,n,a,b,c;
    while(~scanf("%I64d",&n))
    {
        __int64 sum=0;
        scanf("%I64d%I64d%I64d",&a,&b,&c);
        if(b-c>=a)
            sum=n/a;
        else if(b-c<a)
        {   
            if(n>b){
            sum+=(n-b)/(b-c);
            n=b+(n-b)%(b-c);
            }
        //  printf("sum=%I64d n=%I64d\n",sum,n);
            while(n>=b)
            {
                sum+=n/b;
                n=n/b*c+n%b;
            }
            sum+=n/a;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值