First, expand Euclidean algorithm
The algorithm is used to solve the problem of a given two nonzero integers a and B, to find a set of integer solutions (x, y) so that ax + by = gcd ( A, A, b) is established, where GCD (A, a, a, a) represents the greatest common divisor of a and a.
-
- Recursive boundary: When B is 0 o'clock, at this time a is equal to GCD, obviously there is a*1+b*0=gcd set up, at this time x=1,y=0;
- Recursive formula: When calculating gcd (A, B), there is ax1 + by1 = GCD is established, and in the next step gcd (b,a%b) is calculated Bx2 + (a%b) y2 = GCD is established. There are the following recursive expressions:
$ \left\{\begin{matrix}x_{1} = y_{2}\\ Y_{1} = x_{2}-(A/b) y_{2}\end{matrix}\right. $
The code is as follows:
1 /* 2 expand Euclidean algorithm3 */4 5#include <stdio.h>6#include <string.h>7#include <math.h>8#include <stdlib.h>9#include <time.h>Ten#include <stdbool.h> One A intx=0, y=0; - //given two nonzero integers a and B, find a set of integer solutions (x, y), which makes ax + by = gcd (A, a) - intEXGCD (intAintb) { the if(b = =0) {//recursive boundary -x =1; -y =0; - returnA//back to greatest common divisor + } - intg = EXGCD (b, a%b); + inttemp = x;//Store the value of x Ax = y;//push-to-pass aty = temp-a/b*y; - returnG//g is greatest common divisor - } - - intMain () { - intGCD = EXGCD (4,3); inprintf"4*%d + 3*%d =%d\n", x, Y, GCD); - to return 0; +}
When the EXGCD function ends, x and Y are the solutions. Obviously, after getting such a set of solutions, we can get all the solutions by the following formula:
$\left\{\begin{matrix} x^{'} = x + \frac{b}{gcd}*k \ y^{'} = y-\frac{a}{gcd}*k \end{matrix}\right.$ (K is any integer)
That is, all the solutions for x and Y are periodic with B/GCD and A/GCD respectively . So what is the smallest non-negative integer solution of x? From the intuitive point of view is X (B/GCD). However, when X is negative, a negative number (B/GCD) is also obtained for the percent (-15)%4=-3. Consider that even if X is negative, the range of percent (B/GCD) is also (-b/gcd,0), so for any integer, $\left (X\%\FRAC{B}{GCD} + \frac{b}{gcd} \right) \% \FRAC{B}{GCD} The $ is the corresponding minimum non-negative integer solution .
Second, equation ax + by = C Solution
the necessary and sufficient condition for the existence of a solution for ax + by = c is c%gcd = = 0, and a set of solutions (x, y) equals $\left (\FRAC{CX_{0}}{GCD},\FRAC{CY_{0}}{GCD} \right) $.
After you have obtained such a set of solutions, you can get the full solution from the following equation:
$\left\{\begin{matrix} x^{'} = \frac{cx_{0}}{gcd} + \frac{b}{gcd}*k \ y^{'} = \frac{cy_{0}}{gcd}-\frac{a}{gcd}*K \end {matrix}\right.$ (K is any integer)
In addition, you can get the same conclusion as above, for any integer, $\left (X\%\FRAC{B}{GCD} + \frac{b}{gcd} \right) \% \frac{b}{gcd}$ is the smallest nonnegative integer solution of x in Ax+by=c, In general, X can be taken $\frac{cx_{0}}{gcd}$, where x0 is a solution to AX+BY=GCD.
The solution of Ax≡c (mod m) with congruence type
First explain what is the same formula . For integers a, B, M, the introductory m is divisible by a-B (that is (A-a)%m=0), then it is said that the "a" and "M" is the same remainder, the corresponding congruence is a≡b (mod m), M is called the mode of congruence. Obviously, each integer is the same as the only integer in [0,m] .
The solution here is to solve the same-ax≡c (mod m). According to the definition of congruence, there is (AX-C)%m=0 is established, so there is an integer y, so that Ax-c=my is established. Move the item and make it ax+my=c after y=-y.
By the above conclusion, $\left (x, y \right) = \left (\frac{cx_{0}}{gcd\left (a,m \right)},\frac{cy_{0}}{gcd\left (a,m \right)} \righ T) $.
As for all solutions , the following conclusions are drawn:
Set A,c,m is an integer, where m≥1, then
-
- If C%GCD (a,m) ≠0, then the residue Equation ax≡c (mod m) has no solution.
- If C%GCD (a,m) = 0, then the same residue Equation ax≡c (mod m) has exactly gcd (a,m) a different solution in the meaning of modulo m, and the form of the solution is
$x ^{'}=x+\frac{m}{gcd\left (a,m \right)}*k$
where K = 0,1, ..., gcd (a,m)-1.
The solution of inverse element and the calculation of (b/a)%m
The first explanation is what is an inverse (in this case, the multiplicative inverse). Suppose A, B, and M are integers, m>1. and ab≡1 (mod m) is established, then it is said that A and B are mutually modulo m inverse. In layman's words, if the product of the two-digit modulus m equals 1, it is called an inverse of M.
So what's the use of the inverse element? When calculating (b/a)%m, by finding the inverse x of a-mode m, there is (b/a)%m = (b*x)%m = ((b%m) * (x%m))%m is established. This is very useful for solving dividend B's very large problems.
By definition, the inverse of a-mode m is solved with ax≡1 (mod m), and in practice, the smallest positive integer solution of x is referred to as the inverse of a-modulo m .
-
- if GCD (a,m) ≠1, then the same ax≡1 (mod m) has no solution, a does not exist modulo m inverse.
- if GCD (a,m) ≠1, then the same ax≡1 (mod m) has a unique solution on (0,m) , directly using the extended Euclidean algorithm to solve the x can be used (x%m + m)%m to get (0,m) the solution in the range, that is, the required inverse.
In addition, if M is a prime number and a is not a multiple of M, then you can also use the Fermat theorem to get the inverse element directly.
Fermat theorem: Set M is prime, A is any integer and a $\not\equiv$ 0 (mod m), then am-1≡1 (mod m).
Mathematical problems--expanding Euclidean algorithm