Extended Euclidean algorithm-solving indefinite equation, linear congruence equation.

If the two frogs meet after the S-step, the following equations will be fulfilled:

(x+m*s)-(y+n*s) =k*l (k=0,1,2 ...)

Slightly changed to form:

(n-m) *s+k*l=x-y

Make n-m=a,k=b,x-y=c, namely

A*s+b*l=c

As long as there is an integer solution on the equation, two frogs can meet, or not.

The first way to think of a method is to use two times for the loop to enumerate the value of the s,l, see if there is a s,l integer solution, if present, enter the smallest s,

But obviously this method is undesirable, and no one knows how big the smallest s is, and if the smallest s is large then the timeout is obvious.

In fact, the problem with the Euclidean expansion of the principle can be quickly resolved, first to see what is the Euclidean expansion principle:

Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two integers, a, b, and so on. Its computational principle relies on the following theorem:

Theorem: gcd (b) = gcd (b,a mod b)

Proof: 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

Euclidean algorithm is based on this principle, its algorithm is described in C + + language as:

int GCD (int a, int b) {if (b = = 0) return A; return Gcd (b, a% B); }

Of course you can also write an iterative form:

int GCD (int a, int b) {while (b! = 0) {int r = B; b = a% B; A = R; } return A; }

It is essentially the same principle that is used.

Supplement: The extended Euclidean algorithm is used to solve a set of X, Y, and A*X+B*Y=GCD (A, A, A, b) in a known A and a (A, a) (the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations. The following is a

In C + + implementation:

int exgcd (int a, int b, int &x, int &y) {if (b = = 0) {x = 1; y = 0; return A; } int r = EXGCD (b, a% B, x, y); int t = x; x = y; y = t-a/b * y; return R; }

Comparing this implementation with the recursive implementation of GCD, we find that the following X, Y assignment process is found, which is the essence of extending Euclid's algorithm.

Can think like this:

For a ' = B, b ' = a% B, we get x, Y makes a ' x + b ' y = Gcd (a ', B ')

Because b ' = a% b = a-a/b * B (Note: This is a division in the programming language)

Then you can get:

A ' x + b ' y = Gcd (a ', B ') ===> bx + (a-a/b * b) y = GCD (a ', b ') = GCD (A, b) ===> ay +b (x-a/b*y) = GCD (A, B)

Therefore, for A and B, their relative p,q are Y and (x-a/b*y) respectively.

Understanding of the method of solving X, y

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.

Read a lot on the internet on the problem of solving the equations of indefinite equation, can not say that all, all only said a part, after looking at a lot of real to understand the indefinite equation of the whole process, the steps are as follows:

An integer solution that asks A * x + b * y = c.

1, first calculate gcd (A, b), if n can not be divisible by gcd (A, B), then the equation has no integer solution; otherwise, the equation is divided by gcd (A, a), and the new indefinite equation a ' * x + b ' * y = C ', at this time gcd (a ', a) = 1;

2, using the Euclidean algorithm described above to find the equation a ' * x + b ' * y = 1 A set of integer solution X0,y0, then C ' * x0,c ' * y0 is the equation a ' * x + b ' * y = C ' A set of integer solutions;

3, according to the correlation theorem in number theory, can get the equation a ' * x + b ' * y = C ' of all the integer solution is:

In fact, the solution we find is just a group,

A*X0+LCM (A, B) +B*Y0-LCM (A, b) = 1;

A*x +b*y = 1;

X=X0+B/GCD (A, b); Y=y0-a/gcd (A, b);

A/GCD (A, b) *x ' +B/GCD (A, b) *y ' =C/GCD (A, b);

X ' =c/gcd (A, b)*x0+b/gcd (A, b); y ' =c/gcd (A, b) *y0-a/GCD (A,b);

x = C ' * x0 + b ' * t y = C ' * y0-a ' * t (t is an integer)

The above solution is a * x + b * y = N of all integer solutions

Euclidean algorithm GCD and its development ultimate explanation