// 参考->青蛙的约会 exgcd解同余方程

定理证明->点我

 1 /*
 2  * @Promlem:
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-20 17:19:56
 7  * @LastEditTime: 2019-10-20 18:52:09
 8  */
 9 #include<cstdio>
10 #include<cstring>
11 #include<cmath>
12 #include<iostream>
13 #include<string>
14 #include<algorithm>
15 #include<vector>
16 #include<queue>
17 #include<stack>
18 #include<set>
19 #include<map>
20 #define rep(i, n) for(int i=0;i!=n;++i)
21 #define per(i, n) for(int i=n-1;i>=0;--i)
22 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
23 #define rep1(i, n) for(int i=1;i<=n;++i)
24 #define per1(i, n) for(int i=n;i>=1;--i)
25 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
26 #define L k<<1
27 #define R k<<1|1
28 #define mid (tree[k].l+tree[k].r)>>1
29 using namespace std;
30 const int INF = 0x3f3f3f3f;
31 typedef long long ll;
32
33 inline ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
34
35 inline ll exgcd(ll a, ll b, ll &x, ll &y) {
36     if(!b) {
37         x = 1;
38         y = 0;
39         return a;
40     }
41     ll ans = exgcd(b, a%b, x, y);
42     ll temp = x;
43     x = y;
44     y = temp - a/b*y;
45     return ans;
46 }
47
48 int main() {
49     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
50     ll x, y, m, n, l;
51     cin >> x >> y >> m >> n >> l;
52     ll a = l, b = n-m, c = x-y, d = gcd(a, b);
53     if(c % d != 0) cout << "Impossible\n";
54     else {
55         exgcd(a, b, x, y);
56         cout << ((y*c/d)%(l/d)+(l/d))%(l/d) << "\n";
57     }
58     return 0;
59 }
02-01 16:31