exgcd+min

This commit is contained in:
ljcjames 2022-08-17 11:44:41 +08:00
parent 4b55907332
commit e1d7c2a87d
1 changed files with 27 additions and 4 deletions

View File

@ -1,14 +1,37 @@
//weiwancheng
#include <iostream>
#include<cstdio>
#include<algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
int x,y,m,n,l;
long long exgcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
//x,y没赋初值
x=1;
y=0;
return a;
}
long long ret=exgcd(b,a%b,x,y);
long long tmp=x;
x=y;
y=tmp-a/b*y;
return ret;
}
int main()
{
scanf("%d",&x,&y,&m,&n,&l);
long long x,y,m,n,l;
scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);//%lld没打
long long a=m-n,b=l,c=y-x;
if(a<0)//负值没处理好
{
a*=-1;
c*=-1;
}
long long x0,y0;
long long g=exgcd(a,b,x0,y0);
if(c%g!=0)printf("Impossible");//WA 罪魁祸首
else printf("%lld",(c/g*x0%(b/g)+b/g)%(b/g));//负值没处理好
return 0;
}