JZYZOJ1371 青蛙的约会 扩展欧几里得 GTMD数论

时间:2022-07-12 11:36:18

http://172.20.6.3/Problem_Show.asp?id=1371

题意是两个青蛙朝同一个方向跳

http://www.cnblogs.com/jackge/archive/2013/04/22/3034925.html详细的题解,大概是网上能看到的最简单易懂的扩展欧几里得讲解了
 
代码
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
void exgcd(int a,int b,long long &x,long long &y){
if(!b){
x=;y=;
return;
}
exgcd(b,a%b,x,y);
long long w=x;x=y;
y=w-y*(a/b);
}
long long gcd(long long x,long long y){
while(y){
int w=y;y=x%y;x=w;
}
return x;
}
int main(){
long long x,y,m,n,l;
scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l);
long long t,z=x-y,zz=n-m,k,d=gcd(n-m,l);
if(z%d) printf("Impossible\n");
else{
z/=d;
exgcd(zz/d,l/d,t,k);
t=z*t-z*t/l*l;
if(t<)t+=l;
printf("%I64d\n",t);
}
return ;
}