Harvard University Computer Science 20 In-Class Problems 34 Monday, April 25, 2016 Author: Crystal Chang, edited by Jack Dent Executive Summary 1. Fast Multiplication and the Master Theorem on Divide and Conquer • Fast Multiplication: To compute a · b each with 2m bits (or digits), write a = a1 2m + a0 , b = b1 2m + b0 , where a1 , a0 , b1 , b0 are m-bit numbers — the first and last m bits of a and b. Then a · b = a1 b1 (22m + 2m ) − (a1 − a0 )(b1 − b0 )2m + a0 b0 (1 + 2m ) so multiplication requires only 3 m-bit multiplications plus additions. Then T (1) = 1 and T (2m) = 3T (m) + cm, whose solution is T (m) = Θ(mlog2 3 ) • Master Theorem on D + C recurrences: – T (1) = 1, T (n) = aT (n/b) + cne Let L = logb a – Recurrence has the solution: ∗ T (n) = Θ(ne ) if e > L ∗ T (n) = Θ(ne log2 n) if e = L ∗ T (n) = Θ(nL ) if e < L 2. Fast Exponentiation: We can calculate ab more efficiently by squaring and multiplying by a, e.g. a10 = ((a2 )2 a)2 . The process takes Θ(log2 b) operations. Algorithm: Compute ab using registers X, Y, Z, R X := a; Y := 1; Z := b; REPEAT: if Z = 0, then return Y R:= remdr(Z,2); Z:= quotnt(Z,2) if R = 1,then Y := X ∗ Y X := X2 3. Modular Arithmetic: • The notation a ≡ b (mod n) means that a and b have the same remainder when divided by n. • It is most common to pick a prime p for the modulus. • For example, 4 ≡ 7 (mod 3), 1 ≡ −1 (mod 2), and 2300 ≡ 8100 ≡ 1 (mod 7). • Successive squaring for fast exponentiation also works modp. 1 PROBLEM 1 Fast Multiplication: Use the fast multiplication algorithm to compute the following products. (A) 112 × 102 (B) 11102 × 10112 Solution. (A) m = 1, a = 1 ∗ 2 + 1 → a1 = 1, a0 = 1 b = 1 ∗ 2 + 0 → b1 = 1, b0 = 0 ab = a1 b1 (22m + 2m ) − (a1 − a0 )(b1 − b0 )2m + a0 b0 (1 + 2m ) = 1 ∗ 1 ∗ (22 + 2) − (1 − 1)(1 − 0) ∗ 2 + 1 ∗ 0(1 + 2) = 1 ∗ 22 + 1 ∗ 2 = 110 (B) m = 2, a = 11 ∗ 22 + 10 → a1 = 11, a0 = 10 b = 10 ∗ 22 + 11 → b1 = 10, b0 = 11 ab = a1 b1 (22m + 2m ) − (a1 − a0 )(b1 − b0 )2m + a0 b0 (1 + 2m ) = 11 ∗ 10 ∗ (24 + 22 ) − (11 − 10)(10 − 11) ∗ 22 + 10 ∗ 11 ∗ (1 + 22 ) = 110 ∗ (24 + 22 ) − 1 ∗ (−1) ∗ 22 + 110 ∗ (1 + 22 ) = 110 ∗ 24 + 110 ∗ 22 + 1 ∗ 22 + 110 ∗ 1 + 110 ∗ 22 = 1100000 + 11000 + 100 + 110 + 11000 = 10011010 PROBLEM 2 Master Theorem: For each of the following recurrences, use the Master Theorem to give an expression for the runtime T (n). (A) T (n) = 8T (n/2) + 2n4 √ (B) T (n) = 5T (n/5) + n (C) T (n) = 4T (n/2) + n2 Solution. (A) a=8, b=2, e=4, L=log2 8 = 3, e > L → (Case 1) → T (n) = Θ(ne ) = Θ(n4 ) (B) a=5, b=5, e=1/2, L=log5 5 = 1, e < L → (Case 3) → T (n) = Θ(nL ) = Θ(n) (C) a=4, b=2, e=2, L=log2 4 = 2, e = L → (Case 2) → T (n) = Θ(n2 log2 n) PROBLEM 3 Modular Arithmetic: Compute the following exponentiations using the fast exponentiation algorithm. (A) 1315 mod 23 (B) 1711 mod 50 Solution. (A) 1315 = ((((132 ) ∗ 13)2 ) ∗ 13)2 ∗ 13 ≡ ((((169 mod 23) ∗ 13)2 ) ∗ 13)2 ∗ 13 2 ≡ ((((8 ∗ 13) mod 23)2 ) ∗ 13)2 ∗ 13 ≡ ((122 mod 23) ∗ 13)2 ∗ 13 ≡ ((6 ∗ 13) mod 23)2 ∗ 13 ≡ (92 mod 23) ∗ 13 ≡ (12 ∗ 13) mod 23 = 18 (B) 1711 = ((((172 )2 ) ∗ 17)2 ) ∗ 17 ≡ ((((289 mod 50)2 ) ∗ 17)2 ) ∗ 17 ≡ (((392 mod 50) ∗ 17)2 ) ∗ 17 ≡ (((21 ∗ 17) mod 50)2 ) ∗ 17 ≡ ((72 ) ∗ 17) mod 50 = 33 PROBLEM 4 (Bonus) Modify ab = a1 b1 (22m + 2m ) − (a1 − a0 )(b1 − b0 )2m + a0 b0 (1 + 2m ) for base 10 multiplication and then use the resulting formula to calculate 89 × 78. Solution. Base 10 formula: ab = a1 b1 (102m + 10m ) − (a1 − a0 )(b1 − b0 )10m + a0 b0 (1 + 10m ) m = 1, a = 8 ∗ 10 + 9 → a1 = 8, a0 = 9 b = 7 ∗ 10 + 8 → b1 = 7, b0 = 8 ab = a1 b1 (102m + 10m ) − (a1 − a0 )(b1 − b0 )10m + a0 b0 (1 + 10m ) = 8 ∗ 7(102 + 10) − (8 − 9)(7 − 8) ∗ 10 + 9 ∗ 8(1 + 10) = 56 ∗ 102 + 56 ∗ 10 − 10 + 72 ∗ 1 + 72 ∗ 10 = 6942 3
© Copyright 2025 Paperzz