What isGCD?
GCD is the abbreviation of the largest common convention (of course, it is understood that our great party has never tasted it ). At the beginning, let's first define several definitions:
① A | B indicates that a can divide B (A is the approximate number of B)
② A mod B indicates a-[A/B] B ([A/B] is equivalent to a div B in Pascal)
③ Gcd (a, B) indicates the maximum approximate number of A and B
④ Linear combination of A and B indicates AX + by (X, Y is an integer ). We have: if d | A and D | B, d | Ax + by (this is important !)
Linear Combination andGCD
Now we prove an important theorem: gcd (A, B) is the smallest linear combination of A and B.
Proof:
Set gcd (a, B) to d, and the smallest positive linear combination of A and B to S.
Between d | A and D | B,
Required d | S.
And a mod S = a-[A/S] S
= A-[A/S] (AX +)
= A (1-[A/S] X)-B [A/S] y
It is also a linear combination of A and B.
When a mod S <S, A mod s cannot be the smallest linear combination of A and B
Required a mod S = 0, that is, S |
Same reason S | B
∴ S is the common divisor of A and B
∴ S <= d
Mongod | S
Required d = S. Pass.
It is easy to know from this theorem that, if d | A and D | B, d | gcd (A, B)
EuclidAlgorithm
The question is how to quickly calculate GCD (A, B ). It is obviously not a good method (O (n), so a better method is needed.
First, we first propose a theorem: gcd (a, B) = gcd (B, A-bx) (X is a positive integer ).
Proof:
Set gcd (a, B) = D, gcd (B, A-bx) = E, then
Mongod | a, d | B
Mongod | A-bx
Mongod | gcd (B, A-bx), that is, d | E
∵ E | B, E | A-bx
∴ E | bx + (a-bx), that is, E |
∴ E | gcd (a, B), that is, E | D
Required d = E. Pass.
This theorem is very useful because it can quickly reduce the data size.
When x = 1, gcd (a, B) = gcd (B, A-B ). This is the moving and Subtraction Method.
When x reaches the maximum value, that is, when x = [A/B], gcd (a, B) = gcd (B, A mod B ). This is the Euclid algorithm. I don't know if it was proposed by Euclid, but I heard it was formed in the Euclid era, so it is called the Euclid algorithm.ProgramVery simple:
Function
Euclid (A, B: longint): longint;
Begin
If B = 0 Then exit ()
Else exit (Euclid (B, A mod B ));
End;
The Euclid algorithm is better than the moving and subtraction algorithm, which is fast and convenient to use. Both algorithms have an implicit limitation: A> = B. When moving and Subtraction is used, the size must be determined first, but the Euclid algorithm is not. If a <B, a recursion will be converted to gcd (B, A), and then it will run normally.
ExtensionEuclid
As we have mentioned above, gcd (A, B) can be expressed as the smallest linear combination of A and B. Now we need the minimum linear combination of x and y in AX +. This can use our Euclid algorithm.
Start with the simplest scenario. When B = 0, we take x = 1, Y = 0. What if B is equal to 0?
Assume that gcd (a, B) = d, then gcd (B, A mod B) = D. If we have obtained a linear combination of gcd (B, A mod B) to represent the Bx '+ (a mod B) y',
Gcd (a, B) = d
= Bx '+ (a mod B) y'
= Bx '+ (a-[A/B] B) y'
= Ay '+ B (x'-[A/B] y ')
Then, x = y', y = x'-[A/B] Y '. In this way, X and Y can be obtained in the recursive process of Euclid.
Program:
Function gcd (A, B: longint): longint;
VaR P, n, m: longint;
Begin
If B = 0 then
Begin
X: = 1;
Y: = 0;
Exit ();
End
Else
Begin
P: = gcd (B, A mod B );
N: = X;
M: = y;
X: = m;
Y: = n-a div B * m;
Exit (P );
End;
End;
Now we have another question: is X and Y correct? Answer: No. If X and Y meet the requirements, x + BK and Y-ak also meet the requirements. The reason for uncertainty is: "When B = 0, we take x = 1, Y = 0 ." In fact, y can take any positive integer.
Indefinite EquationAx + by = C
Now, the focus of this article is to solve a binary Indefinite Equation. It seems that the expanded Euclid algorithm is a special case of the Indefinite Equation. In fact, the indefinite equation is actually solved by the Euclid algorithm.
For an Indefinite Equation AX + by = C, set gcd (a, B) = D. If AX + by = C has a solution, then d | C (this is also the starting point for many mathematical questions ). Therefore, once D is not the approximate number of C, ax + by = C must have no solutions. When d | C, first obtain the X' and y' of ax '+ by' = d = gcd (a, B), then x = x' * C/D, y = y' * C/D. From the previous section, we can see that as long as AX + by = C has a solution, there will be no number of solutions.
The Euclid algorithm can also be used to solve the same equation ax 127B (mod m ). This is actually no different from the Indefinite Equation AX + my = B. (The Indefinite Equation and the same remainder equation are generally limited in scope, which is also easy to solve, so I won't talk about it)
Others
GCD and related issues are the most fundamental issues in elementary number theory. In fact, deeper elementary number theory also includes:
◆ Chinese Remainder Theorem
◆ Miller-Rabin Testing
◆ Pollard rock Algorithm
Jollwish original, reprinted please describe the source