第五章計數 - 大葉大學

advertisement
Discrete
Mathematics
Chapter 5 Counting(計數)
大葉大學 資訊工程系 黃鈴玲
§5.1 計數的基礎
 A counting problem: (Example 15)
電腦系統的每個使用者有一組密碼,長度為6-8字
元,每個字元是大寫英文字母或十進位的數字,每組
密碼必須包含至少一個數字。共有多少組可能的密碼?
 本節介紹:
各種類型的計數問題
計數的基本技巧
Ch5-2
Basic counting principles
 The product rule (乘法法則):
假設一個程序可以分解為兩個連續的任務,第一個任務有
n1 種方式完成,第二個任務有 n2 種方式完成, 但第二個任
務必須等第一個任務完成才能進行,則完成整個程序的方法
數為 n1 × n2。
台北
n1
台北到台中有n1 條路
n2
台中到高雄有n2 條路
台中
高雄
共n1 × n2 ways
台北到高雄有n1 × n2條路
Ch5-3
Example 2 大禮堂的座椅可以用一個英文字母及一個不超
過100的數字來編號,最多能有多少個不同編號的座位?
Sol:
letter 1  x  100
26× 100 = 2600
x Ν
Def. bit string (位元字串): 由0,1構成的二進位字串
decimal string (十進位字串): 每個字元為0~9的字串
Example 4 長度為7的bit string有多少?
Sol: 1 2 3 4
5
6 7
□ □ □ □ □ □ □
↑
↑
↑
↑ ↑
↑
↑
→ 27 種
0,1 0,1 0,1
0,1 0,1
0,1 0,1
Ch5-4
Example 5
若每個車牌由3 個字母後面接著3個數字的序列構成,
共有多少種不同的車牌?
Sol: □ □ □
□□□
→263.103
letter
digit
Example 6 從一個含有m個元素的集合對應到另一個
含有n個元素的集合,可形成多少函數?
Sol:
a1
b1
f(a1)=?  b1~ bn, 共n種
f
a2
b2
f(a2)=?  b1~ bn, 共n種
.
.
.
.
:
.
.
am
bn
f(am)=?  b1~ bn, 共n種
∴nm
Ch5-5
(跳過)
Example 7 How many one-to-one functions are there
from a set with m elements to one with n element?
(m  n)
Sol:
f(a1) = ? 可以是b1~ bn, 共 n 種
f(a2) = ? 可以是b1~ bn, 但不能= f(a1), 共 n-1 種
f(a3) = ? 可以是b1~ bn, 但不能= f(a1), 也不能=f(a2),
共 n-2 種
:
:
f(am) = ? 不可=f(a1), f(a2), ... , f(am-1), 故共n-(m-1)種
∴共 n.(n-1).(n-2).....(n-m+1)種 1-1 function #
Ch5-6
Exercise 5.1
11. 首尾都是1,長度為n的十進位字串共有幾個?
23. 含有3個數字且滿足下列條件的字串共有幾個?
(a) 同一個數字不出現3次
(b) 以奇數數字開頭
(c) 恰有2個數字是4
Ch5-7
 The sum rule (加法法則):
