Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Inclusion-Exclusion






The Overlap Problem


The addition rule computes the size of a union by adding the sizes of its pieces — but only when those pieces are mutually exclusive. The moment two sets share elements, simply adding their sizes counts the shared elements twice.

The inclusion–exclusion principle is the systematic correction. Add the sizes of individual sets, subtract the sizes of pairwise intersections to remove the double-counting, add back the sizes of triple intersections to compensate for the over-correction, and continue with alternating signs until all intersections are accounted for.

This page builds the principle from the two-set case to the general formula, then turns to its complementary form — counting elements that belong to none of the listed sets. That complementary form produces the derangement count and other "none" results.



Two Sets


For two sets AA and BB, the size of the union is

Inclusion-Exclusion Principle (Two Sets)
AB=A+BAB.|A \cup B| = |A| + |B| - |A \cap B|.
Learn more about this formula: Inclusion-Exclusion Principle (Two Sets) →


Every element of ABA \cup B should be counted once on the left side. On the right, an element belonging to only AA is counted once (by A|A|), an element belonging to only BB is counted once (by B|B|), and an element in both is counted twice — once by A|A| and once by B|B|. Subtracting AB|A \cap B| removes that extra count, restoring the correct total.

Example


In a group of 30 students, 18 take French and 15 take Spanish, with 7 taking both. The number taking at least one of the two languages is

18+157=26.18 + 15 - 7 = 26.


The remaining 4 students take neither.

Three Sets


For three sets, the same logic extends with one more correction:

Inclusion-Exclusion Principle (Three Sets)
ABC=A+B+CABACBC+ABC.|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|.
Learn more about this formula: Inclusion-Exclusion Principle (Three Sets) →


An element belonging to exactly one of the three sets is counted once on the right. An element in exactly two is counted twice by the singles and subtracted once by its single pairwise intersection — net count 11. An element in all three is counted three times by the singles, subtracted three times by the pairwise intersections, and added back once by the triple intersection — net count 33+1=13 - 3 + 1 = 1. Every element ends up counted exactly once.

Example


Among the integers from 1 to 100, the number divisible by 2, 3, or 5 is computed by applying the three-set formula to the divisibility sets. With A=50|A| = 50 (multiples of 2), B=33|B| = 33 (multiples of 3), C=20|C| = 20 (multiples of 5), and the pairwise and triple intersections counting multiples of 66, 1010, 1515, and 3030:

50+33+2016106+3=74.50 + 33 + 20 - 16 - 10 - 6 + 3 = 74.


The General Formula


For nn sets A1,A2,,AnA_1, A_2, \ldots, A_n:

i=1nAi=iAii<jAiAj+i<j<kAiAjAk+(1)n+1A1A2An.\left| \bigcup_{i=1}^{n} A_i \right| = \sum_{i} |A_i| - \sum_{i<j} |A_i \cap A_j| + \sum_{i<j<k} |A_i \cap A_j \cap A_k| - \cdots + (-1)^{n+1} |A_1 \cap A_2 \cap \cdots \cap A_n|.


In compact form,

Inclusion-Exclusion Principle (General)
i=1nAi=k=1n(1)k+11i1<i2<<iknAi1Ai2Aik.\left| \bigcup_{i=1}^{n} A_i \right| = \sum_{k=1}^{n} (-1)^{k+1} \sum_{1 \le i_1 < i_2 < \cdots < i_k \le n} |A_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_k}|.
Learn more about this formula: Inclusion-Exclusion Principle (General) →


Structure of the Formula


The number of terms at level kk — meaning the number of kk-fold intersections being summed — is $\binom{n}{k}$, one term for each kk-subset of the nn sets. The total number of terms across all levels is

k=1n(nk)=2n1,\sum_{k=1}^{n} \binom{n}{k} = 2^n - 1,


which is the number of non-empty subsets of the nn sets. Every non-empty subset contributes exactly one term to the formula.

Why the Alternating Signs Work


An element belonging to exactly mm of the nn sets is counted

(m1)(m2)+(m3)+(1)m+1(mm)\binom{m}{1} - \binom{m}{2} + \binom{m}{3} - \cdots + (-1)^{m+1} \binom{m}{m}


times by the right-hand side. By the binomial theorem with a=1a = 1 and b=1b = -1,

k=0m(1)k(mk)=0,\sum_{k=0}^{m} (-1)^k \binom{m}{k} = 0,


so (m0)=1\binom{m}{0} = 1 minus the alternating sum from k=1k=1 to mm gives 11. Every element in the union is counted exactly once, as required.

The Complementary Form


Often the question is not how many elements belong to some of the AiA_i, but how many belong to none of them. If UU is a universal set containing all candidate elements, the elements outside every AiA_i form the intersection of the complements:

Inclusion-Exclusion Complementary Form
A1A2An=UA1A2An.\left| \overline{A_1} \cap \overline{A_2} \cap \cdots \cap \overline{A_n} \right| = |U| - \left| A_1 \cup A_2 \cup \cdots \cup A_n \right|.
Learn more about this formula: Inclusion-Exclusion Complementary Form →


Expanding the right side using the general formula gives a direct expression for the count of elements satisfying none of the conditions defined by A1,,AnA_1, \ldots, A_n:

UiAi+i<jAiAji<j<kAiAjAk++(1)nA1An.|U| - \sum_{i} |A_i| + \sum_{i<j} |A_i \cap A_j| - \sum_{i<j<k} |A_i \cap A_j \cap A_k| + \cdots + (-1)^n |A_1 \cap \cdots \cap A_n|.


