#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL exgcd(LL a,LL b,LL& x,LL& y)
{
if(a%b==0)
{
x=0,y=1;
return b;
}
LL r,tx,ty;
r=exgcd(b,a%b,tx,ty);
x=ty;
y=tx-a/b*ty;
return r;
}
//sd-tn=y-x;
int main()
{
LL n,d,x,y,xx,yy;
int ci;
cin>>ci;
while(ci--)
{
cin>>n>>d>>x>>y;
LL c=(y-x+n)%n;
LL s,t;
LL gcd=exgcd(d,n,xx,yy);
if(c%gcd)
{
printf("Impossible\n");
continue;
}
xx*=c/gcd;
n/=gcd;
LL res=xx%n;
if(res<0)
res+=n;
cout<<res<<endl;
}
return 0;
}
NEFU 84(五指山)(扩展欧几里得)
最新推荐文章于 2020-03-06 22:18:51 发布