Chapter

Inclusion-Exclusion

This module develops inclusion-exclusion for forbidden conditions, derangements, surjections, fixed points, and distributions.
Log in to track solved progress and bookmarks.

Theory

Key Idea

The principle of inclusion-exclusion counts objects satisfying at least one condition, or equivalently objects avoiding forbidden conditions. The main idea is to add too much, subtract intersections, then add triple intersections back, and so on.

Basic Facts

  • \(|A\cup B|=|A|+|B|-|A\cap B|\).
  • For three sets, pairwise intersections and the triple intersection enter with alternating signs.
  • The number of objects avoiding forbidden properties is \(\sum (-1)^i\) times the number of objects satisfying a chosen set of \(i\) forbidden properties.
  • The number of derangements is \(D_n=n!\sum_{i=0}^{n}\frac{(-1)^i}{i!}\).
  • The number of onto functions from an \(n\)-element set to an \(m\)-element set is \(\sum_{i=0}^{m}(-1)^i\binom mi(m-i)^n\).

When to Use This Method

Use it when the statement contains “at least one”, “none”, “fixed point”, “forbidden position”, “every colour is used”, “every box is nonempty”, or “not divisible by the given numbers”.

How to Recognise the Method

If direct counting with restrictions is inconvenient, count the complement. If objects can violate several restrictions at the same time, simple subtraction will be wrong; inclusion-exclusion is needed.

Typical Mistakes

  • Subtracting objects with two violations twice and not adding them back.
  • Confusing “at least one violation” with “exactly one violation”.
  • Forgetting that after fixing \(i\) fixed points, the remaining \(n-i\) objects are permuted.
  • In function problems, confusing “all values are used” with “values are distinct”.

Mini-Checklist

  • Name the forbidden events \(A_1,\ldots,A_m\).
  • Decide whether you count the union of bad events or its complement.
  • For a chosen set of \(i\) forbidden events, count objects satisfying all of them.
  • Use the sign \((-1)^i\).
  • Check edge cases: \(i=0\), \(i=m\), and small \(n\).

Examples

Example 1. Two Sets

The basic formula is the language for all later problems.

Problem. Prove \(|A\cup B|=|A|+|B|-|A\cap B|\).

Solution.

In \(|A|+|B|\), each element of \(A\setminus B\) and \(B\setminus A\) is counted once, while each element of \(A\cap B\) is counted twice. To leave it counted once, subtract \(|A\cap B|\).

Comment. The point is not the formula, but the reason for the minus sign.

Example 2. Three Divisors

Numbers with several properties often require inclusion-exclusion.

Problem. How many integers from \(1\) to \(1000\) are divisible by at least one of \(2,3,5\)?

Solution.

We get \(\left\lfloor\frac{1000}{2}\right\rfloor+\left\lfloor\frac{1000}{3}\right\rfloor+\left\lfloor\frac{1000}{5}\right\rfloor-\left\lfloor\frac{1000}{6}\right\rfloor-\left\lfloor\frac{1000}{10}\right\rfloor-\left\lfloor\frac{1000}{15}\right\rfloor+\left\lfloor\frac{1000}{30}\right\rfloor=500+333+200-166-100-66+33=734\).

Comment. Each pairwise intersection is subtracted, and the triple intersection is added back.

Example 3. All Letters Used

A surjection means “every value is used”.

Problem. How many words of length \(7\) over the alphabet \(\{A,B,C\}\) contain all three letters?

Solution.

There are \(3^7\) words. Subtract words missing at least one letter. If the missing letter is chosen, there are \(2^7\) words; there are \(3\) choices. Words missing two letters were subtracted twice, so add them back: \(3\cdot1^7\). The answer is \(3^7-3\cdot2^7+3=1806\).

Comment. This is the surjection formula in a small case.

Example 4. Derangements

Fixed points are the classical example.

Problem. How many permutations of \(5\) elements have no fixed points?

Solution.

Let \(A_i\) be the event that element \(i\) is fixed. By inclusion-exclusion, the number of permutations with no fixed points is

\[5!-\binom51 4!+\binom52 3!-\binom53 2!+\binom54 1!-\binom55 0!=44.\]

Comment. After fixed points are chosen, the remaining elements are permuted freely.

Example 5. Exactly \(k\) Fixed Points

Sometimes we first choose the desired fixed points, then forbid all others.

Problem. How many permutations of \(n\) elements have exactly \(k\) fixed points?

Solution.

First choose these \(k\) elements: \(\binom nk\). The remaining \(n-k\) elements must have no fixed points among themselves, so they can be permuted in \(D_{n-k}\) ways. The answer is \(\binom nkD_{n-k}\).

Comment. Inclusion-exclusion is hidden inside \(D_{n-k}\).

Example 6. Forbidden Positions

