First, expand Euclid theorem: For two not all 0 integers a, b, there must be a set of solutions X, Y, making ax+by==gcd (A, b);
intgcdintAintb) { intt,d; if(b==0) {x=1; Y=0;//Unknown Place 1returnA; } d=GCD (b,a%b); T=x; X=y; Y=t-(A/b) *y;//Unknown Place 2returnD;}
X y is represented by a global variable
Unknown Place 1: By the extended Euclidean theorem: Ax+by==gcd (A, B)---1, and at this time b==0, that is gcd (a,0) ==a. The original becomes Ax+by==a--and x==1,y==0. It should be clear enough.
Unclear 2: Here are some of my rules, X, Y represents the value at the first recursion, and x1,y1 represents the value at the second recursion. So
GCD (A, B) ==gcd (b,a%b), both substituting 1, has ax+by==b*x1+ (a%b) *y1. Deform the right.
b*x1+ (a%b) *y1==b*x1+ (A-(A/b) *b) *y1==a*y1+b* (x1-(A/b) *y1), eventually ax+by==a*y1+b* (x1-(A/b) *y1)
That is, the last depth of x equals the y1 of the next depth, and y of the previous depth is equal to the x1-(A/b) *y1 of the next depth. It is important to note that the division used in the deduction above is integer division
So what should be the solution to the general infinitive ax+by==c? Very simple, x1=x* (C/GCD (A, B)), y1=y* (C/GCD (A, b)).
The extended Euclidean (extended-euclid) hypothesis: For the given integers a and b, they satisfy the equation: AX+BY=D=GCD (A, A,), the integer coefficients x, y, and the inference:
AX+BY=GCD (A, B) =gcd (b,a%b) =bx+ (A-(int) a/b*b) y=ay+b (x (A-(int) a/b*y)
Three, extended Euclidean algorithm:
1 int extended_gcd (int a, int b, int &x, int &y)
2 {
3 int ret, TMP;
4 if (!b) {
5 x = 1; y = 0; return A;
6}
7 ret = EXTENDED_GCD (b, a% B, x, y);
8 tmp = x;
9 x = y;
Ten y = tmp-a/b * y;
return ret;
Iv. application: 1, solving the mode linear equation: ax≡b (mod n) theorem one: Set D=GCD (A,n), with extended Euclidean algorithm to solve the linear equation ax ' +ny ' =d. If d|b, then equation axºb (mod n) has a solution value x0=x ' (b/d) mod n Theorem Two: Equation axºb (mod n) has a solution (that is, there is d|b, wherein D=GCD (a,n)), x0 is any solution of the equation, the equation for modulo n has a D different solution, respectively X (i) =x (0) +i (n/d) (I=1 , 2,... d-1)//with extended Euclidean solution equation Ax=b (mod n)
BOOL Modularlinearequation (int a,int b,int N)
{
int x,y,x0,i;
int D=extended_euclid (a,n,x,y); Ax=b (mod n) is equivalent to Ax+ny=b
return false;
x0=x* (b/d)%n;
for (i=1;i<=d;i++)
printf ("%d\n", (x0+i* (n/d))%n);
return true;
}
2, the multiplication inverse: ax≡1 (mod n) ax≡1 (mod n) is equivalent to AX+NY=1=GCD (a,n), call EXTENDED_GCD (A,n,x,y), and when the number of conventions ret=1 (A,n mutual Prime, ret must be equal to 1), When x>0, X is the multiplicative inverse of a. When x<0, the X is converted to the smallest positive integer of mod n: while (x<0) x+=n; Note: There are no pointers in Java, which cannot be called in C + +, and C/C + + can use pointers or references to outgoing parameter x, which allows C + + functions to return multiple values, while Java methods can return at most one value. So in the Java version of the implementation, you can define the x here as a class variable (static), so that you can get the value of X.
Extended Euclidean algorithm