若第一種任務可用 n1 種方法完成, 第二種任務
可用 n2 種方法完成, 且兩種任務不能同時做,
只能做其中一種,則共有 n1+n2 種方法完成其中
一種任務。
台北
台北到高雄,從西部走有n1 條路
n1
n2
台北到高雄,從東部走有n2 條路
高雄
n1 + n2 ways
台北到高雄共有n1 + n2條路
Ch5-8
Example 11
假設任一位數學系的教師或學生都可被推選為大學的委員會
代表,現在數學系有37位教師,83位學生,則共有多少種
不同的選擇?
Sol: 37+83 = 120
Example 12 一位學生可以從三張清單中選擇一個電
腦專題來進行,這三張清單分別有23、15 及 19 個題目,
則他共有多少種選擇?
Sol: 23+15+19=57
Ch5-9
Example 15電腦系統的每個使用者有一組密碼,長度為
6-8字元,每個字元是大寫英文字母或十進位的數字,每
組密碼必須包含至少一個數字。共有多少組可能的密碼?
Sol: 設Pi 代表長度為 i 的可能密碼數,其中i = 6,7或8
則 P6 = 366 - 266
P7 = 367 - 267
P8 = 368 - 268
∴可能的密碼總數為
P6 + P7 + P8 = 366 + 367 + 368 - 266 - 267 - 268種
Ch5-10
Example 14
在計算機語言BASIC的某個版本中,變數名稱必須是長
度為1或2的英文字母或數字,其中英文字母不分大小寫,
但變數名稱必須用英文字母為開頭,而且有五個長度為2
的保留字串不能使用。共有多少種不同的變數名稱?
Sol:
令 Vi 代表長度 i 的變數個數
V1 =26
V2 =2636 – 5
∴共有 26 + 2636 – 5 個不同的變數
Ch5-11
Exercise 5.1
27. 車牌是2個字母後面接4個數字,或是2個數字後
面接4個字母的方式編成,共有幾種不同的車牌?
39. 如果一個字串反轉後,與原來的字串一樣,
(例如:abcdcba) 稱為迴文(palindrome),
長度為n的位元字串共有多少種迴文?
Ch5-12
※ The Inclusion-Exclusion Principle (排容原理)
A B = A  B - A B
A
B
Example 17 有多少bit string長度為8,且用 bit 1 開頭或用
bits 00 做結尾?
Sol:
12345678
□□□□□□□□
↑ ↑
↑
① 1 0,1 0,1 . . . . . . 0,1 → 共27種
②
............ 0 0
→ 共26種
③ 1 ........... 0 0
→ 共25種
 ①+ ②- ③ 共有27 +26 -25 種
Ch5-13
Exercise 5.1
17. 由 5 個ASCII碼構成且至少包含一個@字元的
字串共有幾個?(ASCII碼共有128種)
47. 不超過100且能被4或6其中之一整除的正整數
共有多少個?
Ch5-14
※ Tree Diagrams (樹狀圖)
Example 18
有多少bit string長度為4而且不包含兩個
連續的1?
Sol:
0
0
0
樹根
1
1
1
0
0
0
1
bit 1
0 (0000)
1 (0001)
0 (0010)
0
1
0
1
0
(0100)
(0101)
(1000)
(1001)
(1010)
∴ 8 bit strings
bit 3
Ch5-15
Exercise 5.1
53. 排列字母a、b和c,使得b不緊跟在a之後,
共有多少種不同的排法?
Ch5-16
Ex 38. 含有100個元素的集合,有多少子集合的元素個數大
於1?
Sol:
100  100  100 
100 
  
  
  ...  
 =  = 2100 - 101
(1) 
100   99   98 
 2 
(2)
a1 , a2 ,..., a100
subset :  □,□,..., □
放 放
不放 不放
放
不放
Thm. 1 of §5.4
 2100 - 101
空集合及
只有1個元素的集合
Ch5-17
§5.2
The Pigeonhole Principle (鴿洞原理)
Theorem 1 (鴿洞原理)
若 k+1 或更多的物件要放入 k 個盒子,則至少有一個盒子
裡包含了兩個或更多個物件。
Proof (反證)
假設這 k 個盒子裡,沒有任何盒子裝了至少兩個物件,
則每個盒子最多只裝一個物件, k 個盒子的物件合起來最多
只有k 個,與物件總數 “k+1 或更多” 矛盾。
Example 1. 任取367人,必定有至少兩人的生日同一天,
因為一年最多366天。
Example 2 任取 27 個英文字(word),必有至少兩個字
開頭的字母相同。
Ch5-18
Example 3 一個班級必須有至少多少位同學,才能保證
有兩人的期末考分數相同? (0~100 分)
Sol:
102.
(101+1)
Theorem 2. (廣義的鴿洞原理)
若 N 個物件要放入k 個盒子裡,則一定有至少一個盒子裡
的物件數目 
N
 k 。
 
