嗯...

题目链接:http://poj.org/problem?id=1061

两只青蛙相遇时肯定满足:x+k*m≡y+k*n(mod L)

            x+k*m-(y+k*n)=L*s

            k*(n-m)-s*L=x-y
 
即把模线性方程变形后a*x+b*y=c,用exgcd求解,
先ax+by=gcd(a,b)
判断c整除g
然后解就是(x+k*b/g) ,(y-k*a/g)
注意答案要求非负,所以要进行处理...
 

AC代码:

 #include<cstdio>
#include<iostream> using namespace std; inline void exgcd(long long a, long long b, long long &g, long long &x, long long &y){
if(!b) { g = a; x = ; y = ;}
else { exgcd(b, a % b, g, y, x); y -= x * (a / b);}
} int main(){
long long xx, yy, l, m, n, a, b, c, g, x, y;
scanf("%lld%lld%lld%lld%lld", &xx, &yy, &m, &n, &l);
a = n - m; b = l; c = xx - yy;
exgcd(a, b, g, x, y);//(n-m) * x + l * y = xx - yy
if(c % g) printf("Impossible\n");
else{
c /= g; b /= g;
printf("%lld\n", (x % b * c % b + b) % b);//处理非负
}
return ;
}

AC代码

05-21 01:43