#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll extgcd(ll a,ll b,ll&x,ll&y)
{
if(b==0){
x=1,y=0;return a;
}
ll d=extgcd(b,a%b,x,y);
ll z=x;x=y;y=z-y*(a/b);
return d;
}
int main()
{
ll x0,y0,a,b,l;
cin>>x0>>y0>>a>>b>>l;
if(a-b<0)swap(a,b),swap(x0,y0);
ll n=a-b,r=y0-x0;
ll x,y;
ll gcd=extgcd(n,l,x,y);
if(r%gcd){
puts("Impossible");return 0;
}
x=(x*r/gcd)%(l/gcd);//这里l就是m,r就是b
//while(x<0)x+=l/gcd;
x=(x+l/gcd)%(l/gcd);
cout<<x<<endl;
return 0;
}
线性同余方程(模板)
最新推荐文章于 2024-10-06 21:33:49 发布