About Euclid and extended Euclidean algorithm is attached hereto site when I taught the use of: Thanks: http: //www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html
Here I will combine the results of the large cattle and summarize their findings:
Euclidean algorithm:
Euclidean algorithm known as Euclidean algorithm for calculating the two integers a, b greatest common divisor.
The basic algorithm: Let a = qb + r, where a, b, q, r are integers, gcd (a, b) = gcd (b, r), i.e. gcd (a, b) = gcd (b, a % b).
prove:
a can be expressed as a = kb + r, then r = a mod b
Suppose d is a common divisor of a, b, and there
d | a, d | b, and r = a - kb, so d | r
D is thus (b, a mod b) of the divisor
D is assumed that (b, a mod b) of the divisor, the
d | b, d | r, but a = kb + r (Note: | is divisible symbol)
D is thus (a, b) of the divisor
Therefore (a, b), and (b, a mod b) the divisor is the same, which is equal to the greatest common divisor is also inevitable, is proved.
Algorithm (the most simple):
1 int gcd(int a,int b) 2 { 3 return b ? gcd(b,a%b) : a; 4 }
Application: seeking n, m t least common multiple
t=n*m/gcd(n,m);
optimization:
t=n/gcd(n,m)*m;
* Extended Euclidean algorithm:
The basic algorithm: For incomplete nonnegative integer 0 a, b, GCD (a, b) represents a, b greatest common divisor, there must be an integer for x, y, such that gcd (a, b) = ax + by .
Proof: setting a> b.
1, obviously when b = 0, gcd (a, b) = a. At this time, x = 1, y = 0;
2, ab! = 0 Shi
Provided ax1 + by1 = gcd (a, b);
bx2+(a mod b)y2=gcd(b,a mod b);
There gcd (a, b) = gcd (b, a mod b) according to the principle of simple Euclidean;
Then: ax1 + by1 = bx2 + (a mod b) y2;
即:ax1+by1=bx2+(a-(a/b)*b)y2=ay2+bx2-(a/b)*by2;
The identity theorem: x1 = y2; y1 = x2- (a / b) * y2;
So we got to solve x1, y1 method: x1, y1 value based x2, y2.
The above idea is recursively defined, because gcd constantly recursive solution there will be a time when b = 0, so the recursion can end.
Algorithm CODE:
Non-recursive:
1 __int64 exgcd(__int64 m,__int64 &x,__int64 n,__int64 &y) 2 { 3 __int64 x1,y1,x0,y0; 4 x0=1; 5 y0=0; 6 x1=0; 7 y1=1; 8 __int64 r=(m%n+n)%n; 9 __int64 q=(m-r)/n; 10 x=0; 11 y=1; 12 while(r) 13 { 14 x=x0-q*x1; 15 y=y0-q*y1; 16 x0=x1; 17 y0=y1; 18 x1=x; 19 y1=y; 20 m=n; 21 n=r; 22 r=m%n; 23 q=(m-r)/n; 24 } 25 return n; 26 }
Recursive CODE:
1 int exgcd(int a,int b,int &x,int &y) 2 { 3 if(b==0) 4 { 5 x=1; 6 y=0; 7 return a; 8 } 9 int r=exgcd(b,a%b,x,y); 10 int t=x; 11 x=y; 12 y=t-a/b*y; 13 return r; 14 }
Inference from the extended Euclidean: If gcd (a, b) = 1, said a, b relatively prime.
Lame theorem: when calculating the greatest common divisor of two positive integers with the Euclidean algorithm, the number of division required no more than two to five times smaller multiple of the decimal number integer.
Extended Euclidean algorithm mainly in the following three aspects:
This part of their grasp is not very good, directly attached to the Great God, Karma Ming ~ ~
(1) to solve the indeterminate equation;
(2) solving the linear equations mode (linear congruence equation);
(3) calculating the inverse of the mold element;
(1) using the extended Euclidean algorithm solutions indeterminate equation:
Indefinite equation for integer pa + qb = c, if c mod Gcd (p, q) = 0, the equation solution integers, integers or solution exists.
The above method to find an integer solution already listed, after finding p * a + q * b = Gcd (p, q) is a set of solutions p0, q0, p * a + q * b = Gcd (p, q) other integer solution satisfies:
P = P0 + B / Gcd (P, Q) * t
Q = Q0 - a / Gcd (P, Q) * t (wherein t is an arbitrary integer)
as integer pa + qb = c solution , just each solution p * a + q * b = Gcd (p , q) is multiplied by c / Gcd (p, q) can.
After finding p * a + q * b = Gcd (a, b) a set of solutions p0, q0, should obtain p * a q * b set of solutions + = c is p1 = p0 * (c / Gcd ( a, b)), q1 = q0 * (c / Gcd (a, b)),
other integer p * a + q * b = c satisfies Solution:
. 1 BOOL linear_equation (int A, int B, int C, int & X, int & Y) 2 { . 3 int D = exgcd (A, B, X, Y); . 4 IF (C% D) . 5 return to false; . 6 int K C = / D; . 7 = K * X; Y = K *; // just obtained a set of solutions . 8 return to true; . 9}
(2) Extension Method using Euclidean algorithm die linear equations:
Congruence equation ax≡b (mod n) for the unknowns x solvable, if and only if gcd (a, n) | b. And when the equation has a solution, there equation gcd (a, n) th solution.
Solving equation ax≡b (mod n) is equivalent to solving equation ax + ny = b, (x, y being an integer)
Setting d = gcd (a, n), if the integer x and y, satisfies d = ax + ny (derived using the extended Euclidean). If d | b, then the equation
a * x0 + n * y0 = d, multiplying both sides of equation b / d, (because d | b, can be divisible), to give a * x0 * b / d + n * y0 * b / d = b.
Therefore, x = x0 * b / d, y = y0 * b / d to a solution ax + b, ny =, so that x = x0 * b / d b (mod n) solution of ax =.
A solution ax≡b (mod n) for x0 = x * (b / d) mod n, and d are a solution to the equation xi = (x0 + i * (n / d)) mod n {i = 0. .. d-1}.
Provided ans = x * (b / d), s = n / d;
Equation smallest integer ax≡b (mod n) solution of: (ans% s + s)% s;
Relevant evidence:
Proof equation has a solution are: x0 = x '(b / d) mod n;
the A * X0 = A * X' (B / D) (MOD n-)
A * X0 = D (B / D) (MOD n-) (Since the AX '= D (n-MOD))
= B (n-MOD)
Proof equation has a d of Solutions: xi = x0 + i * ( n / d) (mod n);
the A * XI (MOD n-) = A * (X0 + I * (n-/ d)) (MOD n-)
= (X0 + A * A * I * (n-/ D)) (n-MOD)
= A * X0 (n-MOD) (since D | A)
= B
First look at a simple example:
5x=4(mod3)
Solve for x = 2,5,8,11,14 .......
Whereby a rule can be found, the solution interval is 3.
So the solution of this interval is determined by how it?
If the first try to find a solution, and the spacing between the obtained solution, then the solution set of linear equations can be determined mode.
We set the interval between the solution to dx.
Then there
a*x = b(mod n);
a*(x+dx) = b(mod n);
Two subtraction device, to give:
a*dx(mod n)= 0;
That is a * dx is a multiple, but also a multiple of n, i.e., a * and dx is a common multiple of n. In order to determine dx, we should obtain the least common multiple of n and a, at this time dx is the corresponding the smallest.
Let a greatest common divisor of n and d, then the least common multiple of a and n is (a * n) / d.
Immediately a * dx = a * n / d;
Therefore dx = n / d.
Thus the spacing between the solutions to find out.
code show as below:
1 bool modular_linear_equation(int a,int b,int n) 2 { 3 int x,y,x0,i; 4 int d=exgcd(a,n,x,y); 5 if(b%d) 6 return false; 7 x0=x*(b/d)%n; //特解 8 for(i=1;i<d;i++) 9 printf("%d\n",(x0+i*(n/d))%n); 10 return true; 11 }
(3) with the Euclidean algorithm inverse modulo:
Congruence equation ax≡b (mod n), if gcd (a, n) == 1, then the equation has only one solution.
In this case, if b == 1, congruence equation is ax = 1 (mod n), gcd (a, n) = 1.
In this case said determined modulo n x is a multiplication inverse element of a pair.
For congruence equation ax = 1 (mod n), gcd (a, n) = 1 is solved to solve the equation
ax + ny = 1, x, y are integers. This can be used to obtain the extended Euclidean algorithm, the unique solution of the original equation is congruent with the extended Euclidean algorithm derived from x.
Report problem solving:
Applications on Extended Euclid's most typical is POJ 1061: frog dating
Meaning of the questions we should all understand, analyze it directly:
Two frogs jump t step, coordinate A as a coordinate x + mt, B is y + nt Sufficient Conditions they met:. X + mt-y-nt = pL, i.e. (nm) t + Lp = xy , L> 0.
Provided nm = A, xy = B, find the smallest satisfies At + lp = t B, i.e. seek congruence equation At B (mod l) = the smallest integer solution, particularly solving the three-step process:
(1) Write the equation (nm) t + Lp = xy, by solving the extended Euclidean function, then X 'is a solution, but not a final solution.
(2) if (xy)% gcd (nm, L) == 0, then the solvability.
(3) After solvable: Let M = gcd (nm, L), X = X (xy) / M
Then, (X% (L / M) L / M +)% (L / M) is the final solution.
Code:
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 __int64 exgcd(__int64 m,__int64 &x,__int64 n,__int64 &y) 5 { 6 __int64 x1,y1,x0,y0; 7 x0=1; 8 y0=0; 9 x1=0; 10 y1=1; 11 __int64 r=(m%n+n)%n; 12 __int64 q=(m-r)/n; 13 x=0; 14 y=1; 15 while(r) 16 { 17 x=x0-q*x1; 18 y=y0-q*y1; 19 x0=x1; 20 y0=y1; 21 x1=x; 22 y1=y; 23 m=n; 24 n=r; 25 r=m%n; 26 q=(m-r)/n; 27 } 28 return n; 29 } 30 int main() 31 { 32 __int64 x,y,n,m,l,a1,b1; 33 while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&n,&m,&l)!=EOF) 34 {__int64 s=exgcd(m-n,a1,l,b1); 35 if((x-y)%s||(m==n)) 36 printf("Impossible\n"); 37 else 38 { 39 __int64 S=l/s; 40 a1=a1*((x-y)/s); 41 a1=(a1%S+S)%S; 42 printf("%I64d\n",a1); 43 }} 44 return 0; 45 }
All right. Bye ~ Karma woo. . . .
About Euclid and extended Euclidean algorithm is attached hereto site when I taught the use of: Thanks: http: //www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html
Here I will combine the results of the large cattle and summarize their findings:
Euclidean algorithm:
Euclidean algorithm known as Euclidean algorithm for calculating the two integers a, b greatest common divisor.
The basic algorithm: Let a = qb + r, where a, b, q, r are integers, gcd (a, b) = gcd (b, r), i.e. gcd (a, b) = gcd (b, a % b).
prove:
a can be expressed as a = kb + r, then r = a mod b
Suppose d is a common divisor of a, b, and there
d | a, d | b, and r = a - kb, so d | r
D is thus (b, a mod b) of the divisor
D is assumed that (b, a mod b) of the divisor, the
d | b, d | r, but a = kb + r (Note: | is divisible symbol)
D is thus (a, b) of the divisor
Therefore (a, b), and (b, a mod b) the divisor is the same, which is equal to the greatest common divisor is also inevitable, is proved.
Algorithm (the most simple):
1 int gcd(int a,int b) 2 { 3 return b ? gcd(b,a%b) : a; 4 }
Application: seeking n, m t least common multiple
t=n*m/gcd(n,m);
optimization:
t=n/gcd(n,m)*m;
* Extended Euclidean algorithm:
The basic algorithm: For incomplete nonnegative integer 0 a, b, GCD (a, b) represents a, b greatest common divisor, there must be an integer for x, y, such that gcd (a, b) = ax + by .
Proof: setting a> b.
1, obviously when b = 0, gcd (a, b) = a. At this time, x = 1, y = 0;
2, ab! = 0 Shi
Provided ax1 + by1 = gcd (a, b);
bx2+(a mod b)y2=gcd(b,a mod b);
There gcd (a, b) = gcd (b, a mod b) according to the principle of simple Euclidean;
Then: ax1 + by1 = bx2 + (a mod b) y2;
即:ax1+by1=bx2+(a-(a/b)*b)y2=ay2+bx2-(a/b)*by2;
The identity theorem: x1 = y2; y1 = x2- (a / b) * y2;
So we got to solve x1, y1 method: x1, y1 value based x2, y2.
The above idea is recursively defined, because gcd constantly recursive solution there will be a time when b = 0, so the recursion can end.
Algorithm CODE:
Non-recursive:
1 __int64 exgcd(__int64 m,__int64 &x,__int64 n,__int64 &y) 2 { 3 __int64 x1,y1,x0,y0; 4 x0=1; 5 y0=0; 6 x1=0; 7 y1=1; 8 __int64 r=(m%n+n)%n; 9 __int64 q=(m-r)/n; 10 x=0; 11 y=1; 12 while(r) 13 { 14 x=x0-q*x1; 15 y=y0-q*y1; 16 x0=x1; 17 y0=y1; 18 x1=x; 19 y1=y; 20 m=n; 21 n=r; 22 r=m%n; 23 q=(m-r)/n; 24 } 25 return n; 26 }
Recursive CODE:
1 int exgcd(int a,int b,int &x,int &y) 2 { 3 if(b==0) 4 { 5 x=1; 6 y=0; 7 return a; 8 } 9 int r=exgcd(b,a%b,x,y); 10 int t=x; 11 x=y; 12 y=t-a/b*y; 13 return r; 14 }
Inference from the extended Euclidean: If gcd (a, b) = 1, said a, b relatively prime.
Lame theorem: when calculating the greatest common divisor of two positive integers with the Euclidean algorithm, the number of division required no more than two to five times smaller multiple of the decimal number integer.
Extended Euclidean algorithm mainly in the following three aspects:
This part of their grasp is not very good, directly attached to the Great God, Karma Ming ~ ~
(1) to solve the indeterminate equation;
(2) solving the linear equations mode (linear congruence equation);
(3) calculating the inverse of the mold element;
(1) using the extended Euclidean algorithm solutions indeterminate equation:
Indefinite equation for integer pa + qb = c, if c mod Gcd (p, q) = 0, the equation solution integers, integers or solution exists.
The above method to find an integer solution already listed, after finding p * a + q * b = Gcd (p, q) is a set of solutions p0, q0, p * a + q * b = Gcd (p, q) other integer solution satisfies:
P = P0 + B / Gcd (P, Q) * t
Q = Q0 - a / Gcd (P, Q) * t (wherein t is an arbitrary integer)
as integer pa + qb = c solution , just each solution p * a + q * b = Gcd (p , q) is multiplied by c / Gcd (p, q) can.
After finding p * a + q * b = Gcd (a, b) a set of solutions p0, q0, should obtain p * a q * b set of solutions + = c is p1 = p0 * (c / Gcd ( a, b)), q1 = q0 * (c / Gcd (a, b)),
other integer p * a + q * b = c satisfies Solution:
. 1 BOOL linear_equation (int A, int B, int C, int & X, int & Y) 2 { . 3 int D = exgcd (A, B, X, Y); . 4 IF (C% D) . 5 return to false; . 6 int K C = / D; . 7 = K * X; Y = K *; // just obtained a set of solutions . 8 return to true; . 9}
(2) Extension Method using Euclidean algorithm die linear equations:
Congruence equation ax≡b (mod n) for the unknowns x solvable, if and only if gcd (a, n) | b. And when the equation has a solution, there equation gcd (a, n) th solution.
Solving equation ax≡b (mod n) is equivalent to solving equation ax + ny = b, (x, y being an integer)
Setting d = gcd (a, n), if the integer x and y, satisfies d = ax + ny (derived using the extended Euclidean). If d | b, then the equation
a * x0 + n * y0 = d, multiplying both sides of equation b / d, (because d | b, can be divisible), to give a * x0 * b / d + n * y0 * b / d = b.
Therefore, x = x0 * b / d, y = y0 * b / d to a solution ax + b, ny =, so that x = x0 * b / d b (mod n) solution of ax =.
A solution ax≡b (mod n) for x0 = x * (b / d) mod n, and d are a solution to the equation xi = (x0 + i * (n / d)) mod n {i = 0. .. d-1}.
Provided ans = x * (b / d), s = n / d;
Equation smallest integer ax≡b (mod n) solution of: (ans% s + s)% s;
Relevant evidence:
Proof equation has a solution are: x0 = x '(b / d) mod n;
the A * X0 = A * X' (B / D) (MOD n-)
A * X0 = D (B / D) (MOD n-) (Since the AX '= D (n-MOD))
= B (n-MOD)
Proof equation has a d of Solutions: xi = x0 + i * ( n / d) (mod n);
the A * XI (MOD n-) = A * (X0 + I * (n-/ d)) (MOD n-)
= (X0 + A * A * I * (n-/ D)) (n-MOD)
= A * X0 (n-MOD) (since D | A)
= B
First look at a simple example:
5x=4(mod3)
Solve for x = 2,5,8,11,14 .......
Whereby a rule can be found, the solution interval is 3.
So the solution of this interval is determined by how it?
If the first try to find a solution, and the spacing between the obtained solution, then the solution set of linear equations can be determined mode.
We set the interval between the solution to dx.
Then there
a*x = b(mod n);
a*(x+dx) = b(mod n);
Two subtraction device, to give:
a*dx(mod n)= 0;
That is a * dx is a multiple, but also a multiple of n, i.e., a * and dx is a common multiple of n. In order to determine dx, we should obtain the least common multiple of n and a, at this time dx is the corresponding the smallest.
Let a greatest common divisor of n and d, then the least common multiple of a and n is (a * n) / d.
Immediately a * dx = a * n / d;
Therefore dx = n / d.
Thus the spacing between the solutions to find out.
code show as below:
1 bool modular_linear_equation(int a,int b,int n) 2 { 3 int x,y,x0,i; 4 int d=exgcd(a,n,x,y); 5 if(b%d) 6 return false; 7 x0=x*(b/d)%n; //特解 8 for(i=1;i<d;i++) 9 printf("%d\n",(x0+i*(n/d))%n); 10 return true; 11 }
(3) with the Euclidean algorithm inverse modulo:
Congruence equation ax≡b (mod n), if gcd (a, n) == 1, then the equation has only one solution.
In this case, if b == 1, congruence equation is ax = 1 (mod n), gcd (a, n) = 1.
In this case said determined modulo n x is a multiplication inverse element of a pair.
For congruence equation ax = 1 (mod n), gcd (a, n) = 1 is solved to solve the equation
ax + ny = 1, x, y are integers. This can be used to obtain the extended Euclidean algorithm, the unique solution of the original equation is congruent with the extended Euclidean algorithm derived from x.
Report problem solving:
Applications on Extended Euclid's most typical is POJ 1061: frog dating
Meaning of the questions we should all understand, analyze it directly:
Two frogs jump t step, coordinate A as a coordinate x + mt, B is y + nt Sufficient Conditions they met:. X + mt-y-nt = pL, i.e. (nm) t + Lp = xy , L> 0.
Provided nm = A, xy = B, find the smallest satisfies At + lp = t B, i.e. seek congruence equation At B (mod l) = the smallest integer solution, particularly solving the three-step process:
(1) Write the equation (nm) t + Lp = xy, by solving the extended Euclidean function, then X 'is a solution, but not a final solution.
(2) if (xy)% gcd (nm, L) == 0, then the solvability.
(3) After solvable: Let M = gcd (nm, L), X = X (xy) / M
Then, (X% (L / M) L / M +)% (L / M) is the final solution.
Code:
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 __int64 exgcd(__int64 m,__int64 &x,__int64 n,__int64 &y) 5 { 6 __int64 x1,y1,x0,y0; 7 x0=1; 8 y0=0; 9 x1=0; 10 y1=1; 11 __int64 r=(m%n+n)%n; 12 __int64 q=(m-r)/n; 13 x=0; 14 y=1; 15 while(r) 16 { 17 x=x0-q*x1; 18 y=y0-q*y1; 19 x0=x1; 20 y0=y1; 21 x1=x; 22 y1=y; 23 m=n; 24 n=r; 25 r=m%n; 26 q=(m-r)/n; 27 } 28 return n; 29 } 30 int main() 31 { 32 __int64 x,y,n,m,l,a1,b1; 33 while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&n,&m,&l)!=EOF) 34 {__int64 s=exgcd(m-n,a1,l,b1); 35 if((x-y)%s||(m==n)) 36 printf("Impossible\n"); 37 else 38 { 39 __int64 S=l/s; 40 a1=a1*((x-y)/s); 41 a1=(a1%S+S)%S; 42 printf("%I64d\n",a1); 43 }} 44 return 0; 45 }
All right. Bye ~ Karma woo. . . .