← 学习库 Discrete Mathematics (Levin) · 中英对照 目录

1.6 Advanced Counting Using PIE 容斥原理高级计数

本页译自 LibreTexts · Discrete Mathematics (Levin) 第 1.6 章。公式经 MathJax 渲染,自定义宏已注入。

Investigate! 探究!

You have 11 identical mini key-lime pies to give to 4 children. However, you don't want any kid to get more than 3 pies. How many ways can you distribute the pies?

你有 11 个相同的迷你酸橙派要分给 4 个孩子。但你不想让任何一个孩子拿到超过 3 个派。共有多少种分法?
  1. How many ways are there to distribute the pies without any restriction?
  2. Let's get rid of the ways that one or more kid gets too many pies. How many ways are there to distribute the pies if Al gets too many pies? What if Bruce gets too many? Or Cat? Or Dent?
  3. What if two kids get too many pies? How many ways can this happen? Does it matter which two kids you pick to overfeed?
  4. Is it possible that three kids get too many pies? If so, how many ways can this happen?
  5. How should you combine all the numbers you found above to answer the original question?
  1. 不加任何限制时,共有多少种分法?
  2. 我们要剔除「一个或多个孩子拿到过多派」的分法。若 Al 拿到过多派,共有多少种分法?若 Bruce 过多呢?Cat 呢?还是 Dent?
  3. 若两个孩子都拿到过多派呢?共有多少种这样的分法?具体选哪两个孩子喂过量,是否影响结果?
  4. 是否可能三个孩子都拿到过多派?如果可能,共有多少种分法?
  5. 应如何把上面算出的所有数字组合起来,以回答原问题?

Suppose now you have 13 pies and 7 children. No child can have more than 2 pies. How many ways can you distribute the pies?

现在假设你有 13 个派和 7 个孩子。每个孩子最多拿 2 个派。共有多少种分法?

Stars and bars allows us to count the number of ways to distribute 10 cookies to 3 kids and natural number solutions to \(x+y+z = 11\text{,}\) for example. A relatively easy modification allows us to put a lower bound restriction on these problems: perhaps each kid must get at least two cookies or \(x,y,z \ge 2\text{.}\) This was done by first assigning each kid (or variable) 2 cookies (or units) and then distributing the rest using stars and bars.

例如,隔板法可以数出把 10 块饼干分给 3 个孩子的分法数,以及方程 \(x+y+z = 11\text{,}\) 的自然数解个数。一个相对简单的改动就能给这类问题加上「下界」限制:比如每个孩子至少拿到 2 块饼干,即 \(x,y,z \ge 2\text{。}\) 做法是先给每个孩子(或每个变量)分配 2 块饼干(或 2 个单位),再用隔板法分配余下的部分。

What if we wanted an upper bound restriction? For example, we might insist that no kid gets more than 4 cookies or that \(x, y, z \le 4\text{.}\) It turns out this is considerably harder, but still possible. The idea is to count all the distributions and then remove those that violate the condition. In other words, we must count the number of ways to distribute 11 cookies to 3 kids in which one or more of the kids gets more than 4 cookies. For any particular kid, this is not a problem; we do this using stars and bars. But how to combine the number of ways for kid A, or B or C? We must use the PIE.

如果想要「上界」限制呢?例如,我们要求任何一个孩子拿到的饼干都不超过 4 块,即 \(x, y, z \le 4\text{。}\) 这要困难得多,但仍可解决。思路是先数出所有分配方案,再去掉那些违反条件的方案。换言之,我们必须数出「3 个孩子中有一个或多个拿到超过 4 块饼干」的分配方案数。对某一个具体的孩子来说,这不成问题——可以用隔板法解决。但如何把 kid A、B、C 各自的方案数合并起来?这里就必须用到容斥原理(PIE)。

The Principle of Inclusion/Exclusion (PIE) gives a method for finding the cardinality of the union of not necessarily disjoint sets. We saw in Subsection how this works with three sets. To find how many things are in one or more of the sets \(A\text{,}\) \(B\text{,}\) and \(C\text{,}\) we should just add up the number of things in each of these sets. However, if there is any overlap among the sets, those elements are counted multiple times. So we subtract the things in each intersection of a pair of sets. But doing this removes elements which are in all three sets once too often, so we need to add it back in. In terms of cardinality of sets, we have

容斥原理(Principle of Inclusion/Exclusion,PIE) 给出了一种求若干未必不相交集合之并的基数的方法。我们在某一小节中已经看到它在三个集合上的运作方式。要数出落在集合 \(A\text{、}\) \(B\text{、}\) \(C\) 中「一个或多个」里的元素个数,本应把这三个集合各自的元素个数相加。然而,若集合之间存在重叠,那些重叠元素就会被重复计数。于是我们再减去每一对集合交集里的元素。但这样做会把同时落在三个集合中的元素多减了一次,所以还要把它加回来。用集合的基数表示,即

\begin{equation*} \|A \cup B \cup C\| = \|A\| + \|B\| + \|C\| - \|A \cap B\| - \|A \cap C\| - \|B \cap C\| + \|A\cap B \cap C\|. \end{equation*}

Example 1

示例 1

Three kids, Alberto, Bernadette, and Carlos, decide to share 11 cookies. They wonder how many ways they could split the cookies up provided that none of them receive more than 4 cookies (someone receiving no cookies is for some reason acceptable to these kids).

三个孩子 Alberto、Bernadette 和 Carlos 决定分 11 块饼干。他们想知道,在「谁拿到的饼干都不超过 4 块」的前提下(不知为何,有人一块都没拿到他们也能接受),共有多少种分法。

Solution

解答

