题意 : 中文题不详述。
思路 : 设经过s步后两青蛙相遇,则必满足(x+m*s)-(y+n*s) = K*L(k = 0,1,2....)
变形得:(n-m)*s+K*L = x-y ;
另a = n-m,b = L,c = x-y,则上式变为a*s+b*k = c。于是就变成了扩展欧几里德,求解不定方程,线性同余方程。只要上式存在整数解,则这两个青蛙能相遇,否则不能。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h> using namespace std ; #define LL long long LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b) ;
} void exeulid(LL a,LL b,LL &m,LL &n)
{
if(b == ){
m = ;
n = ;
return ;
}
exeulid(b,a%b,m,n) ;
LL t = m ;
m = n ;
n = t-a/b*n ;
} int main()
{
LL x,y,l,a = ,b,c,r,m,n,j1 = ,j2= ,t;
while(~scanf("%lld %lld %lld %lld %lld",&x,&y,&m,&n,&l))
{
a = n-m ;
b = l ;
c = x-y ;
r = gcd(a,b) ;
if(c % r){
printf("Impossible\n") ;
continue ;
}
a /= r ;
b /= r ;
c /= r ;
exeulid(a,b,j1,j2) ;
t = c*j1/b ;
j1 = c*j1-t*b ;
if(j1 < )
if(b > )
j1 += b ;
printf("%lld\n",j1) ;
}
return ;
}