Extended Euclidean Algorithm

Source: Internet
Author: User

The original mathematics was not good. When I saw the lrj mathematical topic, I went online to Baidu to learn how to prove the Extended Euclidean algorithm.

First, let's talk about the simple Euclidean algorithm, that is, the division of the moving phase, which is very simple.

int gcd(int a,int b){    return b == 0 ? a : gcd(b,a % b);}

The following describes how to extend the Euclidean algorithm.

Returns X and y for a and B so that a * x + B * Y = gcd (A, B );

Let's set a> B, and x> Y

Then when B = 0, gcd (a, B) = A, so x = 1, y = 0;

If B! When the value is 0, we know that

Ax1 + by1 = gcd (a, B) = bx2 + (a % B) y2; (according to the simple Euclidean)

Let's simplify this formula.

Ax1 + by1 = bx2 + (a-(A/B) * B) y2;

Ax1 + by1 = bx2 + A * Y2-(A/B) * B * Y2;

Ax1 + by1 = A * y2 + B * (x2-(A/B) * Y2 );

Therefore, X1 = Y2;

Y1 = (x2-(A/B) * Y2 );

That is to say, the X and Y of each layer can be obtained from the X and Y of the previous layer, and the recursive endpoint is B = 0;

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <stack> # include <queue> # include <map> # include <set> # include <list> # include <string> # include <cmath> # include <sstream> # include <ctime> using namespace STD; # DEFINE _ pI ACOs (-1.0) # define INF 1 <10 # define ESP 1e-6typedef long ll; typedef unsigned long ull; typedef pair <int, int> pill; /* ====================================== ========================================================== ===================================== */Void gcd (int, int B, Int & D, Int & X, Int & Y) {If (! B) {d = A; X = 1; y = 0;} else {gcd (B, A % B, D, Y, X ); y-= x * (a/B) ;}} int main () {int X, Y, a, B, c; while (scanf ("% d ", & A, & B )! = EOF) {x = max (a, B); y = x; if (a <B)/* guarantees A> B */swap (A, B ); gcd (a, B, c, x, y);/* represents a * x + B * Y = C */printf ("(% d * % d) + (% d * % d) = % d \ n ", A, X, B, Y, c);} return 0 ;}


PS: In addition, if multiple groups of solutions are required, assume that a group of solutions (x0, y0) has been obtained, and set: a' = A/gcd (A, B ), B '= B/gcd (a, B), then any other solutions can be written as (x0 + K * B', y0-K * ');

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.