负数取模还是负数,但范围+取模的数就是正数
代码如下
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int exgcd(long long a,long long b,long long& x,long long& y)
{
int d=a;
if(b!=0) {
d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else{
x=1; y=0;
}
return d;
}
int main()
{
long long x,y,n,m,l;
while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)){
long long a=m-n,b=l,c=y-x;
if(a<0) {
a=-a; c=-c;
}
long long X,Y;
long long d=exgcd(a,b,X,Y);
if(c%d!=0) {
printf("Impossible\n");
}
else{
long long s=l/d;
printf("%lld\n",((X*c/d)%s+s)%s);
}
}
}