Permutations with restrictions are naturally counted through events.

Problem. How many permutations of \(1,\ldots,8\) place \(1,2,3\) away from their own positions?

Solution.

The restrictions apply only to the first three numbers. Inclusion-exclusion gives

\[8!-\binom31 7!+\binom32 6!-\binom33 5!=40320-15120+2160-120=27240.\]

Comment. Not all elements must avoid their own positions, only \(1,2,3\).

Example 7. Surjections

The inclusion-exclusion formula for functions.

Problem. How many onto functions are there from a \(6\)-element set to a \(3\)-element set?

Solution.

There are \(3^6\) functions. Subtract functions missing a chosen value: \(\binom31 2^6\). Add back functions missing two values: \(\binom32 1^6\). The answer is \(3^6-3\cdot2^6+3=540\).

Comment. This is the same count as words using all letters.

Example 8. The General Formula

The contribution of one object explains all signs.

Problem. Explain why an object violating exactly \(r\) restrictions contributes \(\sum_{i=0}^m(-1)^i\binom ri\), which is \(0\) if \(r>0\) and \(1\) if \(r=0\).

Solution.

If an object violates exactly \(r\) restrictions, it belongs to the intersection of any chosen group of \(i\) of these \(r\) restrictions. Its total contribution is \(\sum_{i=0}^r(-1)^i\binom ri=(1-1)^r\). For \(r>0\), this is \(0\); for \(r=0\), the contribution is \(1\).

Comment. This is the cleanest explanation of the general formula.

Problems

Problems

#2.1
#2.1

Two Sets

Inclusion-exclusion Grade 8 Grade 9 ★★☆☆☆

In a class, \(18\) students study English, \(14\) study German, and \(6\) study both. How many students study at least one of these languages?

Details
Problem: COM-B2-M02-P001
Difficulty: Level 2 of 5
Tag: Inclusion-exclusion
Grade: Grade 8, Grade 9
#2.2
#2.2

Divisible by \(3\) or \(5\)

Counting Grade 8 Grade 9 ★★☆☆☆

How many integers from \(1\) to \(200\) are divisible by \(3\) or by \(5\)?

Details
Problem: COM-B2-M02-P002
Difficulty: Level 2 of 5
Tag: Counting
Grade: Grade 8, Grade 9
#2.3
#2.3

At Least One Letter \(A\)

Counting Grade 8 Grade 9 ★★☆☆☆

How many words of length \(6\) over the alphabet \(\{A,B,C,D\}\) contain at least one letter \(A\)?

Details
Problem: COM-B2-M02-P003
Difficulty: Level 2 of 5
Tag: Counting
Grade: Grade 8, Grade 9
#2.4
#2.4

All Three Letters

Inclusion-exclusion Grade 8 Grade 9 ★★☆☆☆

How many words of length \(5\) over the alphabet \(\{A,B,C\}\) contain all three letters?

Details
Problem: COM-B2-M02-P004
Difficulty: Level 2 of 5
Tag: Inclusion-exclusion
Grade: Grade 8, Grade 9
#2.5
#2.5

Derangements of Four Elements

Permutations Grade 8 Grade 9 ★★☆☆☆

How many permutations of \(4\) elements have no fixed points?

Details
Problem: COM-B2-M02-P005
Difficulty: Level 2 of 5
Tag: Permutations
Grade: Grade 8, Grade 9
#2.6
#2.6

Not Divisible by \(2,3,5\)

Divisibility Grade 9 Grade 10 ★★★☆☆

How many integers from \(1\) to \(1000\) are divisible by none of \(2,3,5\)?

Details
Problem: COM-B2-M02-P006
Difficulty: Level 3 of 5
Tag: Divisibility
Grade: Grade 9, Grade 10
#2.7
#2.7

Onto Three Values

Inclusion-exclusion Grade 9 Grade 10 ★★★☆☆

How many functions from a \(6\)-element set to a \(3\)-element set are onto?

Details
Problem: COM-B2-M02-P007
Difficulty: Level 3 of 5
Tag: Inclusion-exclusion
Grade: Grade 9, Grade 10
#2.8
#2.8

Three Forbidden Positions

Permutations Grade 9 Grade 10 ★★★☆☆

How many permutations of \(1,\ldots,8\) place \(1,2,3\) away from their own positions?

Details
Problem: COM-B2-M02-P008
Difficulty: Level 3 of 5
Tag: Permutations
Grade: Grade 9, Grade 10
#2.9
#2.9

Derangement Formula

Permutations Grade 9 Grade 10 ★★★☆☆

Prove that the number of permutations of \(n\) elements with no fixed points is \(D_n=n!\sum_{i=0}^{n}\frac{(-1)^i}{i!}\).

