evan's notes
cal 3discreteodeenv systems
中文

On this page

  • Cardinality
  • The product principle
  • Example: Canadian postal codes
  • Why a power set has size 2n2^n2n
  • The sum principle
  • Example: usernames of length 6, 7, or 8
  • The complement principle
  • Example: usernames containing at least one digit
  • Inclusion–exclusion for two sets
  • Example: bit strings
  • Inclusion–exclusion for three or more sets
  • Example: integers with none of three properties

Discrete Structures

§2 Counting Principles

Evan Luo · Sep 8, 2026

Discrete Structures

§2 Counting Principles

Evan LuoSep 8, 2026

8 min read

Most counting problems come down to two questions: how many choices are there, and does anything get counted more than once?

Cardinality

The cardinality of a finite set AAA, written ∣A∣|A|∣A∣, is the number of elements in AAA. For example,

A={2,5,7}⟹∣A∣=3.A=\{2,5,7\} \qquad\Longrightarrow\qquad |A|=3. A={2,5,7}⟹∣A∣=3.

The empty set has no elements, while a set containing the empty set has one element:

∣∅∣=0but∣{∅}∣=1.|\varnothing|=0 \qquad\text{but}\qquad |\{\varnothing\}|=1. ∣∅∣=0but∣{∅}∣=1.

The vertical bars in ∣A∣|A|∣A∣ mean cardinality here, not absolute value.

The product principle

Use the product principle, also called the multiplication principle, when you make several choices in order. If the first choice has mmm options and, after every possible first choice, the second choice has the same nnn options, there are

m⋅nm\cdot n m⋅n

possible combinations. For more steps, multiply the number of options at each step when each count stays the same for every earlier valid choice. If the number of continuations depends on what was chosen earlier, split into cases or add the branch counts instead.

For finite sets AAA and BBB, this gives the cardinality of their Cartesian product:

∣A×B∣=∣A∣ ∣B∣.|A\times B|=|A|\,|B|. ∣A×B∣=∣A∣∣B∣.

If A={2,5,7}A=\{2,5,7\}A={2,5,7} and B={1,2}B=\{1,2\}B={1,2}, then

∣A×B∣=3⋅2=6.|A\times B|=3\cdot2=6. ∣A×B∣=3⋅2=6.

Example: Canadian postal codes

Let LLL be the set of 26 uppercase letters and DDD the set of 10 digits:

L={A,B,…,Z},D={0,1,…,9}.L=\{\mathrm A,\mathrm B,\ldots,\mathrm Z\}, \qquad D=\{0,1,\ldots,9\}. L={A,B,…,Z},D={0,1,…,9}.

In a simplified format model, a Canadian postal code has the pattern

L D L  D L D.L\,D\,L\;D\,L\,D. LDLDLD.

This model ignores the real restrictions on which letters may appear in particular positions. There are three letter positions and three digit positions. Each position can be chosen independently, and characters can repeat, so the number of strings matching the simplified pattern is

26⋅10⋅26⋅10⋅26⋅10=263⋅103=17,576,000.26\cdot10\cdot26\cdot10\cdot26\cdot10 =26^3\cdot10^3 =17{,}576{,}000. 26⋅10⋅26⋅10⋅26⋅10=263⋅103=17,576,000.

For example, Santa's postal code H0H 0H0 follows this pattern. Each 0 is the digit zero, not the letter O. The calculation counts format-matching strings, not actual assignable postal codes.

Why a power set has size 2n2^n2n

The power set P(A)\mathcal P(A)P(A) contains every subset of AAA. Suppose

A={a1,a2,…,an}.A=\{a_1,a_2,\ldots,a_n\}. A={a1​,a2​,…,an​}.

To choose a subset X⊆AX\subseteq AX⊆A, decide what to do with each element aia_iai​:

  • include aia_iai​ in XXX; or
  • leave aia_iai​ out of XXX.

There are two choices for each of the nnn elements, so the product principle gives

∣P(A)∣=2n=2∣A∣.|\mathcal P(A)|=2^n=2^{|A|}. ∣P(A)∣=2n=2∣A∣.

This also works for the empty set:

∣P(∅)∣=20=1,|\mathcal P(\varnothing)|=2^0=1, ∣P(∅)∣=20=1,

