题目链接:http://poj.org/problem?id=1061
题目大意:中文题,不解释
方法:设t为A青蛙和B青蛙的跳的次数,k为绕地球绕的圈数
则得出公式:(x+m*t)- (y+n*t)= k*L
化简得:(x-y)+(m-n)*t = k*L
化简:(m-n)*t - k*L = y-x(由扩展欧几里得定理可知:存在x0,y0使得a*x0+b*y0=gcd(a,b))
最终t = (x0+k(L/d))
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #define LL long long using namespace std; LL exgcd(LL a,LL b,LL &x,LL &y) { LL r,t; if(b==0) { x=1, y=0; return a; } r=exgcd(b, a%b, x, y); t=x; x=y; y=t-a/b*y; return r; } int main() { LL n, L, m, x, y, t; LL xx, yy; cin>>x>>y>>m>>n>>L; LL d = exgcd(n-m, L, xx, yy); if((x-y) % d != 0) cout<<"Impossible"<<endl; else cout<<((xx*(x-y)/d)%(L/d)+(L/d))%(L/d)<<endl; return 0; }