例: 21 個物件放入 10 個盒子
 必定有某個盒子裡的物件數 
Example 5 在100人中,至少有
 21 
10  = 3
 
100 
 12  = 9 人在同一個月份出生。
Ch5-19
Example 6 若成績分為ABCDE五種標準,在一個離
散數學的班級裡,最少要多少位學生,才能保證至
少有6位學生得到相同分數?
Sol: 找最小的N使得 N/5 =6
故 N 為 55+1=26
Ch5-20
Exercise 5.2
5. 證明在任意五個整數中(不一定連續),有兩個整
數除以4的餘數相等。
13. 如果從 1, 2, …, 8 這八個整數中選出5個整數,
一定有一對整數的和等於9。
15. 必須從{1, 2, 3, 4, 5, 6}中選取多少數字,才能保
證至少有一對整數的和等於7?
31. 一個大學有38個不同的課程時段,如果有677門
不同的課程,則需要幾間教室?
Ch5-21
(跳過)
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
# of game 3
2
1
4
5
... 15
30
2
sum  45
存在一段時間的game數和=14
Ch5-22
(跳過)
Sol:
Let aj be the number of games played on or before the
jth day of the month. (第1天~第j天的比賽數和) (1  j  30)
Then a1 , a2 ,..., a30 is an increasing sequence of distinct
integers with 1  a j  45 j
(i.e., 1  a1  a2  a3  ...  a30  45)
Moreover, a1  14, a2  14,..., a30  14 is also an increasing
sequence of distinct integers with 15  a j  14  59
(i.e., 15  a1  14  a2  14  a3  14  ...  a30  14  59)
There are 60 positive integers a1 ,..., a30 , a1  14,..., a30  14
between 1 and 59. Hence,  i and j such that
ai = a j  14 (i.e., 第j  1天~第i天 共14場)
#
Ch5-23
(序列, 數列)
Def. Suppose that a1 , a2 ,..., aN is a sequence of numbers.
A subsequence (子序列) of this sequence is a sequence of
the form ai1 , ai2 ,..., aim where 1  i1  i2  ...  im  N
(i.e., 保持原順序)
例:
sequence: 8, 11, 9, 1, 4, 6, 12, 10, 5, 7
subsequence: 8, 9, 12
()
9, 11, 4, 6 ()
Ch5-24
A sequence is called increasing (遞增) if ai  ai 1
A sequence is called decreasing (遞減) if ai  ai 1
A sequence is called strictly increasing (嚴格遞增) if ai  ai 1
A sequence is called strictly decreasing (嚴格遞減) if ai  ai 1
Def.
例: 1, 2, 6, 12, 17 (嚴格遞增)
1, 2, 6, 6, 10
(遞增)
10, 8, 5, 5, 3
(遞減)
10, 8, 5, 4, 3
(嚴格遞減)
Ch5-25
(跳過)
Theorem 3. Every sequence of n2+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=32+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: 4,3,2,1 8,7,6,5 12,11,10,9 16,15,14,13
Ch5-26
§5.3 Permutations(排列) and Combinations(組合)
Def. 將一集合中的相異的物件做有順序的位置安排,稱為
排列(permutation)。自一集合中取出 r 個物件排列稱為r-排列
(r-permutation)。
Example 2. Let S = {1, 2, 3}.
3,1,2 是 S 的一個排列,而 3,2 是 S 的一個2-排列。
Theorem 1. 從含有n個元素的集合中選取 r 個元素來做排
列的方法數有
n! 。
P(n, r ) = n  (n - 1)  (n - 2)...( n - r  1) =
(n - r )!
Pf:
位置: 1 2 3 … r
□ □ □ … □
放法:
n
n -1 n - 2
…
n - r 1
Ch5-27
Example 4. 從100個參賽者中,可能產生的第一名、第二名
和第三名的組合有幾種?
Sol:
P(100,3) = 100  99  98
Example 6. 假設一銷售員要拜訪8個城市,她必須由某一城
市出發,但可以依任何順序拜訪其他城市。這個銷售員有多
少種不同的走法?
Sol:
7!= 5040
Ch5-28
Exercise 5.3
3. 在 {a, b, c, d, e, f, g} 中有多少排列由 a 做為結尾?
13. 一個包含n個男人與n個女人的團體。將所有人
一男一女交錯排成一列的方式有幾種?
21. 由ABCDEFG的排列中,包含下列字串的排列分別
有多少種?
(a) 字串BCD。
(c) 字串BA與GF。
(e) 字串ABC與CDE。
(f) 字串CBA與BED。
Ch5-29
※ Combination (組合)
Def. 一個集合中元素的 r 組合(r-combination) 是指由
此集合中不考慮順序地取出 r 個元素。
Example 9 Let S be the set {1, 2, 3, 4}.
Then {1, 3, 4} is a 3-combination from S.
Theorem 2 設 n, r 為整數且0  r  n,則一個包含 n 個
相異元素的集合,其 r-組合的個數為
C = C (n, r )
n
r
= (nr )
=
p ( n, r )
r!
=
n!
r!( n-r )!
稱為 二項式係數(binomial coefficient)
pf :
P(n, r ) = C (n, r )  r!
Ch5-30
Example 10. We see that C(4,2)=6, since the
2-combinations of {a,b,c,d} are the six subsets
{a,b}, {a,c}, {a,d}, {b,c}, {b,d} and {c,d}
Corollary 2. Let n and r be nonnegative integers with r  n.
Then C(n, r) = C(n, n-r)
pf : From Thm 2.
n!
n!
C (n, r ) =
=
= C (n, n - r )
r!(n - r )! (n - r )!(n - (n - r ))!
組合意義:選 r 個拿走,相當於是選 n - r 個留下.
Ch5-31
Example 12. 一個10人組成的網球隊中要挑出5個選手到
外校參賽,有多少種組合?
Sol: C(10,5)=252
Example 15. 假設數學系有 9 位教師,資訊系有 11 位
教師。要組成一個包含3位數學教師及4位資訊教師的委
員會,有多少種組合?
Sol:
C (9,3)  C (11,4)
Ch5-32
Exercise 5.3
11. 長度為10的位元字串(bit string)中,滿足下列條件
的字串分別有多少個?
(a) 字串中恰巧有4個1。
(b) 字串中至多有4個1。
(d) 字串中0與1的個數相等。
34. 假設一個部門有10個男人和15個女人,若要組成一
個六人小組,且其中女人的數目多餘男人,有幾種
組合?
Ch5-33
§5.4
Binomial Coefficients (二項式係數)
Example 1.
( x  y)3 = ( x  y)( x  y)( x  y) = ? x3  ? x 2 y  ? xy2  ?? y 3
要產生 xy2 項時,
需從三個括號中選兩個括號提供 y,剩下一個則提供 x
(注意:同一個括號中的 x 跟 y 不可能相乘)
3
2
∴共有 ( )種不同來源的
xy2
 xy2 的係數 =
