# 容斥原理

容斥原理是一个计数公式，它将有限集合并集的确切大小表示为所有交集大小的交错和，从而修正集合重叠时产生的重复计数。它是枚举组合数学的基本工具之一，其美妙之处与其说在于恒等式本身，不如说在于它广泛适用于那些可以重新表述为“计数避开若干不想要条件的对象”的问题<sup>[1](https://www.cambridge.org/core/books/abs/enumerative-combinatorics/sieve-methods/C8C0E9E30042373DDA69C98476E09D54)</sup>。

| 关键事实 | 详情 |
|---|---|
| 两集合情形 | \( \|A \cup B\| = \|A\| + \|B\| - \|A \cap B\| \)；将两个大小相加会把重叠部分计算两次，因此减去一次<sup>[2](https://www.cip.ifi.lmu.de/~grinberg/t/19fco/n/n-temp.pdf)</sup> |
| 一般形式 | 对有限集 \( A_1, \ldots, A_n \)，\( \|A_1 \cup \cdots \cup A_n\| = \sum_{\emptyset \neq I \subseteq [n]} (-1)^{\|I\|-1} \|\bigcap_{i \in I} A_i\| \)，即对所有非空指标子集的交错和<sup>[2](https://www.cip.ifi.lmu.de/~grinberg/t/19fco/n/n-temp.pdf)</sup> |
| 补集形式 | 全集 \( U \) 中不属于任何集合 \( A_i \) 的元素个数为 \( \sum_{I \subseteq [n]} (-1)^{\|I\|} \|\bigcap_{i \in I} A_i\| \)，其中空交集取为 \( U \)<sup>[3](https://www.math.colostate.edu/~hulpke/lectures/m501/notes.html)</sup> |
| 错排 | \( n \) 个元素的无不动点排列数为 \( d(n) = n! \sum_{i=0}^{n} \frac{(-1)^i}{i!} \)<sup>[3](https://www.math.colostate.edu/~hulpke/lectures/m501/notes.html)</sup> |
| 邦费罗尼不等式 | 将交错和在奇数层后截断给出并集的上界，在偶数层后截断给出下界<sup>[4](https://math.iisc.ac.in/~gadgil/MA261/notes/chapter-8.html)</sup> |
| 历史归属 | 最早的表述在十九世纪独立出现于勒让德、达尼埃尔·奥古斯托·达席尔瓦、西尔维斯特和庞加莱的工作中；方法本身更早出现在机会博弈问题中<sup>[5](https://www.tandfonline.com/doi/abs/10.1080/26375451.2022.2082158)</sup> |
| 算法应用 | 与zeta变换结合，该原理为集合划分问题提供精确的 \( 2^n n^{O(1)} \) 时间算法，包括在 \( O(2.2461^n) \) 时间内计算色数<sup>[6](https://epubs.siam.org/doi/10.1137/070683933)</sup> |

## 原理的陈述

对两个有限集，公式是初等的：\( \|A_1 \cup A_2\| = \|A_1\| + \|A_2\| - \|A_1 \cap A_2\| \)。将两个大小相加会把交集中的每个元素计算两次，一次作为每个集合的元素，因此减去一次交集，使每个重叠元素恰好被计算一次<sup>[2](https://www.cip.ifi.lmu.de/~grinberg/t/19fco/n/n-temp.pdf)</sup>。对三个集合，同样的推理需要两次修正：减去三个两两交集，但这会把同时属于三个集合的元素减去三次，因此再加回三重交集<sup>[2](https://www.cip.ifi.lmu.de/~grinberg/t/19fco/n/n-temp.pdf)</sup>：

\[ \|A_1 \cup A_2 \cup A_3\| = \|A_1\| + \|A_2\| + \|A_3\| - \|A_1 \cap A_2\| - \|A_1 \cap A_3\| - \|A_2 \cap A_3\| + \|A_1 \cap A_2 \cap A_3\|. \]

**一般形式。** 对 \( n \) 个有限集 \( A_1, \ldots, A_n \)，交集以指标集 \( [n] = \{1, \ldots, n\} \) 的子集 \( I \) 为索引，符号由 \( I \) 的大小决定：\( \|I\| \) 为奇数的项带正号，\( \|I\| \) 为偶数的项带负号<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>：

\[ \|A_1 \cup A_2 \cup \cdots \cup A_n\| = \sum_{\emptyset \neq I \subseteq [n]} (-1)^{\|I\|-1} \left\| \bigcap_{i \in I} A_i \right\|. \]

按大小对指标子集分组，得到展开式的读法：\( \|I\| = 1 \) 的内层和加上所有单个集合的大小，\( \|I\| = 2 \) 时减去所有两两交集，\( \|I\| = 3 \) 时加上所有三重交集，依此类推直到 \( \|I\| = n \)<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。

**补集形式。** 在应用中，人们通常想要同时满足若干条件的对象，这等同于不属于任何违规集合 \( A_i \) 的对象，其中 \( A_i \) 包含不满足条件 \( i \) 的对象。这类对象的个数为<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>

\[ \|U \setminus (A_1 \cup \cdots \cup A_n)\| = \sum_{I \subseteq [n]} (-1)^{\|I\|} \left\| \bigcap_{i \in I} A_i \right\|, \]

其中空交集理解为 \( U \) 本身<sup>[2](https://www.cip.ifi.lmu.de/~grinberg/t/19fco/n/n-temp.pdf)</sup>。

## 为什么成立

最简洁的一般证明使用指示函数，也称特征函数。对一个包含所有 \( A_i \) 的有限全集，指示函数 \( \mathbf{1}_A \) 在 \( A \) 上等于1，在 \( A \) 外等于0。论证基于三个代数事实：集合的基数等于其指示函数在全集上的取值总和；交集的指示函数等于各指示函数的乘积，因为属于交集意味着属于其中每个集合；补集的指示函数为 \( 1 - \mathbf{1}_A \)<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。

一个元素不属于并集，恰好当它不属于其中任何集合，因此并集补集的指示函数为乘积 \( \prod_i (1 - \mathbf{1}_{A_i}) \)。展开此乘积，对每个指标子集 \( I \) 产生一项，即 \( (-1)^{\|I\|} \prod_{i \in I} \mathbf{1}_{A_i} \)，它是 \( (-1)^{\|I\|} \) 乘以 \( \bigcap_{i \in I} A_i \) 的指示函数。在全集上求和并利用基数是指示函数值之和这一事实，便得到原理的补集形式<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。从这个角度看，该恒等式是德摩根律的乘法表述，而基数作为线性泛函作用于一个多项式恒等式<sup>[8](https://physics2u.com/maths/vault/inclusion-exclusion.html)</sup>。

直接计数证明固定一个元素 \( x \)，检查它被计算了多少次。若 \( x \) 恰好属于其中 \( j \) 个集合，它对右端的总贡献为 \( \sum_{i=0}^{j} \binom{j}{i} (-1)^i = (1-1)^j \)，当 \( j \geq 1 \) 时为0，当 \( j = 0 \) 时为1；因此并集中的每个元素恰好被计算一次<sup>[3](https://www.math.colostate.edu/~hulpke/lectures/m501/notes.html)</sup>。

## 经典应用

**错排。** \( n \) 个对象的错排是无不动点的排列，以无人拿回自己帽子的帽检问题为例。取 \( A_i \) 为固定元素 \( i \) 的排列，固定 \( k \) 个指定元素的交集有 \( (n-k)! \) 个成员，这样的交集有 \( \binom{n}{k} \) 个。补集形式给出<sup>[3](https://www.math.colostate.edu/~hulpke/lectures/m501/notes.html)</sup>

\[ d(n) = \sum_{i=0}^{n} (-1)^i \binom{n}{i} (n-i)! = n! \sum_{i=0}^{n} \frac{(-1)^i}{i!}. \]

因此，均匀随机排列是错排的概率为 \( \sum_{i=0}^{n} (-1)^i / i! \)，随 \( n \) 增大迅速收敛到 \( e^{-1} \)；\( d(n) \) 是 \( n!/e \) 的最近整数<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。

**满射函数。** 从 \( m \) 元集到 \( n \) 元集的满射（映上）函数个数，可通过取 \( A_i \) 为像中遗漏值 \( i \) 的函数来计算。遗漏 \( k \) 个指定值剩下 \( (n-k)^m \) 个函数，因此计数为 \( \sum_{k=0}^{n} (-1)^k \binom{n}{k} (n-k)^m \)<sup>[3](https://www.math.colostate.edu/~hulpke/lectures/m501/notes.html)</sup>。等价地，这是 \( n \) 个符号的字母表上使用每个符号的长度为 \( m \) 的字符串个数<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。

**欧拉函数。** 对具有不同素因子 \( p_1, \ldots, p_r \) 的正整数 \( n \)，\( \{1, \ldots, n\} \) 中与 \( n \) 不互素的整数恰好是至少被一个 \( p_i \) 整除的那些。其中恰好有 \( n / \prod_{i \in I} p_i \) 个被子集 \( I \) 中每个素数整除，容斥给出乘积公式 \( \varphi(n) = n \prod_{i=1}^{r} (1 - 1/p_i) \)<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。

**有界整数解。** 隔板法计数线性方程的非负整数解，下界通过变量替换处理，但上界不能通过简单平移施加，因为一个解可能在一个坐标、两个坐标或更多坐标上违反上界。容斥原理提供了机制：每个被违反的上界定义一个违规集，补集形式组合所得计数<sup>[7](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)</sup>。

## 邦费罗尼不等式与截断

完整公式有 \( 2^n - 1 \) 项，很快变得不实用。邦费罗尼不等式描述截断交错和时会发生什么：在奇数层后停止给出并集的上界，在偶数层后停止给出下界<sup>[4](https://math.iisc.ac.in/~gadgil/MA261/notes/chapter-8.html)</sup>。其中第一个，\( \mathbf{P}(A_1 \cup \cdots \cup A_n) \leq \sum_i \mathbf{P}(A_i) \)，是联合界，无需假设事件是否互斥即成立<sup>[4](https://math.iisc.ac.in/~gadgil/MA261/notes/chapter-8.html)</sup>。第二层给出 \( \mathbf{P}(A) \geq \sum_k \mathbf{P}(A_k) - \sum_{k < \ell} \mathbf{P}(A_k \cap A_\ell) \)<sup>[4](https://math.iisc.ac.in/~gadgil/MA261/notes/chapter-8.html)</sup>。当精确计算不切实际时，这些界很有用；例如，\( n \) 次随机放置后 \( r \) 个瓮中至少一个为空的概率可由两层界夹住：\( r(1-1/r)^n - \binom{r}{2}(1-2/r)^n \leq \mathbf{P}(A) \leq r(1-1/r)^n \)<sup>[4](https://math.iisc.ac.in/~gadgil/MA261/notes/chapter-8.html)</sup>。

## 历史

两集合情形是古老的常识，但一般原理在十九世纪才成形。文献中最早的运用出现在十八世纪，与机会博弈问题相关；最早的表述在十九世纪才独立出现于多位作者的工作中，包括阿德里安-马里·勒让德、葡萄牙数学家达尼埃尔·奥古斯托·达席尔瓦、詹姆斯·约瑟夫·西尔维斯特和亨利·庞加莱<sup>[5](https://www.tandfonline.com/doi/abs/10.1080/26375451.2022.2082158)</sup>。公式的功劳通常归于达席尔瓦（1854）和西尔维斯特（1883）共同所有，偶尔称为达席尔瓦–西尔维斯特公式，但在特定形式下该技术早于他们：约1708–1713年，尼古拉·伯努利和皮埃尔·雷蒙·德·蒙莫尔在相遇问题中正是运用了这样的交错筛法来枚举错排，而勒让德1808年通过筛去小素数倍数来统计 \( x \) 以下素数，将同一原理用于数论。在概率论中，\( \mathbf{P}(\bigcup A_i) \) 的恒等式带有庞加莱的名字<sup>[8](https://physics2u.com/maths/vault/inclusion-exclusion.html)</sup>。

现代结构性的理解来自吉安-卡洛·罗塔，其1964年论文《论组合理论的基础I：莫比乌斯函数理论》将容斥统一为偏序集上的莫比乌斯反演，使莫比乌斯函数成为偏序集理论的基本不变量<sup>[9](https://doi.org/10.1007/978-94-009-7798-3_18)</sup>。抽象地说，该原理相当于计算某个矩阵的逆，这是线性代数的一个次要结果，其价值在于应用<sup>[1](https://www.cambridge.org/core/books/abs/enumerative-combinatorics/sieve-methods/C8C0E9E30042373DDA69C98476E09D54)</sup>。

## 算法应用

在精确算法中，容斥将子集上的计数问题转化为可用幂集上的动态规划求值的和。比约克伦德、胡斯费尔特和科伊维斯托展示了如何利用该原理结合zeta变换，在 \( 2^n n^{O(1)} \) 时间内将 \( n \) 个元素的集合划分为给定族中的 \( k \) 个子集，从而为支配数、色数、最大 \( k \)-割、装箱、列表着色和色多项式提供此复杂度的精确算法；色数在 \( O(2.2461^n) \) 时间内求解，支配数在 \( O(2.8718^n) \) 时间内求解<sup>[6](https://epubs.siam.org/doi/10.1137/070683933)</sup>。相关工作将该原理应用于网络可靠性，其中并集之交的简化容斥表达式将概率公式的一般双指数复杂度降至单指数复杂度<sup>[10](https://arxiv.org/abs/1610.08677)</sup>。

## References

1. [Sieve Methods, Chapter 2 of *Enumerative Combinatorics*, Cambridge University Press](https://www.cambridge.org/core/books/abs/enumerative-combinatorics/sieve-methods/C8C0E9E30042373DDA69C98476E09D54)
2. [Darij Grinberg, *Enumerative Combinatorics: class notes*, Section 2.9](https://www.cip.ifi.lmu.de/~grinberg/t/19fco/n/n-temp.pdf)
3. [Alexander Hulpke, *Combinatorics* lecture notes, Colorado State University, Chapter 3](https://www.math.colostate.edu/~hulpke/lectures/m501/notes.html)
4. [Manjunath Krishnapur, *Probability Models and Statistics* notes, Chapter 8, IISc](https://math.iisc.ac.in/~gadgil/MA261/notes/chapter-8.html)
5. [Ana Patrícia Martins and Teresa Sousa (2022). Formulations of the inclusion–exclusion principle from Legendre to Poincaré, with emphasis on Daniel Augusto da Silva. *British Journal for the History of Mathematics* 37(3)](https://www.tandfonline.com/doi/abs/10.1080/26375451.2022.2082158)
6. [Andreas Björklund, Thore Husfeldt, Mikko Koivisto (2009). Set Partitioning via Inclusion-Exclusion. *SIAM Journal on Computing*](https://epubs.siam.org/doi/10.1137/070683933)
7. [Combinatorics lecture notes, Fall 2026, Unit 5: Inclusion–exclusion and applications](https://guraltsev.github.io/combinatorics-fall2026/lecture-notes/unit-1-enumeration/05-inclusion-exclusion-and-applications.html)
8. [The inclusion–exclusion principle, maths2u](https://physics2u.com/maths/vault/inclusion-exclusion.html)
9. [The Möbius Function of a Partially Ordered Set, Springer Nature](https://doi.org/10.1007/978-94-009-7798-3_18)
10. [Simplification of inclusion-exclusion on intersections of unions with application to network systems reliability, arXiv:1610.08677](https://arxiv.org/abs/1610.08677)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Combinatorics › Enumerative combinatorics › Counting techniques and recurrences › Inclusion–exclusion principle

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/inclusion-exclusion-principle

English original: https://www.edgepedia.org/inclusion-exclusion-principle
