[Study notes] Chinese Remainder Theorem and its extension

1. Chinese Remainder Theorem (CRT)

1.1 problems introduced

  • Given congruence equation, the form

\[ \begin{cases} x\equiv a_1 \pmod {p_1} \\ x\equiv a_2 \pmod {p_2} \\ \dots \\ x\equiv a_n \pmod {p_n} \\ \end{cases} \]

  • Wherein \ (P_i \) pairwise quality.

Solutions of the Equations 1.2

  • We remember \ (M = \ prod_ = {I}. 1 np_i ^ \) , \ (m_i = \ {P_i FRAC {m}} \) .
  • We assume've got a solution \ (x_0 \) , then \ (x_0 + kM (k \ in \ mathbb Z) \) are clearly the solution of equations.
  • I.e. all solutions constituting about \ (M \) a residual category.
  • We therefore only requires a \ ([0, M) \ ) integer solution to the.
  • Chinese remainder theorem gives conclusion, this equation \ ([0, M) \ ) is present in a unique solution .

Solution 1.3 Equations

  • Let's consider how to find the solution.
  • We consider \ (n \) th equations, the \ (i \) equations group

\[ \begin{cases} x_i\equiv 0 \pmod {p_1} \\ x_i\equiv 0 \pmod {p_2} \\ ~~~~ ~ ~~ ~\dots \\ x_i \equiv a_i \pmod {p_i}\\ ~~~~~ ~~ ~ \dots \\ x_i\equiv 0 \pmod {p_{n-1}} \\ x_i\equiv 0 \pmod {p_n} \\ \end{cases} \]

  • Then \ (\ sum_ {i = 1 } ^ nx_i \) to a solution.
  • Only needs to \ (M \) modulo obtain \ ([0, n) \ ) Special Solution therein.
  • Consider \ (x_i \) how demand.
  • Let us consider a system of equations

\[ \begin{cases} y_i\equiv 0 \pmod {p_1} \\ y_i\equiv 0 \pmod {p_2} \\ ~~~~ ~ ~~ ~\dots \\ y_i \equiv 1 \pmod {p_i}\\ ~~~~~ ~~ ~ \dots \\ y_i\equiv 0 \pmod {p_{n-1}} \\ y_i\equiv 0 \pmod {p_n} \\ \end{cases} \]

  • Then we can make \ (x_i = a_iy_i \) .
  • Since \ (p_j | y_i (J \ neq i) \) , and \ (p_i \) between any two relatively prime, so the \ (m_i \) in the mold \ (p_i \) there is an inverse element in the sense, so we can direct order \ (y_i m_i = \ Times m_i ^ {-}. 1 \) , \ (m_i ^ {-}. 1 \) represents the inverse element.
  • Inverse, as the case may be with \ (exgcd \) or Fermat's little theorem requirements.
  • 所以 \(x_i=m_i\times m_i^{-1}\times a_i\)
  • So \ (ANS = \ sum_ = {I}. 1 ^ nm_i \ Times m_i ^ {-}. 1 \ Times a_i \) , in order to prevent overflow, we only need to die \ (M \) made to the sense operation.
  • Note that \ (CRT \) and \ (exCRT \) sometimes multiplication could overflow on how to deal with this problem see [tips] O (1) rapid multiplication - changle_cyx .

1.4 solution in the mold \ (M \) unique in the sense

  • Next we prove that the solution in the mold \ (M \) unique in the sense.
  • I.e., two solutions prove absence \ (x_0, x_1 \) , so \ (M \ x_1 the nmid-x_0 \) .
  • Reductio ad absurdum:
    • Order \ (x_0 \) represents a particular solution of the original equations, there is another solution \ (D x_0 + \) , so \ (M \ the nmid D \) .
    • \(\exists i\in [1,n],p_i\nmid d\),即 \(d\not \equiv 0\pmod {p_i}\)
    • By the \ (x_0 \) is the solution of the original equations available \ (ans_0 \ equiv a_i \ PMOD {P_i} \) .
    • So \ (x_0 + D \ \ equiv a_i PMOD {P_i} \ \ Not) , (D x_0 + \) \ Solution not the original equations, and contradicts the assumption.
    • QED.
  • So we get the Chinese remainder theorem.
  • This unique wide applications in the Chinese remainder theorem, two examples are described in section 3.

