Discrete MathematicsPigeonhole, Inclusion-Exclusion and Generating Functions3 of 4
Pigeonhole, Inclusion-Exclusion and Generating Functions · 3

General Inclusion-Exclusion and Derangements

We already know how to count around two or three overlapping conditions, and now we want to count arrangements that avoid any number of forbidden rules at once.

1

Why Avoiding Rules Is Hard

Suppose four friends (A, B, C, and D) write their names on four slips of paper and draw from a hat for Secret Santa. We want to find how many ways they can draw names so that nobody pulls their own name.

Nobody draws their own name\text{Nobody draws their own name}
A\text{A}
B\text{B}
C\text{C}
D\text{D}
A\text{A}
B\text{B}
C\text{C}
D\text{D}
A\text{A}
C\text{C}
D\text{D}
C\text{C}
D\text{D}
A\text{A}
D\text{D}
A\text{A}
C\text{C}
C\text{C}
D\text{D}
A\text{A}
C\text{C}
A draws their own name\text{A draws their own name}
B\text{B}
C\text{C}
D\text{D}
A\text{A}
B\text{B}
C\text{C}
D\text{D}
A\text{A}
3!=6 ways\htmlClass{lc-blue}{3!} = \htmlClass{lc-blue}{6}\text{ ways}
Secret Santa and Why Avoiding Rules Is Hard
Nobody pulls their own name\text{Nobody pulls their own name}
A\text{A}
B\text{B}
C\text{C}
D\text{D}
A\text{A}
B\text{B}
C\text{C}
D\text{D}
Valid exchanges=?\htmlClass{lc-green}{\text{Valid exchanges}} = \enspace ?
2

General Inclusion-Exclusion

In the four-person Secret Santa puzzle, there are four overlapping bad sets (one for each person who draws their own name), and a problem with ten people has ten bad sets. When we have nn bad sets A1,A2,…,An\bad{A_1}, \bad{A_2}, \dots, \bad{A_n} inside a universe of NN total outcomes, we can no longer draw a Venn diagram to check every region by eye. We need the general alternating pattern for nn sets, and a way to be sure that any bad outcome cancels out to zero no matter how many rules it breaks.

To see why the alternating counts cancel for an outcome that breaks mm rules at once, we will use the Binomial Theorem, which expands (1+x)m(1 + \xsub{x})^m by choosing x\xsub{x} from kk of the mm factors at a time:

(1+x)m=(m0)+(m1)x+(m2)x2+(m3)x3+⋯+(mm)xm\displaystyle \begin{aligned} (1 + \xsub{x})^m &= \bad{\binom{m}{0}} + \bad{\binom{m}{1}}\xsub{x} + \bad{\binom{m}{2}}\xsub{x}^2 \\ &\quad + \bad{\binom{m}{3}}\xsub{x}^3 + \dots + \bad{\binom{m}{m}}\xsub{x}^m \end{aligned}
General Inclusion-Exclusion and Why It Works
NN
A1\htmlClass{lc-orange}{A_1}
A2\htmlClass{lc-orange}{A_2}
A3\htmlClass{lc-orange}{A_3}
An\htmlClass{lc-orange}{A_n}
Clean items=?\htmlClass{lc-green}{\text{Clean items}} = \enspace ?

At each step kk, we compute the overlap sum SkS_k by taking every group of kk bad sets, counting how many outcomes sit in all kk sets at the same time, and adding those counts together:

S1=∑i∣Ai∣,S2=∑i<j∣Ai∩Aj∣,S3=∑i<j<l∣Ai∩Aj∩Al∣,…,Sn=∣A1∩A2∩⋯∩An∣\displaystyle \begin{aligned} S_1 &= \sum_{i} |\bad{A_i}|, \\ S_2 &= \sum_{i < j} |\bad{A_i} \cap \bad{A_j}|, \\ S_3 &= \sum_{i < j < l} |\bad{A_i} \cap \bad{A_j} \cap \bad{A_l}|, \\ &\dots, \\ S_n &= |\bad{A_1} \cap \bad{A_2} \cap \dots \cap \bad{A_n}| \end{aligned}

Note that SkS_k is a sum of overlap sizes, not the number of distinct outcomes that break kk rules: an outcome that breaks mm rules gets counted (mk)\bad{\binom{m}{k}} separate times inside SkS_k, once for every group of kk sets chosen from the mm sets it belongs to.