3
2
()
∴ ( x  y ) 3 = ( 30 ) x 3  (13 ) x 2 y  ( 32 ) x y 2  ( 33 ) y 3
Ch5-34
Theorem 1. (The Binomial Theorem, 二項式定理)
Let x, y be variables, and let n be a positive integer, then
n
( x  y ) n = ( 0n ) x n  (1n ) x n -1 y  ...  ( nn -1 ) xy n -1  ( nn ) y n =  ( nj ) x n - j y j
j =0
( x  y)1 = (10 ) x  (11 ) y = x  y
( x  y) 2 = (02 ) x 2  (12 ) xy  ( 22 ) y 2 = x 2  2 xy  y 2
( x  y)3 = (30 ) x 3  (13 ) x 2 y  (32 ) xy2  (33 ) y 3
( x  y) 4 = (04 ) x 4  (14 ) x3 y  ( 42 ) x 2 y 2  (34 ) xy3  ( 44 ) y 4
Exercise 5.4:
1. 寫出(x+y)5的展開式
Ch5-35
Example 4.
Sol:
在 (2 x - 3 y )
25
的展開式中,x12y13 項的係數為何?
(2 x - 3 y) 25 = (2 x  (-3 y)) 25
25
∴ (13
)  212  (-3)13
n
Corollary 1.
n
n
n
n
n
(
)
=
(
)

