mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-05 02:26:26 +00:00
55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
#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;
|
|
}
|