0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 15:56:26 +00:00

C - ±1 Operation 1

https://atcoder.jp/contests/abc255/submissions/32397800
This commit is contained in:
Baoshuo Ren 2022-06-11 20:52:05 +08:00
parent 55e435d726
commit 47ecb8ab73
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

54
AtCoder/ABC255/C/C.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include <algorithm>
#include <cmath>
using std::cin;
using std::cout;
const char endl = '\n';
long long x, a, d, n;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> x >> a >> d >> n;
if (d > 0) {
if (x <= a) {
cout << std::abs(a - x) << endl;
} else if (x >= a + (n - 1) * d) {
cout << std::abs(x - a - (n - 1) * d) << endl;
} else {
long long k = (x - a) / d;
cout << std::min({
std::abs(a + k * d - x),
std::abs(a + (k - 1) * d - x),
std::abs(a + (k + 1) * d - x),
std::abs(a + (k - 2) * d - x),
std::abs(a + (k + 2) * d - x),
})
<< endl;
}
} else {
if (x >= a) {
cout << std::abs(x - a) << endl;
} else if (x <= a + (n - 1) * d) {
cout << std::abs(a + (n - 1) * d - x) << endl;
} else {
long long k = (x - a) / d;
cout << std::min({
std::abs(a + k * d - x),
std::abs(a + (k - 1) * d - x),
std::abs(a + (k + 1) * d - x),
std::abs(a + (k - 2) * d - x),
std::abs(a + (k + 2) * d - x),
})
<< endl;
}
}
return 0;
}