東京大学 新領域創成科学研究科 メディカル情報生命専攻 2016年8月実施 問題7
Author
zephyr
Description
(1) Given an integer x, we calculate a polynomial, f=∑i=0naixi(n,ai(0≤i≤n) are natural numbers). A single addition or multiplication of two values takes a unit time regardless of the number of the digits in the representation of the values.
-
(A) Let fi=aixi. Find the time complexity (with regard to n) of an algorithm that calculates f0,f1,…,fn individually and then calculates f=∑i=0nfi.
-
(B) Let gi={angi+1x+ai(when i=n)(when i<n). Notice that f=g0 holds. Find the time complexity (with regard to n) of an algorithm that calculates g0, hence f by calculating gn,gn−1,…,g0 in this order.
(2) Given two 2n+1-bit integers a and b, let ahi,bhi be the highest 2n bits of a and b, and alo,blo the lowest 2n bits of a and b, respectively. We calculate the product of a and b when n is fairly large. Assume that computation time for addition or shift is negligible compared to that for multiplication.
-
(A) The multiplication of 2n+1-bit integers can be constructed from the multiplication of 2n-bit integers as follows: ab=r222n+r12n+r0, where r2=ahibhi,r1=ahiblo+alobhi,r0=aloblo. We can recursively apply this reduction until every multiplication only involves integers small enough to fit in the machine word of the computer. Let T(2n+1) be the number of machine-word multiplications required in the multiplication of two 2n+1-bit integers. Express T(2n+1) in terms of T(2n).
-
(B) Solve the recursive equation you answered in (A), and express T(2n) in Landau's O notation.
-
(C) We reduce the number of multiplications in the calculation in (A) by calculating r1 as r1=r2+r0−(ahi−alo)(bhi−blo). Express T(2n) in Landau's O notation, and prove it.
(1) 给定一个整数 x,我们计算一个多项式,f=∑i=0naixi(n,ai(0≤i≤n) 是自然数)。两个值的单次加法或乘法的计算时间是一个单位时间,与值的表示形式中的位数无关。
-
(A) 令 fi=aixi。找到一个算法的时间复杂度(关于 n),该算法分别计算 f0,f1,…,fn,然后计算 f=∑i=0nfi。
-
(B) 令 gi={angi+1x+ai(当 i=n 时)(当 i<n 时)。注意 f=g0 成立。找到一个算法的时间复杂度(关于 n),该算法计算 g0,从而通过依次计算 gn,gn−1,…,g0 得到 f。
(2) 给定两个 2n+1 位的整数 a 和 b,令 ahi,bhi 为 a 和 b 的最高 2n 位,alo,blo 为 a 和 b 的最低 2n 位。我们在 n 较大时计算 a 和 b 的乘积。假设加法或移位的计算时间相对于乘法可以忽略不计。
-
(A) 2n+1 位整数的乘法可以通过 2n 位整数的乘法构造,如下所示:ab=r222n+r12n+r0,其中 r2=ahibhi,r1=ahiblo+alobhi,r0=aloblo。我们可以递归地应用这种缩减,直到每次乘法仅涉及足够小的整数以适应计算机的机器字。设 T(2n+1) 为两个 2n+1 位整数相乘所需的机器字乘法次数。用 T(2n) 表示 T(2n+1)。
-
(B) 解答你在 (A) 中回答的递归方程,并用 Landau 符号 O 表示 T(2n)。
-
(C) 我们通过计算 r1 为 r1=r2+r0−(ahi−alo)(bhi−blo) 来减少 (A) 中的乘法次数。用 Landau 符号 O 表示 T(2n),并证明它。
Kai
(1)
(A)
First, consider calculating each term fi=aixi individually and then summing them to get f=∑i=0nfi.
i=0∑nO(i)=O(0+1+2+⋯+n)=O(2n(n+1))=O(n2)
Thus, the overall time complexity is O(n2).
(B)
The recurrence relation for gi is:
gi={angi+1x+aiwhen i=nwhen i<n
We calculate gn,gn−1,…,g0 in this order:
- Calculating gn takes O(1).
- Calculating gn−1 from gn involves one multiplication and one addition, so it takes O(1).
- Similarly, calculating each gi from gi+1 takes O(1).
Since there are n+1 such calculations, the overall time complexity is O(n).
(2)
(A)
Given:
ab=r222n+r12n+r0
where:
- r2=ahibhi
- r1=ahiblo+alobhi
- r0=aloblo
This involves four multiplications of 2n-bit integers:
T(2n+1)=4T(2n)
(B)
The recurrence relation is:
T(2n+1)=4T(2n)
Let k=n+1. Then we have:
T(2k)=4T(2k−1)
Expanding this, we get:
T(2k)=4kT(20)
Since T(20) is a constant C, we get:
T(2k)=4kC
Therefore, in terms of n:
T(2n)=O(22n)
(C)
Using the optimized method for r1:
r1=r2+r0−(ahi−alo)(bhi−blo)
This involves three multiplications of 2n-bit integers:
T(2n+1)=3T(2n)
Solving this recurrence relation similarly:
T(2k)=3kT(20)
Since T(20) is a constant C, we get:
T(2k)=3kC
Therefore, in terms of n:
T(2n)=O(3n)=O(2nlog23)
knowledge
时间复杂度 递归 复杂度分析
重点词汇
- polynomial 多项式
- time complexity 时间复杂度
- recurrence relation 递归关系
- multiplication 乘法
参考资料
- Introduction to Algorithms, Cormen et al., Chap. 2 (时间复杂度分析)
- Introduction to Algorithms, Cormen et al., Chap. 30 (多项式和大整数运算)