Subtracting the odd steps and adding back the even steps leaves only the clean outcomes outside all nn bad sets:

Clean items=N⏟total−S1⏟singles+S2⏟pairs−S3⏟triples+…+(−1)nSn⏟all n sets\displaystyle \begin{aligned} \counted{\text{Clean items}} &= \underbrace{N}_{\text{total}} - \underbrace{S_1}_{\text{singles}} + \underbrace{S_2}_{\text{pairs}} \\ &\quad - \underbrace{S_3}_{\text{triples}} + \dots \\ &\quad + (-1)^n \underbrace{S_n}_{\text{all } n \text{ sets}} \end{aligned}

When a problem instead asks for the union (the outcomes that break at least one rule), we drop NN and flip the signs:

∣A1∪A2∪⋯∪An∣\displaystyle \bad{|A_1 \cup A_2 \cup \dots \cup A_n|} =S1−S2+S3−⋯+(−1)n−1Sn\displaystyle = S_1 - S_2 + S_3 - \dots + (-1)^{n-1} S_n

For an item that belongs to m≥1m \ge 1 of the nn bad sets, its count across the formula is (mk)\bad{\binom{m}{k}} at step kk and 00 past step mm, so its alternating counts always cancel to (1−1)m=0(1 \xsub{- 1})^m = \counted{0}:

An item in one or more bad sets has its counts across the total, the singles, pairs, triples, quads, and the five-set overlap cancel to 0, while a clean item is counted once.
3

Counting Derangements

Now we can finish the four-person Secret Santa problem and find a single formula for the number of derangements Dn\counted{D_n}, which are rearrangements of nn items where no item ends up in its original spot.

When we simplify the overlap sum SkS_k for nn people, we will rewrite the combination (nk)\bad{\binom{n}{k}} in terms of factorials so it cancels with the ways to shuffle the remaining people:

(nk)\displaystyle \bad{\binom{n}{k}} =n!k!(n−k)!\displaystyle = \frac{n!}{k!\free{(n - k)!}}
Counting Derangements
A\text{A}
B\text{B}
C\text{C}
D\text{D}
2424
A\htmlClass{lc-orange}{A}
B\htmlClass{lc-orange}{B}
C\htmlClass{lc-orange}{C}
D\htmlClass{lc-orange}{D}
D4=?\htmlClass{lc-green}{D_4} = \enspace ?
Text solution
Count all shuffles and pin kk people to their own namesBecause all four people are symmetric, each SkS_k chooses kk of the 44 people to get their own names in (4k)\bad{\binom{4}{k}} ways and shuffles the remaining 4−k\free{4 - k} names in (4−k)!\free{(4 - k)!} ways.
N=4!=24S1=(41)3!=4×6=24S2=(42)2!=6×2=12S3=(43)1!=4×1=4S4=(44)0!=1×1=1\displaystyle \begin{aligned} N &= 4! = 24 \\ S_1 &= \bad{\binom{4}{1}} \free{3!} = \bad{4} \times \free{6} = 24 \\ S_2 &= \bad{\binom{4}{2}} \free{2!} = \bad{6} \times \free{2} = 12 \\ S_3 &= \bad{\binom{4}{3}} \free{1!} = \bad{4} \times \free{1} = 4 \\ S_4 &= \bad{\binom{4}{4}} \free{0!} = \bad{1} \times \free{1} = 1 \end{aligned}
Take the alternating sumSubtract the singles, add the pairs, subtract the triples, and add the four-way overlap.
D4=N−S1+S2−S3+S4=24−24+12−4+1=9\displaystyle \begin{aligned} \counted{D_4} &= N - S_1 + S_2 - S_3 + S_4 \\ &= 24 - 24 + 12 - 4 + 1 = \counted{9} \end{aligned}

So out of 2424 possible drawings, 9\counted{9} are derangements where nobody pulls their own name, and the remaining 24−9=1524 - \counted{9} = \bad{15} drawings have at least one person pull their own name.

For any number of items nn, pinning kk chosen items to their original spots and shuffling the other n−k\free{n - k} items cancels (n−k)!\free{(n - k)!} at every step:

