Download presentation

Presentation is loading. Please wait.

Published byMelissa Whitwell Modified over 2 years ago

1
Mathematics in OI Bill Kwok 20/3/2010 HKOI 2010 Intermediate Training Acknowledgement: References and slides are extracted from: 1. Math in OI, , by Ivan Li CUHK CSC2110 Discrete Math, by Lap Chi

2
Mathematics in OI Greatest Common Divisor Finding Primes Basic Counting Partial Sum and Differencing

3
Greatest Common Divisor Definition – The greatest natural number dividing both n and m – A natural number k dividing both n and m, such that for each natural number h dividing both n and m, we have k divisible by h.

4
Greatest Common Divisor How to find it? – Check each natural number not greater than m and n if it divides both m and n. Then select the greatest one. – Euclidean Algorithm

5
Euclidean Algorithm Assume m > n GCD(m,n) While n > 0 m = m mod n swap m and n Return m

6
Euclidean Algorithm GCD(102, 70) 102 = = GCD(70, 32) 70 = 2x = GCD(32, 6) 32 = 5x6 + 2 = GCD(6, 2) 6 = 3x2 + 0 = GCD(2, 0)

7
Euclidean Algorithm GCD(662, 414) 662 = 1x = GCD(414, 248) 414 = 1x = GCD(248, 166) 248 = 1x = GCD(166, 82) 166 = 2x = GCD(82, 2) 82 = 41x2 + 0 = GCD(2, 0)