2. Expand the Chinese remainder theorem (exCRT)

2.1 problems introduced

  • Chinese remainder theorem to solve the equations modulo twenty-two relatively prime.
  • So for modulus does not meet twenty-two equations relatively prime, how to solve it?
  • 即求解
    \[ \begin{cases} x\equiv a_1 \pmod {p_1} \\ x\equiv a_2 \pmod {p_2} \\ \dots \\ x\equiv a_n \pmod {p_n} \\ \end{cases} \]
  • Where \ (p_i \) no special constraints.

Solutions of the Equations 2.2

  • Not necessarily solvable equations, we can determine the way in the solution process.
  • We remember \ (m_i = \ {text} LCM (P_1, P_2, \ DOTS, P_i) \) , \ (M = \ {text} LCM (P_1, P_2, \ DOTS, P_n) \) .
  • Suppose we get a particular solution \ (x_0 \) , so \ (x_0 + kM (k \ in \ mathbb Z) \) solution are equations.
  • Solution out \ (X \) is also one of the remaining classes.
  • 1.4 similar evidence, we can still get if the equation has a solution, the solution of the equations in the mold \ (M \) unique in the sense.

Solution of Equations 2.3

  • The reason we can not be like prime to do that is the case, because they do not relatively prime, so we need the multiplicative inverse may not exist.
  • We consider front to back, one by one equation to merge.
  • We assume the solution into the first \ (i \) equations, we make \ (x_i \) denotes a modulo \ (m_i \) a unique solution in the sense.
  • For the first \ (1 \) equations, we make direct \ (x_1 = A_1 \) .
  • For the first \ (i (i> 1) \) equations, we know the first \ (i-1 \) equations of the general solution for the \ (x_ {i-1} + km_ {i-1} (k \ in \ the Z-mathbb) \) .
  • So now we add the first \ (i \) limit equations of \ (the X-\ equiv a_i \ PMOD P_i} {\) .
  • Then the first \ (I \) after the simultaneous equations is equivalent to the equation

\[x_{i-1}+km_{i-1}\equiv a_i(mod~p_i)\]

  • Indeterminate equation that is written in the form

\[m_{i-1}k+p_iy=a_i-x_{i-1}\]

  • Then Extended Euclidean Algorithm ( \ (exgcd \) solution at this equation).
  • If there is a \ (I \) , so that this equation has no solution, i.e. \ ((I-m_Low {}. 1, P_i) \ a_i the nmid. 1-I-X_ {} \) , also means that the entire set of equations has no solution .
  • Otherwise letting solve special solution \ (K \) accessible to the mold \ (frac {p_i} {( m_ {i-1}, p_i)} \ \) unique solution in the sense, and even if \ (K \) of \ (\ frac {p_i} {( m_ {i-1}, p_i)} \) modulo.
  • Then get \ (I-x_i = X_ {+}. 1. 1-m_Low {I} K \) , and also make \ (x_i \) of \ (m_i \) modulo to.
  • Finally \ (x_n \) is the answer.
  • Template question: Luo Gu P4777 - Extended Chinese remainder theorem (EXCRT)
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

template <class T>
inline void read(T &x)
{
    static char ch; 
    while (!isdigit(ch = getchar())); 
    x = ch - '0'; 
    while (isdigit(ch = getchar()))
        x = x * 10 + ch - '0'; 
}

typedef long long s64; 

const int MaxN = 1e5 + 5; 

int n; 
s64 p[MaxN], a[MaxN], pre_lcm[MaxN];