Sk\displaystyle S_k =(nk)(n−k)!\displaystyle = \bad{\binom{n}{k}}\free{(n - k)!} =n!k!(n−k)!(n−k)!\displaystyle = \frac{n!}{k!\cancel{\free{(n - k)!}}}\cancel{\free{(n - k)!}} =n!k!\displaystyle = \frac{n!}{k!}

Factoring n!n! out of the alternating sum gives the general derangement formula:

Dn⏟derangements=n!(1−11!+12!−13!+14!−⋯+(−1)n1n!)\displaystyle \begin{aligned} \underbrace{\counted{D_n}}_{\text{derangements}} &= n!\Bigg(1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} \\ &\qquad + \frac{1}{4!} - \dots + (-1)^n \frac{1}{n!}\Bigg) \end{aligned}

Because the alternating sum inside the parentheses gets close to 1e≈0.368\counted{\frac{1}{e}} \approx 0.368 very quickly (it is already 0.3750.375 at n=4n = 4 and about 0.3670.367 at n=5n = 5), about 36.8%\counted{36.8\%} of all shuffles are derangements once nn is 55 or more. In fact Dn\counted{D_n} is always the nearest whole number to n!e\tfrac{n!}{e}, but in practice you should write out the alternating terms.

4

Asymmetric Rules and Forbidden Blocks

In Secret Santa, every person was symmetric, so all (nk)\bad{\binom{n}{k}} overlaps at step kk had the exact same size. Now suppose we arrange the five letters A, B, C, D, E in a row and want to avoid four different rules:

Here the bad sets have different sizes, and combining rules can leave blocks separate, fuse them into a longer chain, or contradict each other completely.

