114 年 國立成功大學工程科學系碩士班己組《計算機概論》
第 1 題20 分
- This question tests your ability to implement efficient algorithms, analyze their performance, and optimize their computational complexity.
(1) Implement an efficient algorithm to compute Fibonacci numbers using matrix exponentiation. Analyze the time complexity and explain why this approach is more efficient than traditional recursive solutions. (5%)
(2) Please finish the code to solve func(n) for following sequence: 1, 1, 2, 6, 24, 120, 720, ... where func(0) = 1, func(1) = 1, func(2) = 6, func(3) = 24, .... (Use recursion to complete the func())
def func(n):
# Please finish the code here.
pass
print(func(n))
(5%)
(3) Solve the following problem: Given a sequence of numbers, find the longest increasing subsequence. Provide the implementation and analyze its time complexity. (5%)
(4) Write Python implementations for both Selection Sort and Insertion Sort. Conduct a performance comparison between the two algorithms for arrays of varying lengths, and present the results in a graphical format. (5%)
登入後即可作答並保存紀錄。
核心觀念
本題綜合考查:
- 矩陣快速冪與 Fibonacci 數列。
- 遞迴定義與基底條件。
- 最長遞增子序列(Longest Increasing Subsequence, LIS)。
- Selection Sort、Insertion Sort 的實作、複雜度與實驗比較。
(1)利用矩陣快速冪計算 Fibonacci 數
定義與推導
Fibonacci 數列定義為:
可表示為矩陣關係:
令
則:
因此, 的左下角元素就是 。
矩陣快速冪
計算 時使用二分冪:
- 若 ,結果為單位矩陣。
- 若 為偶數,先計算 ,再平方。
- 若 為奇數,先計算 ,再乘上 。
Python 實作
def multiply(X, Y):
return [
[
X[0][0] * Y[0][0] + X[0][1] * Y[1][0],
X[0][0] * Y[0][1] + X[0][1] * Y[1][1]
],
[
X[1][0] * Y[0][0] + X[1][1] * Y[1][0],
X[1][0] * Y[0][1] + X[1][1] * Y[1][1]
]
]
def matrix_power(A, n):
result = [
[1, 0],
[0, 1]
]
while n > 0:
if n % 2 == 1:
result = multiply(result, A)
A = multiply(A, A)
n //= 2
return result
def fibonacci(n):
if n < 0:
raise ValueError("n 必須為非負整數")
if n == 0:
return 0
A = [
[1, 1],
[1, 0]
]
return matrix_power(A, n)[0][1]
print(fibonacci(10)) # 55
複雜度分析
矩陣指數每次約減半,因此需要 次矩陣乘法。由於本題矩陣固定為 ,每次乘法皆為常數時間:
迴圈版本的額外空間為:
傳統遞迴:
def fibonacci_slow(n):
if n <= 1:
return n
return fibonacci_slow(n - 1) + fibonacci_slow(n - 2)
會重複計算大量相同子問題,其時間複雜度約為:
因此矩陣快速冪在 很大時明顯有效率得多。
解題技巧
看到下列特徵時,可考慮矩陣快速冪:
- 遞迴關係是線性的。
- 需要計算非常大的第 項。
- 題目要求優於 的演算法。
(2)以遞迴完成 func(n)
題意釐清
題目列出的數列為:
這是階乘數列:
因此合理的函數定義為:
題目文字寫成 func(2) = 6、func(3) = 24,與前述數列索引不一致;以下依數列及階乘定義,採用 func(2)=2、func(3)=6。
遞迴程式
def func(n):
if n < 0:
raise ValueError("n 必須為非負整數")
if n == 0:
return 1
return n * func(n - 1)
n = 6
print(func(n)) # 720
執行推導
以 為例:
複雜度分析
每次遞迴使 減少 ,共呼叫 層:
遞迴呼叫堆疊的空間為:
解題技巧
遞迴函數必須包含:
- 基底條件,避免無限遞迴。
- 每次呼叫都要朝基底條件前進。
- 遞迴式必須符合數列的數學定義。
(3)最長遞增子序列 LIS
核心觀念
子序列不要求元素連續,只要求保留原本的相對順序。
例如:
[10, 9, 2, 5, 3, 7, 101, 18]
其中一組最長遞增子序列為:
[2, 3, 7, 101]
長度為 。
本題採用 tails 方法:
tails[k]表示長度為 的遞增子序列,其結尾元素的最小可能值。- 對每個新元素,以二分搜尋找到第一個大於等於它的位置。
- 將該位置的值替換為較小的結尾值。
tails 的內容不一定本身就是最後的 LIS,但其長度一定等於 LIS 長度。
只求長度的實作
from bisect import bisect_left
def lis_length(nums):
tails = []
for x in nums:
position = bisect_left(tails, x)
if position == len(tails):
tails.append(x)
else:
tails[position] = x
return len(tails)
nums = [10, 9, 2, 5, 3, 7, 101, 18]
print(lis_length(nums)) # 4
執行流程
對數列:
10, 9, 2, 5, 3, 7, 101, 18
tails 的變化如下:
10
9
2
2, 5
2, 3
2, 3, 7
2, 3, 7, 101
2, 3, 7, 18
最後長度為 。
若要求輸出實際子序列
第 2 題20 分
- This question examines your ability to design efficient data structures and algorithms to solve complex problems.
(1) Design a data structure that supports the following operations efficiently:- Insert an element.
- Count the number of elements within a specified range.
- Delete a specific element.
Describe its structure and analyze the time complexity of each operation. (5%)
(2) Given the sequence N = [9,8,7,6,5,4,3,2,1,0], design an algorithm to compute the next lexicographic permutation. Extend your solution to handle sequences of length , and analyze the time complexity. (5%)
(3) For an AVL tree, insert the following numbers: 50, 20, 60, 10, 30, 70. Draw the resulting balanced tree and explain the rebalancing process step-by-step. (5%)
(4) Implement a hybrid sorting algorithm combining Quick Sort and Insertion Sort. Demonstrate how this hybrid algorithm outperforms pure Quick Sort for partially sorted arrays, and provide a time complexity analysis. (5%)
登入後即可作答並保存紀錄。
核心觀念
本題綜合考查四個主題:
- 增強型平衡搜尋樹:在維持排序的同時,額外記錄子樹大小,以支援區間計數。
- 字典序排列:利用 pivot、交換與反轉,原地求下一個字典序排列。
- AVL 樹:每個節點的左右子樹高度差不得超過 。
- 混合排序法:大區間使用 Quick Sort,小區間改用 Insertion Sort,以降低常數成本。
(1)支援插入、區間計數與刪除的資料結構
解題方法
採用「帶有子樹大小資訊的平衡二元搜尋樹」,例如 AVL Tree 或 Red-Black Tree。
每個節點除了儲存:
- 鍵值
key - 左子樹與右子樹
- 平衡資訊
再增加:
若允許重複元素,可在節點中增加 count,並將公式改為:
平衡樹可保證樹高為 。
1. Insert
插入元素時,依照二元搜尋樹規則尋找插入位置,沿途更新各節點的 size,最後透過 AVL 旋轉或 Red-Black Tree 的修正維持平衡。
時間複雜度:
2. Count the number of elements within a range
欲計算區間 中的元素數量,可使用:
其中:
- :小於或等於 的元素數量
- :嚴格小於 的元素數量
計算
從根節點開始:
- 若 ,往左子樹尋找。
- 若 ,則左子樹與目前節點都符合條件,累加:
再往右子樹尋找。
由於每次只沿著一條樹路徑前進,時間複雜度為:
因此區間計數也是:
3. Delete a specific element
依照二元搜尋樹規則尋找指定元素:
- 若沒有子節點,直接刪除。
- 若只有一個子節點,以子節點取代。
- 若有兩個子節點,以中序後繼或中序前驅取代,再刪除原節點。
- 沿途更新
size,並執行平衡修正。
時間複雜度:
複雜度總結
| 操作 | 時間複雜度 |
|---|---|
| Insert | |
| Range Count | |
| Delete | |
| 額外空間 |
解題技巧與常見陷阱
- 普通二元搜尋樹若退化成鏈狀,操作會變成 ,因此必須使用平衡樹。
- 只使用 Heap 無法有效支援任意區間計數。
- 只使用 Hash Table 可快速插入與刪除,卻無法有效處理大小關係與區間查詢。
- 若只需靜態資料的區間計數,也可使用排序陣列加 Binary Search;但本題包含動態插入與刪除,平衡樹更合適。
(2)求下一個字典序排列
核心觀念
對排列 求下一個字典序排列,標準方法如下:
-
從右向左找第一個滿足
的位置 ,稱為 pivot。
-
從右向左找第一個滿足
的位置 。
-
交換 與 。
-
將 到 的部分反轉。
若找不到 pivot,代表目前排列是最大字典序,下一個排列為最小排列,也就是整個序列反轉。
給定序列
此序列由大到小排列,已經是所有排列中的最大字典序。
因此找不到任何 使得:
將整個序列反轉:
所以其下一個字典序排列為:
關鍵程式碼
nextPermutation(A):
n = length(A)
i = n - 2
while i >= 0 and A[i] >= A[i + 1]:
i = i - 1
if i < 0:
reverse(A, 0, n - 1)
return
j = n - 1
while A[j] <= A[i]:
j = j - 1
swap(A[i], A[j])
reverse(A, i + 1, n - 1)
為什麼反轉 suffix?
找到 pivot 後,尾端區間 原本是非遞增排列。交換 pivot 後,要使結果比原排列大且增加幅度最小,就必須將尾端改成最小字典序。
非遞增區間的最小排列就是反轉後的非遞減排列,因此可以用反轉完成。
正確性說明
- 由右向左找 pivot,表示盡量保留較長的字尾。
- 選擇最右側且大於 pivot 的元素,可讓增加幅度最小。
- 將 suffix 排成遞增,確保 suffix 是能形成的最小字典序。
因此所得結果正好是下一個字典序排列。
複雜度分析
最多掃描整個陣列數次:
使用原地交換與反轉:
即使 ,仍可在線性時間內完成。
解題技巧
看到「next lexicographic permutation」時,直接記住:
若整個序列為非遞增排列,答案就是整個序列反轉。
(3)AVL Tree 插入與再平衡
核心觀念
AVL 樹對每個節點定義平衡因子:
合法 AVL 樹必須滿足:
若出現 或 ,便需要旋轉。
逐步插入
插入 50
50
平衡因子:
不需旋轉。
插入 20
50
/
20
仍然平衡。
插入 60
第 3 題20 分
- This question assesses your ability to work with different numerical representation systems, their limitations, and their practical applications.
(1) Assume a 16-bit floating-point format with the following structure:- 1-bit sign.
- 5-bit exponent using Excess-15 encoding.
- 10-bit mantissa (including an implicit leading 1).
Represent the number -45.75 in this format, and discuss how the system handles overflow when adding a large number to this value. (5%)
(2) Perform the addition of 45.75 and -45.75 using a fixed-point representation in the format ssss.ffff. Provide the result in binary and decimal forms. (5%)
(3) Design an algorithm to convert a 16-bit floating-point number into a fixed-point representation. Clearly describe the steps, and analyze the time and space complexity of your algorithm. (5%)
(4) Compare the floating-point and fixed-point representations regarding precision and range. Discuss their trade-offs and provide an example of an application where one is preferred over the other. (5%)
登入後即可作答並保存紀錄。
核心觀念
本題考查四種能力:
- 浮點數的正規化、符號位元、偏移指數與尾數表示。
- 固定位元數造成的表示範圍限制與溢位。
- 固定小數點加法與二補數運算。
- 浮點數轉固定點數的演算法,以及兩者在精確度、範圍與應用上的取捨。
浮點數通常表示為
其中 為符號位元, 為尾數小數部分, 為實際指數。
Excess-15 編碼的關係為
其中 是儲存在指數欄位中的值。
解題方法
(1)以 16 位元浮點格式表示
步驟一:轉換成二進位
整數部分:
小數部分:
因此
步驟二:正規化
將二進位小數點左移五位:
所以:
- 符號 ,因為數值為負。
- 實際指數 。
- 尾數為 。
步驟三:計算 Excess-15 指數
步驟四:填入 10 位元尾數
題目指定 10 位元尾數包含隱含的 leading 1,因此將
補足為 10 位元:
其中最後的 0 為補位。
因此 16 位元格式為:
也就是
溢位處理
若將很大的正數與 相加,首先會將兩數的指數對齊。當大數的指數遠大於 時, 的尾數必須向右移動許多位,最後可能完全落在可保留精度之外,因此結果會近似於大數本身。
若相加後的實際指數大於此格式可表示的最大指數,便會發生浮點溢位。系統常見的處理方式為:
- 使用 IEEE 754 類似規格時,結果設為正無限大或負無限大。
- 沒有無限大設計時,飽和至最大可表示值。
- 設定溢位旗標,通知處理器或程式進行例外處理。
本題未指定特殊值編碼,因此重點是:指數欄位無法容納結果的實際指數時,產生 overflow;若未超過範圍,則大數會使 因精度不足而被捨去。
(2)以 ssss.ffff 固定小數格式相加
格式限制
ssss.ffff 僅有:
- 小數點左側 4 位元。
- 小數點右側 4 位元。
若採用一般 4 位元整數欄位, 的二進位形式為:
它需要小數點左側 6 個位元,無法直接放入 ssss.ffff。
若 ssss 是 4 位元二補數整數欄位,可表示的數值範圍更小,通常為:
因此 與 都無法在嚴格的 ssss.ffff 格式中表示。題目資訊不足之處在於未說明是否允許運算前先擴大整數欄位。
合理假設下的運算
假設保留 4 位小數位元,並將整數欄位擴大至足以容納 的寬度,則:
負數使用二補數表示。使用足夠寬度後:
相加:
所以結果為:
十進位結果為:
需要注意,若嚴格要求結果也必須寫成 4 位整數欄位加 4 位小數欄位,則可寫成:
但兩個輸入操作數本身已超出 ssss.ffff 的表示範圍。
(3)16 位元浮點數轉固定點表示法
假設目標固定點格式具有 個小數位元,總寬度為 ,並採用二補數表示負數。
演算法步驟
- 讀取 16 位元浮點數。
- 分離:
- 1 位元符號 。
- 5 位元指數欄位 。
- 10 位元尾數欄位 。
- 計算實際指數:
- 還原實際數值:
其中 表示包含 leading 1 的正規化尾數。例如尾數位元為 1011011100 時:
- 將浮點數縮放為固定點整數:
- 若 不是整數,依需求採用截斷、四捨五入或銀行家捨入。
- 檢查 是否超出 位元二補數範圍:
第 4 題20 分
- This question evaluates your understanding of Boolean algebra, logic gate design, and optimization. It focuses on deriving and implementing efficient circuits while analyzing their performance.
(1) Simplify the given Boolean expression using a Karnaugh map. Ensure you minimize the number of terms in the expression and clearly show each step of simplification.
(5%)
(2) Design a logic circuit with three inputs (A, B, C) and two outputs (Y1, Y2) based on the following criteria:- Y1=1 if and only if the number of 1s in the input is even.
- Y2=1 if and only if the input contains at least two 1s.
Use Boolean expressions to describe the outputs and ensure logical correctness. (5%)
(3) Analyze the delay time of the designed circuit. Assume that each logic gate introduces a delay of 10ns, and calculate the maximum delay path in the circuit. (5%)
(4) Draw the logic circuit diagram corresponding to your design. Optimize the circuit to use the minimum number of gates, ensuring it meets the functional requirements. (5%)
登入後即可作答並保存紀錄。
核心觀念
本題涵蓋三項重點:
- 使用 Karnaugh map 化簡四變數 Boolean expression,並利用 don’t-care terms 擴大分組。
- 使用 XOR、XNOR 與 majority function 設計邏輯電路。
- 以「最長閘級路徑」分析 propagation delay:
以下假設所有閘皆為二輸入閘,且 XOR、XNOR、AND、OR 均視為一個邏輯閘,每個閘的延遲為 。
(1) 使用 Karnaugh map 化簡
題目給定:
其中:
- :必須輸出的 minterm
- :don’t-care,可視為 或
- 未列出的 minterm:視為
採用列變數 、欄變數 ,並依 Gray code 排列:
| 1 | 1 | 1 | ||
| 0 | 1 | 0 | ||
| 0 | 0 | 1 | 0 | |
| 1 | 1 | 1 |
第一組:化簡為
將 與 的兩列全部圈成一組,共 格:
在此群組中,、、 都會變化,只有 固定,因此得到:
第二組:化簡為
將 圈成一組:
此群組中:
- 固定
- 固定
- 、 變化
因此得到:
其中 、 是 don’t-care,可用來擴大群組。
第三組:化簡為
將 圈成一組:
此群組中:
- 固定
- 固定
- 、 變化
因此得到:
化簡結果
將三個群組相加:
此式已涵蓋所有指定的 minterms,且沒有涵蓋任何指定為 的 minterm。
(2) 設計兩個輸出 、
:輸入中 的數量為偶數
三個輸入中, 的數量為偶數的情況包括:
- :有 個
- :有 個
- :有 個
- :有 個
因此:
此功能也可使用 XOR 表示。三個輸入的 XOR 在 的數量為奇數時輸出 ,所以偶數同位元輸出為其反相:
也可寫成:
:輸入中至少有兩個
符合條件的輸入為:
因此可由三個兩兩相乘項表示:
理由如下:
This question evaluates your understanding of cryptographic principles, algorithm design, and the implications of modern threats to encryption. (20%)
第 5-(1) 題5 分
Design a key distribution protocol that ensures secure communication between two parties. Your protocol must prevent man-in-the-middle attacks and minimize the number of communication rounds required. (5%)
登入後即可作答並保存紀錄。
圖中確認第 5 題題幹為:「This question evaluates your understanding of cryptographic principles, algorithm design, and the implications of modern threats to encryption. (20%)」,第 5-(1) 題內容為:「Design a key distribution protocol that ensures secure communication between two parties. Your protocol must prevent man-in-the-middle attacks and minimize the number of communication rounds required. (5%)」。以下依題意作答。
核心觀念
本題考的是**金鑰分配協定(Key Distribution Protocol)**的設計,核心涉及:
- Diffie-Hellman 金鑰交換(DH Key Exchange):兩方在不安全通道上協商出共享秘密。
- 中間人攻擊(Man-in-the-Middle Attack, MITM)的防禦:原始 DH 協定無法抵禦 MITM,必須結合公鑰認證機制(如憑證或已知可信公鑰)來綁定身份。
- 通訊回合數最小化:以一回合(one round-trip, 1-RTT)的並行交換完成金鑰推導,後續受保護訊息的成功驗證才構成明確金鑰確認(Explicit Key Confirmation)。
關鍵數學基礎為離散對數問題下的 DH 公式:
其中 為生成元, 為大質數,, 分別為雙方公鑰。
解題方法
一、系統假設與參數設定
- 公開參數:大質數 與生成元 (亦可改用橢圓曲線群)。
- Alice 持有長期私鑰 ,對應公鑰 。
- Bob 持有長期私鑰 ,對應公鑰 。
- 可信公鑰假設:雙方已透過可信管道(如 CA 憑證、預先交換或 TOFU 機制)取得對方的正確公鑰,確保公鑰與身份的綁定。此假設是抵禦 MITM 的根本保障。
二、協定流程(1-RTT 並行交換)
Round 1(唯一一回合)—— Alice 與 Bob 同時(並行)執行:
| 方向 | 傳送內容 |
|---|---|
| Alice → Bob | 臨時公鑰 ( 為 Alice 隨機選取的臨時私鑰) |
| Bob → Alice | 臨時公鑰 ( 為 Bob 隨機選取的臨時私鑰) |
雙方亦可在此訊息中附上對臨時公鑰的數位簽章(以長期私鑰簽署),即 Alice 附上 ,Bob 附上 ,收方以已知可信公鑰驗章。
金鑰推導:
收到對方臨時公鑰後,各自計算共享秘密:
第 5-(2) 題5 分
Using the RSA algorithm, encrypt the message with the following parameters. (5%)
Public key .
Private key .
Modulus .
Verify the decryption process and confirm the result matches the original message.
登入後即可作答並保存紀錄。
核心觀念
RSA 加密與解密分別使用:
其中明文整數必須滿足 。公鑰指數 與私鑰指數 通常滿足 ,因此在符合條件時,解密可還原原明文的模 餘數。
解題方法
原卷圖列出的參數是明文 、公鑰指數 、私鑰指數 、模數 。先檢查明文範圍:,所以這個明文整數不符合 RSA 單一區塊的輸入條件;直接計算時,實際參與模指數運算的是 。
計算密文:
因為 ,逐次平方求模:
因此:
第 5-(3) 題5 分
Analyze the computational complexity of RSA encryption and discuss its performance in large-scale data transmission compared to symmetric encryption algorithms such as AES. (5%)
登入後即可作答並保存紀錄。
核心觀念
題目考查 RSA 與對稱式加密在計算成本上的差異。RSA 加密使用公開金鑰 ,將訊息區塊 加密為
AES 則使用共享的對稱金鑰處理資料。比較時要同時看運算量、每次運算的成本,以及傳輸大量資料時的整體效率。
解題方法
圖中第 (3) 題要求分析 RSA 加密的計算複雜度,並比較其與 AES 在大量資料傳輸時的效能;同頁第 (2) 題另列有 、、,那是前一小題的加解密參數,不影響本題的複雜度比較。
RSA 加密的主要運算是模指數 。用平方乘法計算時,約需 次模乘,因此若將一次模乘視為基本運算,RSA 加密的運算量為 次模乘。若進一步考慮大整數運算,令模數 有 位元,一次乘法成本記為 ,則複雜度約為
常見情況下,公開指數 選得較小,例如 ,可減少模乘次數;但每次模乘仍涉及大整數運算。
第 5-(4) 題5 分
Explain the potential threat posed by quantum computing to RSA encryption. Provide an overview of an encryption algorithm that is resistant to quantum attacks, such as lattice-based cryptography. (5%)
登入後即可作答並保存紀錄。
核心觀念
量子運算對 RSA 的威脅,來自 Shor 演算法能在足夠大型且具容錯能力的量子電腦上,有效率地分解大整數。RSA 的公開金鑰包含 與公開指數 ;若攻擊者分解出 ,便能計算 ,再由 求出私密指數 ,進而解密資料。
格基密碼的安全性則建立在格上困難問題之上,例如帶誤差學習(LWE)問題。這類問題在適當參數下,沒有已知的有效量子演算法可解。
解題方法
圖中第 5-(4) 題要求說明量子運算對 RSA 的威脅,並概述一種抗量子加密演算法,並以格基密碼為例。作答時先連結 RSA 安全性與整數分解,再說明格基加密如何利用小誤差構造公開金鑰,讓持有私鑰者消去主要項並還原訊息。
以簡化的 LWE 公開金鑰加密為例,令 ,選取私鑰向量 與小誤差向量 ,計算: