Simple Number Theory (Extended Euclid, congruence) (unfinished)

Extended Euclid

There is now a variable equation \ (AX + by = c \) , we need to set a particular solution of this equation is obtained, and the \ (x, y \) are integers. According to Shu sad theorem, to get integer solutions must be satisfied \ (\ gcd (A, b) | c \) , (all subsequent \ (gcd (a, b) \) will be abbreviated as \ ((a, b ) \) )

First, we can first determine the equation \ (ax '+ by' = (a, b) \) of the particular solution.
\ [\ Begin {align} \ because & c = {c \ over (a, b)} (a, b) \\ \ therefore & a {c \ over (a, b)} x '+ b {c \ over ( a, b)} y '= {c \ over (a, b)} (a, b) \\ \ Rightarrow & a {c \ over (a, b)} x' + b {c \ over (a, b )} y '= c \\ \
therefore & x = {c \ over (a, b)} x', y = b {c \ over (a, b)} y '\ end {align} \] so we original equation can be obtained a group of specialized understanding. So now the question is: The second equation particular solution on how to find it.

First, if the \ (B = 0 \) , then obviously possible to obtain \ (A =. 1, B = 0 \) .

接着考虑一般情况:
\[ \begin{align} \because& (a,b)=(b, a\% b)\\ \therefore& ax+by=bx'+(a\% b)y'\\ \Rightarrow& ax+by=bx'+(a-\lfloor{a\over b}\rfloor b)y'\\ \Rightarrow& ax+by=ay'+b(x'-\lfloor{a\over b}\rfloor y')\\ \therefore&x=y', y=x'-\lfloor{a\over b}\rfloor y' \end{align} \]

void exgcd(int a, int b, int& x, int& y) {
    if (!b) {
        a = 1, b = 0;
        return ;
    }
    exgcd(b, a % b, x, y);
    int t = x;
    x = y, y = t-(a / b) y;
}

Congruence equation

There is now a congruence equation, the form \ (AX \ equiv B \ PMOD P \) , is now necessary to calculate \ (X \) .

In fact, we know congruence equation can be transformed into a variable equation: \ (BP = AX + b \) and this obviously can use the extended Euclidean equation solving, and solvability condition is clearly \ ((a, p) | b \)

\ (\ Rightarrow \) congruence equation (template)

Inverse

As we all know, can not be used in modular arithmetic division, but we know, is divided by a number \ (= \) multiplied by the reciprocal of this number. So if there is something similar reciprocal modulo it in? We need to know, assuming that the number \ (x \) so we need to be satisfied that it is clear that \ (AX \ equiv 1 \ PMOD m \) , that is to say \ (x \) is \ (a \) mode \ (m \) reciprocal under, this thing called the inverse element.

The method for solving the inverse element, there are two, one is to use congruent equation, one is linear sieve, and now we look at the code in terms of proven linear screen.

Guess you like

Origin www.cnblogs.com/juruohjr/p/11314412.html