0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-30 17:16:26 +00:00
OI-codes/AcWing/222/222.cpp

47 lines
774 B
C++

#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
long long x, y, n, m, l;
long long exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
long long g = exgcd(b, a % b, x, y);
long long t = x;
x = y;
y = t - a / b * y;
return g;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> x >> y >> m >> n >> l;
long long b = n - m,
c = x - y,
xx, yy;
if (b < 0) {
b = -b;
c = -c;
}
long long g = exgcd(b, l, xx, yy);
if (c % g) {
cout << "Impossible" << endl;
} else {
cout << (c / g * xx % (l / g) + (l / g)) % (l / g) << endl;
}
return 0;
}