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代码):
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; }
相关文章推荐
- Codeforces 622F 「数学数论」「数学规律」
- CodeForces 396A 数论 组合数学
- codeforces 55D beautiful number [数学+数位DP]【动态规划+数论】
- 数论一(hdoj 简单数学题、推理题)
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
- ACdream 1148 GCD SUM(数学:推理+数论:莫比乌斯函数)
- CodeForces 258C Little Elephant and LCM(数学推理)
- Codeforces 396A 数论,组合数学
- CodeForces 659 A. Round House(简单数学推理)
- codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)
- CodeForces 706A Beru-taxi (数学计算,水题)
- CodeForces 623 B.Array GCD(数论+dp)
- 数论?? 数学 计算直线的交点数 DBSDFZ OJ
- 【CodeForces】115D Unambiguous Arithmetic Expression 组合数学
- codeforces 615 D. Multipliers (数论 + 小费马定理 + 素数)
- Codeforces 933 B. A Determined Cleanup(数学)
- 数学推理相关的几个名词及LaTeX用法
- codeforces 559C|51nod1486 Gerald and Giant Chess(组合数学+逆元)
- 【CodeForces】Toy Army(数学)
- codeforces 711E 数学