CF 382B Number Busters

26 篇文章 0 订阅
Arthur and Alexander are number busters. Today they've got a competition.

Arthur took a group of four integers a, b, w, x (0 ≤ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b ≥ x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1; b = w - (x - b).

You've got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c ≤ a.

Input

The first line contains integers a, b, w, x, c (1 ≤ a ≤ 2·109, 1 ≤ w ≤ 1000, 0 ≤ b < w, 0 < x < w, 1 ≤ c ≤ 2·109).

Output

Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.

Example
Input

4 2 3 1 6

Output

2

Input

4 2 3 1 7

Output

4

Input

1 2 3 2 6

Output

13

Input

1 1 2 1 1

Output

0

【题意】

现在有五个数据a,b,w,x,c。每一分钟都会产生下面的效果:

c--;
if(b>=x)
    b-=x;
else
{
    a--;
    b=w-(x-b);
}

问你经过多少秒之后会出现a>=c的情况。

【分析】
数学问题吧。推导过程如下:
首先把上面的化简一下:

c--;
if(b>=x)
    b-=x;
else 
{
    a--;
    b+=w-x;
}

然后我们发现c下降的永远比a快,所以上面的过程等价于:

if(b>=x)
{
    c--;
    b-=x;
}
else
    b+=w-x;

这样总共需要的变换次数就显而易见为c-a。然后我们发现c的减少只出现在b>=x的时候,也就是b减少的时候,b什么时候增加呢?显然是b < x的时候。所以容易发现整个过程其实就是一个b增加然后减少的过程。就是询问需要增减的步数。

所以代码就显而易见了。
【代码】

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
long long my_floor(long long a,long long b) {
    if(a%b) return a/b+1;
    return a/b;
}
int main() {
//    freopen("in.txt","r",stdin);
    long long a,b,w,x,c;
    while(scanf("%lld%lld%lld%lld%lld",&a,&b,&w,&x,&c)!=EOF) {
        if(c<=a) {
            printf("0\n");
            continue;
        }
        long long need = c-a;
        long long t1 = my_floor(need*x - b,w - x);
        long long t2 = need;
        printf("%lld\n",t1+t2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值