Without the “no more than 4” restriction, the answer would be \({13 \choose 2}\text{,}\) using 11 stars and 2 bars (separating the three kids). Now count the number of ways that one or more of the kids violates the condition, i.e., gets at least 4 cookies.

若没有「不超过 4 块」的限制,答案是 \({13 \choose 2}\text{,}\) 用 11 颗星和 2 根竖线(分隔三个孩子)即可得出。现在要数出「一个或多个孩子违反条件(即拿到至少 4 块饼干)」的方案数。

Let \(A\) be the set of outcomes in which Alberto gets more than 4 cookies. Let \(B\) be the set of outcomes in which Bernadette gets more than 4 cookies. Let \(C\) be the set of outcomes in which Carlos gets more than 4 cookies. We then are looking (for the sake of subtraction) for the size of the set \(A \cup B \cup C\text{.}\) Using PIE, we must find the sizes of \(\|A\|\text{,}\) \(\|B\|\text{,}\) \(\|C\|\text{,}\) \(\|A\cap B\|\) and so on. Here is what we find.

设 \(A\) 为「Alberto 拿到超过 4 块饼干」的结果集合,设 \(B\) 为「Bernadette 拿到超过 4 块饼干」的结果集合,设 \(C\) 为「Carlos 拿到超过 4 块饼干」的结果集合。那么(为了计算要减去的量)我们要求的是集合 \(A \cup B \cup C\) 的大小。由容斥原理,必须先求出 \(\|A\|\text{、}\) \(\|B\|\text{、}\) \(\|C\|\text{、}\) \(\|A\cap B\|\) 等的大小。结果如下。

Combining all of these we see

综合以上各项,我们得到

\begin{equation*} \|A \cup B \cup C\| = {8 \choose 2} + {8 \choose 2} + {8 \choose 2} - {3 \choose 2} - {3 \choose 2} - {3 \choose 2} + 0 = 75. \end{equation*}

Thus the answer to the original question is \({13 \choose 2} - 75 = 78 - 75 = 3\text{.}\) This makes sense now that we see it. The only way to ensure that no kid gets more than 4 cookies is to give two kids 4 cookies and one kid 3; there are three choices for which kid that should be. We could have found the answer much quicker through this observation, but the point of the example is to illustrate that PIE works!

因此原问题的答案是 \({13 \choose 2} - 75 = 78 - 75 = 3\text{。}\) 现在看来这很合理:要保证没有孩子拿到超过 4 块饼干,唯一的办法是给两个孩子各 4 块、给另一个孩子 3 块;而具体让哪个孩子拿 3 块有 3 种选择。凭这一观察我们本可以更快地得到答案,但这个例子的用意在于说明容斥原理确实有效!

For four or more sets, we do not write down a formula for PIE. Instead, we just think of the principle: add up all the elements in single sets, then subtract out things you counted twice (elements in the intersection of a pair of sets), then add back in elements you removed too often (elements in the intersection of groups of three sets), then take back out elements you added back in too often (elements in the intersection of groups of four sets), then add back in, take back out, add back in, etc. This would be very difficult if it wasn't for the fact that in these problems, all the cardinalities of the single sets are equal, as are all the cardinalities of the intersections of two sets, and that of three sets, and so on. Thus we can group all of these together and multiply by how many different combinations of 1, 2, 3, … sets there are.

对四个或更多集合,我们不写出容斥原理的公式,而是直接运用其思想:先把各个单独集合中的元素全部相加;再减去被重复计数的部分(即落在「两个集合之交」里的元素);再把被减掉过多的部分加回来(即落在「三个集合之交」里的元素);再把被加回过多的部分减掉(即落在「四个集合之交」里的元素);如此加加减减交替进行。若不是因为这类问题中「所有单独集合的基数都相等,所有两两交集的基数也都相等,三个集合交集的基数亦然……」,这样做会非常麻烦。因此我们可把同类项合并,再乘以「含 1 个、2 个、3 个……集合的组合」各有多少种。

Example 2

示例 2

How many ways can you distribute 10 cookies to 4 kids so that no kid gets more than 2 cookies?

把 10 块饼干分给 4 个孩子,要求每个孩子拿到的都不超过 2 块,共有多少种分法?

Solution

解答

There are \({13 \choose 3}\) ways to distribute 10 cookies to 4 kids (using 10 stars and 3 bars). We will subtract all the outcomes in which a kid gets 3 or more cookies. How many outcomes are there like that? We can force kid A to eat 3 or more cookies by giving him 3 cookies before we start. Doing so reduces the problem to one in which we have 7 cookies to give to 4 kids without any restrictions. In that case, we have 7 stars (the 7 remaining cookies) and 3 bars (one less than the number of kids) so we can distribute the cookies in \({10 \choose 3}\) ways. Of course we could choose any one of the 4 kids to give too many cookies, so it would appear that there are \({4 \choose 1}{10 \choose 3}\) ways to distribute the cookies giving too many to one kid. But in fact, we have over counted.

把 10 块饼干分给 4 个孩子共有 \({13 \choose 3}\) 种分法(用 10 颗星和 3 根竖线)。我们要从中减去「某个孩子拿到 3 块或更多饼干」的情形。这样的情形有多少?我们可以先强行给 kid A 3 块饼干,使他必然拿到 3 块以上。这就把问题化简为:把剩下的 7 块饼干无限制地分给 4 个孩子。此时有 7 颗星(余下的 7 块饼干)和 3 根竖线(比孩子数少 1),故分法数为 \({10 \choose 3}\)。当然,4 个孩子中任意一个都可能被多给,所以表面上「有一个孩子拿到过多饼干」的分法共有 \({4 \choose 1}{10 \choose 3}\) 种。但实际上我们重复计数了。

