Discrete
Mathematics
Chapter 5 Counting
§5.1 The Basics of counting
Ú We will present two basic counting principles, the product rule and the sum rule.
Ú The product rule:
Suppose that a procedure can be broken down into two tasks. If there are n1 ways to do the
first task and n2 ways to do the second task after the first task has been done, then there are
n1 n2 ways to do the procedure.
n_{1}
n_{2}
n_{1 }× n_{2 } ways
Example 1 A new company with just two employees, Sanchez and Patel, rents a floor of a building with 12 offices. How many ways are there to assign different offices to these two employees?
Sol: 12*11 =132 ways
Example 2 The chair of an auditorium (大禮堂) is to be labeled with a letter and a positive integer not exceeding 100. What is the largest number of chairs that can be labeled differently?
Sol: 26 × 100 = 2600 ways to label chairs.
letter
Ν
∈
≤
≤ x
x 100 1
Example 4 How many different bit strings are there of length seven?
Sol: 1 2 3 4 5 6 7
□ □ □ □ □ □ □
↑ ↑ ↑ ↑ ↑ ↑ ↑ 0,1 0,1 0,1 . . . 0,1
→ 2^{7} 種
Example 5
How many different license plates (車牌) are
available if each plate contains a sequence of 3 letters followed by 3 digits ?
Sol: □ □ □ □ □ □ →26^{3}．10^{3} letter digit
Example 6 How many functions are there from a set with m elements to one with n elements?
Sol: f(a_{1})=? 可以是b_{1}～ b_{n}, 共n種 f(a_{2})=? 可以是b_{1}～ b_{n}, 共n種
：
f(a_{m})=? 可以是b_{1}～ b_{n}, 共n種
∴n^{m}
a_{1} a_{2}
．
．
．
a_{m}
b_{1} b_{2}
．
．
．
b_{n} f
Example 7 How many onetoone functions are there from a set with m elements to one with n element?
(m ≤ n)
Sol: f(a_{1}) = ? 可以是b_{1}～ b_{n}, 共 n 種
f(a_{2}) = ? 可以是b_{1}～ b_{n}, 但不能= f(a_{1}), 共 n−1 種
f(a_{3}) = ? 可以是b_{1}～ b_{n}, 但不能= f(a_{1}), 也不能=f(a_{2}), 共 n−^{2 種}
：
：
f(a_{m}) = ? 不可=f(a_{1}), f(a_{2}), ... , f(a_{m−1}), 故共n−(m−1)種
∴共 n．(n−1)．(n−2)．...．(n−m+1)種 11 function _{#}
Ú
A password on a computer system consists of 6,7, or 8 characters. Each of these characters must be a digit or a letter of the alphabet. Each password must contain at least one digit. How many such passwords are there?
Ú This section introduces

a variety of other counting problems

the basic techniques of counting.
Basic Counting Principles
Ú The sum rule:
If a first task can be done in n_{1} ways and a second task in n_{2} ways, and if these tasks cannot be done at the same time. then there are n_{1}+n_{2} ways to do either task.
Example 11
Suppose that either a member of faculty or a student is chosen as a representative to a university committee. How many different
choices are there for this representative if there are 37 members of the faculty and 83 students?
Sol: 37+83=120
n_{1} n_{2}
n_{1 }+ n_{2 }ways
Example 12 A student can choose a computer
project from one of three lists. The three lists contain 23, 15 and 19 possible projects respectively.
How many possible projects are there to choose from?
Sol: 23+15+19=57 projects.
Example 14 Each user on a computer system has a password which is 6 to 8 characters long, where each character is an uppercase letter or a digit. Each
password must contain at least one digit.
How many possible passwords are there?
Sol: P_{i }: # of possible passwords of length i , i=6,7,8 P_{6} = 36^{6 }− 26^{6}
P_{7} = 36^{7 }− 26^{7}
P_{8} = 36^{8 }− 268
∴ P_{6} + P_{7} + P_{8} = 36^{6 }+ 36^{7 }+ 36^{8 }− 26^{6} − 26^{7 }− 26^{8}種
Example 13
In a version of Basic, the name of a variable
is a string of one (only alphanumeric characters )or two alphanumeric characters, (An alphanumeric characters is either one of the 26 English letters or one of the 10 digits) where uppercase and lowercase letters are not distinguished. Moreover, a variable name must begin with a letter and must be different from the five strings of two characters that are reserved for programming use.
How many different variable names are there in this version of Basic?
Sol:
Let V_{i} be the number of variable names of length i.
V_{1 }=26
V_{2 }=26．36 – 5
∴26 + 26．36 – 5 different names.
※ The InclusionExclusion Principle (排容原理)
A B
Example 17 How many bit strings of length eight either start with a 1 bit or end with the two bits 00 ?
Sol:
１ ２ ３ ４ ５ ６ ７ ８
□ □ □ □ □ □ □ □
↑ ↑ . . .
① 1 0,1 0,1 → 共2^{7}種
② . . . 0 0 → 共2^{6}種
③ 1 . . . 0 0 → 共2^{5}種
∴ 2^{7 }+2^{6 }−2^{5 }種
B A
B A
B
A U = + − I
0
1
bit 1
※ Tree Diagrams
Example 19 How many bit strings of length four do not have two consecutive 1s ?
Sol:
Exercise: 11, 17, 21, 27, 36, 37, 45, 49
0 0 0 0 0 1 1 0 1
0 1
(0000) (0001) (0010) (0100) (0101) (1000) (1001) (1010)
∴ 8 bit strings 0
0 1 1
0
bit 3
Ex 36. How many subsets of a set with 100 elements have more than one element ?
Sol:
Ex 37. A palindrome (迴文) is a string whose reversal is identical to the string. How many bit strings of length n are palindromes ?
(e.g., abcdcba 是迴文, abcd 不是 ) Sol: If a_{1}a_{2} ... a_{n} is a palindrome, then
a_{1}=a_{n}, a_{2}=a_{n−1}, a_{3}=a_{n−2}, …
101 2
2
... 100 98
100 99
100 100
100 )
1
( ⎟⎟ = = ^{100} −
⎠
⎜⎜ ⎞
⎝ + ⎛
⎟⎟ +
⎠
⎜⎜ ⎞
⎝ + ⎛
⎟⎟⎠
⎜⎜ ⎞
⎝ + ⎛
⎟⎟⎠
⎜⎜ ⎞
⎝
⎛ L
Thm. 4 of §5.3
{
_{1}^{,} _{2} ^{,...,} _{100}}
) 2
( a a a
{
^{□}^{ }^{,}^{□}^{ }^{,...,}^{□}^{ }}
^{ } ^{2} ^{101}:
subset ∴ ^{100} −
放 不放
放 不放
放 不放
空集合及
只有1個元素的集合
string.
種 2 ^{2 ⎥}^{⎥}
⎢⎢ ⎤
⎡
⇒
n
§5.2 The Pigeonhole Principle (鴿籠原理)
Thm 1 (The Pigeonhole Principle)
If k+1 or more objects are placed into k boxes, then there is at least one box containing two or more of the objects.
Proof
Suppose that none of the k boxes contains more than one object. Then the total number of objects would
be at most k. This is a contradiction.
Example 1. Among any 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays.
Example 2 In any group of 27 English words, there must be at least two that begin with the same letter.
Example 3 How many students must be in a class to guarantee that at least two students receive the
same score on the final exam ? (0~100 points) Sol: 102. (101+1)
Thm 2. (The generalized pigeon hole principle) If N objects are placed into k boxes, then
there is at least one box containing at least objects.
e.g. 21 objects, 10 boxes ⇒ there must be one box
containing at least objects.
⎥⎥⎤
⎢⎢⎡ k N
1021 =3
⎥⎥⎤
⎢⎢⎡
Example 5 Among 100 people there are at least
who were born in the same month. ( 100 objects, 12 boxes )
12 9 100 =⎥⎥⎤
⎢⎢⎡
Example 6 What is the minimum number of students required in a D.M class to be sure that at least six will
receive the same grade, if there are five possible grades, A, B, C, D and E?
Sol: [N/5] = 6; N= 5*5+1 =26
Example 10 During a month with 30 days a baseball team plays at least 1 game a day, but no more
than 45 games. Show that there must be a period
of some number of consecutive days during which the team must play exactly 14 games.
day 1 2 3 4 5 ... 15 30
# of game 3 2 1 2
sum ≤ 45
存在一段時間的game數和=14
Some Elegant Applications of the Pigeonhole Principle
Sol:
Let a_{j} be the number of games played on or before the jth day of the month. (第1天～第j天的比賽數和)
Then is an increasing sequence of distinct integers with
Moreover, is also an increasing sequence of distinct integers with
There are 60 positive integers
between 1 and 59. Hence, such that
) 30 1
( ≤ j ≤
30 2
1
, a ,..., a a
j a_{j} ≤ ∀
≤ 45 1
) 45 ...
1 ., .
(i e ≤ a_{1} < a_{2} < a_{3} < < a_{30} ≤
14 ,...,
14 ,
14 _{2} _{30}
1 + a + a +
a
59 14
15 ≤ a_{j} + ≤
) 59 14
...
14 14
14 15
., .
(i e ≤ a_{1} + < a_{2} + < a_{3} + < < a_{30} + ≤
14 ,...,
14 ,
,..., _{30} _{1} _{30}
1 a a + a +
a j i and
∃
場) 共14
天 天～第 第 1
., . (
14 i e j i
a
a_{i} = _{j} + +
#
Def. Suppose that is a sequence of numbers.
A subsequence of this sequence is a sequence of the form where
e.g. sequence: 8, 11, 9, 1, 4, 6, 12, 10, 5, 7 subsequence: 8, 9, 12 (9)
9, 11, 4, 6 (8)
Def. A sequence is called increasing (遞增) if A sequence is called decreasing (遞減) if
A sequence is called strictly increasing (嚴格遞增) if A sequence is called strictly decreasing (嚴格遞減) if
aN
a
a_{1}, _{2},...,
im
i
i
a a
a , ,...,
2 1
N i
i
i < < < _{m} ≤
≤ ...
1 _{1} _{2}
)
., .
(i e 保持原順序
+1
≤ _{i}
i a
a
+1
≥ _{i}
i a
a
+1
< _{i}
i a
a
+1
> _{i}
i a
a
Thm 3. Every sequence of n^{2}+1 distinct real numbers contains a subsequence of length n+1 that is either strictly increasing or strictly decreasing.
Example 12. The sequence 8, 11, 9, 1, 4, 6, 12, 10, 5, 7 contains 10=3^{2}+1 terms (i.e., n=3).
There is a strictly increasing subsequence of length four, namely, 1, 4, 5, 7. There is also a decreasing subsequence of length 4, namely, 11, 9, 6, 5.
Exercise 21 Construct a sequence of 16 positive integers that has no increasing or decreasing
subsequence of 5 terms.
Sol:
Exercise: 5, 13 (參考習題：21, 23)
13 , 14 , 15 , 16 9
, 10 , 11 , 12 5
, 6 , 7 , 8 1 , 2 , 3 , 4
§4.3 Permutations(排列) and Combinations(組合)
Def. A permutation of a set of distinct objects is an ordered arrangement of these objects. An ordered arrangement of r elements of a set is called an rpermutation.
Example 1. Let The arrangement 3,1,2 is a permutation of S. The arrangement 3,2 is a
2permutation of S.
Thm 1. The number of rpermutation of a set with n distinct elements is
位置： 1 2 3 … r
□ □ □ … □
↑ ↑ ↑ … ↑ 放法：
}.
3 , 2 , 1
= { S
)!
( ) ! 1 )...(
2 (
) 1 (
) ,
( n r
r n n n
n n r
n
P = ⋅ − ⋅ − − + = −
n
n−1n − 2
^{n}^{−r} ^{+}^{1}Example 2’. How many different ways are there to select 4 different players from 10 players on a team to play
four tennis matches, where the matches are ordered ? Sol:
Example 4. Suppose that a saleswoman has to visit
8 different cities. She must begin her trip in a specified city, but she can visit the other cities in any order she wishes. How many possible orders can the saleswoman use when visiting these cities ?
Sol:
. 5040 7
8 9 10 )
4 , 10
( = × × × =
P
5040
!
7 =
Def. An rcombination of elements of a set is an unordered selection of r elements from the set.
Example 6 Let S be the set {1, 2, 3, 4}.
Then {1, 3, 4} is a 3combination from S.
Thm 2 The number of rcombinations of a set with n elements, where n is a positive integer and r is an integer with , equals
pf :
)!
(
!
!
! ) ,
)
(( )
,
( n r
^{n}_{r} ^{p} _{r}^{n} ^{r} _{r} _{n}^{n} _{r}C
C
_{r}^{n}= = = =
_{−}n r ≤
≤ 0
! )
, ( )
,
( n r C n r r
P = ×
稱為 binomial coefficient
Example 7. We see that C(4,2)=6, since the 2combinations of {a,b,c,d} are the six subsets {a,b}, {a,c}, {a,d}, {b,c}, {b,d} and {c,d}
Corollary 1. Let n and r be nonnegative integers with r ≤ n.
Then C(n,r) = C(n,n−r) pf : From Thm 2.
組合意義：選 r 個拿走,相當於是選 n − r 個留下.
) ,
))! ( (
( )!
(
! )!
(
! ) !
,
( C n n r
r n n
r n
n r
n r r n
n
C = −
−
−
= −
= −
Example 8. How many ways are there to select 5
players from a 10member tennis team to make a trip to a match at another school ?
Sol: C(10,5)=252
Example 11. How many ways are there to select a committee if the committee is to consist of 3
faculty members from the math department and 4 from the computer science department, if there are 9 faculty members of the math department and
11 of the computer science department ? Sol:
Exercise: 3, 11, 13, 21, 33, 34.
) 4 , 11 ( )
3 , 9
( C
C ×
§4.5 Binomial Coefficients (二項式係數)
Example 1.
要產生 xy^{2} 項時，
需從三個括號中選兩個括號提供 y，剩下一個則提供 x (注意：同一個括號中的 x 跟 y 不可能相乘)
∴共有 種不同來源的 xy^{2}
⇒ xy^{2} 的係數 =
∴
Thm 1. (The Binomial Theorem, 二項式定理)
Let x,y be variables, and let n be a positive integer, Then
3 2
2 3
3 ( )( )( ) ? ? ?
)
(x + y = x + y x + y x + y = x + x y + xy + y
) (^{3}_{2}
) (^{3}_{2}
3 3
3 2
3 2 2
3 1 3
3 0
3
( ) ( ) ( ) ( )
)
( x + y = x + x y + xy + y
∑
=−
−
−
− + + + =
+
=
+ ^{n}
j
j j n n
j n
n n n
n n n
n n
n
n x x y xy y x y
y x
0 1
1 1
1
0) ( ) ... ( ) ( ) ( )
( )
(
Example 4. What is the coefficient of x^{12}y^{13} in the expansion of ?
Sol:
∴
Cor 1. Let n be a positive integer. Then
pf : By Thm 1, let x = y = 1
Cor 2. Let n be a positive integer. Then pf : by Thm 1. (1−1)^{n }= 0
)25
3 2
( x − y
25
25 (2 ( 3 ))
) 3 2
( x − y = x + − y
13 12
25
13 ) 2 ( 3)
( ⋅ ⋅ −
) ( ...
) ( ) ( ) ( )
1 1
( +
^{n}=
_{0}^{n}+
_{1}^{n}+
_{2}^{n}+ +
^{n}_{n}∑
==
n −
k
n k k 0
0 )
( ) 1 (
∑
== +
+ +
n =
k
n n
n n
n n
k 0
1
0) ( ) ... ( ) 2 (
) (
Thm 2. (Pascal’s identity)
Let n and k be positive integers with n ≥ k Then
⎟⎟⎠
⎜⎜ ⎞
⎝ +⎛
⎟⎟⎠
⎜⎜ ⎞
⎝
⎛
= −
⎟⎟⎠
⎜⎜ ⎞
⎝
⎛ +
k n k
n k
n
1 1
PASCAL’s triangle
) (^{0}_{0}
)
(^{1}_{0} (^{1}_{1})
)
( _{0}^{2} (_{1}^{2} ) (^{2}_{2} )
)
(^{3}_{0} (_{1}^{3} ) (^{3}_{2} ) (^{3}_{3} )
) (_{3}^{4}
…
1
1 1
1 2 1
1 3 3 1
… 4
1 4 6 1
pf : ①利用 來證
②(組合意義):
)!
(
! ) !
,
( j i j
j i i
C = −
Suppose that T is a set containing n+1 elements.
Let and
A subset of T with k elements either contains a together with k−1 elements of S,
or contains elements of S. _{#} T
a∈ S = T −{a}.
n
‧ a
k
取法
=
a
k n
‧ 0
a
k−1 +
n
‧ 1
Thm 3. (Vandermode’s Identity)
pf :
∑
= +⋅
−
= +
≤
≤ Ζ
∈
r
k
k n C k
r m C r
n m
C
n m r
r n m
0
) , ( )
, ( )
, (
, 0
, ,
,
∑
=⋅
−
=
=
= +
r
k
k n C k
r m C r
n m
C
0
) , ( )
, (
個的方法 取
) (
m n
m n m n m n
↓↓ + ↓↓ +...+ ↓↓
0, r 1, r−1 r, 0
)!
(
!
!
! ) ,
) (
( ) ,
(n r ^{n}_{r} ^{p} _{r}^{n} ^{r} _{r} _{n}^{n} _{r} C
C_{r}^{n} = = = = _{−}
Ex 33. Here we will count the number of paths between the origin (0,0) and point (m,n) such that each path is made up of a series of
steps, where each step is a move one unit to the right or a more one unit upward.
1 4 10 20 35 56 (5,3) 1 3 6 10 15 21
1 2 3 4 5 6 (0,0) 1 1 1 1 1
0 1 1 0 0 0 1 0
Each path can be Represented by a bit String consisting of m Os and n 1s.
(→) (↑) 56 8 7
! 3
! 5
! 8 5
3 5
=
×
=
⎟ =
⎠⎞
⎜⎝
⎛ +
There are paths of the desired type._{⎟⎟}
⎠
⎜⎜ ⎞
⎝
⎛ + n
n m
Exercise: 7, 21
§5.5 Generalized Permutations and Combinations
∴⎜⎝⎛ + − ⎟⎠⎞ 種方法 _{#} r
n
r 1
Thm 1. The number of rpermutations of a set of n objects with repetition allowed is n^{r}.
Thm 2. There are rcombinations from a set with n elements when repetition of elements is allowed.
pf : (視為有 r 個＊，要放入 n 個位置，即需插入 n−1 個bar 將之隔開)
) , 1 (r n r C + −
＊＊｜＊｜｜＊＊＊
a_{1}出現2次
a_{2}出現1次 a3不出現
a_{4}出現3次
} ,
, , , ,
{a_{1} a_{1} a_{2} a_{4} a_{4} a_{4}
取法
} ,
, ,
{a_{1} a_{2} a_{3} a_{4} 例： 設n = 4, 集合為
Example 4. Suppose that a cookie shop has 4 different kinds of cookies. How many different ways can
6 cookies be chosen?
Sol: 6個cookie插入3個bar ⇒ 種
Example 5. How many solutions does the equation have, where are nonnegative integers?’
Sol: 11個1間要插入2個bar
⎟⎠
⎜ ⎞
⎝⎛ + 6
3 6
3 11
2
1 + x + x = x
3 2 1, x , x x
⎟⎠
⎜ ⎞
⎝⎛ + 11
2
⇒ 11 種
※若上題改要求 則原式
可改成
此時相當於是
其中 且
∴5個1間要插入2個bar 種 (注意： case
相當於 )
※若上題再改為
則需排除 (即 的情況)
因
∴共有 種
3
, 2
,
1 _{2} _{3}
1 ≥ x ≥ x ≥
x
3 11
2
1 + x + x = x
5 3 2 1 11 )
3 (
) 2 (
) 1
(x_{1} − + x_{2} − + x_{3} − = − − − =
3 5
2
1 + y + y =
y y_{1} = x_{1} −1, y_{2} = x_{2} −2, … Ν
3 ∈
2 1, y , y y
⎟⎠
⎜ ⎞
⎝⎛ + 5
2 5
1 y , 3
,
1 _{2} _{3}
1 = y = =
y
4 x
, 5
,
2 _{2} _{3}
1 = x = =
x
3 x
, 2
, 3
1≤ x_{1} ≤ x_{2} ≥ _{3} ≥
1 > 3
x x_{1} > 4
2 9 11 )
3 (
) 2 (
) 4
(x_{1} − + x_{2} − + x_{3} − = − =
⎟⎠
⎜ ⎞
⎝⎛ +
⎟−
⎠⎞
⎜⎝
⎛ +
2 2 2 5
2 5
※ Permutations with indistinguishable objects
Example 8. How many different strings can be made by reordering the letters of the word
SUCCESS ? Sol:
有3個S, 2個C, 1個U 及 1個E,
可放S的位置有 種
剩下的4個位置中可放C的有 種
剩下的2個位置中可放U的有 種
剩下的1個位置中可放E的有 種
∴共 種
) (^{7}_{3}
) (^{4}_{2}
) (_{1}^{2}
) (^{1}_{1}
) )(
)(
)(
(^{7}_{3} ^{4}_{2} _{1}^{2} ^{1}_{1}
Thm 3. The number of different permutations of n objects, where type 1： n_{1}種
type 2： n_{2}種 is type k： n_{k}種
pf :
※ Distributing objects into Boxes
Example 9. How many ways are there to distribute hands of 5 cards to each of four players from the standard deck of 52 cards?
Sol: player 1： 種
player 2： 從剩下的牌再發5張
!
!
!
!
2
1 n nk
n
n
M L
⎟⎟⎠
⎜⎜ ⎞
⎝
⎛ − − − −
⎟⎟⎠
⎜⎜ ⎞
⎝
⎛ − −
⎟⎟⎠
⎜⎜ ⎞
⎝
⎟⎟⎛ −
⎠
⎜⎜ ⎞
⎝
⎛ _{−}
k
k
n
n n
n n n
n n n n
n n n
n _{1} _{2} _{1}
3 2 1 2
1 1
L L
!
!
!
!
2
1 n k
n n
L
=
⎟⎠
⎜ ⎞
⎝⎛ 5 47
⎟⎠
⎜ ⎞
⎝⎛ 5 52
注意：上題相當於將52張牌放進5個不同的box的放法, 即 box 1 的給 player 1
box 2 的給 player 2 box 3 的給 player 3 box 4 的給 player 4 而 box 5 的是剩下的牌.
Thm 4. The number of ways to distribute n distinguishable objects into k distinguishable boxes so that n_{i}
objects are placed into box equals (跟Thm 3相等)
Exercise: 15, 20, 17, 25, 31, 55
!
!
!
!
2
1 n nk
n
n L
k i
i, =1,2,L,
! 32
! 5
! 5
! 5
! 5
! 52
! 32
! 5
! 37
! 37
! 5
! 42
! 42
! 5
! 47
! 47
! 5
! 52 5
37 5
42 5
47 5
52⎟⎠⎞⎜⎝⎛ ⎟⎠⎞⎜⎝⎛ ⎟⎠⎞⎜⎝⎛ ⎟⎠⎞ = ⋅ ⋅ ⋅ =
⎜⎝
∴⎛