本文原在 2025-02-08 07:35 发布于本人洛谷博客。
〇、整除
定义 \(b\mid a\Leftrightarrow a\bmod b=0\)。
- 求证:\(c\mid a\) 且 \(c\mid b \Rightarrow c\mid (a+b),c\mid ab\)。
证明:设 \(a=k_1c,b=k_2c,\therefore a+b=c(k_1+k_2),ab=c^2k_1k_2\)。
一、辗转相除法
- 求证:\(\gcd(a,b)=\gcd(b,a\bmod b)\)。
证明:设 \(a\bmod b=c\),即 \(a=kb+c\)。
\(\therefore c=a-kb\)。
设 \(d\mid a,d\mid b\)。
\(\therefore d\mid kb\),\(\therefore d\mid c\)。
\(\therefore a,b\) 的公因数,均为 \(a\bmod b\) 的因数,即均为 \(b,a\bmod b\) 的公因数。
设 \(d'\mid b,d'\mid c\)。
\(\therefore d'\mid kb\),\(\therefore d'\mid a\)。
\(\therefore b,a\bmod b\) 的公因数,均为 \(a\) 的因数,即均为 \(a,b\) 的公因数。
\(\therefore \gcd(a,b)=\gcd(b,a\bmod b)\)。
二、裴蜀定理
- 求证:\(ax+by=\gcd(a,b)\) 有整数解。
证明:https://oi-wiki.org/math/number-theory/bezouts/。
三、exgcd
- 求 \(ax+by=\gcd(a,b)\) 的一组整数解。
解:设已知 \(bx'+(a\bmod b)y'=\gcd(b,a\bmod b)\) 的一组整数解 \(x',y'\)。
\(\because \gcd(a,b)=\gcd(b,a\bmod b)\),
\(\therefore ax+by=bx'+(a\bmod b)\times y'\)。
\(\therefore ax+by=bx'+(a-\left\lfloor\frac{a}{b}\right\rfloor\times b)\times y'\)。
\(\therefore ax+by=ay'+b(x'-\left\lfloor\frac{a}{b}\right\rfloor\times y')\)。
- 若 \(a,b\) 互质,求 \(a\) 在模 \(b\) 意义下的逆元 \(x\),即求 \(ax\equiv 1\pmod b\) 的一个解。
解:原方程等价于 \(ax+by=1\),其中 \(y<0\)。
而 \(\gcd(a,b)=1\),所以 exgcd 可求。
- 求证:\(ax+by=c\) 有整数解 \(\Leftrightarrow \gcd(a,b) \mid c\)。
证明:
①充分性:设 \(d=\gcd(a,b)\),则 \(a=k_1d,b=k_2d\)。
\(\therefore c=ax+by=d(k_1x+k_2y)\)。
\(\therefore d\mid c\)。
②必要性:\(\because\) 由裴蜀定理,\(ax+by=d\) 有整数解。
\(\therefore a\times \frac{cx}{d}+b\times \frac{cy}{d}=c\)。
\(\because d\mid c\),\(\therefore \frac{cx}{d},\frac{cy}{d}\) 是整数。
- 若 \(ax+by=c\) 有解:
(1)求其的一组特解 \(x',y'\)。
解:由 exgcd 易求 \(ax+by=\gcd(a,b)\) 的一组解 \(x_0,y_0\)。
\(\because ax+by=c\) 有解,\(\therefore\gcd(a,b)\mid c\)。
\(\therefore a\times \frac{cx_0}{\gcd(a,b)}+b\times \frac{cy_0}{\gcd(a,b)}=c\)
\(\therefore\) 解得 \(\left\{\begin{matrix}x'= \frac{cx_0}{\gcd(a,b)}\\ y'=\frac{cy_0}{\gcd(a,b)}\end{matrix}\right.\)。
(2)求其的通解公式。
解:设 \(a(x+m)+b(y+n)=c\),化简得 \(am+bn=0\)。
设 \(d=\gcd(a,b)\)。易得 \(\left\{\begin{matrix}m=k\times \frac{b}{d}\\n=-k\times \frac{a}{d} \end{matrix}\right.\)。
(3)找出解的个数,判断是否有解满足 \(x>0,y>0\)。
解:即 \(x'+m>0,y'+n>0\)。
化简得 \(\left\lceil\frac{-dx'+1}{b}\right\rceil\le k\le \left\lfloor\frac{dy'-1}{a}\right\rfloor\)
解的个数即为 \(k\) 的个数。
若 \(k\) 存在,则有解满足上述条件。
(4)分别给出 \(x\) 和 \(y\) 最小的正整数值。
解:由一次函数,\(x\) 随着 \(k\) 增大而增大,\(y\) 随着 \(k\) 增大而减小。
四、中国剩余定理
求解以下方程组:
有如下方法:
-
令 \(mod=\prod b_i\),令 \(m_i=mod\div b_i\)。
-
求模 \(b_i\) 意义下的 \(m_i^{-1}\),记作 \(m'_i\)。
-
答案为 \((\sum a_im_im'_i)\bmod mod\)。
求证:以上方法的正确性。
证明:若 \(i\ne j\),则由构造方式,有 \(b_i\mid m_j\),即 \(m_j=0\pmod{b_i}\)。
\(\therefore a_jm_jm_j'=0\pmod{b_i}\)。
若 \(i=j\),则 \(m_im_i^{-1}=1\pmod{b_i}\)。
综上,\(x\equiv \sum a_jm_jm'_j\equiv a_i+\sum_{i\ne j}a_jm_jm'_j\equiv a_i\pmod{b_i}\)。