模意义下的数学
Ruiqun2009
·
2023-02-22 23:11:17
·
个人记录
为了追上同学的博客写的。他这篇不就是抄的深进吗
容易证明模意义下加减乘运算仍然成立(即 a\;\mathrm{op}\;b\equiv (a\bmod p)\;\mathrm{op}\;(b\bmod p)\pmod p ,其中 \mathrm{op} 为加减乘中的任意一种)。
由于幂运算可以做到 O(\log n) ,所以模意义下也很容易做到。
模意义下除法就是乘以逆元(Inverse Number)。
通过两个互素的模数 m_0,m_1 ,可以合并出模 m_0\times m_1 意义下的答案,这个过程可以使用中国剩余定理(Chinese Remainder Theorem)。当模数不互素时可以使用扩展中国剩余定理。
Lemma 1 (Extended Euclidean Algorithm & Bézout's Identity)
Bézout's Identity: \forall a,b,c\in\mathbb{Z}^* ,\exists x,y,ax+by=c\Longleftrightarrow c|\gcd(a,b)
Proof. 设 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\bmod b)
所以 ax_1+by_1=bx_2+(a\bmod b)y_2
又因为 a\bmod b=a-(\lfloor\frac{a}{b}\rfloor\times b)
所以 ax_1+by_1=bx_2+(a-(\lfloor\frac{a}{b}\rfloor\times b))y_2
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=b ,所以 x_1=y_2,y_1=x_2-\lfloor\frac{a}{b}\rfloor y_2
将 x_2,y_2 不断代入递归求解直至 \gcd (最大公约数(Greatest Common Divisor),下同)为 0 递归 x=1,y=0 回去求解。
// 返回值为 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;
}
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)定义为 \varphi(x) ,其意义为 \lbrack 1,x\rbrack 中与 x 互素的正整数的个数。
不难得到,当 x 为质数时 \varphi(x)=x-1 。
例如 \varphi(12)=4 ,因为 \lbrack 1,12\rbrack 中 \{1,5,7,11\} 和 12 互素。
Reduced Residue System
在上述定义中,我们称 12 的缩系(Reduced Residue System)为 \{1,5,7,11\} 。
不难证明 x 的缩系是一个阶数为 \varphi(x) 的群。
Lemma 2. Euler's Theorem
定理内容:\forall a,n\in\mathbb{Z}^+,a\perp n ,有 a^{\varphi(n)}\equiv 1\pmod n 。
Proof. 我们设集合 R 为 n 的缩系以及集合 R',R'' ,满足 \forall i\in\lbrack 1,\varphi(n)\rbrack ,有 R'_ i=aR_i\bmod n,R''_ i=aR_i (其中 R_i 代表集合 R 的第 i 个元素,这里认为下标从 1 开始)。
如果 R\cong R' ,则
\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}
两边同时乘以 \prod\limits_{x\in R}x 的逆元即可得到 a^{\varphi(n)}\equiv 1\pmod n 。
下面是 R\cong R' 的证明:
\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}
于是我们假设 R'_ i=R'_ j ,有 aR_i\equiv aR_j\pmod n 。
\begin{array}{l}
\because \gcd(a,n)=1\\
\therefore R_i=R_j\\
\therefore i=j
\end{array}
与 i\neq j 矛盾,故 R\cong R' 。定理得证。
Lemma 3. Fermat's Little Theorem
**Proof.** 由于 $p\in\text{prime}$,$\varphi(p)=p-1$。将这个式子带入 Euler's Theorem 中即可。
习题:
1. [【模板】扩展欧拉定理](https://www.luogu.com.cn/problem/P5091)(可能过难)