您的位置:首页 > 其它

Codeforces 382 B. Number Busters(数论推公式)

2014-08-09 16:08 429 查看
B. Number Busters

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Sample test(s)

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会减少一个,但是a有两种选择,当b>=x时,a不变,b=b-x;当b<x时,a减少一个,并且b=w-(x-b),问几秒后a=c?

题解:

这里可以看出如果是b>=X时,a与c之间的差值才会减小,否则只会B增加但是a与c之间的差值不会减小,这里将b=w-(x-b)可以变为b=b+(w-x);即每一次B要加时都是加上(w-x),这样我们可以设a减少了M次,其中b<x时有n次,这样b>=x时有m-n次,即a减少了n次,那么因为最后a=b所以 c-m=a-n 即 m-n=c-a 因为每一次 b>=x 吧b都会减少一个x,一共减了m-n次,又因为本身b是有一定的初始值的,本身的初始值可以减 b/x 次 x那么剩下的(m-n)-b/x 次减少的b的值就要由加的n次(w-x)来提供了,又因为本身b在减少完
b/x 次X后还会剩下 b%x 的值,所以由N次(w-x)提供的就是值就是 ((m-n)-b/x)*x-b%x,所以((m-n)-b/x)*x-b%x=(w-x)*n。又因为前面求出(m-n)=(c-a)所以[b]((c-a)-b/x)*x-b%x=(w-x)*n。这里还要注意一点:要是((c-a)-b/x)*x-b%x能整除(w-x)那么直接就能求出n,要是不能整除的话求出的
n 还要加 1 ,原因很简单能多不能少呀,要求的是 m 这里已经求出n来了,那么由[b] m-n=c-a 可得m=n+c-a,即答案。
[/b][/b]

[b][b]题目链接:http://codeforces.com/problemset/problem/382/B[/b][/b]

[b]代码:[/b]

#include<iostream>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;

int main(){
LL a,b,w,x,c;
scanf("%lld%lld%lld%lld%lld",&a,&b,&w,&x,&c);
if(c<=a) printf("0\n");
else {
LL p1=(c-a-b/x)*x-b%x;
LL p2=w-x;
LL p;
if(p1%p2==0) p=p1/p2;
else p=p1/p2+1;
printf("%lld\n",p+c-a);
}
return 0;
}
要是还有哪里不明白欢迎提出来,欢迎各位大神们提出错误
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: