#include<iostream>usingnamespace std;typedeflonglong ll;
ll ex_gcd(ll a, ll b, ll &x, ll &y){if(b ==0){
x =1;
y =0;return a;}
ll d =ex_gcd(b, a % b, x, y);
ll t = x;
x = y;
y = t -(a / b)* y;return d;}voidsolve(ll a, ll b, ll c){
ll x, y;
ll d =ex_gcd(a, b, x, y);if(c % d !=0){
cout <<"Impossible"<< endl;return;}
x = x * c / d;//原式应该是(n-m)*t+k*L=x-y//ex_gcd求解的式子是a*x+b*y=gcd(a,b),即(n-m)*x+L*y=gcd(n-m,L)//要变成原式子 对等式两边同时乘以 c/d ,就变成了 (n-m)*(x*c/d)+L*(y*c/d)=gcd(n-m,L)*c/d//又因为d=gcd(n-m,L) 化简:(n-m)*(x*c/d)+L*(y*c/d)=c//所以 (x*c/d) 就是对应的 t
ll t = b / d;//这里用到了定理三//定理一:如果d = gcd(a, b),则必能找到正的或负的整数k和l,使 d = a*x+ b*y。//定理二:若gcd(a, b) = 1,则方程ax ≡ c (mod b)在[0, b-1]上有唯一解。//定理三:若gcd(a, b) = d,则方程ax ≡ c (mod b)在[0, b/d - 1]上有唯一解。if(t <0) t =-t;
x =(x % t + t)% t;
cout << x << endl;}intmain(){
ll x, y, m, n, L;while(cin >> x >> y >> m >> n >> L){solve(n - m, L, x - y);}return0;}
同余方程 + 扩展欧几里得vjudge原题链接#include <iostream>using namespace std;typedef long long ll;ll ex_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; re...