Test instructions: Known frog 1 position x, Speed M, frog 2 position y, speed N, latitude length is L, ask them to meet the minimum number of hops.
Idea: Set the minimum number of hops to K, then (x + k*m)-(y + k*n) = Q*l; After finishing get k* (n-m) + q*l = y-x; At this time K and L are variables. Euclidean expansion has a linear equation a*x+b*y = C, when and only if C is an integer multiple of GDC (A, B), so this problem we can use this algorithm to obtain a x0 (x0 has been multiplied by multiples), so x0 to meet the test instructions of a solution, X solution is x0 + K "B/GCD (A, B "(The specific proof is not given, the RUJIA algorithm competition entry number based on the basis of a good proof), but the topic requires us to require the smallest positive integer x, using the following theorem:
If GCD (A, b) = d, then the equation ax≡c (mod b) has a unique solution on [0, b/d-1], that is, X has a unique solution in this interval, and this solution is the optimal solution we want, we assume that b/d = C, that through
(x%r + R)% r operation we can convert the X into the (0,r-1) range, which is the optimal solution we're looking for. The code is as follows:
Note: In fact GCD = 0 when the need for special judgment, but the data of this problem is not difficult for us, I also think of the AC after. And this problem data is relatively large, to use a long long. Finally, I want the reader to know that the sign on both sides of the equation does not affect our judgment.
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;#defineLL Long LongLL x, y; ll EX_GCD (ll A,ll b) {if(b = =0) {x=1; Y=0; returnA; } LL gcd= EX_GCD (b,a%b);///The X-y solution is not difficult, and two equations are equally solvable .LL tmp =x; X=y; Y= tmp-(A/b) *y; returngcd;}intMain () {LL x1,y1,m,n,l; while(~SCANF ("%i64d%i64d%i64d%i64d%i64d",&x1,&y1,&m,&n,&l)) {LL a= (nm); LL b=l; LL C= (x1-y1); LL GCD=EX_GCD (A, b); if(c% GCD! =0) {puts ("Impossible"); Continue; } LL k= c/GCD; LL tmp= b/GCD; X= x * k;///we need to expand it to K times, because X is the case of the greatest common divisor we find. x = (x%tmp + tmp)% tmp;///as the above operationprintf"%i64d\n", x); } return 0;}
POJ 1061 Date of the Frog (Euclidean expansion)