poj 1061 青蛙的约会
一只青蛙1一开始在x位置,另一只青蛙2在y位置。青蛙1每次跳m米,青蛙2每次跳n米,并且都是向右跳的。地球经线长度是L,然后地球是圆的,也就是说,对L取模;问多少次后它们能跳到一起。如果它们永远不能相遇,就输出Impossible
求一个k,使x + k*m ≡ y + k*n (mod L) ,就变成(n-m) * k ≡ x-y (mod L)咯。然后这个方程其实就等价于(n-m)*k + L*H = x-y咯。这就是ax + by = c求整数x的模型。
要求ax + by = c的整数x解;就可以利用扩展欧几里得出答案;
View Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> #define LL long long using namespace std; LL ExGcd( LL a ,LL b ,LL &x ,LL &y) { if( b == 0 ) { x = 1; y = 0; return a; } LL Mod = ExGcd( b , a % b , x ,y ); LL temp = x; x = y; y = temp - (a/b) * y; return Mod; } int main( ) { LL x,y,n,m,L,X,Y; while( scanf( "%I64d %I64d %I64d %I64d %I64d",&x,&y,&n,&m,&L )==5 ) { LL A = x - y,B = m - n; LL mod = ExGcd( B , L , X ,Y ); if( A % mod ) { printf( "Impossible\n" ); } else { B /= mod; L /= mod; A /= mod; LL temp = (A*X)%L; while( temp < 0 ) temp += L; printf( "%I64d\n",temp ); } } //system( "pause" ); return 0; }


浙公网安备 33010602011771号