Extended Euclidean algorithm
Let's start with what's called Euclid's Algorithm.
There are two number a B, now we ask for a B greatest common divisor, how to ask? Enumerate their factors? Not realistic, when a B is very large, the enumeration looks so naïve, how to do?
Euclid has a very useful theorem: Gcd (A, b) = gcd (b, a%b) so that we can solve the greatest common divisor of a and B in almost log time complexity, which is the Euclidean algorithm, which is described in the C + + language as follows:
Because it is written in a recursive, it looks very concise, but also very good memory. So what is the extension of Euclid?
Now that we know that the greatest common divisor of A and B are gcd, then we will be able to find such x and Y, so that: A*x + b*y = GCD This is an indefinite equation (in fact, a loss graph equation), there are many solutions is certain, but as long as we find a special set of solutions x0 and y0 that , we can use x0 and y0 to express the general solution of the indefinite equation:
x = x0 + (B/GCD) *t
y = y0– (A/GCD) *t
Why not:
x = x0 + b*t
y = y0–a*t
That's because:
B/GCD is a factor of B, A/GCD is a factor, right? So, since the value range of T is an integer, do you Say (B/GCD) *t take more value or b*t fetch more value? Similarly, (A/GCD) *t take more value or A*GCD fetch more value? That must be asked again, then why not a smaller number, it has to be b/gcd and A/GCD?
Notice: we make B = b/gcd, a = A, gcd, so, A and B must be mutual, right? Does that mean that the smallest factor is a and B? If there is nothing to understand, see "Basic Number Theory" (Harbin Institute of Technology Press), this book on the general solution of the indefinite equation is very clear
Now, we know that there must be X and Y to make: a*x + b*y = gcd, so, how do you find this special solution x and y? Just add a few tweaks to the Euclidean algorithm.
We observed that the Euclidean algorithm stopped in the state: A= gcd, b = 0, so, can this give us a way to solve X y? Because, at this time, as long as a = GCD coefficient is 1, then as long as the coefficient of B is 0 or other value (no matter how much, anyway, any number multiplied by 0 equals 0 but a coefficient must be 1), then we will have: a*1 + b*0 = gcd
Of course this is the final state, but can we reverse from the final state to the original state?
Assuming that we are currently dealing with the greatest common divisor of A and B, and finding that X and y make a*x + b*y= GCD, we have already found the next state: A%b of B and greatest common divisor, and found a set of X1 and Y1 to make: B*x1 + (a%b) *y1 = GC D, is there a relationship between the two neighboring states?
We know: A%b = A-(A/b) *b (the "/" here refers to the division of integers, such as 5/2=2, 1/3=0), then we can get further:
GCD = B*x1 + (A-(A/b) *b) *y1
= b*x1 + a*y1– (A/b) *b*y1
= A*y1 + b* (x1–a/b*y1)
Compare our states before: Ask for a set of X and Y to make: a*x + b*y = gcd, do you find anything?
Over here:
x = y1
y = x1–a/b*y1
The above is the whole process of extending Euclid's algorithm, still using recursion to write:
Still very short, compared to Euclid's algorithm, just add a few more statements.
This is the theoretical part, Euclidean algorithm part we can only be used to solve greatest common divisor, but the expansion of the Euclidean algorithm is different, we can find out greatest common divisor, but also by the way to solve the a*x + b*y = GCD general solution x and Y
What is the use of expanding Euclid?
Solve the general solution of shape such as a*x +b*y = c, but generally no one will be bored to let you write a series of solutions, is to let you choose some special solution in the solution, such as a number for another number of the multiplication of inverse element
What is multiplicative inverse?
Here, we call x the multiplication inverse of a about m
How is that? can be equivalent to an expression such as: A*x + m*y = 1
Do you see what's coming? Yes, when GCD (A, m)! = 1 is not solved this is also a*x + B*y = C has the necessary and sufficient conditions for the solution: C% gcd (A, b) = = 0
Then multiply inverse, generally, we can find countless groups of solutions to meet the conditions, but generally let you solve the smallest group of solutions, how to do? We solved a special solution x0 so, we use x0% m in fact to get the smallest solution. Why?
Can think like this:
is the general solution of X not x0 + m*t?
So, in other words, a about m inverse is a about m congruence, then according to the minimum integer principle, there must be a minimum positive integer, it is a on the inverse of M, and the smallest must be between (0, M), and only one, which is good explanation.
Perhaps someone noticed, here, I write the general solution is not x0 + (M/GCD) *t, but think about it, GCD = 1, so write with not write is the same, but, because of the particularity of the problem, sometimes we get the special solution x0 is a negative, and sometimes our m It's also a negative number.
When M is negative, we take the absolute value of M, and when the x0 is negative, the result of his m is still negative (the result of computer calculation is this, although the definition of this is not the case), at this time, we still let x0 abs (m) to take the mold, and then the result plus ABS (m) on the line , so it is not difficult for us to write the following code to solve a multiplication inverse of a number a for another number m:
Extended Euclidean algorithm