青蛙相遇可列方程
(x+mt)%L=(y+nt)%L,即(m-n)*t+k*L=y-x
可转化为ax+by=c
先求出ax+by=gcd,再将解得的x*(c/gcd)
求出最小整数解即可:
用x=(x*b+b)%b....(这里的b为b/gcd
#include <iostream>
#define LL long long
using namespace std;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL r=exgcd(b,a%b,x,y);
LL t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
LL x,y,m,n,L;
cin>>x>>y>>m>>n>>L;
LL a,b,c,gcd;
a=n-m;
b=L;
c=x-y;
if(a<0)
{
a=-a;
c=-c;
}
gcd=exgcd(a,b,x,y);
if(m==n||c%gcd)
{
cout<<"Impossible"<<endl;
}
else
{
c/=gcd;
b/=gcd;
long long ans=x*c;//x是ax+by=gcd的解,ax+by=c的解只需x*(c/gcd)
// long long ans=t%b;
// if(ans<=0)
// ans+=b;
ans=(ans%b+b)%b;//使解>0,且最小
cout<<ans<<endl;
}
return 0;
}