( x + k m ) m o d    L = ( y + k n ) m o d    L (x+km) \mod L = (y+kn) \mod L (x+km)modL=(y+kn)modL
( x − y ) = k ( n − m ) + ∗ t L (x-y) = k(n-m) + * tL (x−y)=k(n−m)+∗tL
即是用拓展欧几里得解
#include<cstdio>
#include <algorithm>
using namespace std;
#define fst first
#define sec second
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcopy(a,b,sizeof b)
typedef long long LL;
typedef pair<int,int> P;
const int MAX_N = 510;
const int MAX_M = 10000;
LL exgcd(LL a,LL b,LL& X,LL& Y) {
LL q,temp;
if (b == 0) {
X = 1; Y= 0;
return a;
} else {
q = exgcd(b,a % b,X,Y);
temp = X;
X = Y;
Y = temp - (a / b) * Y;
return q;
}
}
int main() {
LL x,y,m,n,L;
scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L);
LL a,b;
if (n < m) {
swap(n,m);
swap(x,y);
}
LL t = exgcd(n-m,L,a,b);
if ((x - y) % t != 0) {
printf("Impossible\n");
} else {
a = a * (x - y) / t;
a = (a % L + L) % L;
printf("%lld\n",a);
}
return 0;
}