One: Euclid algorithm (Euclidean method)
Basic algorithm: Set A=qb+r, where a,b,q,r are integers, then gcd (A, B) =gcd (b,r), gcd (A, B) =gcd (b,a%b).
Prove:
A can be expressed as A = kb + R, then r = a mod b
Assuming D is a number of conventions for a, B, there are
D|a, d|b, and r = a-kb, so d|r
So d is the number of conventions (B,a mod b)
Assuming D is the number of conventions (B,a mod b), then
D | B, D |r, but a = KB +r
So d is also the number of conventions (A, B)
therefore (b) and (b,a mod b) The number of conventions is the same, and their greatest common divisor are necessarily equal, to be certified
Algorithm implementation:
- int gcd (int A,int b) { //recursive algorithm
- return b gcd (b, a%b): A;
- }
- int GCD (INTA, int b) { //iterative algorithm
- While (b! = 0) {
- int r = b;
- b = a%b;
- A = R;
- }
- return A;
- }
Two extended Euclidean algorithms:
Basic algorithm: For a non-negative integer A,B,GCD (A, B) that is not exactly 0, the greatest common divisor of A/b is bound to have an integer pair of x, Y, which makes gcd (A, b) =ax+by.
Proof: Set A>b.
1, obviously when B=0,GCD (A, b) =a. At this time x=1,y=0;
When 2,ab!=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;
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 the recursive solution of GCD will always have a time b=0, so recursion can end.
- Recursive code
- NT EXGCD (int A, int B, int&x, int&y) {
- if (b = = 0) {
- x = 1;
- y = 0;
- return A;
- }
- int r = EXGCD (b, A%b, y, x);
- int t = x;
- y = y-a/b*t;
- return R;
- //non-recursive algorithm
- NT EXGCD (int m, int n, int &x, int &y) {
- int x1, y1, x0, y0;
- x0 = 1; y0 = 0;
- x1 = 0; y1 = 1;
- x = 0; y = 1;
- int r = m%n;
- int q = (m-r)/n;
- While (r) {
- x = x0-q*x1;
- y = y0-q*y1;
- x0 = x1; y0 = y1;
- x1 = x; y1 = y;
- m = n; n = r; R = m%n;
- Q = (m-r)/n;
- }
- return n;
Rujia's:
- void gcd (int A, int b, int& D, int& x, int& y) {
- if (!b) {d = A; x = 1; y = 0;}
- else {gcd (b, a%b, D, y, x); y-= x* (A/b);}
- }
The x found above (when a>b) is the nearest positive integer that is closest to 0. (Don't know why)
The application of the extended Euclidean algorithm mainly has the following two aspects:
(1) solving indefinite equation;
(2) solving the modal linear equation (linear with congruence) and inverse element;
(1) solving indefinite equation;
1. For indefinite integer equation ax+by=m, if M mod Gcd (A, B) = 0, then the equation has an integer solution, otherwise there is no integer solution.
Prove:
首先扩展欧几里德主要是用来与求解线性方程相关的问题,所以我们从一个线性方程开始分析。现在假设这个线性方程为a*x+b*y=m,如果这个线性方程有解,那么一定有gcd(a,b) | m,即a,b的最大公约数能够整除m(m%gcd(a,b)==0)。证明很简单,由于a%gcd(a,b)==b%gcd(a,b)==0,所以a*x+b*y肯定能够整除gcd(a,b),如果线性方程成立,那么就可以用m代替a*x+b*y,从而得到上面的结论,利用上面的结论就可以用来判断一个线性方程是否有解。
2.AX+BY=GCD (A, b) multiply both sides by M/GCD (A, B) a (X*C/GCD (A, B)) +b (Y*C/GCD (A, b)) =m;
The above has listed a method for finding an integer solution, after finding a group of solutions for a*x+ b*y = GCD (A, B) x0,y0, a set of solutions for the indefinite equation ax+by=m satisfies x = m (x0)/gcd, y = m* (y0)/gcd;
So the general solution is x = m* (x0)/gcd + k*lcm/a, y = m* (y0)/GCD + k*lcm/b (where k is any integer);
Prove:
Make A1=A/GCD (A, B), B1=B/GCD (A, B), M1=M/GCD (A, B).那么x,y的一组解就是x0*m1,y0*m1,但是由于满足方程的解无穷多个,在实际的解题中一般都会去求解x或是y的最小正数的值。以求x为例,又该如何求解呢?还是从方程入手,现在的x,y已经满足a*x+b*y=m,那么a*(x+n*b)+b*(y-n*a)=m显然也是成立的。可以得出x+n*b(n=…,-2,-1,0,1,2,…)就是方程的所有x解的集合,由于每一个x都肯定有一个y和其对应,所以在求解x的时候可以不考虑y的取值。取k使得x+k*b>0,x的最小正数值就应该是(x+k*b)%b,但是这个值真的是最小的吗??如果我们将方程最有两边同时除以gcd(a,b),则方程变为a1*x+b1*y=m1,同上面的分析可知,此时的最小值应该为(x+k*b1)%b1,由于b1<=b,所以这个值一定会小于等于之前的值。在实际的求解过程中一般都是用while(x<0)x+=b1来使得为正的条件满足,为了更快的退出循环,可以将b1改为b(b是b1的倍数),并将b乘以一个倍数后再加到x上。
代码:
- //Indefinite equations
- void Linear_equation (int A, int b, int c, int &x, int &y) {
- int d = EXGCD (A, B, X, y);
- if (c%d)
- return;
- int k = C/D;
- x *= k; Y *= K; //A group of solutions
- return;
- }
Related topics: poj2115 poj2142 poj1061.
(2) solving the modal linear equation (linear with congruence) and inverse element;
The congruence equation Ax≡b (mod n) has a solution for unknown x, when and only if GCD (a,n) | B. And when the equation has a solution, the equation has a gcd (a,n) solution.
The solution equation Ax≡b (mod n) is equivalent to the solution Equation ax+ (-ny) = B, (x, y is an integer).
There are two theorems in the introduction of algorithms:
Theorem One: Set d = gcd (A, N), assumed to Integer x ', y ', has d = Ax ' + NY ', if D | b, then the equation ax = b (mod n) has a solution with a value of x0, satisfying:,
x0 = X ' (b/d) (mod n)
Theorem two: Assuming that the equation ax = b (mod n) has a solution, x0 is any solution to the equation, then the equation has a D different solution to modulo n, respectively: Xi = x0 + i * (n/d), where i = 1,2,3......d-1
With these two theorems, it is not difficult to solve the equation.
Code:
- //modulo linear equations
- BOOL Modular_linear_equation (int A, int b, int n) {
- int x, y, x0, I;
- int d = EXGCD (A, n, x, y);
- if (b%d)
- return false;
- x0 = x* (b/d)%n; //Special Solution
- For (i = 0; i < D; i++)
- printf ("%d\n", (x0 + i* (n/d))%n);
- return true;
- }
Related Topics hdu1576.
The congruence equation Ax≡b (mod n), if gcd (a,n) = = 1, the equation has only the unique solution.
In this case, if b== 1, the congruence equation is ax=1 (mod n), gcd (a,n) = 1.
At this point the X is calculated as the inverse of the modulo n multiplication of a.
For the congruence Equation ax= 1 (mod n), the solution of gcd (a,n) = 1 is to solve the equation
ax+ ny= 1,x, y is an integer. This can be extended Euclidean algorithm to find out that the only solution to the original congruence equation is to expand the Euclidean algorithm to derive the X.
Code:
- LL INV (ll A, ll N) {
- ll d, X, y;
- D = EXGCD (A, n, x, y);
- return (d = = 1)? (x%n + N)%n:-1;
- }
Related topics: hdu2115.
Extended Euclidean algorithm detailed