Recursion (part 1) HKUCS Provinci Training Chan Ho Leung
A problem Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles.
A problem Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles.
A problem Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles.
A problem Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles.
A problem Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles.
A solution Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles. 2 n-1
A solution Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles. 2 n If a problem can be reduced to a smaller problem, I should be able to solve it easily.
Tower of Hanoi Legend: Inside a Vietnamese temple there are three rods (labeled as r1, r2, and r3), surrounded by 64 golden disks of different sizes. The monks of Hanoi, by the command of an ancient prophecy, have been moving these disks according to the following rules at each step, take the upper disk of one rod and place it to the top of another rod no bigger disk can be placed on top of a smaller disk
Game time
Smaller problem? ABC move disks from A to C A B C 1.Move top n-1 disks from A to B 2.Move a disk from A to C 3.Move the top n-1 disks from B to C
Program #include using namespace std; void hanoi( char A, char B, char C, int n ){ if( n==1 ) cout << A << “ to “ << C << endl; }else{ hanoi( A, C, B, n-1 ); cout << A << “ to “ << C << endl; hanoi( B, A, C, n-1 ); } int main(){ hanoi( ‘L’, ‘M’, ‘R’, 5); } If a problem can be reduced to a smaller problem, I should be able to solve it easily.
Recursion Solving a problem by reducing it into a smaller problem is called recursion. Solve it by solving same problem with smaller size ◦ [The problem can be reduced to a smaller problem.] ◦ [The problem is recursive.] Small enough => solve it directly ◦ [The problem has base case]
UVA 374 Given B, P, and M, find B P mod M Idea 1. B P = B * B P-1 B 1 = B 0 ≤ B, P ≤ 2,147,483,647 1 ≤ M ≤ Too slow to find B P Idea 2. B P = B P/2 * B P/2 * (P%2?B:1) B 1 = B Things to know: 1.Can int store 2,147,483,647? 2.Can int store B P ?
Modular arithmetic Modular arithmetic is the arithmetic of congruence, sometimes known informally as "clock arithmetic.“ – From mathworld.wolfram.com Arithmetic where we only consider the remainder E.g., 4 = 9 (mod 5)(5 is called “modulus”) 10 = 5 (mod 5) = 0 (mod 5) 14 = 27 (mod 13) -1 = 12 (mod 13) = 19 (mod 13) = 6 (mod 13) 5 – 7 = -2 (mod 13) = 11 (mod 13) 5*7 = 35 (mod 13) = 9 (mod 13)
Properties of modular arithmetic Note that 4 = 9 = 14 (mod 5) Define the residue of N (mod D) as the smallest integer r in {0,1,…, D-1} such that N = r (mod D). Denote the residue of N (mod D) by N%D. (“N mod D”) For any integers A, B and modulus D, A + B = A%D + B%D (mod D) A - B = A%D - B%D (mod D) A * B = A%D * B%D (mod D) e.g., let D = = 21%5 + 33%5 = 4 (mod 5) 21 * 33 = 1 * 3 (mod 5) Proof. Note that A can be uniquely written as kD + A%D for some integer k. Similarly, B = k’D + B%D. Hence, A + B = (k+k’)D + A%D + B%D
UVA 374 Given B, P, and M, find B P mod M Idea 1. B P = B * B P-1 B 1 = B 0 ≤ B, P ≤ 2,147,483,647 1 ≤ M ≤ Too slow to find B P Idea 2. B P = B P/2 * B P/2 * (P%2?B:1) B 1 = B Things to know: 1.Can int store 2,147,483,647? 2.Can int store B P ? 3.B P % M = B P/2 % M * B P/2 %M * ( )%M 4.Can int store the right hand side? 5.B P % M = [B P/2 % M * B P/2 %M]%M * ( )%M 6.B 1 % M = B % M
UVA 374 #include using namespace std; int mod( int B, int P, int M ){ if( P==0 ){ return 1%M; }else if( P==1 ){ return B%M; }else{ int half = mod( B, P/2, M ); return (half*half)%M*(P%2?B:1)%M; } int main(){ int B, P, M; while( cin >> B ){ cin >> P >> M; cout << mod( B, P, M ) << endl; } This program has 1 bug.
UVA Simulation probably does not work Hint lines What is the rank of the card that remains? Assume n is even S = x1 x2 x3 x4 x5 x6 x7 x8 x9 … x2k After removing all odd cards S’ = x2 x4 x6 x8 … x2k If someone can tell you the rank of the remaining card in S’… Assume n is odd S = x1 x2 x3 x4 x5 x6 x7 x8 x9 … x2k x2k+1 After removing all odd cards S’ = x4 x6 x8 … x2k 2 If someone can tell you the rank of the remaining card in S’ …
What is the rank that remains? S(n): which rank survive? S(n/2): which rank survive? S(2): which rank survive? S(1): which rank survive? There are at most log n recursive calls
What is recursion good for? When you see the recursive relationship. ◦ Very easy to write (usually) When you want to do brute force testing.
UVA Exercises 110 – Sorting 297 – Quadtree ◦ The problems are long, but are not difficult – Simple addition ◦ A bit hard to think ◦ need to use matrix 10549, 195, 536 ◦ not sure
Job opportunities $200 per week as web master ◦ Duties: update the homepage, build and organize the content