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.
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).
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.
4 2 3 1 6
2
4 2 3 1 7
4
1 2 3 2 6
13
1 1 2 1 1
0
题目链接:http://codeforces.com/contest/382/problem/B
这个题的意思是说给你5个数a,b,w,x,c;
我们有两种操作,一种是当b>=x时,b-=x;
另一种是当b<x时,b=w-(x-b);
我的方法是算一下循环次数,然后直接看最后一次,然后华丽丽的超时了,看了大神们的思路,我只感觉我还是个孩子,为什么要这样伤害我。
我们首先会发现只有当b<x时c和a的差距才会拉近,我们简单的处理一下
c=c-a;
w=w-x;
这里很容易就能知道原因是什么
然后我们会发现,第一种情况很明显发生了c次(计算之后的c)
然后我们假设第二种情况发生了k次,我们在推理的过程中会发现b永远是>=0的,所以b-c*x+k*w>=0
所以k>=((c*x)-b)/w;
然后向上取整就可以了。
借鉴大神们的思路
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define LL long long
using namespace std;
int main(){
LL a,b,w,x,c;
cin>>a>>b>>w>>x>>c;
c-=a;
w-=x;
if(c<=0)
return 0*printf("0\n");
else{
LL k=ceil((1.0*(x*c-b))/w);
cout<<c+k<<endl;
}
return 0;
}