ex_gcd Extended Euclidean

### 要去了解ex_gcd 先来介绍下 贝族定理

当 a,b为整数时一定存在 整数x,y使得 ax+by=gcd(a,b)
In other words, when there is an integer solution, ax+by=m, and m must be an integer multiple of gcd(a,b).
When ax+by=1, gcd(a,b)=1; that is, a and b mutually White

gcd()

int gcd (int a, int b){
    
    
	if(a%b)
		return gcd(b,a%b)
	return  a;

ex_gcd)()

When the recursive boundary of gcd is reached,
then a=gcd(a,b), b=0, there must exist x=1, y=0.
This is reversed by recursion.
We can know


a=b;
 b=  gcd(a,b)
   =a-(a/b)*b
   带入 式子中  
 b*x1+((a-(a/b)*b)*y1
 =a*y1-b*(x1-a/b*y1)
 由此我们知道  
上一级的情况
x=y1;
y=x1-a/b*y1;
int ans;
int ex_gcd(int a, int b, int x ,int y ){
    
    
	if(b==0){
    
    
	x=1,y=0;
	return a; 
	}
	ans=ex_gcd(b,a%b,x,y);
	int tmp =y;//得到上一步的x,y
	y=x-a/b*y;
	x=y;
	return ans;  //返回  gcd();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325804764&siteId=291194637