A2∩A4\htmlClass{lc-orange}{A_2} \cap \htmlClass{lc-orange}{A_4}
2!=2 ways\htmlClass{lc-blue}{2!} = 2\text{ ways}
11
22
33
44
55
D\text{D}
E\text{E}
A\text{A}
B\text{B}
C\text{C}
A2∩A3\htmlClass{lc-orange}{A_2} \cap \htmlClass{lc-orange}{A_3}
3!=6 ways\htmlClass{lc-blue}{3!} = 6\text{ ways}
11
22
33
44
55
B\text{B}
C\text{C}
D\text{D}
A\text{A}
E\text{E}
A1∩A4\htmlClass{lc-orange}{A_1} \cap \htmlClass{lc-orange}{A_4}
Impossible: 0 ways\text{Impossible: }\htmlClass{lc-orange}{0}\text{ ways}
11
22
33
44
55
D\text{D}
E\text{E}
A\text{A}
Asymmetric Rules and Forbidden Blocks
A\text{A}
B\text{B}
C\text{C}
D\text{D}
E\text{E}
11
22
33
44
55
A1:\htmlClass{lc-orange}{A_1}:
A\text{A}
A2:\htmlClass{lc-orange}{A_2}:
B\text{B}
C\text{C}
A3:\htmlClass{lc-orange}{A_3}:
C\text{C}
D\text{D}
A4:\htmlClass{lc-orange}{A_4}:
D\text{D}
E\text{E}
A\text{A}
Clean arrangements=?\htmlClass{lc-green}{\text{Clean arrangements}} = \enspace ?
Text solution
Pin each single rule and count the free objectsFix A in spot 1 or glue each forbidden block into a single tile before shuffling the remaining objects.
N=5!=120S1=4!⏟A1: A first+4!⏟A2: [BC], A, D, E+4!⏟A3: [CD], A, B, E+3!⏟A4: [DEA], B, C=24+24+24+6=78\displaystyle \begin{aligned} N &= 5! = 120 \\ S_1 &= \underbrace{\free{4!}}_{\bad{A_1}:\text{ A first}} + \underbrace{\free{4!}}_{\bad{A_2}:\text{ [BC], A, D, E}} \\ &\quad + \underbrace{\free{4!}}_{\bad{A_3}:\text{ [CD], A, B, E}} + \underbrace{\free{3!}}_{\bad{A_4}:\text{ [DEA], B, C}} \\ &= 24 + 24 + 24 + 6 = 78 \end{aligned}
Inspect each pair of rules one by oneCheck all (42)=6\bad{\binom{4}{2}} = \bad{6} pairs to see whether the two rules stay separate, fuse at a shared letter, or contradict each other.
S2=3!⏟A1∩A2: A first, [BC], D, E+3!⏟A1∩A3: A first, [CD], B, E+0⏟A1∩A4: impossible+3!⏟A2∩A3: [BCD], A, E+2!⏟A2∩A4: [BC], [DEA]+2!⏟A3∩A4: [CDEA], B=6+6+0+6+2+2=22\displaystyle \begin{aligned} S_2 &= \underbrace{\free{3!}}_{\bad{A_1} \cap \bad{A_2}:\text{ A first, [BC], D, E}} \\ &\quad + \underbrace{\free{3!}}_{\bad{A_1} \cap \bad{A_3}:\text{ A first, [CD], B, E}} \\ &\quad + \underbrace{\bad{0}}_{\bad{A_1} \cap \bad{A_4}:\text{ impossible}} \\ &\quad + \underbrace{\free{3!}}_{\bad{A_2} \cap \bad{A_3}:\text{ [BCD], A, E}} \\ &\quad + \underbrace{\free{2!}}_{\bad{A_2} \cap \bad{A_4}:\text{ [BC], [DEA]}} \\ &\quad + \underbrace{\free{2!}}_{\bad{A_3} \cap \bad{A_4}:\text{ [CDEA], B}} \\ &= 6 + 6 + 0 + 6 + 2 + 2 = 22 \end{aligned}
Inspect the triples and the four-way overlapAny combination containing both A1\bad{A_1} and A4\bad{A_4} has 0\bad{0} ways, while compatible blocks fuse further.
S3=2!⏟A1∩A2∩A3: A first, [BCD], E+0⏟A1∩A2∩A4+0⏟A1∩A3∩A4+1!⏟A2∩A3∩A4: [BCDEA]=2+0+0+1=3S4=0⏟A1∩A2∩A3∩A4\displaystyle \begin{aligned} S_3 &= \underbrace{\free{2!}}_{\bad{A_1} \cap \bad{A_2} \cap \bad{A_3}:\text{ A first, [BCD], E}} \\ &\quad + \underbrace{\bad{0}}_{\bad{A_1} \cap \bad{A_2} \cap \bad{A_4}} \\ &\quad + \underbrace{\bad{0}}_{\bad{A_1} \cap \bad{A_3} \cap \bad{A_4}} \\ &\quad + \underbrace{\free{1!}}_{\bad{A_2} \cap \bad{A_3} \cap \bad{A_4}:\text{ [BCDEA]}} \\ &= 2 + 0 + 0 + 1 = 3 \\ S_4 &= \underbrace{\bad{0}}_{\bad{A_1} \cap \bad{A_2} \cap \bad{A_3} \cap \bad{A_4}} \end{aligned}
Take the alternating sumCombine the overlap sums to count the arrangements that break none of the four rules.
N−S1+S2−S3+S4\displaystyle N - S_1 + S_2 - S_3 + S_4 =120−78+22−3+0\displaystyle = 120 - 78 + 22 - 3 + 0 =61\displaystyle = \counted{61}

So 61\counted{61} of the 120120 arrangements of A, B, C, D, E avoid all four rules.

See how each pair and triple of rules interacts:

Combining different rules can leave blocks separate, join shared letters into a single chain, or make an arrangement impossible.
5

Onto Functions and Non-Empty Boxes

Our last problem looks different on the surface, but brings back the clean symmetry of (nk)\bad{\binom{n}{k}}. We want to find how many ways to assign 66 distinct tasks to 44 coworkers so that every coworker receives at least one task. Assigning mm distinct objects to nn distinct boxes with no box left empty is the exact same problem as counting onto functions from a domain of mm inputs to a codomain of nn outputs.

