"Congruence in number theory" study notes

Euclidean algorithm (GCD)

\(gcd(a,b) = gcd(b, a \% b)\)

Extended Euclidean Algorithm (EXGCD)

Indefinite Equations: $ ax + by = gcd (a, b) $

Special Solution

When (b = 0 \) \ time, \ (GCD (A, 0) = A \) , so the solution to give \ (x = 1, y = 0 \)

Assume:

\[ \begin{cases} ax_1 + by_1 = gcd(a,b) \\ bx_2 + (a\%b)y_2 = gcd(b,a\%b) \end{cases} \]

因为\(gcd(a,b) = gcd(b, a\%b)\),所以:
\[ \begin{aligned} ax_1 + by_1 &= bx_2 + (a\%b)y_2 \\ &= bx_2 + (a - \left \lfloor a / b \right \rfloor*b)y_2 \\ &= bx_2 + ay_2 - \left \lfloor a / b \right \rfloor*by_2 \\ &= ay_2 + b(x_2 - \left \lfloor a / b \right \rfloor y_2) \end{aligned} \]

因此\[ax_1 + by_1 = ay_2 + b(x_2 - \left \lfloor a / b \right \rfloor y_2)\]

特解:
\[ \begin{cases} x_1 = y_2 \\ y_1 = x_2 - \left \lfloor a / b \right \rfloor * y_2 \\ \end{cases} \]

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

Seek the general solution

Provided $ k_1 = a / gcd (a , b), k_2 = b / gcd (a, b) $. \ (k_1, k_2 \) must Coprime

Dividing while in situ on both sides of the equation \ (GCD (A, B) \) , to give \ (AX / GCD (A, B) + by / GCD (A, B) =. 1 \) , i.e. $ k_1x + k_2y = 1 $

Obviously other solutions with respect Laid solution, \ (X, Y \) opposite change in size. Set \ (X \) is increased \ (D_1 \) , \ (Y \) reduce \ (D_2 \) , available \ [k_1 (x + d_1) + k_2 (y - d_2) = 1 \]

Because of \ (+ k_2y k_1x. 1 = \) , so \ [k_1d_1 = k_2d_2 \]

Since (k_1, k_2) coprime, so \ [d_1 = k_2, d_2 = k_1 \]

Therefore solutions have general solution \ [(x + k \ ast \ left \ lfloor b / gcd (a, b) \ right \ rfloor, y - k \ ast \ left \ lfloor a / gcd (a, b) \ right \ rfloor) \]

Solving linear equations uncertain

Seeking \ (ax + by = c \ )

Shu Pei Theorem

\ (ax + by = c \ ) has a solution if and only if \ (gcd (a, b) | c \)

Special Solution

Expanding \ (c / gcd (a, b) \) times can. That is:
\ [\ Cases the begin {} X '= X * C / GCD (A, B) \\ Y' = Y * C / GCD (A, B) \ Cases End {} \]

Seeking the smallest positive integer solution

Because of \ (X \) a general solution is \ (K * X + B / GCD (A, B) \) . The smallest positive integer solution directly from the solution of the modular Laid \ (b / gcd (a, b) \) takes a positive value.

Solving linear congruent equation

Seeking \ (ax≡b \ (mod \ m ) \)

Equivalent to $ ax + my = b $, the equation can be indefinite.

thought

These two examples tell us that when there is an uncertain factor \ (k \) , the equations and congruence can be transformed into each other .

Fermat's Little Theorem

When \ (P \) when prime number of positive integers \ (A \) have

\[a^{P-1} \equiv 1 \pmod P\]

Euler's theorem

Euler's theorem Fermat's little theorem to the \ (\ P) case is not a prime number.

\[a^{\varphi(P)} \equiv 1 \pmod P, a \perp P\]

Multiplicative inverse

If two integers \ (a, b \) satisfies \ (ab & \ equiv. 1 \ PMOD P \) , called \ (B \) is \ (A \) in the mold \ (P \) multiplicative inverse in the sense of (vice versa).

Multiplicative inverse can be used directly \ (exgcd \) to solve the congruence equation, or the use of Euler's theorem.

Linear recurrence inversion yuan:

Set \ (P = K \ R & lt AST I + \) , there are \ [K * I + R & lt ≡ 0 \ PMOD P \] . Referred to as \ (a ^ {- 1} \) or \ (inv (a) \)

Simultaneously multiplying both sides \ (i ^ {- 1} * r ^ {- 1} \) available
\ [\ begin {aligned} k * i * i ^ {- 1} * r ^ {-1} + r * i ^ {- 1} * r ^ {- 1} ≡ 0 \ pmod P \\ k * r ^ {- 1} + i ^ {- 1} ≡ 0 \ pmod P \\ i ^ {- 1} ≡ - \ left \ lfloor \ dfrac {p } {i} \ right \ rfloor * (p \% i) ^ {- 1} \ pmod P \\ \ end {aligned} \]

Extended Chinese remainder theorem (EXCRT)

Seek congruence equation:

\[ \left \{ \begin{matrix} x ≡ a_1 \pmod {m_1} \\ x ≡ a_2 \pmod {m_2} \\ \cdots \\ x ≡ a_r \pmod {m_r} \\ \end{matrix}\right. \]

Not here Chinese remainder theorem, Chinese remainder theorem as extensions of completely contain it.

Our one by one in order to solve the equation, each time using the last solution. We have assumed that before obtaining \ (i-1 \) solutions of equations of \ (ANS \) . Set \ (M = \ Prod \ limits_ {J ^ = {I}. 1. 1-M_j} \) , apparently \ (ans + k \ ast M \) before satisfy \ (i-1 \) equations. We want to make it satisfies the first \ (i \) equations, equivalent to

\[ans+k \ast M \equiv a_i \pmod {m_i}\]

We \ (exgcd \) to obtain \ (k \) , into the update \ (M \) and \ (ans \) can be. If this congruence equation has no solution, no solution is representative of the problem.

Complexity \ (O (n-\ log m) \) .

inline int EXCRT(){
    int ans=a[1],M=m[1],g,x,y,c;
    for(int i = 2; i <= n; ++i){
        c = (a[i]-ans%m[i]+m[i])%m[i];
        exgcd(M,m[i],g,x,y);
        if(c%g != 0) return -1;
        x = qmul(x,c/g,m[i]);
        ans += x*M;
        M *= m[i]/g;
        ans = (ans+M)%M;
    }
    return ans;
}

Guess you like

Origin www.cnblogs.com/qixingzhi/p/11817434.html