There is nothing to say. Find out the formula, first find a group of special solutions, and finally obtain the desired solutions...
Train of Thought: Set up a frog to jump X times before meeting, get the equation (a + XM) % L = (B + Xn) % L to release X (m-N) + yl = B-. according to the Extended Euclidean algorithm, a group of solutions of X (m-n) + yl = gcd (m-N, L) can be obtained,
When (B-A) is a multiple of gcd (m-N, L), the original equation has an integer solution. Now we need to find the minimum number of beats X. The idea is referring to comeon4mydream's ~~ Materials ~~
To minimize the number of beats X, simply put, it is achieved through the general solution x = X1 + K * l/gcd (m-N, L ).Calculate the number of the first return values greater than 0.This number is increased in units of L/gcd (m-N, L.
# Include <iostream>
Using namespace STD;
_ Int64 gcd (_ int64 A ,__ int64 B ){
If (B = 0) return;
Return gcd (B, A % B );
}
_ Int64 kzgcd (_ int64 A ,__ int64 B ,__ int64 & X ,__ int64 & Y ){
If (B = 0) {x = 1, y = 0; return ;}
Int res = kzgcd (B, A % B, Y, X );
Y-= A/B * X;
Return res;
}
Int main (){
_ Int64 X1, Y1, m, n, l;
While (CIN> x1> Y1> m> N> L ){
_ Int64 c = x1-y1;
_ Int64 A = N-m;
_ Int64 B = L;
_ Int64 x = 0, y = 0;
_ Int64 flag = gcd (A, B );
If (n = M | C % flag) {cout <"impossible" <Endl; continue ;}
A/= Flag, B/= Flag, C/= flag;
Kzgcd (a, B, x, y );
X * = C; // X is the exclusive solution of AX + by = 1. Now it must be converted to the exclusive solution of AX + by = C.
X % = B; // returns the minimum positive integer.
While (x <0) x + = B;
Cout <x <Endl;
}
}