跳到正文

算法·理论

整除进阶(真正的数论)

std__int64_t约 9 分钟
原文

这是 2025年11月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

前言

本文章由五年级蒟蒻 tangtianyao0123 独立编写完成,难度偏高。

前置芝士——多元对称有序枚举

在接下来的学习中,你将多次碰到 1a1+1a2+⋯+1an=x\frac{1}{a_1} + \frac{1}{a_2} + \cdots + \frac{1}{a_n} = x 的问题,你必须知道怎样枚举整数解。

例题一

题目

求方程 1a+1b+1c=12\frac{1}{a} + \frac{1}{b} + \frac{1}{c} = \frac{1}{2} 的正整数解数量。

由于原式是对称式,那么不妨设 a≤b≤ca \le b \le c,易知 1a<12≤3a\frac{1}{a} < \frac{1}{2} \le \frac{3}{a},得 3≤a≤63 \le a \le 6,枚举 aa 得到 1b+1c=xy\frac{1}{b} + \frac{1}{c} = \frac{x}{y},两边同乘 ybcybc 后因式分解求解即可。

完整解答

不妨设

a≤b≤ca \le b \le c

易知

1a<12≤3a\frac{1}{a} < \frac{1}{2} \le \frac{3}{a}∴3≤a≤6\therefore 3 \le a \le 6

若 a=3a = 3