inline s64 qmul(s64 b, s64 p, const s64 &mod)
{
    b = (b % mod + mod) % mod; 
    p = (p % mod + mod) % mod; 
    s64 res = 0; 
    for (; p; p >>= 1, (b = b + b) > mod ? (b -= mod) : 0)
        if (p & 1)
        {
            res = res + b; 
            if (res >= mod)
                res -= mod; 
        }
    return res; 
}

inline s64 ex_gcd(const s64 &a, const s64 &b, s64 &x, s64 &y)
{
    if (!b)
        return x = 1, y = 0, a; 
    s64 res = ex_gcd(b, a % b, y, x); 
    y -= a / b * x; 
    return res; 
}

inline s64 solve_equ(const s64 &a, const s64 &b, const s64 &c)
{
    s64 x, y; 
    s64 d = ex_gcd(a, b, x, y); 
    return qmul(x, c / d, b / d); 
}

int main()
{
    read(n); 
    for (int i = 1; i <= n; ++i)
        read(p[i]), read(a[i]); 
    pre_lcm[1] = p[1]; 
    for (int i = 2; i <= n; ++i)
        pre_lcm[i] = p[i] / std::__gcd(pre_lcm[i - 1], p[i]) * pre_lcm[i - 1]; 
    
    s64 x = a[1]; 
    for (int i = 2; i <= n; ++i)
    {
        s64 k = solve_equ(pre_lcm[i - 1], p[i], a[i] - x); 
        x = x + qmul(pre_lcm[i - 1], k, pre_lcm[i]); 
        if (x >= pre_lcm[i])
            x -= pre_lcm[i]; 
    }
    
    std::cout << x << std::endl; 
    
    return 0; 
}

3. Simple Application Chinese Remainder Theorem

  • We give two very very very simple example (dalao do not d).

    3.1 Euler function is a multiplicative function of proof

  • Euler function \ (\ varphi (n-) = \ sum_ {I} ^ n-1 = [I \ n-PERP] \) , i.e. \ (1 \) to \ (\ n-) with \ (n-\) prime the number of numbers.
  • Euler function is a multiplicative function.
  • If \ (P \ PERP Q \) , then \ (\ varphi (PQ) = \ varphi (P) \ varphi (Q) \) .
  • Proof is given below from the perspective of the Chinese Remainder Theorem:
    • We set \ (A \) represents \ (1 \) to \ (P \) with (P \) \ prime number set composed, \ (B \) represents \ (1 \) to \ (Q \ ) with \ (Q \) prime number set composed, \ (C \) represents \ (1 \) to \ (PQ \) with (PQ \) \ a set of prime numbers.
    • Since \ (P \ PERP Q \) , so \ (i \ perp pq \ Leftrightarrow i \ perp p and I \ PERP Q \) .
    • The subtractive removed, we know \ ((n-, m) = (n-, m + KN), K \ in \ mathbb the Z \) .
    • Combined with the above conclusion, we can get
    \[ C=\{x|\begin{cases}x\equiv a(mod~p)\\ x\equiv b(mod~q)\end{cases},a\in A,b\in B\} \]
    • The Chinese remainder theorem, we have for each \ (A \ in A, B \ in B \) , we can obtain congruence equation
      \ [\ begin {cases} x \ equiv a (mod ~ p) \\ x \ equiv b (mod ~ q) \ end {cases} \]
    • The unique solution \ (X \) , and apparently \ ([1, pq] \ ) in the presence of a not \ (X \) , so \ (X \) is the solution of equations of two different congruence .
    • Thus we can \ (A \ B Times \) (Cartesian product) and \ (C \) to establish a bijection between.
    • Therefore set \ (C \) the number of elements is set equal to \ (A \) and set \ (B \) the product of the number of elements.
    • QED.

