题目分析:
设时间为t,则两个青蛙的位置分别为(x+mt)mod L、(y+nt) mod L,相遇即是(x+mt)%L=(y+nt)%L,即(m-n)*t+k*L=y-x。
OK,现在已经符合ax+by=c的方程了,设a=m-n,b=L,c=y-x,然后套用模板求出特解t的值,注意t>0,所以要用通解公式得出最小正整数(为啥刚开始我就没想到这一点呢)。最后注意用long long~
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
using namespace std;
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{ if(b==0){
x=1;y=0;
return a;
}else{
long long r=extend_gcd(b,a%b,y,x);
y-=x*(a/b);
return r;
}
}
int main()
{
long long x,y,m,n,L;
long long a,b,c,gcd;
while(scanf("%lld %lld %lld %lld %lld",&x,&y,&m,&n,&L)!=EOF)
{ a=m-n;
b=L;
c=y-x;
if(a<0)
{ a=-a;
c=-c;
}
gcd=extend_gcd(a,b,x,y); //传的是地址 x,y的值已经被清空 这个纠结了好久
if(c%gcd!=0)
cout<<"Impossible"<<endl;
else{
x=x*c/gcd;
int t=b/gcd; //通解 xo=x+b/gcd(a,b) yo=y+a/gcd(a,b).
if(x>=0)
x=x%t;
else
x=x%t+t;
cout<<x<<endl;
}
}
return 0;
}