Algorithms note: Extended Euclidean Algorithm

1. introduction

Extended Euclidean Algorithm Euclidean algorithm (also known Euclidean algorithm) extension. In addition to calculating the greatest common divisor of two integers a, b, this algorithm can find an integer x, y (which is likely to be a negative number). When it comes to the greatest common factor generally, we will refer to a very basic fact: given two integers a and B, there will be such that x and y are integers ax + by = gcd (a, b). There are two numbers a, b, Euclidean them, their greatest common divisor available - as is well known. Then, the collection was removed division algorithm formula generated, go back, integer solutions can be obtained ax + gcd (a, b) is by =.

You should remember a high school problem contact: given integers a, b, n, asked equation ax + by = n when there is an integer solution, how to find all the integer solutions?

In fact, the solvability of the necessary and sufficient condition is gcd (a, b) is divisible by n, explained as follows:
We first make
a=gcd(a,b)*a1,b=gcd(a,b) * b2
Thereax+by=gcd(a,b )*a1x+gcd(a,b) * b1y=gcd(a,b)(a1x+b2y)=n
But if a1, b1, x, y are integers, then n / gcd (a, b) it must be an integer, n must be a multiple of gcd (a, b) will have the integer solutions.

E.g.
4x + 6y = 8,2x + 3y = 4 has an integer solution, 4x + 6y = 7 there is no integer solution

If it is determined solvable A method of solving problems is to find a solution that is (x0, y0), then the solution formula is as follows:
X = X0 + BT
Y = yO-AT (T is an arbitrary integer)
Therefore, the problem is converted how to find (x0, y0), and using the extended Euclidean algorithm can find this particular solution

2. Extended Euclidean Algorithm

When the equation ax + by = satisfied when gcd (a, b), can be extended Euclidean algorithm for (x0, y0), the following procedures:

void extend_gcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    extend_gcd(b,a%b,x,y);
    int tmp=x;
    x=y;
    y=tmp-(a/b)*y;
}

Sometimes, in order to simplify the description, the ax + by = gcd (a, b) by dividing both sides gcd (a, b), to give cx + dy = 1, where c = a / gcd (a, b), d = b / gcd (a, b). Obviously, c and d are relatively prime, cx + dy = 1 is the general solution
X = X0 + dt;
Y = yO-CT (T is an arbitrary integer)

Spread

1. seeking ax + by = n is an integer of Arbitrary equation

After Euclidean algorithm ax + by = gcd (a, b) with a spreading by means of which any further solutions equation ax + by = n integer to obtain a solution, the following steps:
(1) Analyzing the equation ax + by = n is integers solution, solvability condition gcd (a, b) is divisible by n
(2) Euclidean algorithm a solution ax + by = gcd (a, b) with the extension request (X0, yO)
( 3) in ax0 + by0 = gcd (a, b) on both sides simultaneously multiplying n / gcd (a, b) , to give:
ax0n / GCD (A, B) + by0n / GCD (A, B) = n-
(. 4) control ax + by = n, it is obtained a solution (x, y) is:
X = x0n / GCD (a, B)
Y = y0n / GCD (a, B)

Applications

Extended Euclidean Algorithm is a useful tool commonly used in the following situations contest the title:
(1) solving the indefinite equation
(2) solving the inverse mode
(3) to solve the congruence equation
though with extended Euclidean algorithm can count ax + by = gcd (a, b) of the general solution, but generally do not have this requirement, but some special requirements for solutions, for example, solving the inverse, is a common inverse modulo division operation tool

Published 55 original articles · won praise 12 · views 8950

Guess you like

Origin blog.csdn.net/qq_45740533/article/details/104110498