Onto Functions and Non-Empty Boxes
f ⁣:{1,2,3,4,5,6}→{A,B,C,D}\htmlClass{lc-violet}{f}\colon \{1, 2, 3, 4, 5, 6\} \to \{\text{A}, \text{B}, \text{C}, \text{D}\}
11
22
33
44
55
66
A\text{A}
B\text{B}
C\text{C}
D\text{D}
tasks\text{tasks}
coworkers\text{coworkers}
Onto functions=?\htmlClass{lc-green}{\text{Onto functions}} = \enspace ?
Text solution
Define a bad set for each empty box and seal kk boxes shutWith 44 coworkers as boxes and 66 tasks as objects, sealing kk chosen boxes leaves 4−k\free{4 - k} open boxes for each of the 66 tasks.
N=46=4096S1=(41)36=4×729=2916S2=(42)26=6×64=384S3=(43)16=4×1=4S4=(44)06=0\displaystyle \begin{aligned} N &= 4^6 = 4096 \\ S_1 &= \bad{\binom{4}{1}} \free{3^6} = \bad{4} \times \free{729} = 2916 \\ S_2 &= \bad{\binom{4}{2}} \free{2^6} = \bad{6} \times \free{64} = 384 \\ S_3 &= \bad{\binom{4}{3}} \free{1^6} = \bad{4} \times \free{1} = 4 \\ S_4 &= \bad{\binom{4}{4}} \free{0^6} = 0 \end{aligned}
Take the alternating sumSubtract the single empty boxes, add back the pairs, and subtract the triples.
Onto functions=46−(41)36+(42)26−(43)16=4096−2916+384−4=1560\displaystyle \begin{aligned} &\counted{\text{Onto functions}} \\ &= 4^6 - \bad{\binom{4}{1}}\free{3^6} + \bad{\binom{4}{2}}\free{2^6} - \bad{\binom{4}{3}}\free{1^6} \\ &= 4096 - 2916 + 384 - 4 = \counted{1560} \end{aligned}

So out of 40964096 total functions, 1560\counted{1560} are onto functions where every coworker gets at least one task.

In general, when distributing mm distinct inputs (objects) into nn distinct outputs (boxes) with m≥nm \ge n, sealing kk of the nn boxes leaves (n−k)m\free{(n - k)^m} ways to place the objects:

Onto functions=∑k=0n(−1)k(nk)(n−k)m=nm⏟all n open−(n1)(n−1)m⏟1 box sealed+(n2)(n−2)m⏟2 boxes sealed−…+(−1)n−1(nn−1)1m⏟n−1 boxes sealed\displaystyle \begin{aligned} &\counted{\text{Onto functions}} \\ &= \sum_{k=0}^{n} (-1)^k \bad{\binom{n}{k}} \free{(n - k)^m} \\ &= \underbrace{n^m}_{\text{all } n \text{ open}} - \underbrace{\bad{\binom{n}{1}}\free{(n - 1)^m}}_{1 \text{ box sealed}} \\ &\quad + \underbrace{\bad{\binom{n}{2}}\free{(n - 2)^m}}_{2 \text{ boxes sealed}} - \dots \\ &\quad + (-1)^{n-1}\underbrace{\bad{\binom{n}{n - 1}}\free{1^m}}_{n - 1 \text{ boxes sealed}} \end{aligned}

Key concepts

Use General Inclusion-Exclusion whenever a problem asks you to avoid multiple rules at once, such as "nobody gets their own name," "none of these blocks appear," or "no box is left empty." If a rule is required rather than avoided (for instance, "A must be in the first spot" or "Task 1 must go to coworker A"), do not create a bad set for it; just lock that requirement in place from the very beginning when you count NN and every SkS_k.

Once you flip the forbidden conditions into nn bad sets A1,…,An\bad{A_1}, \dots, \bad{A_n}, the whole problem comes down to finding the overlap sums SkS_k by pinning kk rules in place and counting the remaining choices:

Symmetric fixed points (Derangements)

When nn items cannot land in their own spots, pinning kk items leaves (n−k)!\free{(n - k)!} shuffles for the rest, so Sk=(nk)(n−k)!=n!k!S_k = \bad{\binom{n}{k}}\free{(n - k)!} = \frac{n!}{k!} and

Dn=n!(1−11!+12!−13!+⋯+(−1)n1n!)\displaystyle \begin{aligned}\counted{D_n} &= n!\Bigg(1 - \frac{1}{1!} + \frac{1}{2!} \\ &\qquad - \frac{1}{3!} + \dots + (-1)^n \frac{1}{n!}\Bigg)\end{aligned}
For 44 people, D4=24(1−1+12−16+124)=9\counted{D_4} = 24\left(1 - 1 + \frac{1}{2} - \frac{1}{6} + \frac{1}{24}\right) = \counted{9}. If only rr of the nn people are forbidden from drawing their own names, there are only rr bad sets, so you choose the pinned people from those rr and still shuffle all n−k\free{n - k} remaining people: Sk=(rk)(n−k)!S_k = \bad{\binom{r}{k}}\free{(n - k)!}.

