1234567891011121314151617181920212223242526272829303132333435363738 |
- #include <bits/stdc++.h>
-
- using namespace std;
- using LL=long long;
-
- LL e_gcd(LL a, LL b, LL &x, LL &y) {
- if (b == 0) {
- x = 1;
- y = 0;
- return a;
- }
- LL ans = e_gcd(b, a % b, x, y);
- LL temp = x;
- x = y;
- y = temp - a / b * y;
- return ans;
- }
-
- LL cal(LL a, LL b, LL c) {
- LL x, y;
- LL gcd = e_gcd(a, b, x, y);
- if (c % gcd != 0) return -1;
- x *= c / gcd;
- b /= gcd;
- b = abs(b);
- LL ans = x % b;
- if (ans <= 0) ans += b;
- return ans;
- }
-
- int main() {
- for (LL x, y, m, n, L; cin >> x >> y >> m >> n >> L;) {
- LL ans = cal(m - n, L, y - x);
- if (ans == -1) cout << "Impossible" << endl;
- else cout << ans << endl;
- }
- return 0;
- }
|