P1516 青蛙的约会
程序员文章站
2022-06-02 19:47:25
...
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll extend_gcd(ll a,ll b,ll &x,ll &y) {
if (b == 0) {
x = 1; y = 0; return a;
}
ll gcd = extend_gcd(b, a%b, y, x);
y -= (a/b) * x;
return gcd;
}
int main() {
ll x, y, m, n, L; cin >> x >> y >> m >> n >> L;
ll x0, y0, A = n-m, C = x-y;
if (A < 0) { A = -A; C = -C;}
ll gcd = extend_gcd(A, L, x0, y0);
if (C % gcd == 0) {
ll ans = (x0*C/gcd % (L/gcd) + L/gcd) % (L/gcd);
cout << ans << endl;
} else {
cout << "Impossible" << endl;
}
return 0;
}