∴1b+1c=16\therefore \frac{1}{b} + \frac{1}{c} = \frac{1}{6}∴bc−6b−6c=0\therefore bc - 6b - 6c = 0∴(b−6)(c−6)=36\therefore (b - 6)(c - 6) = 36∴{a=3,3,3,3,3b=7,8,9,10,12c=42,24,18,15,12\therefore \begin{cases} a = 3, 3, 3, 3, 3 \\ b = 7, 8, 9, 10, 12 \\ c = 42, 24, 18, 15, 12 \end{cases}

若 a=4a = 4

∴1b+1c=14\therefore \frac{1}{b} + \frac{1}{c} = \frac{1}{4}∴bc−4b−4c=0\therefore bc - 4b - 4c = 0∴(b−4)(c−4)=16\therefore (b - 4)(c - 4) = 16∴{a=4,4,4b=5,6,8c=20,12,8\therefore \begin{cases} a = 4, 4, 4 \\ b = 5, 6, 8 \\ c = 20, 12, 8 \end{cases}

若 a=5a = 5

∴1b+1c=310\therefore \frac{1}{b} + \frac{1}{c} = \frac{3}{10}∴9bc−30b−30c=0\therefore 9bc - 30b - 30c = 0∴(3b−10)(3c−10)=100\therefore (3b - 10)(3c - 10) = 100∴{a=5b=5c=10\therefore \begin{cases} a = 5 \\ b = 5 \\ c = 10 \end{cases}

若 a=6a = 6

∴1b+1c=13\therefore \frac{1}{b} + \frac{1}{c} = \frac{1}{3}bc−3b−3c=0bc - 3b - 3c = 0∴(b−3)(c−3)=9\therefore (b - 3)(c - 3) = 9∴{a=6b=6c=6\therefore \begin{cases} a = 6 \\ b = 6 \\ c = 6 \end{cases}

综上,共 6×6+3×3+1=466 \times 6 + 3 \times 3 + 1 = 46 组。

例题二

当上题中的三个数变为四个数时,枚举量会增加不少;如果 12\frac{1}{2} 变成让你求的几个数,你还会这么有耐心的枚举吗?

题目

(20072007,白俄罗斯数学奥林匹克)求所有的正整数 nn,使它能整除它的不同的四个因数之和。

建议先阅读下个版块的定义与性质。
如果这四个因数为 a1,a2,a3,a4a_1, a_2, a_3, a_4,那么不方便体现四个因数与 nn 的关系,在枚举因数时消不掉 nn,所以设四个因数为 na1,na2,na3,na4\frac{n}{a_1}, \frac{n}{a_2}, \frac{n}{a_3}, \frac{n}{a_4} 在枚举时就消掉了 nn,所以问题就变成了 1a1+1a2+1a3+1a4=k∈Z+\frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + \frac{1}{a_4} = k \in \Z^{+}。简单放缩得 k=1,2k = 1, 2,然后逐一枚举即可。

完整解答

设 nn 的四个因数为 na1,na2,na3,na4\frac{n}{a_1}, \frac{n}{a_2}, \frac{n}{a_3}, \frac{n}{a_4}, 其中 a1,a2,a3,a4∈Z+,a1<a2<a3<a4a_1, a_2, a_3, a_4 \in \Z^{+}, a_1 < a_2 < a_3 < a_4,且 a1,a2,a3,a4∣na_1, a_2, a_3, a_4 \mid n。

∴[a1,a2,a3,a4]∣n\therefore [a_1, a_2, a_3, a_4] \mid n

由题意

n∣na1+na2+na3+na4n \mid \frac{n}{a_1} + \frac{n}{a_2} + \frac{n}{a_3} + \frac{n}{a_4}∴1a1+1a2+1a3+1a4∈Z+\therefore \frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + \frac{1}{a_4} \in \Z^{+}

设

1a1+1a2+1a3+1a4=k∈Z+\frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + \frac{1}{a_4} = k \in \Z^{+}1a1+1a2+1a3+1a4=k≤1+12+13+14<3\frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + \frac{1}{a_4} = k \le 1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} < 3∴k=1,2\therefore k = 1, 2

若 k=1k = 1

∴1a1+1a2+1a3+1a4=1\therefore \frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + \frac{1}{a_4} = 1∴1a1<1<4a1\therefore \frac{1}{a_1} < 1 < \frac{4}{a_1}∴a1=2,3\therefore a_1 = 2, 3

若 a1=2a_1 = 2 由例题一的解,6∣n6 \mid n 或 20∣n20 \mid n。


若 a1=3a_1 = 3

1a2+1a3+1a4=23\frac{1}{a_2} + \frac{1}{a_3} + \frac{1}{a_4} = \frac{2}{3}

(省略枚举过程,毕竟简单,跟上一题一样。) 解得 6∣n6 \mid n 或 20∣n20 \mid n。


若 k=2k = 2 若 a3≤4a_3 \le 4,则 LHS≤1+12+14+15<1=RHS\text{LHS} \le 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{5} < 1 = \text{RHS},矛盾。

∴a1=1,a2=2,a3=3,a4=6\therefore a_1 = 1, a_2 = 2, a_3 = 3, a_4 = 6∴6∣n\therefore 6 \mid n

综上,6∣n6 \mid n 或 20∣n20 \mid n。

定义与性质

定义

对于非零整数 aa,和整数 bb,若存在整数 qq,使得 b=aqb=aq,则称 a∣ba\mid b,读作 aa 整除 bb。

性质

传递性

若 a∣ba\mid b,b∣cb\mid c,则 a∣ca\mid c。

证明
∵a∣b,b∣c\because a\mid b,b\mid c∴∃p,q∈Z,b=ap,c=bq\therefore \exists p,q\in\Z,b=ap,c=bq∴c=bq=apq=a(pq),pq∈Z\therefore c=bq=apq=a(pq),pq\in\Z∴a∣c\therefore a\mid c

可加减性

若 a∣ba\mid b,a∣ca\mid c,则 a∣b±ca\mid b \pm c。

证明
∵a∣b,a∣c\because a\mid b,a\mid c∴∃p,q∈Z,b=ap,c=aq\therefore \exists p,q\in\Z,b=ap,c=aq∴p±c=ap±aq=a(p±q),p±q∈Z\therefore p\pm c=ap\pm aq=a(p\pm q),p\pm q\in\Z∴a∣b±c\therefore a\mid b\pm c

可乘性

若 a∣ba\mid b,c∣dc\mid d,则 ac∣bdac\mid bd。

证明
∵a∣b,c∣d\because a\mid b,c\mid d∴∃p,q∈Z,b=ap,d=cq\therefore \exists p,q\in\Z,b=ap,d=cq∴bd=(ac)(pq),pq∈Z\therefore bd=(ac)(pq),pq\in\Z∴ac∣bd\therefore ac\mid bd

线性性质

这里的线性不是线性 DP 求最长上升子序列

若 a∣ba\mid b,a∣ca\mid c,则 ∀x,y∈Z,a∣bx±cy\forall x,y\in\Z ,a\mid bx \pm cy。

证明
∵a∣b,1∣x\because a\mid b,1\mid x∴a∣bx\therefore a\mid bx

同理

a∣cya\mid cy∴a∣bx+cy\therefore a\mid bx+cy

可消性

若 ma∣mbma\mid mb,则 a∣ba\mid b

证明
∵ma∣mb\because ma\mid mb∴∃p∈Z,mb=map\therefore \exists p\in\Z,mb=mapb=apb=ap∴a∣b\therefore a\mid b