We must get rid of the outcomes in which two kids have too many cookies. There are \({4 \choose 2}\) ways to select 2 kids to give extra cookies. It takes 6 cookies to do this, leaving only 4 cookies. So we have 4 stars and still 3 bars. The remaining 4 cookies can thus be distributed in \({7 \choose 3}\) ways (for each of the \({4 \choose 2}\) choices of which 2 kids to over-feed).

我们还要去掉「两个孩子都拿到过多饼干」的情形。选出 2 个孩子多给饼干有 \({4 \choose 2}\) 种选法。这要先发出 6 块饼干,只剩 4 块。于是有 4 颗星和仍为 3 根竖线,剩下的 4 块饼干可分法为 \({7 \choose 3}\) 种(对应每一种选出 2 个孩子的 \({4 \choose 2}\) 种选法)。

But now we have removed too much. We must add back in all the ways to give too many cookies to three kids. This uses 9 cookies, leaving only 1 to distribute to the 4 kids using stars and bars, which can be done in \({4 \choose 3}\) ways. We must consider this outcome for every possible choice of which three kids we over-feed, and there are \({4 \choose 3}\) ways of selecting that set of 3 kids.

但现在我们减得过多了。必须把「三个孩子都拿到过多饼干」的情形全部加回来。这需要先发出 9 块饼干,只剩 1 块用隔板法分给 4 个孩子,分法有 \({4 \choose 3}\) 种。而具体选哪三个孩子喂过量,又有 \({4 \choose 3}\) 种选法,都必须考虑进去。

Next we would subtract all the ways to give four kids too many cookies, but in this case, that number is 0.

接下来本应减去「四个孩子都拿到过多饼干」的情形,但此时该数目为 0。

All together we get that the number of ways to distribute 10 cookies to 4 kids without giving any kid more than 2 cookies is:

综合起来,把 10 块饼干分给 4 个孩子且每个孩子都不超过 2 块的分法数为:

\begin{equation*} {13 \choose 3} - \left( {4 \choose 1}{10 \choose 3} - {4 \choose 2}{7 \choose 3} + {4\choose 3}{4\choose 3} \right) \end{equation*}

which is

\begin{equation*} 286 - (480 - 210 + 16) = 0. \end{equation*}

This makes sense: there is NO way to distribute 10 cookies to 4 kids and make sure that nobody gets more than 2. It is slightly surprising that

这很合理:要把 10 块饼干分给 4 个孩子且保证每人不超过 2 块,根本不可能。略微出人意料的是,

\begin{equation*} {13 \choose 3} = \left( {4 \choose 1}{10 \choose 3} - {4 \choose 2}{7 \choose 3} + {4\choose 3}{4\choose 3} \right) \end{equation*}

but since PIE works, this equality must hold.

但既然容斥原理成立,这个等式必然成立。

Just so you don't think that these problems always have easier solutions, consider the following example.

为了让大家不误以为这类问题总有更简单的做法,请看下面这个例子。

Example 3

示例 3

Earlier (Example 1.5.3) we counted the number of solutions to the equation

前面(例 1.5.3)我们数过方程

\begin{equation*} x_1 + x_2 + x_3 + x_4 + x_5 = 13 \end{equation*}

where \(x_i \ge 0\) for each \(x_i\text{.}\)

其中每个 \(x_i \ge 0\text{。}\)

How many of those solutions have \(0 \le x_i \le 3\) for each \(x_i\text{?}\)

在这些解中,有多少个满足对每个 \(x_i\) 都有 \(0 \le x_i \le 3\text{?}\)

Solution

解答

We must subtract off the number of solutions in which one or more of the variables has a value greater than 3. We will need to use PIE because counting the number of solutions for which each of the five variables separately are greater than 3 counts solutions multiple times. Here is what we get:

我们必须减去「一个或多个变量取值大于 3」的解的个数。这里要用到容斥原理,因为若分别数出五个变量各自大于 3 的解数,会把同一解重复计数。结果如下:

We also need to account for the fact that we could choose any of the five variables in the place of \(x_1\) above (so there will be \({5 \choose 1}\) outcomes like this), any pair of variables in the place of \(x_1\) and \(x_2\) (\({5 \choose 2}\) outcomes) and so on. It is because of this that the double counting occurs, so we need to use PIE. All together we have that the number of solutions with \(0 \le x_i \le 3\) is

还要考虑到:上面 \(x_1\) 的位置可以由五个变量中任意一个充当(于是这类情形有 \({5 \choose 1}\) 种),\(x_1\) 与 \(x_2\) 的位置也可由任意一对变量充当(有 \({5 \choose 2}\) 种),依此类推。正因如此才会出现重复计数,所以必须动用容斥原理。综合起来,满足 \(0 \le x_i \le 3\) 的解的个数为

\begin{equation*} {17 \choose 4} - \left( {5\choose 1}{13 \choose 4} - {5 \choose 2}{9 \choose 4} + {5 \choose 3}{5 \choose 4} \right) = 15. \end{equation*}

Counting Derangements 错位排列计数

Investigate! 探究!

For your senior prank, you decide to switch the nameplates on your favorite 5 professors' doors. So that none of them feel left out, you want to make sure that all of the nameplates end up on the wrong door. How many ways can this be accomplished?

毕业恶作剧时,你打算把你最喜爱的 5 位教授门上的姓名牌互换。为了让每位教授都不被冷落,你想确保所有的姓名牌最终都挂错了门。共有多少种互换方式?

The advanced use of PIE has applications beyond stars and bars. A derangement of \(n\) elements \(1,2,3,\ldots,n\) is a permutation in which no element is fixed. For example, there are \(6\) permutations of the three elements \(1,2,3\text{:}\)