Details
Problem: COM-B2-M02-P009
Difficulty: Level 3 of 5
Tag: Permutations
Grade: Grade 9, Grade 10
#2.10
#2.10

Exactly Two Fixed Points

Permutations Grade 9 Grade 10 ★★★☆☆

How many permutations of \(7\) elements have exactly \(2\) fixed points?

Details
Problem: COM-B2-M02-P010
Difficulty: Level 3 of 5
Tag: Permutations
Grade: Grade 9, Grade 10
#2.11
#2.11

Zero, One, and Two Occur

Inclusion-exclusion Grade 9 Grade 10 ★★★☆☆

How many strings of length \(8\) over decimal digits contain each of the digits \(0,1,2\) at least once?

Details
Problem: COM-B2-M02-P011
Difficulty: Level 3 of 5
Tag: Inclusion-exclusion
Grade: Grade 9, Grade 10
#2.12
#2.12

Two Pairs of Restrictions

Permutations Grade 9 Grade 10 ★★★☆☆

How many permutations of \(1,\ldots,6\) place \(1\) away from position \(1\), \(2\) away from position \(2\), \(3\) away from position \(3\), and \(4\) away from position \(4\)?

Details
Problem: COM-B2-M02-P012
Difficulty: Level 3 of 5
Tag: Permutations
Grade: Grade 9, Grade 10
#2.13
#2.13

General Formula for Surjections

Inclusion-exclusion Grade 10 Grade 11 ★★★★☆

Prove that the number of onto functions from an \(n\)-element set to an \(m\)-element set is \(\sum_{i=0}^{m}(-1)^i\binom mi(m-i)^n\).

Details
Problem: COM-B2-M02-P013
Difficulty: Level 4 of 5
Tag: Inclusion-exclusion
Grade: Grade 10, Grade 11
#2.14
#2.14

Exactly \(k\) Fixed Points

Permutations Grade 10 Grade 11 ★★★★☆

Prove that the number of permutations of \(n\) elements with exactly \(k\) fixed points is \(\binom nkD_{n-k}\).

Details
Problem: COM-B2-M02-P014
Difficulty: Level 4 of 5
Tag: Permutations
Grade: Grade 10, Grade 11
#2.15
#2.15

Recurrence for Derangements

Permutations Grade 10 Grade 11 ★★★★☆

Prove the recurrence \(D_n=(n-1)(D_{n-1}+D_{n-2})\) for \(n\ge2\).

Details
Problem: COM-B2-M02-P015
Difficulty: Level 4 of 5
Tag: Permutations
Grade: Grade 10, Grade 11
#2.16
#2.16

Forbidden Positions in Rook Form

Permutations Grade 10 Grade 11 ★★★★☆

How many permutations \(\pi\) of \(1,\ldots,5\) satisfy \(\pi(1)\ne1\), \(\pi(1)\ne2\), \(\pi(2)\ne1\)?

Details
Problem: COM-B2-M02-P016
Difficulty: Level 4 of 5
Tag: Permutations
Grade: Grade 10, Grade 11
#2.17
#2.17

Contribution of One Object

Inclusion-exclusion Grade 10 Grade 11 ★★★★☆

Prove the general inclusion-exclusion formula by explaining the contribution of one object that belongs to exactly \(r\) of the sets \(A_1,\ldots,A_m\).

Details
Problem: COM-B2-M02-P017
Difficulty: Level 4 of 5
Tag: Inclusion-exclusion
Grade: Grade 10, Grade 11
#2.18
#2.18

First \(r\) Not Fixed

Permutations Grade 10 Grade 11 ★★★★★

Prove that the number of permutations of \(n\) elements in which elements \(1,2,\ldots,r\) are not fixed is \(\sum_{i=0}^{r}(-1)^i\binom ri(n-i)!\).

Details
Problem: COM-B2-M02-P018
Difficulty: Level 5 of 5
Tag: Permutations
Grade: Grade 10, Grade 11
#2.19
#2.19

All Boxes Nonempty

Inclusion-exclusion Grade 10 Grade 11 ★★★★★

How many ways are there to distribute \(n\) distinct balls among \(m\) distinct boxes so that every box is nonempty? Derive a formula.

Details
Problem: COM-B2-M02-P019
Difficulty: Level 5 of 5
Tag: Inclusion-exclusion
Grade: Grade 10, Grade 11
#2.20
#2.20

No Fixed Points and No Transpositions

Permutations Grade 10 Grade 11 ★★★★★

Derive a formula for the number of permutations of \(n\) elements with no fixed points and no cycles of length \(2\).

Details
Problem: COM-B2-M02-P020
Difficulty: Level 5 of 5
Tag: Permutations
Grade: Grade 10, Grade 11

Ladders

No published ladders were found.
Previous Chapter
Next Chapter