8
Greatest Common Divisor What if we want the greatest number which divides n 1,n 2, …, n m-1 and n m ? Apply GCD two-by-two gcd(n 1,n 2, …,n m ) = gcd(n 1,gcd(n 2,gcd(n 3,…gcd(n m-1,n m )…))

9
Applications Solve mx + ny = a for integers x and y Can be solved if and only if a is divisible by gcd(m,n)

10
Die Hard Simon says: On the fountain, there should be 2 jugs, do you see them? A 5-gallon and a 3-gallon. Fill one of the jugs with exactly 4 gallons of water and place it on the scale and the timer will stop. You must be precise; one ounce more or less will result in detonation. If you're still alive in 5 minutes, we'll speak.

11
3 Gallon Jug5 Gallon Jug Start with empty jugs: (0,0) Fill the big jug: (0,5) Die Hard

12
3 Gallon Jug5 Gallon Jug Pour from big to little: (3,2) Die Hard

13
3 Gallon Jug5 Gallon Jug Empty the little: (0,2) Die Hard

14
3 Gallon Jug5 Gallon Jug Pour from big to little: (2,0) Die Hard

15
3 Gallon Jug5 Gallon Jug Fill the big jug: (2,5) Die Hard

16
3 Gallon Jug5 Gallon Jug Pour from big to little: (3,4) Done!! Die Hard

17
3 Gallon Jug5 Gallon Jug What if you have a 9 gallon jug instead? 9 Gallon Jug Can you do it? Die Hard

18
3 Gallon Jug 9 Gallon Jug Supplies: Water Die Hard

19
Applications Simplifying a fraction m/n If gcd(m,n) > 1, then the fraction can be simplified by dividing gcd(m,n) on the numerator and the denominator.

20
Least Common Multiple Definition – The least natural number divisible by both n and m – A natural number k divisible by both n and m, such that for each natural number h divisible by both n and m, we have k divides h. Formula – lcm(m,n) = mn/gcd(m,n)

21
Test for a prime number a prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself. For each integer greater than 1 and less than p, check if it divides p Actually we need only to check integers not greater than sqrt(p) (Why?)

22
Finding Prime Numbers For each integer, check if it is a prime Prime List Sieve of Eratosthenes

23
Prime List Stores a list of prime numbers found For each integer, check if it is divisible by any of the prime numbers found If not, then it is a prime. Add it to the list.

24
Sieve of Eratosthenes Stores an array of Boolean values Comp[i] which indicates whether i is a known composite number

25
Sieve of Eratosthenes for i = 2 … n If not Comp[i] output i j = 2*i while j n Comp[j] = true j = j + i

26
Optimization Consider odd numbers only Do not forget to add 2, the only even prime

27
If sets A and B are disjoint, then |A B| = |A| + |B| A B Sum Rule Class has 43 women, 54 men, so total enrollment = = lower case letters, 26 upper case letters, and 10 digits, so total characters = = 62 Basic Counting

28
Given two sets A and B, the Cartisean product Product Rule If |A| = m and |B| = n, then |A B| = mn. A = {a, b, c, d}, B = {1, 2, 3} A B = {(a,1),(a,2),(a,3), (b,1),(b,2),(b,3), (c,1),(c,2),(c,3), (d,1),(d,2),(d,3) } If there are 4 men and 3 women, there are possible married couples.

29
Product Rule: Counting Strings The number of length-4 strings from alphabet B ::= {0,1} = |B B B B| = 2 · 2 · 2 · 2 = 2 4

30
At Least One Seven How many # 4-digit numbers with at least one 7? count by 1st occurrence of 7: 7xxx + o7xx + oo7x + ooo · · = 3439 |4-digit numbers with at least one 7|= |4-digit numbers| |those with no 7s| = 10 4 – 9 4 = 3439 Method 1: Method 2: (counting the complement) (counting by partitioning)

31
Permutations A permutation of a set S is a sequence that contains every element of S exactly once. For example, here are all six permutations of the set {a, b, c}: (a, b, c) (a, c, b) (b, a, c) (b, c, a) (c, a, b) (c, b, a) How many permutations of an n-element set are there?

32
There are n choices for the first element. For each of these, there are n 1 remaining choices for the second element. For every combination of the first two elements, there are n 2 ways to choose the third element, and so forth. Thus, there are a total of n · (n 1) · (n 2) · · · 3 · 2 · 1 = n! permutations of an n-element set. How many permutations of an n-element set are there? Permutations Stirlings formula:

33
There are n choices for the first element. For each of these, there are n 1 remaining choices for the second element. There are n – k + 1 remaining choices for the last element. Thus, there are a total of n · (n 1) · (n 2) · · · (n – k + 1) to choose k elements. Combinations How many subsets of k elements of an n-element set? Any ordering of the first k elements give the same subset!

34
Example : Rectangles How many rectangles can be formed in an 4 × 3 grid?

35
two horizontal edges two vertical edges Rectangle Example : Rectangles

36
Poker Hands There are 52 cards in a deck. Each card has a suit and a value. 4 suits ( ) 13 values (2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A) Five-Card Draw is a card game in which each player is initially dealt a hand, a subset of 5 cards. How many different hands?

37
Example 1: Four of a Kind A Four-of-a-Kind is a set of four cards with the same value. How many different hands contain a Four-of-a-Kind?

38
A hand with a Four-of-a-Kind is completely described by a sequence specifying: 1. The value of the four cards. 2. The value of the extra card. 3. The suit of the extra card. There are 13 choices for (1), 12 choices for (2), and 4 choices for (3). By generalized product rule, there are 13x12x4 = 624 hands. Only 1 hand in about 4165 has a Four-of-a-Kind! Example 1: Four of a Kind

39
Example 2: Full House A Full House is a hand with three cards of one value and two cards of another value. How many different hands contain a Full House?

40
There is a bijection between Full Houses and sequences specifying: 1. The value of the triple, which can be chosen in 13 ways. 2. The suits of the triple, which can be selected in (4 3) ways. 3. The value of the pair, which can be chosen in 12 ways. 4. The suits of the pair, which can be selected in (4 2) ways. By generalized product rule, there are Only 1 hand in about 634 has a Full House! Example 2: Full House

41
Example 3: Two Pairs How many hands have Two Pairs; that is, two cards of one value, two cards of another value, and one card of a third value?

42
1. The value of the first pair, which can be chosen in 13 ways. 2. The suits of the first pair, which can be selected (4 2) ways. 3. The value of the second pair, which can be chosen in 12 ways. 4. The suits of the second pair, which can be selected in (4 2) ways 5. The value of the extra card, which can be chosen in 11 ways. 6. The suit of the extra card, which can be selected in 4 ways. Number of Two pairs = Example 3: Two Pairs Double Count! So the answer is

43
Example 4: Every Suit How many hands contain at least one card from every suit? 1. The value of each suit, which can be selected in 13x13x13x13 ways. 2. The suit of the extra card, which can be selected in 4 ways. 3. The value of the extra card, which can be selected in 12 ways. Double count! So the answer is 13 4 x4x12/2 =

44
Partial Sum Motivation How to find the sum of the 3 rd to the 6 th element of an array a[i] ? a[3] + a[4] + a[5] + a[6] How to find the sum of the 1000 th to the th element? A for-loop will take much time In order to find the sum of a range in an array efficiently, we need to do some preprocessing.

45
Partial Sum Use an array s[i] to store the sum of the first i elements. s[i] = a[1] + a[2] + … + a[i] The sum of the j th element to the k th element = s[k] – s[j-1] We usually set s[0] = 0

46
Partial Sum How to compute s[i] ? During input s[0] = 0 for i = 1 to n input a[i] s[i] = s[i-1] + a[i]

47
Differencing Motivation How to increment the 3 rd to the 6 th element of an array a[i] ? a[3]++, a[4]++, a[5]++, a[6]++ How to increment the 1000 th to the th element? A for-loop will take much time In order to increment(or add an arbitrary value to) a range of elements in an array efficiently, we will use a special method to store the array.

48
Differencing Use an array d[i] to store the difference between a[i] and a[i- 1]. d[i] = a[i] - a[i-1] When the the j th element to the k th element is incremented, d[j] ++, d[k+1] - - We usually set d[1] = a[1]

49
Differencing Easy to compute d[i] But how to convert it back to a[i]? Before (or during) output a[0] = 0 for i = 1 to n a[i] = a[i-1] + d[i] output a[i] Quite similar to partial sum, isnt it?

50
Relation between the two methods They are inverse of each other Denote the partial sum of a by a Denote the difference of a by a The difference operator We have ( a) = ( a) = a

51
Comparison Partial sum - Fast sum of range query Difference - Fast range incrementation Ordinary array - Fast query and incrementation on single element

52
Runtime Comparison Range Query Single Query Single Update Range Update Partial sumConstant (treat a single element as a range) Linear (Have to update all sums involved) Linear Ordinary array LinearConstant Linear DifferenceLinear (Convert it back to the original array) LinearConstant

53
Tappy World 2-D world Raise 1 unit height form 3 to 7

54
Tappy World 2-D world Sink 2 unit form 2 to 5

55
Tappy World Output:

56
Question?

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google