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

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代码):

/*
*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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值