MathCompass

Inclusion-Exclusion Principle

Combinatorics Advanced

πŸ“‹ Prerequisites

When sets overlap, you can't just add their sizes β€” you'd double-count the overlap. The principle of inclusion-exclusion (PIE) fixes this: add the singles, subtract the pairs, add back the triples, and so on. It's the generalization of complementary counting to multiple forbidden conditions.

πŸ“š Key Concepts

Two Sets: The Basic Case

For two sets A and B:

\[ |A \cup B| = |A| + |B| - |A \cap B| \]

Add both sets, then subtract the overlap that was counted twice.

Example: 30 students take math, 25 take physics, 10 take both. Total taking at least one = 30 + 25 - 10 = 45.

Three Sets

For three sets A, B, C:

\[ |A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| \]

Pattern: add single sets, subtract pairwise intersections, add back triple intersection.

The triple intersection was subtracted too many times, so we add it back once.

General Inclusion-Exclusion Principle

For n sets $A_1, A_2, ..., A_n$:

\[ \left| \bigcup_{i=1}^{n} A_i \right| = \sum |A_i| - \sum |A_i \cap A_j| + \sum |A_i \cap A_j \cap A_k| - ... + (-1)^{n+1} |A_1 \cap ... \cap A_n| \]

Alternating sum over all non-empty subsets. Odd-sized intersections added, even-sized subtracted.

Total of \(2^{n}\) - 1 terms.

Complement Form (Most Useful)

Most competition problems ask "how many have NONE of the bad properties?"

If U is the total and $A_i$ are the sets of items with property i:

\[ |U - \bigcup A_i| = |U| - \sum |A_i| + \sum |A_i \cap A_j| - \sum |A_i \cap A_j \cap A_k| + ... + (-1)^n |A_1 \cap ... \cap A_n| \]

Items with none of the properties = Total - at least one property.

This is the form you'll use 90% of the time. "None of the bad things" is the complement of "at least one bad thing."

Derangements (ι”™δ½ζŽ’εˆ—)

A derangement is a permutation where no element appears in its original position.

Number of derangements of n elements, denoted !n:

\[ !n = n! \left(1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + ... + (-1)^n \frac{1}{n!}\right) \]

Classic PIE application: let A_{i} = permutations where element i is fixed. Count permutations with none fixed.

Examples: !3 = 2, !4 = 9, !5 = 44

Counting Divisible Numbers

"How many numbers from 1 to 100 are divisible by 2 or 3 or 5?"

Classic PIE problem. Count multiples of each, subtract multiples of pairwise products, add back multiples of lcm of all three.

⌊100/2βŒ‹ + ⌊100/3βŒ‹ + ⌊100/5βŒ‹ - ⌊100/6βŒ‹ - ⌊100/10βŒ‹ - ⌊100/15βŒ‹ + ⌊100/30βŒ‹

Surjective Functions (Onto Functions)

Number of onto functions from an n-element set to a k-element set:

\[ \sum_{i=0}^{k} (-1)^i \binom{k}{i} (k-i)^n \]

PIE: total functions \(k^{n},\) minus those missing at least one element, add back those missing two, etc.

When to Use PIE

Symmetry in PIE

Often all single-set sizes are equal, all pairwise intersections equal, etc. Then PIE simplifies:

$ |\bigcup A_i| = \binom{n}{1}|A_1| - \binom{n}{2}|A_1 \cap A_2| + \binom{n}{3}|A_1 \cap A_2 \cap A_3| - ... $

Use binomial coefficients to count how many terms of each size there are. This saves a lot of computation.

⚠️ Common Mistake: Forgetting the alternating signs β€” add singles, subtract pairs, add triples, etc. Also: in the complement form, the total |U| is positive (sign +), then the pattern alternates from there. Don't confuse derangement formula: !n is approximately n!/e, but for exact values use the full sum. And for "divisible by a or b," \(use lcm(a,b) for the intersection, not a \\times b (only equal when gcd(a,b)=1).\)
πŸ’‘ Key Insight: PIE is complementary counting generalized to multiple constraints. When there's one bad thing, you subtract it. Two bad things? Subtract both, add back the overlap (it was subtracted twice). Three bad things? The pattern continues β€” alternating + and -. This is one of the deepest ideas in combinatorics. Always look for symmetry: if all single-set counts are equal and all pairs equal, etc., use binomial coefficients to batch the terms and save time.

