// Euclid problem (Euclidean problem) // PC/Ultraviolet IDs: 110703/10104, popularity: A, success rate: average level: 1 // verdict: accepted // submission date: 2011-06-10 // UV Run Time: 0.684 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net /// you can find two numbers x and y Based on Euclid, so that: /// A * x + B * Y = gcd (A, B) (1) /// due to gcd (a, B) = gcd (B, a'), where a' = A-B * [A/B], according to mathematical induction, assume that // has found the integers 'X' and y', so that: /// B * x' + a' * y' = gcd (A, B) (2) /// substitute the expression of a' into the formula (2, get: // B * x' + (a-B * [A/B]) * y' = gcd (a, B) (3) /// the starting condition is a * 1 + 0*0 = gcd (A, 0: /// x = y' // y = x'-B * [A/B] * y' // you can obtain the equation (1) from the preceding A group of solutions, because equation (1) and cool equation A * x except gcd (A, B) (mod B) // are equivalent, in the process of obtaining GCD through Euclid, we can obtain a solution of the homogeneous equation, then all solutions can be expressed: /// x = y' + K * B/gcd (a, B) // y = (gcd (A, B) -A * y'-A * K * B/gcd (a, B)/B // K is an arbitrary integer. Given that both A and B are positive integers, you do not need to consider the case where a or (and) B is 0. // Because gcd (a, B) <= min (a, B), X and Y must have min (x, y) <= 0, max (x, y)> 0. //// You can expand the GCD process and obtain the corresponding solution. The output can meet the requirements. The X and Y obtained by the algorithm are the absolute values and // the smallest (why is this true? Because the starting condition A * 1 + 0*0 = gcd (A, 0) determines that it must be the absolute value and // the smallest ). In addition, the description of the question is a bit confusing. We should first consider the minimum absolute value, and then consider the condition x <= y. # Include <iostream> # include <cmath> using namespace STD; long gcd (long a, long B, long * X, long * Y) {long Tx, Ty; long g; if (B> A) return gcd (B, A, Y, x); If (B = 0) {* x = 1; * Y = 0; return ;} G = gcd (B, A % B, & Tx, & ty); * x = ty; * Y = TX-floor (a/B) * ty; return g ;} int main (int ac, char * AV []) {long a, B; long g; long X, Y; while (CIN> A> B) {G = gcd (a, B, & X, & Y); cout <x <"" <Y <"" <G <Endl ;} return 0 ;}