Proof: Set A>b.
Reasoning 1, obviously when B=0,GCD (A, b) =a. At this point x=1,y=0;//reasoning 1
When reasoning 2,a*b!=0
Set AX1+BY1=GCD (A, b);
bx2+ (a mod b) y2=gcd (b,a mod b);
According to the naïve Euclid principle there is gcd (A, B) =gcd (b,a MoD);
Then: ax1+by1=bx2+ (a mod b) y2;
namely: ax1+by1=bx2+ (A-(A/b) *b) y2=ay2+bx2-(A/b) *by2;
According to the identity theorem: x1=y2; Y1=x2-(A/b) *y2;//reasoning 2
This gives us a way to solve x1,y1: The value of X1,y1 is based on X2,y2.
The idea above is defined recursively, because GCD's constant recursive solution is bound to have a time b=0,
So recursion can end.
The general solution and special solutions of the extended Euclidean algorithm:
For AX+BY=GCD (A, B) it is easy to find a set of special solutions by EXGCD () x0,y0
Then the form of its general solution can be deduced as follows: Set its other group of solutions to X1,y1
Then there is the a*x1+b*y1=a*x0+b*y0, then you can get: A (x1-x0) =b (y0-y1)
On both sides with the exception of GCD (A, B) a/gcd (A, B) =a1,b/gcd (A, B) =b1, a1* (x1-x0) =b1* (y0-y1);
And A1,B1 must have x1-x0 =k*b1, bring into the a1*k=y0-y1,y1=y0-a1*k;
Similarly, the (y0-y1) =K*A1 can be brought into the X1-X0=K*B1,X1=X0+K*B1;
Then for the more general equation Ax+by=c, if C%GCD (A, B)!=0 is not solved, otherwise
The general solution is x=x0*k+t*b1;y=y0*k-t*a1 (T is any integer, the derivation process is the same as that of the upper general solution), paying special attention to:
The general solution of the formula is not equal to the enlargement of the general general solution K Times!!!!!!!!
Then for many problems, it is not the first set of solutions, but the solution to meet certain requirements
At this point, X, y should be treated as a function of k and then solved accordingly (for example, the smallest integer solution of x can be obtained
Just by making the x=0, and then finding the corresponding T and returning the tape.
int exgcd (int a,int b,int& x,int &y)//extended Euclidean algorithm, recursive implementation
{
if (b==0)//When the b=0 is ended recursively, a set of solutions is obtained
{
X=1;
y=0;
return A; Returns a greatest common divisor of a, b
}
int R=EXGCD (b,a%b,x,y); Recursive solution
int t=x; After the solution is over, the child problem gets the solution of the parent problem
X=y;
Y=t-(A/b) *y;
return R;
}
void exgcd1 (int a,int b,int &d,int &x,int &y)
{
if (b==0)
{
X=1;
y=0;
D=a; D record greatest common divisor of a, b
}
Else
{
EXGCD1 (B,A%B,D,Y,X);
Y=y-x* (A/b);
}
}
#include<iostream>int main(){ int x,y,d; cout<<exgcd(15,5,x,y)<<endl; cout<<"15x+5y=5的一组整数解为:"<<endl<<"x=:"<<x<<" y=:" <<y<<endl; getchar();getchar(); return0;}
Extended Euclidean algorithm