because P(∅)={∅}\mathcal P(\varnothing)=\{\varnothing\}P(∅)={∅}.

The sum principle

Two sets are disjoint when they have no elements in common:

A∩B=∅.A\cap B=\varnothing. A∩B=∅.

For disjoint finite sets, the sum principle, also called the addition principle, says

∣A∪B∣=∣A∣+∣B∣.|A\cup B|=|A|+|B|. ∣A∪B∣=∣A∣+∣B∣.

Because the sets do not overlap, nothing is counted twice. The same rule works for any number of pairwise-disjoint finite sets.

Example: usernames of length 6, 7, or 8

Suppose a username may contain uppercase letters, lowercase letters, and digits. There are

26+26+10=6226+26+10=62 26+26+10=62

allowed characters in total.

Let SnS_nSn​ be the set of valid usernames of length nnn. Since characters can repeat, the product principle gives

∣S6∣=626,∣S7∣=627,∣S8∣=628.|S_6|=62^6, \qquad |S_7|=62^7, \qquad |S_8|=62^8. ∣S6​∣=626,∣S7​∣=627,∣S8​∣=628.

A string cannot have two different lengths, so S6S_6S6​, S7S_7S7​, and S8S_8S8​ are disjoint. The sum principle therefore gives

∣S6∪S7∪S8∣=626+627+628=221,918,520,426,688.|S_6\cup S_7\cup S_8| =62^6+62^7+62^8 =221{,}918{,}520{,}426{,}688. ∣S6​∪S7​∪S8​∣=626+627+628=221,918,520,426,688.

The complement principle

Suppose A⊆BA\subseteq BA⊆B and BBB is finite. The complement principle counts the elements that are in BBB but not in AAA:

∣B∖A∣=∣B∣−∣A∣.|B\setminus A|=|B|-|A|. ∣B∖A∣=∣B∣−∣A∣.

This works because BBB splits into two parts that do not overlap:

B=A∪(B∖A).B=A\cup(B\setminus A). B=A∪(B∖A).

The sets AAA and B∖AB\setminus AB∖A do not overlap, so the sum principle gives

∣B∣=∣A∣+∣B∖A∣.|B|=|A|+|B\setminus A|. ∣B∣=∣A∣+∣B∖A∣.

Solving this equation for ∣B∖A∣|B\setminus A|∣B∖A∣ gives the complement principle.

Example: usernames containing at least one digit

Let BBB be the set of all valid usernames of length 6, 7, or 8, and let A⊆BA\subseteq BA⊆B contain the usernames with no digits. A username in BBB can use any of the 62 characters, while a username in AAA can use only the 52 letters. Therefore,

∣B∣=626+627+628,∣A∣=526+527+528.\begin{aligned} |B|&=62^6+62^7+62^8,\\ |A|&=52^6+52^7+52^8. \end{aligned} ∣B∣∣A∣​=626+627+628,=526+527+528.​

The set B∖AB\setminus AB∖A contains exactly the usernames with at least one digit. By the complement principle,

∣B∖A∣=(626+627+628)−(526+527+528)=167,410,949,583,040.\begin{aligned} |B\setminus A| &=(62^6+62^7+62^8)-(52^6+52^7+52^8)\\ &=167{,}410{,}949{,}583{,}040. \end{aligned} ∣B∖A∣​=(626+627+628)−(526+527+528)=167,410,949,583,040.​

Inclusion–exclusion for two sets

If finite sets AAA and BBB overlap, simply adding ∣A∣+∣B∣|A|+|B|∣A∣+∣B∣ counts every element of A∩BA\cap BA∩B twice: once in AAA and once in BBB.

The inclusion–exclusion principle fixes this by subtracting the overlap once:

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.|A\cup B|=|A|+|B|-|A\cap B|. ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.
Two-set inclusion–exclusion animationThe animation counts A, adds B, highlights the overlap that was counted twice, and subtracts one copy to leave the union.2×ABU
1. Count A2. Add B3. The overlap was counted twice4. Subtract one copy of A ∩ B
Adding |A| and |B| counts A ∩ B twice. Subtracting one copy leaves every element of A ∪ B counted exactly once.

Another way to see the formula is to split the union into three regions that do not overlap:

A∖B,A∩B,B∖A.A\setminus B, \qquad A\cap B, \qquad B\setminus A. A∖B,A∩B,B∖A.

Since the regions do not overlap, we can add their sizes:

∣A∪B∣=∣A∖B∣+∣A∩B∣+∣B∖A∣=(∣A∣−∣A∩B∣)+∣A∩B∣+(∣B∣−∣A∩B∣)=∣A∣+∣B∣−∣A∩B∣.\begin{aligned} |A\cup B| &=|A\setminus B|+|A\cap B|+|B\setminus A|\\ &=(|A|-|A\cap B|)+|A\cap B|+(|B|-|A\cap B|)\\ &=|A|+|B|-|A\cap B|. \end{aligned} ∣A∪B∣​=∣A∖B∣+∣A∩B∣+∣B∖A∣=(∣A∣−∣A∩B∣)+∣A∩B∣+(∣B∣−∣A∩B∣)=∣A∣+∣B∣−∣A∩B∣.​

Example: bit strings

A bit is either 000 or 111. How many bit strings of length 8 start with 111 or end with 000000?

Let

A={length-8 bit strings that start with 1}A=\{\text{length-8 bit strings that start with }1\} A={length-8 bit strings that start with 1}

and

B={length-8 bit strings that end with 00}.B=\{\text{length-8 bit strings that end with }00\}. B={length-8 bit strings that end with 00}.

For AAA, the first bit is fixed and the remaining seven bits are free, so

∣A∣=27.|A|=2^7. ∣A∣=27.

For BBB, the last two bits are fixed and the remaining six are free, so

∣B∣=26.|B|=2^6. ∣B∣=26.

A string in A∩BA\cap BA∩B starts with 111 and ends with 000000. Five positions remain free, so

∣A∩B∣=25.|A\cap B|=2^5. ∣A∩B∣=25.

By inclusion–exclusion,

∣A∪B∣=27+26−25=160.|A\cup B|=2^7+2^6-2^5=160. ∣A∪B∣=27+26−25=160.

Inclusion–exclusion for three or more sets

For three finite sets, inclusion–exclusion becomes

∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.\begin{aligned} |A\cup B\cup C| ={}&|A|+|B|+|C|\\ &-|A\cap B|-|A\cap C|-|B\cap C|\\ &+|A\cap B\cap C|. \end{aligned} ∣A∪B∪C∣=​∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.​

Start by adding the sizes of the three sets, then subtract the three pairwise overlaps. A point in all three sets was added three times and then subtracted three times, so it is missing from the count. Add the triple intersection once to put it back.

Three-set inclusion–exclusion diagramThree overlapping circles labelled A, B, and C, with their triple intersection identified.
The central region belongs to all three sets. It is added three times, removed three times with the pairwise intersections, then added back once.

For finite sets A1,A2,…,AnA_1,A_2,\ldots,A_nA1​,A2​,…,An​, write out the pattern like this:

∣A1∪A2∪⋯∪An∣=∣A1∣+∣A2∣+⋯+∣An∣−(∣A1∩A2∣+∣A1∩A3∣+⋯+∣An−1∩An∣)+(∣A1∩A2∩A3∣+⋯+∣An−2∩An−1∩An∣)−all quadruple intersections+all quintuple intersections−⋯+(−1)n+1∣A1∩A2∩⋯∩An∣.\begin{aligned} |A_1\cup A_2\cup\cdots\cup A_n| ={}&|A_1|+|A_2|+\cdots+|A_n|\\ &-\bigl(|A_1\cap A_2|+|A_1\cap A_3|+\cdots+|A_{n-1}\cap A_n|\bigr)\\ &+\bigl(|A_1\cap A_2\cap A_3|+\cdots+|A_{n-2}\cap A_{n-1}\cap A_n|\bigr)\\ &-\text{all quadruple intersections}\\ &+\text{all quintuple intersections}-\cdots\\ &+(-1)^{n+1}|A_1\cap A_2\cap\cdots\cap A_n|. \end{aligned} ∣A1​∪A2​∪⋯∪An​∣=​∣A1​∣+∣A2​∣+⋯+∣An​∣−(∣A1​∩A2​∣+∣A1​∩A3​∣+⋯+∣An−1​∩An​∣)+(∣A1​∩A2​∩A3​∣+⋯+∣An−2​∩An−1​∩An​∣)−all quadruple intersections+all quintuple intersections−⋯+(−1)n+1∣A1​∩A2​∩⋯∩An​∣.​

