Mathematics in OI Bill Kwok 20/3/2010 HKOI 2010 Intermediate Training Acknowledgement: References and slides are extracted from: 1. Math in OI, 7-2-2009,

Slides:



Advertisements
Similar presentations
Opening Objective By the end of this PowerPoint you should be able to:
Advertisements

Chapter 11. Hash Tables.
4-1 Chapter 4 Counting Techniques.
Discrete Probability Rosen 5.1.
Combinatorics & Probability Section 3.4. Which Counting Technique? If the problem involves more than one category, use the Fundamental Principle of Counting.
Beginning Probability
CSC 2510 Test 3 1. Give the names of the formula or rule that provides the answer for each of the following problems. Do not try to solve the problems!
Counting and Gambling Section 4Objectives: Understand how to use the fundamental counting principle to analyze counting problems that occur in gambling.
9.5 Counting Subsets of a Set: Combinations
Counting. Counting in Algorithms How many comparisons are needed to sort n numbers? How many steps to compute the GCD of two numbers ? How many steps.
Great Theoretical Ideas in Computer Science for Some.
Tucker, Applied Combinatorics1 SECTION 5.2 Simple Arrangements and Selections Aaron Desrochers and Ben Epstein.
4/16/2015 MATH 224 – Discrete Mathematics Counting Basic counting techniques are important in many aspects of computer science. For example, consider the.
Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
Decimals and Fractions
Copyright © 2013, 2010, and 2007, Pearson Education, Inc.
College of Information Technology & Design
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Counting and Probability The outcome of a random process is sure to occur, but impossible to predict. Examples: fair coin tossing, rolling a pair of dice,
Discrete Structures & Algorithms More Counting. + + ( ) + ( ) = ? Counting II: Recurring Problems and Correspondences.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 10Feb 12, 2004Carnegie.
Discrete Mathematics Lecture 4 Harper Langston New York University.
3 Gallon Jug5 Gallon Jug Greatest Common Divisor Lecture 8: Sep 30.
Discrete Mathematics Lecture 5
Discrete Mathematics Lecture 6 Alexander Bukharovich New York University.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Algebraic Structures: An Activities Based Course MATH 276 UWM MATH 276 UWM.
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
Mathematics of Cryptography Part I: Modular Arithmetic
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 9/8/2015 MATH 224 – Discrete Mathematics Basic finite probability is given by the formula, where |E| is the number of events and |S| is the total number.
MATH 224 – Discrete Mathematics
Mathematics in OI Prepared by Ivan Li. Mathematics in OI A brief content Greatest Common Divisor Modular Arithmetic Finding Primes Floating Point Arithmetic.
Copyright © Cengage Learning. All rights reserved. CHAPTER 9 COUNTING AND PROBABILITY.
1 9/23/2015 MATH 224 – Discrete Mathematics Basic finite probability is given by the formula, where |E| is the number of events and |S| is the total number.
Counting. Techniques for counting Rule 1 Suppose we carry out have a sets A 1, A 2, A 3, … and that any pair are mutually exclusive (i.e. A 1  A 2 =
Basic Counting Lecture 12: Oct 28. This Lecture We will study some basic rules for counting. Sum rule, product rule, generalized product rule Permutations,
1 Melikyan/DM/Fall09 Discrete Mathematics Ch. 6 Counting and Probability Instructor: Hayk Melikyan Today we will review sections 6.4,
Basic Counting. This Lecture We will study some basic rules for counting. Sum rule, product rule, generalized product rule Permutations, combinations.
Number Theory.  A prime number is a natural number greater than 1 that has exactly two factors (or divisors), itself and 1.  Prime numbers less than.
Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing.
Greatest Common Divisor
Prerequisites: Fundamental Concepts of Algebra
Chapter 6 With Question/Answer Animations 1. Chapter Summary The Basics of Counting The Pigeonhole Principle Permutations and Combinations Binomial Coefficients.
Our Lesson: Review of factors Confidential.
© The McGraw-Hill Companies, Inc., Chapter 4 Counting Techniques.
Homework Homework due now. Reading: relations
Solving Problems Quickly UAkron Programming Team January 20, 2012.
COMPSCI 102 Discrete Mathematics for Computer Science.
Discrete Mathematics for Computer Science. + + ( ) + ( ) = ? Counting II: Recurring Problems and Correspondences Chapter 9 slides 1-54.
1 Counting by Mapping Lecture 15: Nov 4 A B …… f.
DISCRETE COMPUTATIONAL STRUCTURES
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science John LaffertyCS Fall 2005 Lecture 7Sept 20, 2005Carnegie.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science V. AdamchikCS Spring 2006 Lecture 6Feb 2, 2005Carnegie.
Part I: Numbers and Operations Lesson 1: Numbers.
FRACTIONS Fraction: a numerical quantity that is not a whole number Numerator: the number above the line in a common fraction showing how many of the parts.
Section The Product Rule  Example: How many different license plates can be made if each plate contains a sequence of three uppercase English letters.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Section 6.3. Section Summary Permutations Combinations.
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
ICS 253: Discrete Structures I
Mathematical Induction Recursion
Basic Counting.
COUNTING AND PROBABILITY
Counting Discrete Mathematics.
Basic Counting Lecture 9: Nov 5, 6.
Counting II: Recurring Problems And Correspondences
Presentation transcript:

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

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

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.

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

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

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)

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)

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 )…))

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

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.

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

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

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

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

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

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

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

3 Gallon Jug 9 Gallon Jug Supplies: Water Die Hard

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.

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)

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?)

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

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.

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

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

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

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

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.

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

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)

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?

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:

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!

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

two horizontal edges two vertical edges Rectangle Example : Rectangles

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?

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?

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

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?

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

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?

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

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 =

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.

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

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]

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.

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]

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?

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

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

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

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

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

Tappy World Output:

Question?