This is the natural form for problems phrased as "how many elements avoid all of the following conditions" — derangements, surjective functions, arrangements with forbidden positions, and divisibility problems requiring relative primality to several primes.

Application: Counting Derangements


A derangement of {1,2,,n}\{1, 2, \ldots, n\} is a permutation in which no element appears in its original position. The derangement formula is derived by applying the complementary form of inclusion–exclusion to the set of all permutations.

Setup


For each ii, let AiA_i denote the set of permutations of {1,,n}\{1, \ldots, n\} in which position ii is fixed (the element ii remains in its original position). The derangements are the permutations belonging to none of A1,A2,,AnA_1, A_2, \ldots, A_n.

Intersection Sizes


The size of any kk-fold intersection Ai1Ai2AikA_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_k} equals (nk)!(n-k)! — fixing kk specific positions leaves the remaining nkn-k positions to be permuted freely. The size depends only on kk, not on which kk positions are chosen.

Derivation


The total number of permutations is n!n!. Applying the complementary form of inclusion–exclusion:

!n=n!(n1)(n1)!+(n2)(n2)!+(1)n(nn)0!.!n = n! - \binom{n}{1}(n-1)! + \binom{n}{2}(n-2)! - \cdots + (-1)^n \binom{n}{n} \cdot 0!.


Simplifying each term using (nk)(nk)!=n!k!\binom{n}{k}(n-k)! = \frac{n!}{k!} gives the explicit derangement formula:

!n=n!k=0n(1)kk!.!n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}.


The permutations page states this formula and applies it. The derivation above is what justifies it.

Worked Examples


Three standard problem types beyond derangements.

Divisibility


How many integers from 1 to 100 are divisible by none of 2, 3, or 5?

Let AiA_i be the set of integers divisible by the ii-th prime in the list. From the three-set example above, ABC=74|A \cup B \cup C| = 74. The complementary count is

10074=26.100 - 74 = 26.


These are the integers from 1 to 100 that share no prime factor with 30.

Surjective Functions


How many functions from a set of size nn to a set of size kk are surjective (every element of the codomain has at least one preimage)?

For each element ii of the codomain, let AiA_i be the set of functions that miss ii — that is, no element of the domain maps to ii. A surjective function avoids every AiA_i. The size of any jj-fold intersection Ai1AijA_{i_1} \cap \cdots \cap A_{i_j} is (kj)n(k-j)^n, since the domain elements may map freely to the remaining kjk-j codomain elements. The total count of functions is knk^n. By inclusion–exclusion:

Surjective Functions Count
#{surjections [n][k]}=j=0k(1)j(kj)(kj)n.\#\{\text{surjections } [n] \to [k]\} = \sum_{j=0}^{k} (-1)^j \binom{k}{j} (k-j)^n.
Learn more about this formula: Surjective Functions Count →


Arrangements With Forbidden Positions


In how many ways can nn items be arranged so that none of them occupies a specified set of mm forbidden positions? Define AiA_i as the set of arrangements where item ii does occupy its forbidden position. The complementary count, computed via inclusion–exclusion on the AiA_i, gives the answer. The derangement formula is the special case where each of the nn items has exactly one forbidden position and the forbidden positions are all distinct.
Problem type What to count Define Ai as Final formula
Divisibility / coprimality integers in 1..N divisible by none of given primes integers in 1..N divisible by the i-th prime N − |A₁ ∪ A₂ ∪ … ∪ An|
Surjective functions surjections from an n-set to a k-set functions missing codomain element i; |j-fold intersection| = (k − j)n Σj=0k (−1)j C(k, j) (k − j)n
Derangements permutations of 1..n with no fixed point permutations fixing position i; |k-fold intersection| = (n − k)! !n = n! · Σk=0n (−1)k ⁄ k!
Forbidden positions arrangements of n items avoiding specified forbidden positions arrangements with item i in its forbidden position n! − |A₁ ∪ … ∪ An| via inclusion–exclusion (derangements are the special case)

Related Concepts


Set Theory — unions, intersections, complements, and the cardinality identities are the formal structure inclusion–exclusion operates on.

Probability — the probabilistic version of inclusion–exclusion is the standard tool for computing the probability of a union of non-disjoint events.

Inclusion–Exclusion at a Glance


The page built the principle from two sets to the general n-set formula, derived its complementary form, and applied that form to derangements and several other standard problems. The table below collects the formula variants alongside the situations each one solves.
Form Formula When to use Example
Two sets |A ∪ B| = |A| + |B| − |A ∩ B| two overlapping groups; correct for the double-count once 18 French + 15 Spanish − 7 both = 26
Three sets singles − pairs + triple three overlapping conditions; one over-correction needs adding back multiples of 2, 3, or 5 in 1..100: 74
n sets (union form) Σk=1n (−1)k+1 Σ |k-fold intersections|; 2n − 1 terms total general count of a union of n overlapping sets any union problem with explicit intersection sizes
n sets (complementary form) |U| − Σ |Ai| + Σ |Ai ∩ Aj| − … + (−1)n |A₁ ∩ … ∩ An| "satisfy NONE of the conditions" / "avoid all forbidden cases" integers in 1..100 coprime to 30: 26
Derangement specialization !n = n! · Σk=0n (−1)k ⁄ k! ≈ n! ⁄ e no item lands in its own original position shuffled hats: probability no one gets own hat → 1⁄e