容斥原理的进阶应用不限于隔板法。\(n\) 个元素 \(1,2,3,\ldots,n\) 的一个错位排列(derangement),是指其中没有任何元素保持在原位的排列。例如,三个元素 \(1,2,3\) 共有 \(6\) 个排列:

\begin{equation*} 123 \sim 132 \sim 213 \sim 231 \sim 312 \sim 321. \end{equation*}

but most of these have one or more elements fixed: \(123\) has all three elements fixed since all three elements are in their original positions, \(132\) has the first element fixed (1 is in its original first position), and so on. In fact, the only derangements of three elements are

但其中大多数都有一个或多个元素保持在原位:\(123\) 的三个元素全在原位,\(132\) 的第一个元素在原位(1 仍在它原来的第一位),等等。事实上,三个元素中唯一的错位排列只有

\begin{equation*} 231 \text{ and } 312. \end{equation*}

If we go up to 4 elements, there are 24 permutations (because we have 4 choices for the first element, 3 choices for the second, 2 choices for the third leaving only 1 choice for the last). How many of these are derangements? If you list out all 24 permutations and eliminate those which are not derangements, you will be left with just 9 derangements. Let's see how we can get that number using PIE.

若增加到 4 个元素,共有 24 个排列(因为第一位有 4 种选择,第二位 3 种,第三位 2 种,最后一位只剩 1 种)。其中有多少个是错位排列?如果把全部 24 个排列列出来,再剔除那些不是错位排列的,就只剩下 9 个错位排列。下面看看如何用容斥原理得到这个数。

Example 4

示例 4

How many derangements are there of 4 elements?

4 个元素的错位排列共有多少个?

Solution

解答

