Ultraviolet A Problem 10104 Euclid problem (Euclidean problem)

Source: Internet
Author: User
Tags dot net
// 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.