Expand Euclid's theorem

reference 

Bezu Theorem: If ab is an integer, then there must be an integer such that xy ax + by = gcd (a, b)

                  That is, if ax + by = c solvable, then c must be a multiple of gcd (c necessarily divisible by gcd, c% gcd == 0)

If ax + by = 1 has a solution, then the gcd (a, b) = 1.

 

Euclid was removed divided by seeking the greatest common divisor ab

gcd(a,b)=gcd(b,a%b)

int gcd(int a,int b){

    if(b==0) return a;
    
    return gcd(b,a%b);
}

 ax + by = gcd (a, b) solvable 

Recursive boundary when b = 0, a = gcd function returns a i.e. a * 1 (1,0) when the set of solutions for boundary reaches recursive + b * 0 = gcd

Recursive i.e. gcd not change with the number of layers varies recursive gcd (a, b) = gcd (b, a% b) in a recursive process

              ax1+by1=gcd=bx2+(a%b)y2

              Simultaneous (a / b) * b + a% b = a to give 

               x1 = y2, y1 = x2- (a / b) y2 at this time to establish a relationship between this layer and the next layer of recursion, x1 y1 is this layer solution, x2, y2 are the next layer solution

// ax+by=gcd
int exgcd(int a,int b,int &x,int &y){
	
	if(b==0){
		x=1,y=0;
		return a;
	}
	
	int g=exgcd(b,a%b,x,y);
	
	int x2=x,y2=y;
	x=y2;
	y=x2-(a/b)*y2;
	
	return g;
}

  Can be expanded Euclidean algorithm ax + by a set of solutions (x0, y0) by = gcd may be negative. So how general solution is obtained by special solution

  Suppose x0 + s1, y0-s2 another set of equations for the solution of the original, the simultaneous ax0 + by0 = gcd have a * s1 = b * s2

   s1 / s2 = b / a by b / gcd with a / gcd prime order s1 and s2 takes a minimum value, s1 = b / gcd, s2 = a / gcd

  Solution is obtained through equation x = x0 + b / gcd * k y = y0-a / gcd * k k is an integer

  x is the smallest non-negative integer solution (x% (b / gcd) + b / gcd) / (b / gcd)

 If gcd = 1 ab prime i.e., the formula can be simplified

  

   

 

 

 

Published 138 original articles · won praise 18 · views 7034

Guess you like

Origin blog.csdn.net/qq_924485343/article/details/104664036