Euclid, extended Euclid algorithm

 Greatest Common Divisor

 

Euclid's algorithm:

Theorem 1 : Let a, b, c, and q be integers, and b>0. If a = q b+c, then gcd(a, b) = gcd(b, c)

The proof method uses the set method, which means that the divisor of one must be the divisor of the other number, so that the two numbers are equal.

 

Use this theorem to write Euclidean algorithm

(1) Iterative version

(2) Recursive version

 

Obviously, recursive is easier to write than iterative, and it is not easy to make mistakes, but will the efficiency be much worse?

 

Theorem 2 : (Proved by the French mathematician Lame) Euclid's algorithm requires no more than 5 times the decimal digits of the smaller number of m and n

Theorem 3: (slightly weak point)...no more than 2 log(n+1), where n is a smaller number

 

With the above theorem, there are reasons to believe in the advantages of the recursive version, but you must know both when you learn!

 

 

 

 

Extended Euclidean Algorithm (Extended Euclidean Algorithm)

Can be used to find the integer solution problem of a binary linear equation system

ax + by = m, gcd(a, b) = 1 is required, otherwise the two sides are divided by GCD, and the final result is multiplied by GCD

(3) Iteratively extended Euclidean algorithm

 

(4) Recursive extended Euclid algorithm

 

Easy to write and remember recursively.

 

Knowing a solution, all integer solutions of the equation can be expressed

x = x0 + b k

y = y 0 + ak

In this way, if the smallest positive integer solution is required, it can be calculated!

 

Principle: taken from the Internet

Let a1=a/gcd(a,b), b1=b/gcd(a,b), and m1=m/gcd(a,b). If we can first find x1 and y1 that satisfy the equation a*x1+b*y1=gcd(a,b), then x=x1*m1, y=y1*m1 can be found. By Euclid's algorithm gcd(a,b)=gcd(b,a%b), so a*x1+b*y1 =gcd(a,b)=gcd(b,a%b) = b*x2+ (a%b)*y2 , now you can get the formula used in the extended Euclidean algorithm by doing some transformations. Let k=a/b (quotient) and r=a%b (remainder), then a=k*b+r. So r=ak*b, put in the above formula, get a*x1+b*y1 = b*x2+(a-(a/b)*b)y2 = a*y2+b*(x2-(a/b )*y2) => x1=y2, y1=x2-(a/b)*y2 . With these two formulas, we know the changes of the corresponding parameters x and y when using Euclidean to find the greatest common divisor. Now look back and look at the code of the extended Euclidean algorithm to understand it well. In fact, the extended Euclidean is to find the greatest common divisor of a and b while also satisfying the equation a*x1+b *y1=gcd(a,b) A set of x1 and y1 values ​​are calculated. The highlighted part of the code below is the code of the standard Euclidean algorithm.

 

application:

(1) Find the inverse of a prime number

 

 (2) The existence of positive integer solutions of USACO 4.1 nuggets 

ax+by=c, where gcd(a,b)=1, a>0, b>0;  a posivesolution exists if c>=a*b
 PROOF:
from Extended Euclidean, we know that there exists a solution ax0+by0=c
**   we want to find xn, yn, where xn>0, yn>0. xn = x0 + b*t, yn = y0 - a*t;
**   -x0/b <= t <= y0/a。for c >= a*b, we know that x0/b+y0/a>=1, so it exists

 

(3) FZU APRIL arrangement

Note that the intermediate result may exceed INT, so use LONG LONG

Guess you like

Origin blog.csdn.net/zjsxzjb/article/details/6262667