(
)

...

(
)
=
2
令 n 為正整數,則  k
0
1
n
k =0
pf : By Thm 1, let x = y = 1
(1  1) n = ( 0n )  (1n )  ( n2 )  ...  ( nn )
n
k n
Corollary 2.令 n 為正整數,則  (-1) ( k ) = 0
k =0
pf : by Thm 1,
(1-1)n = 0
Ch5-36
Exercise 5.4
8. 在(3x2y)17 的展開式中,x8y9 項的係數為何?
7. 在 (2-x)19 的展開式中,x9 項的係數為何?
Ch5-37
Theorem 2. (帕斯卡等式 Pascal’s identity)
Let n and k be positive integers with n  k
Then
 n  1  n   n 

 = 
   
 k   k - 1  k 
PASCAL’s triangle
( 00 )
(10 )
(x+y)2展開
式係數
( 30 )
2
0
1
2
1
( )
(13 )
1
2
2
( )
1
1
(11 )
( )
4
1
4
6
1
…
…
( 34 )
1
1
3
3
1
( 33 )
( 32 )
2
Ch5-38
 n  1  n   n 

 = 
   
 k   k - 1  k 
pf : ①(algebraic proof, 代數證明)
 n  1 = (n  1)!
 k 

 k!(n  1 - k )!
n!
n!
 n   n =

 k - 1  k 

   (k - 1)!(n - k  1)! k!(n - k )!
=
k  n!
(n - k  1)  n!
(n  1)  n!

=
k!(n - k  1)! k!(n - k  1)! k!(n - k  1)!
②(combinatorial proof, 組合意義證明):
n
1
‧
k
取法
=
n
1
‧
k-1 1
+
n 1
‧
k 0

 n  1  n 1  n  1 

 = 
     
 k   k - 11  k  0 
 n  n
   
= 
 k - 1  k 
Ch5-39
Exercise 5.4
12.
10 
在帕斯卡三角形中,某一列的數字為  k  ,
其中0 k  10,亦即
1 10 45 120 210 252 210 120 45 10 1
利用帕斯卡等式,找出下一列的數字。
Ch5-40
(Vandermode’s Identity)
Theorem 3.
(跳過)
m, n, r    , 0  r  m, n
r
C (m  n, r ) =  C (m, r - k )  C (n, k )
k =0
pf :
m
n
C(m+n, r) =
mn
mn
mn
↓ ↓ + ↓ ↓ +...+ ↓ ↓
r, 0
r-1, 0
0, r
=
r
=  m  n    m  n   ...   m  n 
 r  0   r - 1 1 
 0  r 
r
=  C (m, r - k )  C (n, k )
k =0
Ch5-41
Ex 33. 計算在xy平面中由原點 (0,0) 走到點 (m, n)的路徑
數目,其中。每條路徑由一連串的移動組成,每次移動只
能往右走一單位長或往上走一單位長。
1
4
10
20
35 56 (5,3)
1
3
6
10
15
21
1
2
3
4
5
6
將每條路徑用位元字串(bit string)
來表示,其中 0表示往右移動,
1表示往上移動
紅色路徑對應的字串: 0 1 1 0 0 0 1 0
(0,0)
1
1
1
1
1
8!
故由(0,0)走到(5, 3)的路徑數,就是  5  3 
=
= 56


長度為8,包含5個0及3個1的字串數  5  5!3!
m  n
由(0,0)走到(m, n)的路徑數為  n 


Ch5-42
Download