✏️ Example Problems

πŸ“ Example 1 (Two-set PIE)

In a class of 30 students, 18 play basketball and 15 play soccer.
If 7 students play both sports, how many play at least one sport?

\(Inclusion-Exclusion for two sets: |A \\cup B| = |A| + |B| - |A \\cap B|\)

Step 1: Identify the sets.

A = basketball players, |A| = 18

B = soccer players, |B| = 15

\(A \\cap B = both sports, |A \\cap B| = 7\)

Step 2: Apply formula.

\[ |A \cup B| = 18 + 15 - 7 = 26 \]

Why subtract 7? The 7 students who play both were counted twice β€” once in basketball, once in soccer. We subtract them once to correct the overcount.

Venn diagram check: Basketball only = 18 - 7 = 11, Soccer only = 15 - 7 = 8, Both = 7. Total = 11 + 8 + 7 = 26 βœ“

Answer: 26

πŸ“ Example 2 (Derangements)

How many derangements of 4 elements are there?
(Permutations of {1,2,3,4} where no number is in its original position.)

Use inclusion-exclusion (complement form).

\(Let A_{i} = permutations where element i is fixed (in position i).\)

\(We want permutations with NONE fixed: total - |A_{1} \\cup A_{2} \\cup A_{3} \\cup A_{4}|\)

Step 1: Total permutations = 4! = 24

Step 2: \(\(Single fixed positions. |A_{i}\(| = 3! = 6 for each i. There are \binom{4}{1} = 4 such sets.\)\)\)

\(Sum = 4 \\times 6 = 24\)

Step 3: \(Two fixed positions. |A_{i} \(\\cap A\)_{j}\(| = 2! = 2. \binom{4}{2} = 6 pairs.\)\)

\(Sum = 6 \\times 2 = 12\)

Step 4: \(Three fixed positions. |A_{i} \(\\cap A\)_{j} \(\\cap A\)_{k}\(| = 1! = 1. \binom{4}{3} = 4 triples.\)\)

\(Sum = 4 \\times 1 = 4\)

Step 5: \(All four fixed. |A_{1} \\cap A_{2} \\cap A_{3} \\cap A_{4}| = 0! = 1. \binom{4}{4} = 1.\)

Step 6: PIE (complement form).

\[ !4 = 24 - 24 + 12 - 4 + 1 = 9 \]

List them for n=4: 2143, 2341, 2413, 3142, 3412, 3421, 4123, 4312, 4321 = 9 derangements βœ“

Answer: 9

πŸ“ Example 3 (Divisible counting)

How many integers from 1 to 100 are divisible by 2 or 3?

Two-set PIE: divisible by 2 OR 3 = divisible by 2 + divisible by 3 - divisible by both (i.e., by lcm(2,3)=6)

Step 1: Count multiples of 2 in [1, 100].

⌊100/2βŒ‹ = 50 numbers (2, 4, 6, ..., 100)

Step 2: Count multiples of 3 in [1, 100].

⌊100/3βŒ‹ = 33 numbers (3, 6, 9, ..., 99)

Step 3: Count multiples of both 2 and 3 = multiples of 6.

⌊100/6βŒ‹ = 16 numbers (6, 12, ..., 96)

Step 4: Apply PIE.

\[ |A \cup B| = |A| + |B| - |A \cap B| = 50 + 33 - 16 = 67 \]

Check: Multiples of 2 only: 50 - 16 = 34. Multiples of 3 only: 33 - 16 = 17. Both: 16. Total = 34 + 17 + 16 = 67 βœ“

Answer: 67

Previous
Next