On the extended Euclidean algorithm (Extended Euclidean algorithm), I was working on the classic topic of frog dating to get access to this algorithm. There is also an AC code and a solution to the problem.
Content: Known A, B, solves a set of X, Y, and satisfies the Bézout equation: Ax+by =gcd (A, B)
Extending Euclid's algorithm, like its name, is an extension of Euclid's algorithm. What is an extension? First, the algorithm retains the essence of Euclid's algorithm, and can seek the greatest common divisor of a and B. Second, a set of solutions (x, y) of the Ax+by =gcd (A, b) of the two-yuan equation is known as a, B.
Prove:
Suppose A>b,
(1) b=0 gcd (A, b) = A, Ax = a, then x=1,y=0; (Here I recommend not to GCD (a,0) as greatest common divisor, but a computer-calculated value)
(2) Suppose AX1+BY1=GCD (A, B) (equation i) bx2+ (a%b) Y2=GCD (b,a%b) (equation two), obtained by Euclidean algorithm gcd (A, B) =gcd (b,a%b),
Ax1+by1 = bx2+ (a%b) y2, i.e. ax1+by1=bx2+ (a-a/b*b) y2 ax1+by1=ay2+b (x2-a/b*y2)
In accordance with the polynomial identity theorem (A, b as a variable), x1=y2; Y1=x2-a/b*y2;
(On the surface, it is a set of solutions known to the equation, we can get a set of solutions to the equation two, a set of solutions to the two known equations, we can get a set of solutions to the equation one, but the reality is that it is impossible to first know the solution of Fang (x1,y1). The above thought is recursive definition, the continuous use of gcd (A, B) =gcd (b,a%b), to b=0 (the coefficient of y is 0), by the solution of (1), according to the relationship between the solution, finally can get the solution of the equation Ax+by =gcd (A, c).
Recursive form code:
1#include <iostream>2 using namespacestd;3 4 intEXGCD (intAintBint&x,int&y)5 {6 if(b==0)7 {8x=1;9y=0;Ten returnA; One } A intGCD=EXGCD (b,a%b,x,y); - intX2=x,y2=y; -x=Y2; theY=x2-(A/b) *Y2; - returngcd; - } - + intMain () - { + intx,y,a,b; Acout<<"Please enter a and B:"<<Endl; atCin>>a>>b; -cout<<"greatest common divisor of A and B:"<<Endl; -COUT<<EXGCD (a,b,x,y) <<Endl; -cout<<"a set of solutions for AX+BY=GCD (A, B) is:"<<Endl; -cout<<x<<" "<<y<<Endl; - return 0; in}
Non-recursive form codes:
1#include <iostream>2 using namespacestd;3 4 intEXGCD (intAintBint&x,int&y)5 {6 intx1,y1,x0,y0;7x0=1; y0=0;8x1=0; y1=1;9x=0; y=1;Ten intr=a%b; One intq= (A-R)/b; A while(R) - { -x=x0-q*x1; y=y0-q*Y1; thex0=x1; y0=Y1; -X1=x; y1=y; -A=b; B=r; r=a%b; -q= (A-R)/b; + } - returnb; + } A at intMain () - { - intx,y,a,b; -cout<<"Please enter a and B:"<<Endl; -Cin>>a>>b; -cout<<"greatest common divisor of A and B:"<<Endl; inCOUT<<EXGCD (a,b,x,y) <<Endl; -cout<<"a set of solutions for AX+BY=GCD (A, B) is:"<<Endl; tocout<<x<<" "<<y<<Endl; + return 0; -}
Run:
There are also two points to note:
1. The extended Euclidean algorithm is an extension of Euclid's algorithm, which can be gcd (A, B), and many people are unaware of it.
2.x,y can use global variables, and parameter passing is not a reference.
Euclidean algorithm learning algorithm and extended Euclidean algorithm (ii)