Symmetric empty boxes (Onto functions)

When mm distinct objects go into nn distinct boxes (m≥nm \ge n) with none empty, sealing kk boxes leaves n−k\free{n - k} choices for each of the mm objects, so Sk=(nk)(n−k)mS_k = \bad{\binom{n}{k}}\free{(n - k)^m} and

Onto functions=∑k=0n(−1)k(nk)(n−k)m\displaystyle \begin{aligned}&\counted{\text{Onto functions}} \\ &= \sum_{k=0}^{n} (-1)^k \bad{\binom{n}{k}} \free{(n - k)^m}\end{aligned}
For 66 tasks and 44 coworkers, 46−(41)36+(42)26−(43)16=15604^6 - \bad{\binom{4}{1}}\free{3^6} + \bad{\binom{4}{2}}\free{2^6} - \bad{\binom{4}{3}}\free{1^6} = \counted{1560}.

Asymmetric rules (Forbidden blocks and positions)

When the bad sets have different sizes or interact differently, do not multiply by (nk)\bad{\binom{n}{k}}. List each pair and triple, fuse blocks that share letters (like [BC]\bad{\text{[BC]}} and [CD]\bad{\text{[CD]}} becoming [BCD]\bad{\text{[BCD]}}), and write 0\bad{0} whenever two rules contradict each other (like A being first while also inside [DEA]\bad{\text{[DEA]}}).

In every case, finish with N−S1+S2−S3+⋯+(−1)nSnN - S_1 + S_2 - S_3 + \dots + (-1)^n S_n to count the clean outcomes, or S1−S2+S3−⋯+(−1)n−1SnS_1 - S_2 + S_3 - \dots + (-1)^{n-1} S_n (which is N−Clean itemsN - \counted{\text{Clean items}}) to count the outcomes that break at least one rule.

6

Practice

1

Five letters are placed at random into five addressed envelopes, one per envelope. In how many ways does no letter go into its own envelope, and in how many ways does at least one letter go into its own envelope?

No letter in its own envelope is a derangement of n=5n = 5 items; once you have D5D_5, how do the remaining shuffles out of 5!5! relate to the second question?

