卷別
資料結構與演算法
證明國立臺灣大學 2025 · 考試 · 第 18 題
(5 points) Consider the task of incrementing a -bit binary counter from all zeroes to all ones. Let be an array of length that simulates the counter, where represents the least significant bit and represents the most significant bit. We implement the INCREMENT() operation as follows:
function INCREMENT()
i = 0
while A[i] == 1 do
A[i] = 0
i = i + 1
A[i] = 1
Unfortunately, this counter is implemented on a deficient random access memory machine (RAM). On this machine, each read-access to a single array entry takes 1 unit of time. However, a write to an entry may take different amount of time. In particular, for any index (), if is a multiple of 3 but not a multiple of 5, the memory stick will fizz and it will take exactly units of time to write the data into . If is a multiple of 5 but not a multiple of 3, the memory stick will buzz and it will take exactly units of time to write the data into . Writing into all other entries not mentioned above takes 1 unit of time as usual.
Please design a potential function such that the amortized time (using ) per INCREMENT() operation is still when incrementing from zero. Make sure your potential function is rigorously defined, can be evaluated into an actual number by human beings, and as clean and efficient without ambiguity. For example, if you left any undetermined variable (such as "for some constant ") in your expression, you must state an actual number for that variable (e.g., ""). You do not need to verify your answer in the answer sheet. This problem has no partial score.