跳到正文

个人记录

模意义下的数学

Ruiqun2009约 6 分钟
原文
文章信息与版本

文章信息

发布
2023年2月22日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 6 分钟

版本

  1. v1当前

为了追上同学的博客写的。他这篇不就是抄的深进吗

容易证明模意义下加减乘运算仍然成立(即 a  op  b≡(a mod p)  op  (b mod p)(modp)a\;\mathrm{op}\;b\equiv (a\bmod p)\;\mathrm{op}\;(b\bmod p)\pmod p,其中 op\mathrm{op} 为加减乘中的任意一种)。

由于幂运算可以做到 O(log⁡n)O(\log n),所以模意义下也很容易做到。

模意义下除法就是乘以逆元(Inverse Number)。

通过两个互素的模数 m0,m1m_0,m_1,可以合并出模 m0×m1m_0\times m_1 意义下的答案,这个过程可以使用中国剩余定理(Chinese Remainder Theorem)。当模数不互素时可以使用扩展中国剩余定理。

Lemma 1 (Extended Euclidean Algorithm & Bézout’s Identity)

Bézout’s Identity: ∀a,b,c∈Z∗,∃x,y,ax+by=c⟺c∣gcd⁡(a,b)\forall a,b,c\in\mathbb{Z}^* ,\exists x,y,ax+by=c\Longleftrightarrow c|\gcd(a,b)

Proof. 设 ax1+by1=gcd⁡(a,b),bx2+(a mod b)y2=gcd⁡(b,a mod b)ax_1+by_1=\gcd(a,b),bx_2+(a\bmod b)y_2=\gcd(b,a\bmod b)

由 Euclidean Algorithm 可知:gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a,b)=\gcd(b,a\bmod b)

所以 ax1+by1=bx2+(a mod b)y2ax_1+by_1=bx_2+(a\bmod b)y_2

又因为 a mod b=a−(⌊ab⌋×b)a\bmod b=a-(\lfloor\frac{a}{b}\rfloor\times b)

所以 ax1+by1=bx2+(a−(⌊ab⌋×b))y2ax_1+by_1=bx_2+(a-(\lfloor\frac{a}{b}\rfloor\times b))y_2

ax1+by1=ay2+bx2−⌊ab⌋×by2=ay2+b(x2−⌊ab⌋y2)ax_1+by_1=ay_2+bx_2-\lfloor\frac{a}{b}\rfloor\times by_2=ay_2+b(x_2-\lfloor\frac{a}{b}\rfloor y_2)

因为 a=a,b=ba=a,b=b,所以 x1=y2,y1=x2−⌊ab⌋y2x_1=y_2,y_1=x_2-\lfloor\frac{a}{b}\rfloor y_2

将 x2,y2x_2,y_2 不断代入递归求解直至 gcd⁡\gcd(最大公约数(Greatest Common Divisor),下同)为 0 递归 x=1,y=0 回去求解。

C++
// 返回值为 gcd(a,b)
int exgcd(int a, int b, int &x, int &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
C++
void exgcd(int a, int b, int &x, int &y) {
    if (!b) {
        x = 1;
        y = 0;
        return;
    }
    exgcd(b, a % b, y, x);
    y -= a / b * x;
}

Bézout’s Identity 也是这么证明的。

Euler Totient Function

欧拉函数(Euler Totient Function)定义为 φ(x)\varphi(x),其意义为 [1,x]\lbrack 1,x\rbrack 中与 xx 互素的正整数的个数。

不难得到,当 xx 为质数时 φ(x)=x−1\varphi(x)=x-1。

例如 φ(12)=4\varphi(12)=4,因为 [1,12]\lbrack 1,12\rbrack 中 {1,5,7,11}\{1,5,7,11\} 和 1212 互素。

Reduced Residue System

在上述定义中,我们称 1212 的缩系(Reduced Residue System)为 {1,5,7,11}\{1,5,7,11\}。

不难证明 xx 的缩系是一个阶数为 φ(x)\varphi(x) 的群。

Lemma 2. Euler’s Theorem

定理内容:∀a,n∈Z+,a⊥n\forall a,n\in\mathbb{Z}^+,a\perp n,有 aφ(n)≡1(modn)a^{\varphi(n)}\equiv 1\pmod n。

Proof. 我们设集合 RR 为 nn 的缩系以及集合 R′,R′′R',R'',满足 ∀i∈[1,φ(n)]\forall i\in\lbrack 1,\varphi(n)\rbrack,有 Ri′=aRi mod n,Ri′′=aRiR'_ i=aR_i\bmod n,R''_ i=aR_i(其中 RiR_i 代表集合 RR 的第 ii 个元素,这里认为下标从 11 开始)。

如果 R≅R′R\cong R',则

∏x∈Rx=∏x∈R′x≡∏x∈R′′x=∏x∈Rx×aφ(n)(modn)\begin{aligned} \prod_{x\in R}x&=\prod_{x\in R'}x\\ &\equiv\prod_{x\in R''}x\\ &=\prod_{x\in R}x\times a^{\varphi(n)}\pmod n \end{aligned}

两边同时乘以 ∏x∈Rx\prod\limits_{x\in R}x 的逆元即可得到 aφ(n)≡1(modn)a^{\varphi(n)}\equiv 1\pmod n。

下面是 R≅R′R\cong R' 的证明:

∀i∈[1,φ(n)]∵gcd⁡(a,n)=1,gcd⁡(Ri,n)=1∴gcd⁡(aRi,n)=1∴gcd⁡(Ri′,n)=1∵∀j∈[1,φ(n)]∖{i},Ri′≠Rj′\begin{array}{l} \forall i\in\lbrack 1,\varphi(n)\rbrack\\ \because \gcd(a,n)=1,\gcd(R_i,n)=1\\ \therefore \gcd(aR_i,n)=1\\ \therefore \gcd(R'_ i,n)=1\\ \because \forall j\in\lbrack 1,\varphi(n)\rbrack\setminus\{i\},R'_ i\neq R'_ j\\ \end{array}

于是我们假设 Ri′=Rj′R'_ i=R'_ j,有 aRi≡aRj(modn)aR_i\equiv aR_j\pmod n。

∵gcd⁡(a,n)=1∴Ri=Rj∴i=j\begin{array}{l} \because \gcd(a,n)=1\\ \therefore R_i=R_j\\ \therefore i=j \end{array}

与 i≠ji\neq j 矛盾,故 R≅R′R\cong R'。定理得证。

Lemma 3. Fermat’s Little Theorem

∀a,p∈Z+,p∈prime\forall a,p\in\mathbb{Z}^+,p\in\text{prime},有 ap−1≡1(modp)a^{p-1}\equiv 1\pmod p。

Proof. 由于 p∈primep\in\text{prime},φ(p)=p−1\varphi(p)=p-1。将这个式子带入 Euler’s Theorem 中即可。

习题:

  1. 【模板】扩展欧拉定理(可能过难)
原文
存档评论1 条抓取于