3.2 Solving the number of special topics modulus

  • To give a classic topic: BZOJ1951 [SDOI2010] ancient pig culture
  • Title effect: seeking \ (G ^ {\ sum_ {D |} n-n-C_ {D}} ^ {} \ MOD P (P = 999 911 659, n-, G \ ^ LE10. 9) \) .
  • We know that \ (p \) is a prime number.
  • I am sure you will ask, most number of topics modulus is not prime it, this is what's special? ?
  • But you find that \ (G \) index is large, the exact value can not be obtained directly.
  • Euler's theorem we know \ (A ^ B \ equiv A ^ B {\% \ varphi (P)} (~ MOD P), A \ PERP P \) .
  • When \ | (p G \) , the answer is \ (0 \) .
  • Otherwise, \ (G \ the p-PERP \) , we can solve by Euler's theorem.
  • Is our index need only take \ (\ sum_ {d | n } C_ {n} ^ {D} \ MOD \ varphi (P) \) , i.e., \ (\ sum_ {d | n } C_ {n} ^ D} {\ MOD 999 911 658 \) .
  • Now the question is, if we directly \ (Lucas \) Theorem to solve, does not meet the conditions of the modulus is a prime number.
  • Some people say: I will expand Lucas! ! (But the authors also do not)
  • But this question is no need to use any extensions \ (Lucas \) .
  • Since \ (= 2 999 911 658 \ times3 \ times4679 \ times35617 \) , i.e., the number of prime factors of each number are \ (1 \) .
  • So we find that we simply requires \ (\ sum_ {d | n } C_ {n} ^ {d} \) these results after four modulo number, with \ (the CRT \) be merged ( \ ( CRT \) tells us find answers to these four lists congruence equation, you can get mold \ (M \) a unique solution in the sense).
  • Then transformed into modulus of issues of prime numbers, the direct use of \ (Lucas \) Theorem fix it.
  • \ (Lucas \) Theorem ( \ (P \) is a prime number): \ (\ Binom {n-} {m} = \ Binom {n-/ P} {m / P} \ Binom {n-\% P} {m \ P}% \) .
#include <bits/stdc++.h>
//999911658=2*3*4679*35617

const int mod = 999911659; 

const int p[5] = {0, 2, 3, 4679, 35617}; 
const int MaxN = 4e4 + 5; 

int n, G; 
int a[5], fac[MaxN], inv[MaxN]; 

inline void add(int &x, const int &y, const int &mod)
{
    x += y; 
    x >= mod ? x -= mod : 0; 
}

inline int qpow(int a, int b, const int &mod)
{
    int res = 1; 
    for (; b; b >>= 1, a = 1LL * a * a % mod)
        if (b & 1)
            res = 1LL * res * a % mod; 
    return res; 
}

inline int lucas(const int &n, const int &m, const int &mod)
{
    if (n < m) return 0; 
    if (n < mod && m < mod) return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; 
    
    return 1LL * lucas(n % mod, m % mod, mod) * lucas(n / mod, m / mod, mod) % mod; 
}

inline int solve(const int &p)
{
    fac[0] = 1; 
    for (int i = 1; i < p; ++i)
        fac[i] = 1LL * fac[i - 1] * i % p; 
    inv[p - 1] = qpow(fac[p - 1], p - 2, p); 
    for (int i = p - 2; i >= 0; --i)
        inv[i] = 1LL * inv[i + 1] * (i + 1) % p; 
    
    int res = 0; 
    for (int i = 1; i * i <= n; ++i)
        if (n % i == 0)
        {
            add(res, lucas(n, i, p), p); 
            if (i * i != n)
                add(res, lucas(n, n / i, p), p); 
        }
    return res; 
}

int main()
{
    scanf("%d%d", &n, &G); 
    if (G == mod)
    {
        puts("0"); 
        return 0; 
    }
    
    for (int i = 1; i <= 4; ++i)
        a[i] = solve(p[i]); 
    
    int lcm = mod - 1, ans = 0; 
    for (int i = 1; i <= 4; ++i)
    {
        int x = lcm / p[i]; 
        add(ans, 1LL * x * qpow(x, p[i] - 2, p[i]) % lcm * a[i] % lcm, lcm); 
    }
    printf("%d\n", qpow(G, ans, mod)); 
    
    return 0; 
}

Guess you like

Origin www.cnblogs.com/cyx0406/p/11906419.html