We count all permutations, and subtract those which are not derangements. There are \(4! = 24\) permutations of 4 elements. Now for a permutation to not be a derangement, at least one of the 4 elements must be fixed. There are \({4 \choose 1}\) choices for which single element we fix. Once fixed, we need to find a permutation of the other three elements. There are \(3!\) permutations on 3 elements. But now we have counted too many non-derangements, so we must subtract those permutations which fix two elements. There are \({4 \choose 2}\) choices for which two elements we fix, and then for each pair, \(2!\) permutations of the remaining elements. But this subtracts too many, so add back in permutations which fix 3 elements, all \({4 \choose 3}1!\) of them. Finally subtract the \({4 \choose 4}0!\) permutations (recall \(0! = 1\) which fix all four elements. All together we get that the number of derangements of 4 elements is:

我们先数出全部排列,再减去那些不是错位排列的。4 个元素共有 \(4! = 24\) 个排列。一个排列若不是错位排列,则 4 个元素中至少有一个被固定。固定单个元素有 \({4 \choose 1}\) 种选法。固定后,只需对剩下 3 个元素求排列,共 \(3!\) 种。但这时我们把「固定两个元素」的排列多减了,所以要减去固定两个元素的排列。选出两个固定元素有 \({4 \choose 2}\) 种,对每一对,剩下 2 个元素有 \(2!\) 种排列。然而这又减得过多,于是把固定 3 个元素的排列加回来,共 \({4 \choose 3}1!\) 种。最后再减去固定全部四个元素的 \({4 \choose 4}0!\) 个排列(注意 \(0! = 1\),即固定所有四个元素的情况)。综合起来,4 个元素的错位排列个数为:

\begin{equation*} 4! - \left( {4 \choose 1}3! - {4 \choose 2}2! + {4 \choose 3} 1! - {4 \choose 4}0! \right) = 24 - 15 = 9. \end{equation*}

Of course we can use a similar formula to count the derangements of any number of elements. However, the more elements we have, the longer the formula gets. Here is another example:

当然,我们也可以用类似的公式数出任意多个元素的错位排列数。只是元素越多,公式越长。再看一例:

Example 5

示例 5

Five gentlemen attend a party, leaving their hats at the door. At the end of the party, they hastily grab hats on their way out. How many different ways could this happen so that none of the gentlemen leave with their own hat?

五位绅士参加一场聚会,把帽子留在门口。聚会结束时,他们匆匆出门,随手抓起一顶帽子。要使没有一位绅士拿到自己的帽子,共有多少种可能的拿法?

Solution

解答

We are counting derangements on 5 elements. There are \(5!\) ways for the gentlemen to grab hats in any order—but many of these permutations will result in someone getting their own hat. So we subtract all the ways in which one or more of the men get their own hat. In other words, we subtract the non-derangements. Doing so requires PIE. Thus the answer is:

我们在数 5 个元素的错位排列。绅士们随意抓帽共有 \(5!\) 种顺序——但其中许多排列会导致有人拿到自己的帽子。因此要减去「一个或多个绅士拿到自己帽子」的情形,也就是减去非错位排列。这一步需要容斥原理。于是答案为:

\begin{equation*} 5! - \left( {5 \choose 1}4! - {5 \choose 2}3! + {5 \choose 3}2! - {5 \choose 4}1! + {5\choose 5}0! \right). \end{equation*}

Counting Functions 函数计数

Investigate! 探究!

We have seen throughout this chapter that many counting questions can be rephrased as questions about counting functions with certain properties. This is reasonable since many counting questions can be thought of as counting the number of ways to assign elements from one set to elements of another.

本章中我们一再看到,许多计数问题都可以重新表述为「具有某些性质的函数」的计数问题。这很自然,因为许多计数问题本质上就是在数「把一个集合的元素分配给另一个集合的元素」的方式数。

Example 6

示例 6

You decide to give away your video game collection so to better spend your time studying advance mathematics. How many ways can you do this, provided:

你决定把电子游戏收藏送人,以便把更多时间用于学习进阶数学。在满足以下条件时,各有几种送法:
  1. You want to distribute your 3 different PS4 games among 5 friends, so that no friend gets more than one game?
  2. You want to distribute your 8 different 3DS games among 5 friends?
  3. You want to distribute your 8 different SNES games among 5 friends, so that each friend gets at least one game?
  1. 把 3 款不同的 PS4 游戏分给 5 个朋友,每位朋友最多拿一款?
  2. 把 8 款不同的 3DS 游戏分给 5 个朋友?
  3. 把 8 款不同的 SNES 游戏分给 5 个朋友,要求每位朋友至少拿到一款?

In each case, model the counting question as a function counting question.

在每种情形下,都把该计数问题建模为函数计数问题。

Solution

解答

We must use the three games (call them 1, 2, 3) as the domain and the 5 friends (a,b,c,d,e) as the codomain (otherwise the function would not be defined for the whole domain when a friend didn't get any game). So how many functions are there with domain \(1,2,3\) and codomain \(a,b,c,d,e\text{?}\) The answer to this is \(5^3=125\text{,}\) since we can assign any of 5 elements to be the image of 1, any of 5 elements to be the image of 2 and any of 5 elements to be the image of 3.

必须把三款游戏(记为 1、2、3)作为定义域,把 5 个朋友(a,b,c,d,e)作为上域(否则当某个朋友没拿到游戏时,函数就无法对整个定义域都有定义)。那么定义域为 \(1,2,3\)、上域为 \(a,b,c,d,e\) 的函数有多少个?答案是 \(5^3=125\text{,}\) 因为 1 的像可以是 5 个元素中的任意一个,2 的像也是 5 个中的任意一个,3 的像同样如此。

But this is not the correct answer to our counting problem, because one of these functions is \(f= \begin{pmatrix}1 & 2 & 3 \\ a & a & a\end{pmatrix}\text{;}\) one friend can get more than one game. What we really need to do is count injective functions. This gives \(P(5,3) = 60\) functions, which is the answer to our counting question.

但这并不是我们计数问题的正确答案,因为这些函数中有一个是 \(f= \begin{pmatrix}1 & 2 & 3 \\ a & a & a\end{pmatrix}\text{:}\) 一个朋友可能拿到不止一款游戏。我们真正需要数的是单射函数。单射函数共有 \(P(5,3) = 60\) 个,这才是该计数问题的答案。

Again, we need to use the 8 games as the domain and the 5 friends as the codomain. We are counting all functions, so the number of ways to distribute the games is \(5^8\text{.}\)

同样地,要把 8 款游戏作为定义域、5 个朋友作为上域。我们在数所有函数,因此分发游戏的方式数为 \(5^8\text{。}\)

This question is harder. Use the games as the domain and friends as the codomain (otherwise an element of the domain would have more than one image, which is impossible). To ensure that every friend gets at least one game means that every element of the codomain is in the range. In other words, we are looking for surjective functions. How do you count those?

这个问题更难。同样把游戏作为定义域、朋友作为上域(否则定义域中的某个元素会有多个像,那是不可能的)。要保证每位朋友至少拿到一款游戏,就意味着上域中每个元素都在值域里。换言之,我们要数的是满射函数。该如何数呢?

In Example 1.1.5 we saw how to count all functions (using the multiplicative principle) and in Example 1.3.4 we learned how to count injective functions (using permutations). Surjective functions are not as easily counted (unless the size of the domain is smaller than the codomain, in which case there are none).

在例 1.1.5 中,我们用乘法原理数出了所有函数的个数;在例 1.3.4 中,我们学会了用排列数单射函数。满射函数则没那么容易计数(除非定义域比上域还小,那样满射函数数为 0)。

The idea is to count the functions which are not surjective, and then subtract that from the total number of functions. This works very well when the codomain has two elements in it:

思路是:先数出不是满射的函数,再从函数总数中减去它们。当上域只有两个元素时,这个方法非常顺手:

Example 7

示例 7

How many functions \(f: 1,2,3,4,5 \to a,b\) are surjective?

有多少个函数 \(f: 1,2,3,4,5 \to a,b\) 是满射?

Solution

解答

There are \(2^5\) functions all together, two choices for where to send each of the 5 elements of the domain. Now of these, the functions which are not surjective must exclude one or more elements of the codomain from the range. So first, consider functions for which \(a\) is not in the range. This can only happen one way: everything gets sent to \(b\text{.}\) Alternatively, we could exclude \(b\) from the range. Then everything gets sent to \(a\text{,}\) so there is only one function like this. These are the only ways in which a function could not be surjective (no function excludes both \(a\) and \(b\) from the range) so there are exactly \(2^5 - 2\) surjective functions.

函数总共有 \(2^5\) 个,因为定义域中 5 个元素各有 2 个去向可选。在这些函数中,非满射的函数必然把上域中的某一个或多个元素排除在值域之外。先考虑值域中不含 \(a\) 的函数,这只可能以一种方式发生:所有元素都被发往 \(b\text{。}\) 另一种可能是把 \(b\) 排除在值域外,那样所有元素都被发往 \(a\text{,}\) 这样的函数也只有 1 个。函数非满射的情形只有这两种(不存在同时把 \(a\) 和 \(b\) 都排除在值域外的函数),所以满射函数恰好有 \(2^5 - 2\) 个。

When there are three elements in the codomain, there are now three choices for a single element to exclude from the range. Additionally, we could pick pairs of two elements to exclude from the range, and we must make sure we don't over count these. It's PIE time!

当上域有三个元素时,可单独排除的元素有 3 种选择。此外,我们还可以成对地排除两个元素,且必须注意不要重复计数。又到了使用容斥原理的时候!

Example 8

示例 8

How many functions \(f: 1,2,3,4,5 \to a,b,c\) are surjective?

有多少个函数 \(f: 1,2,3,4,5 \to a,b,c\) 是满射?

Solution

解答

Again start with the total number of functions: \(3^5\) (as each of the five elements of the domain can go to any of three elements of the codomain). Now we count the functions which are not surjective.

还是先算函数总数:\(3^5\)(因为定义域中 5 个元素各自可发往上域中的 3 个元素之一)。现在我们来数非满射的函数。

Start by excluding \(a\) from the range. Then we have two choices (\(b\) or \(c\) for where to send each of the five elements of the domain. Thus there are \(2^5\) functions which exclude \(a\) from the range. Similarly, there are \(2^5\) functions which exclude \(b\text{,}\) and another \(2^5\) which exclude \(c\text{.}\) Now have we counted all functions which are not surjective? Yes, but in fact, we have counted some multiple times. For example, the function which sends everything to \(c\) was one of the \(2^5\) functions we counted when we excluded \(a\) from the range, and also one of the \(2^5\) functions we counted when we excluded \(b\) from the range. We must subtract out all the functions which specifically exclude two elements from the range. There is 1 function when we exclude \(a\) and \(b\) (everything goes to \(c\)), one function when we exclude \(a\) and \(c\text{,}\) and one function when we exclude \(b\) and \(c\text{.}\)

先排除值域中的 \(a\)。此时每个定义域元素只有两种选择(发往 \(b\) 或 \(c\)),所以排除 \(a\) 的函数有 \(2^5\) 个。同理,排除 \(b\) 的函数有 \(2^5\) 个,排除 \(c\) 的也有 \(2^5\) 个。这样我们是否数全了所有非满射函数?是的,但实际上我们重复计数了。例如,把所有元素都发往 \(c\) 的函数,既在我们排除 \(a\) 时数到的 \(2^5\) 个之中,也在我们排除 \(b\) 时数到的 \(2^5\) 个之中。所以必须把「恰好排除两个元素」的函数全部减去。排除 \(a\) 和 \(b\)(所有元素都发往 \(c\))的函数有 1 个,排除 \(a\) 和 \(c\) 的有 1 个,排除 \(b\) 和 \(c\) 的也有 1 个。

We are using PIE: to count the functions which are not surjective, we added up the functions which exclude \(a\text{,}\) \(b\text{,}\) and \(c\) separately, then subtracted the functions which exclude pairs of elements. We would then add back in the functions which exclude groups of three elements, except that there are no such functions. We find that the number of functions which are not surjective is

这里用的就是容斥原理:数非满射函数时,我们先把排除 \(a\text{、}\) \(b\text{、}\) \(c\) 的函数分别相加,再减去排除「两个元素组成的一对」的函数。接下来本应把排除「三个元素组成的一组」的函数加回来,但这样的函数并不存在。于是我们得到非满射函数的个数为

\begin{equation*} 2^5 + 2^5 + 2^5 - 1 - 1 - 1 + 0. \end{equation*}

Perhaps a more descriptive way to write this is

或许更具描述性的写法是

\begin{equation*} {3 \choose 1}2^5 - {3 \choose 2}1^5 + {3 \choose 3}0^5. \end{equation*}

since each of the \(2^5\)'s was the result of choosing 1 of the 3 elements of the codomain to exclude from the range, each of the three \(1^5\)'s was the result of choosing 2 of the 3 elements of the codomain to exclude. Writing \(1^5\) instead of 1 makes sense too: we have 1 choice of were to send each of the 5 elements of the domain.

因为每一个 \(2^5\) 都来自「从 3 个上域元素中选出 1 个排除在值域外」,而那三个 \(1^5\) 每一个都来自「选出 2 个上域元素排除」。把 1 写成 \(1^5\) 也同样合理:此时定义域中 5 个元素各自只有 1 种发派选择。

Now we can finally count the number of surjective functions:

现在我们终于可以数出满射函数的个数了:

\begin{equation*} 3^5 - \left( {3 \choose 1}2^5 - {3 \choose 2}1^5 \right) = 150. \end{equation*}

You might worry that to count surjective functions when the codomain is larger than 3 elements would be too tedious. We need to use PIE but with more than 3 sets the formula for PIE is very long. However, we have lucked out. As we saw in the example above, the number of functions which exclude a single element from the range is the same no matter which single element is excluded. Similarly, the number of functions which exclude a pair of elements will be the same for every pair. With larger codomains, we will see the same behavior with groups of 3, 4, and more elements excluded. So instead of adding/subtracting each of these, we can simply add or subtract all of them at once, if you know how many there are. This works just like it did in for the other types of counting questions in this section, only now the size of the various combinations of sets is a number raised to a power, as opposed to a binomial coefficient or factorial. Here's what happens with \(4\) and \(5\) elements in the codomain.

你也许会担心:当上域大于 3 个元素时,数满射函数会繁琐得令人却步。我们固然要用容斥原理,但超过 3 个集合时公式会非常长。不过我们很走运。如上例所示,无论排除哪一个单元素,被排除的单元素所对应的函数个数是相同的;同理,排除「一对元素」的函数个数对每一对也都相同。上域更大时,排除「三个、四个或更多元素组成的一组」也会呈现同样的规律。因此,只要知道各类组合各有多少种,我们就可以一次性地把它们全部加(或减)起来,而不必逐个处理。这与本节其他类型的计数问题做法一致,只是此刻各类集合组合的大小是「某数的若干次幂」,而非二项式系数或阶乘。下面看看上域有 \(4\) 个和 \(5\) 个元素时的情况。

Example 9

示例 9
  1. How many functions \(f: 1,2,3,4,5 \to a,b,c,d\) are surjective?
  2. How many functions \(f: 1,2,3,4,5 \to a,b,c,d,e\) are surjective?
  1. 有多少个函数 \(f: 1,2,3,4,5 \to a,b,c,d\) 是满射?
  2. 有多少个函数 \(f: 1,2,3,4,5 \to a,b,c,d,e\) 是满射?

Solution

解答

There are \(4^5\) functions all together; we will subtract the functions which are not surjective. We could exclude any one of the four elements of the codomain, and doing so will leave us with \(3^5\) functions for each excluded element. This counts too many so we subtract the functions which exclude two of the four elements of the codomain, each pair giving \(2^5\) functions. But this excludes too many, so we add back in the functions which exclude three of the four elements of the codomain, each triple giving \(1^5\) function. There are \({4 \choose 1}\) groups of functions excluding a single element, \({4 \choose 2}\) groups of functions excluding a pair of elements, and \({4 \choose 3}\) groups of functions excluding a triple of elements. This means that the number of functions which are not surjective is:

函数总共有 \(4^5\) 个;我们要减去非满射的函数。上域中四个元素任意一个都可以被排除,每排除一个元素,剩余函数有 \(3^5\) 个。这样会重复计数,于是再减去「排除上域中两个元素」的函数,每一对对应 \(2^5\) 个函数。但这又减得过多,所以把「排除上域中三个元素」的函数加回来,每一组三元对应 \(1^5\) 个函数。其中,排除单个元素的函数组有 \({4 \choose 1}\) 组,排除一对元素的有 \({4 \choose 2}\) 组,排除三元组的有 \({4 \choose 3}\) 组。因此非满射函数的个数为:

\begin{equation*} {4 \choose 1}3^5 - {4 \choose 2}2^5 + {4 \choose 3}1^5. \end{equation*}

We can now say that the number of functions which are surjective is:

于是满射函数的个数为:

\begin{equation*} 4^5 - \left( {4 \choose 1}3^5 - {4 \choose 2}2^5 + {4 \choose 3}1^5 \right). \end{equation*}

The number of surjective functions is:

满射函数的个数为:

\begin{equation*} 5^5 - \left( {5 \choose 1}4^5 - {5 \choose 2}3^5 + {5 \choose 3}2^5 - {5 \choose 4}1^5 \right). \end{equation*}

We took the total number of functions \(5^5\) and subtracted all that were not surjective. There were \({5 \choose 1}\) ways to select a single element from the codomain to exclude from the range, and for each there were \(4^5\) functions. But this double counts, so we use PIE and subtract functions excluding two elements from the range: there are \({5 \choose 2}\) choices for the two elements to exclude, and for each pair, \(3^5\) functions. This takes out too many functions, so we add back in functions which exclude 3 elements from the range: \({5 \choose 3}\) choices for which three to exclude, and then \(2^5\) functions for each choice of elements. Finally we take back out the 1 function which excludes 4 elements for each of the \({5 \choose 4}\) choices of 4 elements.

我们取函数总数 \(5^5\),再减去所有非满射的函数。从 上域选出单个元素排除在值域外,有 \({5 \choose 1}\) 种选法,每种对应 \(4^5\) 个函数。但这样会重复计数,于是用容斥原理减去「排除两个元素」的函数:选出两个排除元素有 \({5 \choose 2}\) 种,每一对对应 \(3^5\) 个函数。这又减得过多,所以把「排除 3 个元素」的函数加回来:选出三个排除元素有 \({5 \choose 3}\) 种,每种对应 \(2^5\) 个函数。最后,对 \({5 \choose 4}\) 种「选 4 个元素排除」的每种选法,再把那个排除全部 4 个元素的函数减回去。

If you happen to calculate this number precisely, you will get 120 surjections. That happens to also be the value of \(5!\text{.}\) This might seem like an amazing coincidence until you realize that every surjective function \(f:X \to Y\) with \(\left\|X\right\| = \left\|Y\right\|\) finite must necessarily be a bijection. The number of bijections is always \(\left\|X\right\|!\) in this case. What we have here is a combinatorial proof of the following identity:

若你恰好精确算出这个数,会得到 120 个满射。这恰好也等于 \(5!\text{。}\) 这看似惊人的巧合,直到你意识到:只要 \(f:X \to Y\) 是满射且 \(\left\|X\right\| = \left\|Y\right\|\) 为有限集,它就必然是双射。此情形下双射的个数恒为 \(\left\|X\right\|!\)。这其实给出了下面恒等式的一个组合证明:

\begin{equation*} n^n - \left( {n\choose 1}(n-1)^n - {n \choose 2}(n-2)^n + \cdots + {n \choose n-1}1^n \right) = n!. \end{equation*}

We have seen that counting surjective functions is another nice example of the advanced use of the Principle of Inclusion/Exclusion. Also, counting injective functions turns out to be equivalent to permutations, and counting all functions has a solution akin to those counting problems where order matters but repeats are allowed (like counting the number of words you can make from a given set of letters).

我们看到,计数满射函数是容斥原理进阶应用的又一个好例子。此外,计数单射函数实际上等价于排列;而计数所有函数,其解法与「顺序有关但允许重复」的计数问题(例如用给定字母集能组成多少个单词)如出一辙。

These are not just a few more examples of the techniques we have developed in this chapter. Quite the opposite: everything we have learned in this chapter are examples of counting functions!

这些并非只是本章所开发技巧的又几个例子。恰恰相反,本章学到的一切,都是「计数函数」的例子!

Example 10

示例 10

How many 5-letter words can you make using the eight letters \(a\) through \(h\text{?}\) How many contain no repeated letters?

用从 \(a\) 到 \(h\) 这 8 个字母,能组成多少个 5 字母单词?其中有多少个不含重复字母?

Solution

解答

By now it should be no surprise that there are \(8^5\) words, and \(P(8,5)\) words without repeated letters. The new piece here is that we are actually counting functions. For the first problem, we are counting all functions from \(1,2,\ldots, 5\) to \(a,b,\ldots, h\text{.}\) The numbers in the domain represent the position of the letter in the word, the codomain represents the letter that could be assigned to that position. If we ask for no repeated letters, we are asking for injective functions.

至此你应该不会惊讶:共有 \(8^5\) 个单词,不含重复字母的有 \(P(8,5)\) 个。这里的新意在于,我们实际上在数函数。第一个问题数的是所有从 \(1,2,\ldots, 5\) 到 \(a,b,\ldots, h\) 的函数。定义域中的数字表示字母在单词中的位置,上域表示可分配给该位置的字母。若要求不含重复字母,那就是在要求单射函数。

If \(A\) and \(B\) are any sets with \(\|A\| = 5\) and \(\|B\| = 8\text{,}\) then the number of functions \(f: A \to B\) is \(8^5\) and the number of injections is \(P(8,5)\text{.}\) So if you can represent your counting problem as a function counting problem, most of the work is done.

若 \(A\)、\(B\) 是任意满足 \(\|A\| = 5\)、\(\|B\| = 8\) 的集合,则函数 \(f: A \to B\) 的个数为 \(8^5\),单射个数为 \(P(8,5)\text{。}\) 所以,只要能把计数问题表示成函数计数问题,工作就完成了一大半。

Example 11

示例 11

How many subsets are there of \(1,2,\ldots, 9\text{?}\) How many 9-bit strings are there (of any weight)?

集合 \(1,2,\ldots, 9\) 有多少个子集?有多少个 9 位比特串(任意权重均可)?

Solution

解答

We saw in Section 1.2 that the answer to both these questions is \(2^9\text{,}\) as we can say yes or no (or 0 or 1) to each of the 9 elements in the set (positions in the bit-string). But \(2^9\) also looks like the answer you get from counting functions. In fact, if you count all functions \(f: A \to B\) with \(\|A\| = 9\) and \(\|B\| = 2\text{,}\) this is exactly what you get.

第 1.2 节 中我们看到,这两个问题的答案都是 \(2^9\text{,}\) 因为对集合中的 9 个元素(或比特串中的 9 个位置),我们都可以各自回答「是」或「否」(或 0 或 1)。而 \(2^9\) 看起来也像是计数函数得到的答案。事实上,若你数所有满足 \(\|A\| = 9\)、\(\|B\| = 2\) 的函数 \(f: A \to B\),得到的正是这个数。

This makes sense! Let \(A = 1,2,\ldots, 9\) and \(B = y, n\text{.}\) We are assigning each element of the set either a yes or a no. Or in the language of bit-strings, we would take the 9 positions in the bit string as our domain and the set \(0,1\) as the codomain.

这很合理!令 \(A = 1,2,\ldots, 9\),\(B = y, n\text{。}\) 我们是在给集合中的每个元素指派「是」或「否」。换用比特串的语言,就是把比特串中的 9 个位置作为定义域,把集合 \(0,1\) 作为上域。

So far we have not used a function as a model for binomial coefficients (combinations). Think for a moment about the relationship between combinations and permutations, say specifically \({9 \choose 3}\) and \(P(9,3)\text{.}\) We do have a function model for \(P(9,3)\text{.}\) This is the number of injective functions from a set of size 3 (say \(1,2,3\) to a set of size 9 (say \(1,2,\ldots, 9\) since there are 9 choices for where to send the first element of the domain, then only 8 choices for the second, and 7 choices for the third. For example, the function might look like this:

到目前为止,我们还没有用函数来为二项式系数(组合)建模。先想一想组合与排列的关系,具体而言就是 \({9 \choose 3}\) 与 \(P(9,3)\text{。}\) 对 \(P(9,3)\) 我们确实有一个函数模型:它是从大小为 3 的集合(比如 \(1,2,3\))到大小为 9 的集合(比如 \(1,2,\ldots, 9\))的单射函数个数,因为定义域第一个元素有 9 种去处,第二个只剩 8 种,第三个只剩 7 种。例如,这样的函数可能形如:

\begin{equation*} f(1) = 5 \qquad f(2) = 8 \qquad f(3) = 4. \end{equation*}

This is a different function from:

这与下面这个函数不同:

\begin{equation*} f(1) = 4 \qquad f(2) = 5 \qquad f(3) = 8. \end{equation*}

Now \(P(9,3)\) counts these as different outcomes correctly, but \({9\choose 3}\) will count these (among others) as just one outcome. In fact, in terms of functions \({9 \choose 3}\) just counts the number of different ranges possible of injective functions. This should not be a surprise since binomial coefficients counts subsets, and the range is a possible subset of the codomain. 4 A more mathematically sophisticated interpretation of combinations is that we are defining two injective functions to be equivalent if they have the same range, and then counting the number of equivalence classes under this notion of equivalence.

\(P(9,3)\) 正确地把这两个算作不同结果,但 \({9\choose 3}\) 会把它们(及其他)只算作一个结果。事实上,从函数的角度看,\({9 \choose 3}\) 数的是单射函数可能具有的不同的「值域」个数。这不奇怪,因为二项式系数数的是子集,而值域正是上域的一个可能子集。4 对组合还有一种更数学化的理解:我们规定两个单射函数若具有相同的值域,就视为等价,然后在这种等价意义下数等价类的个数。

While it is possible to interpret combinations as functions, perhaps the better advice is to instead use combinations (or stars and bars) when functions are not quite the right way to interpret the counting question.

尽管组合可以用函数来解释,但当函数并不是解释某个计数问题的恰当方式时,或许更好的建议是改用组合(或隔板法)。