For no letter in its own envelope, use the derangement formula with n=5n = 5:
D5=5!(1−11!+12!−13!+14!−15!)=120−120+60−20+5−1=44\displaystyle \begin{aligned} \counted{D_5} &= 5!\Bigg(1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} \\ &\qquad + \frac{1}{4!} - \frac{1}{5!}\Bigg) \\ &= 120 - 120 + 60 - 20 + 5 - 1 \\ &= \counted{44} \end{aligned}
(Watch out: don't stop at the 4!4! term from the four-person example. Forgetting the final −5!5!=−1-\frac{5!}{5!} = -1 term gives 45\wrong{45} instead of 44\counted{44}.)
Subtracting the 44\counted{44} derangements from the 5!=1205! = 120 total shuffles gives the ways where at least one letter goes into its own envelope:
120−44\displaystyle 120 - \counted{44} =76\displaystyle = \bad{76}
44\counted{44} ways where no letter goes into its own envelope, and 76\bad{76} ways where at least one letter goes into its own envelope.
2

How many onto functions are there from {1,2,3,4,5}\{1, 2, 3, 4, 5\} to {a,b,c,d}\{a, b, c, d\}?

Treat the m=5m = 5 inputs as distinct tasks and the n=4n = 4 outputs as distinct boxes: if you seal kk boxes shut, how many open boxes can each of the 55 inputs choose from?

With m=5m = 5 inputs and n=4n = 4 outputs, define 44 bad sets for the 44 outputs that could be missed, and seal kk outputs at step kk:
Onto functions=45−(41)35+(42)25−(43)15=1024−4(243)+6(32)−4(1)=1024−972+192−4=240\displaystyle \begin{aligned} &\counted{\text{Onto functions}} \\ &= 4^5 - \bad{\binom{4}{1}}\free{3^5} + \bad{\binom{4}{2}}\free{2^5} - \bad{\binom{4}{3}}\free{1^5} \\ &= 1024 - \bad{4}(\free{243}) + \bad{6}(\free{32}) - \bad{4}(\free{1}) \\ &= 1024 - 972 + 192 - 4 = \counted{240} \end{aligned}
(Watch out: each of the 55 inputs chooses among the open boxes, so the powers are (4−k)5\free{(4 - k)^5}, not (5−k)4\wrong{(5 - k)^4}; also remember to multiply each power by (4k)\bad{\binom{4}{k}} for the ways to choose which kk boxes are empty. Omitting (4k)\bad{\binom{4}{k}} gives 1024−243+32−1=8121024 - 243 + 32 - 1 = \wrong{812}.)
240\counted{240}
3

Six coworkers (four designers and two interns) put all six of their names into a hat and each draw one name at random. In how many ways can they draw names so that none of the four designers draws their own name (whether or not the interns draw their own names)?

Only the four designers have a forbidden outcome, so how many bad sets AiA_i are there, and when kk designers are pinned to their own names, how many remaining people shuffle freely?

Define 44 bad sets, one for each of the 44 designers drawing their own name. When kk chosen designers are pinned to their own names in (4k)\bad{\binom{4}{k}} ways, all 6−k\free{6 - k} remaining coworkers (including both interns) shuffle the remaining names in (6−k)!\free{(6 - k)!} ways:
N−S1+S2−S3+S4=6!−(41)5!+(42)4!−(43)3!+(44)2!=720−4(120)+6(24)−4(6)+1(2)=720−480+144−24+2=362\displaystyle \begin{aligned} &N - S_1 + S_2 - S_3 + S_4 \\ &= 6! - \bad{\binom{4}{1}}\free{5!} + \bad{\binom{4}{2}}\free{4!} \\ &\quad - \bad{\binom{4}{3}}\free{3!} + \bad{\binom{4}{4}}\free{2!} \\ &= 720 - \bad{4}(\free{120}) + \bad{6}(\free{24}) \\ &\quad - \bad{4}(\free{6}) + \bad{1}(\free{2}) \\ &= 720 - 480 + 144 - 24 + 2 = \counted{362} \end{aligned}
(Watch out: don't use D6=265\wrong{D_6 = 265}, which also forbids the two interns from drawing their own names, and don't shuffle only the 4−k4 - k remaining designers with (4−k)!\wrong{(4 - k)!}, which forgets the two interns and gives 9\wrong{9}.)
362\counted{362}
4

How many permutations of the six letters A, B, C, D, E, F avoid all four of these conditions: A1A_1: F is in the last (6th) position; A2A_2: the block AB appears; A3A_3: the block BC appears; A4A_4: the block FCA appears?

Because the blocks have different lengths and share letters differently, write out the singles, the (42)=6\binom{4}{2} = 6 pairs, and the (43)=4\binom{4}{3} = 4 triples individually. Which pairs of rules contradict each other?

Start with N=6!=720N = 6! = 720 and count each single rule:
S1=5!⏟A1: F is 6th+5!⏟A2: [AB]+5!⏟A3: [BC]+4!⏟A4: [FCA]=120+120+120+24=384\displaystyle \begin{aligned} S_1 &= \underbrace{\free{5!}}_{\bad{A_1}:\text{ F is 6th}} + \underbrace{\free{5!}}_{\bad{A_2}:\text{ [AB]}} \\ &\quad + \underbrace{\free{5!}}_{\bad{A_3}:\text{ [BC]}} + \underbrace{\free{4!}}_{\bad{A_4}:\text{ [FCA]}} \\ &= 120 + 120 + 120 + 24 = 384 \end{aligned}
Now check each of the 6\bad{6} pairs:
∣A1∩A2∣=4!=24(F is 6th; shuffle [AB], C, D, E)∣A1∩A3∣=4!=24(F is 6th; shuffle [BC], A, D, E)∣A1∩A4∣=0(F cannot be 6th and alsoat the front of [FCA])∣A2∩A3∣=4!=24(fuses into [ABC] with D, E, F)∣A2∩A4∣=3!=6(fuses into [FCAB] with D, E)∣A3∩A4∣=0(C cannot come right afterboth B and F)\displaystyle \begin{aligned} |\bad{A_1} \cap \bad{A_2}| &= \free{4!} = 24 \\ &\quad {\scriptstyle (\text{F is 6th; shuffle [AB], C, D, E})} \\ |\bad{A_1} \cap \bad{A_3}| &= \free{4!} = 24 \\ &\quad {\scriptstyle (\text{F is 6th; shuffle [BC], A, D, E})} \\ |\bad{A_1} \cap \bad{A_4}| &= \bad{0} \\ &\quad {\scriptstyle (\text{F cannot be 6th and also}} \\ &\quad {\scriptstyle \text{at the front of [FCA]})} \\ |\bad{A_2} \cap \bad{A_3}| &= \free{4!} = 24 \\ &\quad {\scriptstyle (\text{fuses into [ABC] with D, E, F})} \\ |\bad{A_2} \cap \bad{A_4}| &= \free{3!} = 6 \\ &\quad {\scriptstyle (\text{fuses into [FCAB] with D, E})} \\ |\bad{A_3} \cap \bad{A_4}| &= \bad{0} \\ &\quad {\scriptstyle (\text{C cannot come right after}} \\ &\quad {\scriptstyle \text{both B and F})} \end{aligned}
S2\displaystyle S_2 =24+24+0+24+6+0\displaystyle = 24 + 24 + 0 + 24 + 6 + 0 =78\displaystyle = 78
(Watch out: in A1∩A4\bad{A_1} \cap \bad{A_4} and A3∩A4\bad{A_3} \cap \bad{A_4}, check whether the rules can actually happen together before counting. Treating one of A1∩A4\bad{A_1} \cap \bad{A_4} and A3∩A4\bad{A_3} \cap \bad{A_4} as 3!=6\wrong{3! = 6} instead of 0\bad{0} raises S2S_2 and gives 414\wrong{414}; treating both that way gives 420\wrong{420}.)
Among the 44 triples, any triple containing A4\bad{A_4} includes one of the impossible pairs above (A1∩A4\bad{A_1} \cap \bad{A_4} or A3∩A4\bad{A_3} \cap \bad{A_4}), leaving only A1∩A2∩A3\bad{A_1} \cap \bad{A_2} \cap \bad{A_3}:
S3=3!⏟A1∩A2∩A3: F is 6th, [ABC], D, E+0+0+0=6,S4=0\displaystyle \begin{aligned} S_3 &= \underbrace{\free{3!}}_{\bad{A_1} \cap \bad{A_2} \cap \bad{A_3}:\text{ F is 6th, [ABC], D, E}} \\ &\quad + \bad{0} + \bad{0} + \bad{0} = 6, \\ S_4 &= \bad{0} \end{aligned}
Combine the alternating sum:
720−384+78−6+0\displaystyle 720 - 384 + 78 - 6 + 0 =408\displaystyle = \counted{408}
408\counted{408}
5

Six distinct tasks (1 through 6) are assigned to four coworkers (A, B, C, D). Task 1 must be assigned to coworker A, and every coworker must receive at least one task. How many valid assignments are there?

Once Task 1 is locked to coworker A, how many tasks are left to assign freely, and which coworkers can still end up with zero tasks?

Lock Task 1 to coworker A first. Each of the remaining 55 tasks (Tasks 2 through 6) has 44 choices of coworker, giving N=45=1024N = 4^5 = 1024 total assignments.
Because coworker A already has Task 1, A can never have zero tasks. Only the other 33 coworkers (B, C, and D) can be left empty, giving 33 bad sets:
N−S1+S2−S3=45−(31)35+(32)25−(33)15=1024−3(243)+3(32)−1(1)=1024−729+96−1=390\displaystyle \begin{aligned} &N - S_1 + S_2 - S_3 \\ &= 4^5 - \bad{\binom{3}{1}}\free{3^5} + \bad{\binom{3}{2}}\free{2^5} - \bad{\binom{3}{3}}\free{1^5} \\ &= 1024 - \bad{3}(\free{243}) + \bad{3}(\free{32}) - \bad{1}(\free{1}) \\ &= 1024 - 729 + 96 - 1 = \counted{390} \end{aligned}
(Watch out: don't include coworker A as a fourth bad set with (4k)\wrong{\binom{4}{k}}, which tries to seal a box that already contains Task 1 and gives 240\wrong{240} instead of 390\counted{390}.)
390\counted{390}