exgcd。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; long long p,q,m,n,l,x,y,a,b,c; long long gcd(long long a,long long b) { if (b==0) return a; return gcd(b,a%b); } void exgcd(long long a,long long b,long long &x,long long &y) { if (b==0) {x=1;y=0;return;} exgcd(b,a%b,x,y); long long t=x;x=y;y=t-a/b*y; } int main() { scanf("%lld%lld%lld%lld%lld",&p,&q,&m,&n,&l); a=m-n;b=l;c=q-p; long long d=gcd(a,b); if (c%d) printf("Impossible\n"); else { a/=d;b/=d;c/=d; exgcd(a,b,x,y); b=abs(b);x=((c*x)%b+b)%b;if (!x) x+=b; printf("%lld\n",x); } return 0; }