思路:
初始位置x, y, 每次跳跃距离x, y, 圈长l,即解方程(n − m) * t ≡ x − y (mod l),即(n − m)t+kl=x−y,设a = n - m, b = l, c = x - y,
即求at + bk = c 的t最小整数解。
设g = gcd(a, b), 可求出at + bk = g的t的特解t0
由x = (x % (b / d) + b / d) % (b / d);找到最小整数解,即为答案
注意如果a小于0,即n-m<0,则要把等式左右都取相反数,即a=-1,c=-c
// Decline is inevitable,
// Romance will last forever.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define LL long long
#define int long long
#define endl '\n'
const int maxn = 3e2 + 10;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, -1, 1}; //{0, 0, 1, 1, 1,-1,-1,-1}
const int dy[] = {1, -1, 0, 0}; //{1,-1, 1, 0,-1, 1, 0,-1}
const int P = 998244353;
int a, b, c;
int d, x, y, m, n, l;
int t;
int exgcd(int a, int b, int &x, int &y) {
if(!b) {x = 1; y = 0; return a;}
int d =exgcd(b, a%b, x, y);
int z = x; x = y; y = z - y * (a / b);
return d;
}
void solve() {
cin >> x >> y >> m >> n >> l;
a = n - m; b = l; c = x - y;
if(a < 0) a=-a, c=-c;
int g = exgcd(a, b, t, l);
if(c%g) {
cout << "Impossible\n";
return;
}
t = (c/g*t%(b/g)+b/g) % (b/g);
cout << t << endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
solve();
return 0;
}