您的位置:首页 > 其它

CodeForces 382B Number Busters(数论-数学推理)

2015-03-04 00:29 489 查看
Description

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 Input

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;

每一秒发生一次如下变化:

x>=b时,b=b-x,c–;

x < b时,a–,b=w-(x - b);

问:变化到a>=c需要多少秒。

分析:

按题意直接做,while循环大法好。

TL。

找到x>=b时的规律,改为秒数直接加上b/x,b=b%x。

TL。

看来只好从x < b上动刀了,

首先做点简化,c = c - a,w = w - x,这样一来b就只存在两种变化:

b>=x时,b-=x;

b < x时,b+=w;

第一种变化显然发生了c次(此处的c已经等于c - a了);

第二种变化假设发生了k次,我们知道b永远是>=0的,因为题目中一开始x < b,所以b - c * x +k * w >= 0;

所以k >= ((c*x)-b)/w;

对右式向上取整即可得到k的最小值

则答案即使k + c。//分析能力太渣,第三种方法全赖于大神们前车之鉴

代码如下(附之前两次TL代码):

[code]/*
*Author : Flint_x 
*Created Time : 2015-03-02 22:58:43 
*File name : j.cpp 
*/

#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

//int main(){
//  lint a,b,w,x,c;
//  while(cin >> a){
//      cin >> b >> w >> x >> c;
//      lint count = 0;
//      while(c > a){
//          if(b >= x) b -= x;
//          else{
//              a --;
//              b = w - (x - b);
//          }
//          c --;
//          count ++;
//      }
//      cout << count << endl;
//  }
//  return 0;
//}
//int main(){
//  lint a,b,w,x,c;
//  while(cin >> a){
//      cin >> b >> w >> x >> c;
//      lint count = 0;
//      while(c > a){
//          if(b >= x){
//              lint  m = b / x;
//              if( c - m < a){
//                  count += c - a;
//                  c = a;
//              }
//              else{
//                  count += b/x;
//                  b = b % x;
//                  c = c - m;
//              }
//          }
//          else{
//              a --;
//              b = w - (x - b);
//              c --;
//              count ++;
//          }
//          
//          
//      }
//      cout << count << endl;
//  }
//  return 0;
//}

int main(){
    lint a,b,w,x,c;
    while(cin >> a){
        cin >> b >> w >> x >> c;
        lint count = 0;
        c -= a;
        w -= x;
        lint k;
        if( c <= 0 ) count = 0;
        else
        {
            k = ceil((1.0*(x*c - b))/w);
            count = c + k;
        }
        cout << count << endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: