列出式子是\( mx+s1\equiv nx+s2(mod\ L) (m-n)x+Ly=s2-s1 \),注意如果n-m<0的话,就把ac都乘-1变成正数,然后exgcd求解,最后注意x为负的话要取正
#include<iostream>
#include<cstdio>
using namespace std;
long long n,m,s1,s2,l,a,b,c,x,y,d;
void exgcd(long long a,long long b,long long &d,long long &x,long long &y)
{
if(!b)
{
d=a,x=1,y=0;
return;
}
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
int main()
{
scanf("%lld%lld%lld%lld%lld",&s1,&s2,&m,&n,&l);
a=m-n,b=l,c=s2-s1;
if(a<0)
a=-a,c=-c;
exgcd(a,b,d,x,y);//cerr<<x<<" "<<y<<endl;
if(c%d!=0)
puts("Impossible");
else
{
x=x*c/d,l=l/d;
printf("%lld\n",(x%l+l)%l);
}
return 0;
}