I. Question
Poj 1061 frog appointment
[Questions about "Euclidean for maximum common approx." and "Extended Euclidean algorithm]
Ii. Source Project
# Include <iostream> using namespace STD; # define ll long longll gcd (ll a, LL B) {return B? Gcd (B, A % B): A;} // find x, y that satisfied the equation AX + by = D, which minimize the {| x | + | Y | }. PS: D = gcd (A, B ). void exgcd (ll a, LL B, ll & D, ll & X, ll & Y) {If (! B) {d = A, x = 1, y = 0;} else {exgcd (B, A % B, D, Y, X ); y-= x * (a/B);} // 1. calculate GCD (a, B) first. If n cannot be divisible by gcd (a, B, the equation has no integer solution. Otherwise, the two sides of the equation are divided by gcd (a, B) at the same time to obtain the new uncertainty equation a' * x + B '* Y = n ', in this case, gcd (A', B ') = 1; // 2. Use the Euclidean algorithm mentioned above to obtain a group of integers in the equation a' * x + B '* y = 1 to solve x0 and Y0, then n' * x0, n '* y0 is a group of Integer Solutions for equation a' * x + B' * Y = n'. // 3. According to the related Theorem in number theory, all integers of the equation a' * x + B '* Y = n' are obtained as follows: // X = n' * x0 + B '* t // y = n' * y0-a' * t // (T is an integer) bool getans (LL A, LL B, ll C, ll & ANS) // AX + by = C minimum integer solution {ll r = gcd (a, B), y0; if (C % R) // no solutions {return false;} A/= r; B/= r; C/= r; exgcd (a, B, R, ANS, y0); // at this point, the above description solves ll T = C * ANS/B; ans = C * ANS-T * B;/* all solutions of the equation at this time are: X = C * ANS-B * T. the smallest possible value of X is 0. If x = 0, the value of t at the hour of X can be obtained, however, since x = 0 is the possible minimum value, in fact, it is possible that X cannot get 0 at all. Therefore, the entire division method of the computer can be seen: in t = C * K1/B's t generation back to X = C * ANS-B * t, the obtained X may be less than 0. In this case, t = t + 1, the obtained X must be greater than 0. If X is greater If the value is 0, no correction is required. */If (ANS <0) {ans + = B;} return true;} int main () {ll X, Y, m, n, l; while (CIN> x> Y> m> N> L) {ll A = N-M, B = L, c = x-y; ll ans; bool flag = getans (a, B, c, ANS); If (! Flag) {cout <"impossible" <Endl; continue;} cout <ans <Endl ;}}
Iii. Solutions
It takes the same time for the two frogs to jump once. If we set it to t, x + MT is the distance from the coordinate origin to the end, and Y + nt is the distance from B, to meet each other, the subtraction must be an integer multiple of the perimeter of the ground, set to K * l; then: (x + MT)-(Y + nt) = kl; deformation: (m-N) T-(Y-x) = kl; that is, (m-N) T mod L = Y-X; linear homogeneous equation. This equation has solutions when and only when y-X can be counted as gcd (m-N, L) by M-N and l, that is, gcd (m-N, l) | Y-X. At this time, if x0 is a solution of the equation, that is, when T = x0, (m-N) T mod L = Y-X is true, then all the solutions can be expressed:
{X0 + K (L/gcd (m-N, L) | (kb integer )}.
The expanded application of Euclidean algorithms has the following three theorems:
Theorem 1: If D = gcd (a, B), a positive or negative integer k and l must be found so that D = A * x + B * Y.
Theorem 2: If gcd (a, B) = 1, the equation ax ≡ C (mod B) has a unique solution on [0, b-1.
Theorem 3: If gcd (a, B) = D, the equation ax ≡ C (mod B) has a unique solution on [0, B/d-1.
Proof: The same remainder equation is equivalent to ax + by = C. If there is a solution, and the two sides are divided by D, there will be a/D * x + B/d * Y = C/D, that is, A/D * x ≡ C/D (mod B/d), obviously gcd (A/D, B/d) = 1, soTheorem 2We know that X has a unique solution on [0, B/d-1. Therefore, the X of AX + by = C has a unique solution on [0, B/d-1], that is, in [0, b/d-1.
If we obtain a specific solution x of ax ≡ C (mod B), r = B/gcd (a, B), we can see that X is in [0, there is a unique solution on the r-1], so we can use X = (X % R + r) % R to find the minimum non-negative integer solution x! (X % R may be a negative value, which is kept within [-(r-1), 0], and a positive value is kept within [0, r-1. In addition, R is kept in [1, 2R-1], so the R is in [0, r-1] Again ).
Iv. Experiences
When the results obtained after running are correct, I submitted them. But failed.
Why?
Enter question!
At first, there was no while (CIN> x> Y> m> N> L in my program,
I wrote CIN> x> Y> m> N> L; no while judgment.
Therefore, the question requires that the "input only contains five integers in one row". During the input process, even if one row and five rows are lost, the task will run as usual and will not jump out.
Pay attention to the details and input and output!