Euclid & Expand Euclid (Euclid & Extend-euclid)Euclidean algorithm (Euclid)background:
Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two positive integers, a, B. --Baidu Encyclopedia
Code:
The recursive code is fairly concise:
int gcd (int a,int b) {return b = = 0? a:gcd (b, a% b);}
Analysis:
Method said is the division method, naturally there is nothing good to introduce.
Fresh will surely feel like this recursion will not explode stack? In fact, here is not a stack, because the number of recursive layer is very small, do not believe you can take some large number of tests, LRJ Petition and Purple book on the GCD function of the recursive layer of not more than 40785lgN + 1.6723, where n=max{a,b}. Let the GCD function recursively the highest number of layers is gcd (f (n), F (n-1)), f (n) is the number of Fibonacci!! As for why Bo Master did not prove that there are ideas of the small partner trouble in the comments under the next, (*^__^*) Hee ...
Expand Euclid's (extend-euclid) background:
The extended Euclidean algorithm is used to solve a set of x, y [x, y, and all integers] in known A and b to satisfy the Bézout equation: ax+by = gcd (A, B) =d(the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations. --Baidu Encyclopedia
Several Euclid's important conclusions:
1) gcd (A, b) = gcd (B,a%b);
2) gcd (a,0) = A.
Code:
typedef __int64 Ll;void EXGCD (ll A, ll B, ll& D, ll& X, ll &y) { if (!b) { d = A, x = 1, y = 0; } Else { EXGCD (b, a% B, D, y, x); Y-= x * (A/b); }}
Analysis:
Set the following two equations:
Ax+by = GCD (A, b) = D;
BX ' + (a%b) y ' = gcd (b,a%b);
Then by the important conclusion (1) there is gcd (A, b) = gcd (B,a%b),
So ax+by = bx ' + (a%b) y ' = bx ' + (a–[a/b]*b) y ' = Ay ' + b (x ' –[a/b]y '),
The identity relationship is : x = y ', y = (x ' –[a/b]y '), [A/b] indicates that A/b value is rounded down.
........
Then it is now possible to use EXGCD (A,b,d,x,y) to represent the equation Ax+by = d, so that it is recursively down from above until B = 0, recursion ends, at which point d = gcd (a,0) =a, x = 1,y = 0; "Because ax+0*y = gcd (a,0) ~"
Several applications of Euclidean to solve indefinite equations
For example: solving an indefinite integer equation Ax+by = c
Ask Ax+by = c, make D =gcd (A, b);
Then (A/d) * x + (b/d) * y = C/D
Because (A/D), (b/d), x, y are integers, the necessary and sufficient condition to ensure that the original indefinite integer equation ax+by = c has a solution is that C/D is an integer, that is c is a multiple of gcd (A, b) .
If there is a solution, then K = C/D;
So, the equation ax+by = D; If there is an extension Euclidean to find a set of solutions for (X0,Y0), then ax0+by0 = D; The equation is multiplied by K at the same time, i.e. k* (ax0+by0) = D*k = C; By the identity relation, the solution of the original equation (x0,y0):
X0 = KX0 = C/D * X0 , y0 = KY0 = C/D *y0 .
of indefinite equationsGeneral Solution:
if (X0,Y0) is an indefinite integer equation Ax+by = C of a set of solutions, then his arbitrary integer solution can be expressed as (x0+ KB ', Y0-ka '), where a ' = A/GCD (A, A, b), B. ' = B/GCD (A, A, a).
Examples:
POJ 1061 frog Dating Link: http://poj.org/problem?id=1061
Solving the modal linear equation
The method is similar to the above, the same equation is converted into a conventional linear equations can be, as above, when it comes to a solution of the congruence equation, in fact, refers to a congruence equivalence class ....
Specific content to be added ...
The inverse element of the modulus of evaluation
I want to add ...
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Euclid & Expand Euclid Algorithm Explanation (Euclid & extend-euclid algorithm)