The signs alternate as the intersections get larger:

  1. add the size of each set;
  2. subtract every pairwise intersection;
  3. add every triple intersection;
  4. subtract every quadruple intersection;
  5. add every quintuple intersection, and keep alternating.

For four sets, this becomes: add the four set sizes, subtract the six pairwise intersections, add the four triple intersections, then subtract the four-way intersection.

Example: integers with none of three properties

How many integers from 1 through 1000 have none of these properties: perfect square, perfect cube, or multiple of 7?

Let

U={1,2,…,1000},U=\{1,2,\ldots,1000\}, U={1,2,…,1000},

and define

A={n∈U∣n=k2 for some integer k},B={n∈U∣n=k3 for some integer k},C={n∈U∣7 divides n}.\begin{aligned} A&=\{n\in U\mid n=k^2\text{ for some integer }k\},\\ B&=\{n\in U\mid n=k^3\text{ for some integer }k\},\\ C&=\{n\in U\mid 7\text{ divides }n\}. \end{aligned} ABC​={n∈U∣n=k2 for some integer k},={n∈U∣n=k3 for some integer k},={n∈U∣7 divides n}.​

Here, AAA contains the perfect squares, BBB contains the perfect cubes, and CCC contains the multiples of 7.

The floor function ⌊x⌋\lfloor x\rfloor⌊x⌋ is the greatest integer less than or equal to xxx. We use it here because the square and cube roots must be whole numbers:

∣A∣=⌊1000⌋=31,∣B∣=⌊10003⌋=10,|A|=\left\lfloor\sqrt{1000}\right\rfloor=31, \qquad |B|=\left\lfloor\sqrt[3]{1000}\right\rfloor=10, ∣A∣=⌊1000​⌋=31,∣B∣=⌊31000​⌋=10,

and

∣C∣=⌊10007⌋=142.|C|=\left\lfloor\frac{1000}{7}\right\rfloor=142. ∣C∣=⌊71000​⌋=142.

Now count the intersections:

  • A number that is both a square and a cube is a sixth power, so ∣A∩B∣=⌊10006⌋=3.|A\cap B|=\left\lfloor\sqrt[6]{1000}\right\rfloor=3. ∣A∩B∣=⌊61000​⌋=3.
  • A square divisible by 7 has a square root divisible by 7. Among 1,2,…,311,2,\ldots,311,2,…,31, there are four multiples of 7, so ∣A∩C∣=4.|A\cap C|=4. ∣A∩C∣=4.
  • A cube divisible by 7 has a cube root divisible by 7. Among 1,2,…,101,2,\ldots,101,2,…,10, only 777 qualifies, so ∣B∩C∣=1.|B\cap C|=1. ∣B∩C∣=1.
  • A number in all three sets would be a sixth power whose base is divisible by 7. The smallest is 76=117,649>10007^6=117{,}649>100076=117,649>1000, so ∣A∩B∩C∣=0.|A\cap B\cap C|=0. ∣A∩B∩C∣=0.

Inclusion–exclusion gives

∣A∪B∪C∣=31+10+142−3−4−1+0=175.|A\cup B\cup C| =31+10+142-3-4-1+0 =175. ∣A∪B∪C∣=31+10+142−3−4−1+0=175.

The question asks for the numbers outside all three sets, so subtract the union from UUU:

∣U∖(A∪B∪C)∣=∣U∣−∣A∪B∪C∣=1000−175=825.\begin{aligned} |U\setminus(A\cup B\cup C)| &=|U|-|A\cup B\cup C|\\ &=1000-175\\ &=825. \end{aligned} ∣U∖(A∪B∪C)∣​=∣U∣−∣A∪B∪C∣=1000−175=825.​

Source: https://notes.ohevan.com/notes/discrete-structures/02-counting-principles

© 2026 Evan Luo. All rights reserved.

Back to Discrete Structures

0 reads

·Last edited 3 days ago
  • newsletter

  • about me

  • sponsor

© 2026 Evan Luo. All rights reserved.