Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Similar documents
CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS. Jaehyun Park

Lecture 8 Mathematics

CS 310 Advanced Data Structures and Algorithms

1 Elementary number theory

Know the Well-ordering principle: Any set of positive integers which has at least one element contains a smallest element.

Lecture Notes, CSE 232, Fall 2014 Semester

Honors Precalculus: Solving equations and inequalities graphically and algebraically. Page 1

Math Introduction to Advanced Mathematics

Lemma (x, y, z) is a Pythagorean triple iff (y, x, z) is a Pythagorean triple.

Integers and Mathematical Induction

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm

The Chinese remainder theorem

Modular Arithmetic. Marizza Bailey. December 14, 2015

r=1 The Binomial Theorem. 4 MA095/98G Revision

Performance Level Descriptors. Mathematics

COP 4516: Math for Programming Contest Notes

1 Elementary number theory

MAT 003 Brian Killough s Instructor Notes Saint Leo University

Conditionals !

About the Author. Dependency Chart. Chapter 1: Logic and Sets 1. Chapter 2: Relations and Functions, Boolean Algebra, and Circuit Design

Basics of Computational Geometry

Finite Math - J-term Homework. Section Inverse of a Square Matrix

Stage 1 Place Value Calculations Geometry Fractions Data. Name and describe (using appropriate vocabulary) common 2d and 3d shapes

Algorithmic number theory Cryptographic hardness assumptions. Table of contents

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

Topic 10 Part 2 [474 marks]

UNIT-II NUMBER THEORY

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Introduction to Modular Arithmetic

Florida Math 0018 Correlation of the ALEKS course Florida Math 0018 to the Florida Mathematics Competencies - Lower

x = 12 x = 12 1x = 16

Correlation of the ALEKS courses Algebra 1 and High School Geometry to the Wyoming Mathematics Content Standards for Grade 11

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

correlated to the Michigan High School Mathematics Content Expectations

JMG. Algebra II. Resigning from a job. Working with Radian Measures & the Unit Circle Students. How to Effectively Write a Letter of Resignation

Stage 1 (intervention) Stage 2 Stage 3 Stage 4. Advanced 7-8. Secure 4-6

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

Mathematics Appendix 1: Examples of formal written methods for addition, subtraction, multiplication and division

Prentice Hall Mathematics: Pre-Algebra 2004 Correlated to: The Pennsylvania Math Assessment Anchors and Eligible Content (Grade 11)

2-D Geometry for Programming Contests 1

Chapter 4. Number Theory. 4.1 Factors and multiples

Mathematics TEKS Grade Level Changes/Additions Summary

SE/TE: SE/TE: 80, N / A SE/TE: 3, 282 SE/TE: 3 4

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

Section 1.2 Fractions

DEPARTMENT - Mathematics. Coding: N Number. A Algebra. G&M Geometry and Measure. S Statistics. P - Probability. R&P Ratio and Proportion

Foundations for Functions Knowledge and Skills: Foundations for Functions Knowledge and Skills:

Mathematics Scope & Sequence Algebra I

Texas High School Geometry

INDEPENDENT SCHOOL DISTRICT 196 Rosemount, Minnesota Educating our students to reach their full potential

Mathematics (

Pick s Theorem and Lattice Point Geometry

Interactive Math Glossary Terms and Definitions

Lecture 6: Combinatorics Steven Skiena. skiena

Geometry. Zachary Friggstad. Programming Club Meeting

Grade 7 Math Curriculum Map Erin Murphy

Y9 Maths. Summative Assessment 1 hour written assessment based upon modules 1-5 during Autumn 2. Term Cycle 1

YEAR 12 Core 1 & 2 Maths Curriculum (A Level Year 1)

Unit 2: Accentuate the Negative Name:

Prentice Hall Mathematics: Course Correlated to: Colorado Model Content Standards and Grade Level Expectations (Grade 6)

Chislehurst and Sidcup Grammar School Mathematics Department Year 9 Programme of Study

Integrated Math I. IM1.1.3 Understand and use the distributive, associative, and commutative properties.

Voluntary State Curriculum Algebra II

Caltech Harvey Mudd Mathematics Competition March 3, 2012

Prentice Hall Mathematics: Course Correlated to: Massachusetts State Learning Standards Curriculum Frameworks (Grades 7-8)

Introductory Combinatorics

Let denote the number of partitions of with at most parts each less than or equal to. By comparing the definitions of and it is clear that ( ) ( )

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

CME Project, Algebra Correlated to: The Pennsylvania Math Assessment Anchors and Eligible Content (Grade 11)

Structured programming

2009 HMMT Team Round. Writing proofs. Misha Lavrov. ARML Practice 3/2/2014

Math 221 Final Exam Review

Chapter 3: Theory of Modular Arithmetic 1. Chapter 3: Theory of Modular Arithmetic

Practice Test - Chapter 6

CS321 Introduction To Numerical Methods

Chapter 1: Number and Operations

Module 7 Highlights. Mastered Reviewed. Sections ,

Matija Gubec International School Zagreb MYP 0. Mathematics

Course Number 432/433 Title Algebra II (A & B) H Grade # of Days 120

Bulgarian Math Olympiads with a Challenge Twist

Glossary Common Core Curriculum Maps Math/Grade 6 Grade 8

Richard Lander School Maths Key Stage 3

Segment Addition Postulate: If B is BETWEEN A and C, then AB + BC = AC. If AB + BC = AC, then B is BETWEEN A and C.

Year 8 Mathematics Curriculum Map

AQA GCSE Further Maths Topic Areas

2 Computation with Floating-Point Numbers

TI- Nspire Testing Instructions

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Introduction to Sets and Logic (MATH 1190)

Montana City School GRADE 5

Integrated Algebra 2 and Trigonometry. Quarter 1

When two polygons have the same shape and only differ in size, we say they are similar polygons.

8-1 Simple Trigonometric Equations. Objective: To solve simple Trigonometric Equations and apply them

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

36 Modular Arithmetic

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

CMP Book: Investigation Number Objective: PASS: 1.1 Describe data distributions and display in line and bar graphs

Transcription:

Mathematics Jaehyun Park CS 97SI Stanford University June 29, 2015

Outline Algebra Number Theory Combinatorics Geometry Algebra 2

Sum of Powers n k=1 k 3 k 2 = 1 n(n + 1)(2n + 1) 6 = ( k ) 2 = ( 1 2 n(n + 1) ) 2 Pretty useful in many random situations Memorize above! Algebra 3

Fast Exponentiation Recursive computation of a n : 1 n = 0 a n a n = 1 = (a n/2 ) 2 n is even a(a (n 1)/2 ) 2 n is odd Algebra 4

Implementation (recursive) double pow(double a, int n) { if(n == 0) return 1; if(n == 1) return a; double t = pow(a, n/2); return t * t * pow(a, n%2); } Running time: O(log n) Algebra 5

Implementation (non-recursive) double pow(double a, int n) { double ret = 1; while(n) { if(n%2 == 1) ret *= a; a *= a; n /= 2; } return ret; } You should understand how it works Algebra 6

Linear Algebra Solve a system of linear equations Invert a matrix Find the rank of a matrix Compute the determinant of a matrix All of the above can be done with Gaussian elimination Algebra 7

Outline Algebra Number Theory Combinatorics Geometry Number Theory 8

Greatest Common Divisor (GCD) gcd(a, b): greatest integer divides both a and b Used very frequently in number theoretical problems Some facts: gcd(a, b) = gcd(a, b a) gcd(a, 0) = a gcd(a, b) is the smallest positive number in {ax + by x, y Z} Number Theory 9

Euclidean Algorithm Repeated use of gcd(a, b) = gcd(a, b a) Example: gcd(1989, 867) = gcd(1989 2 867, 867) = gcd(255, 867) = gcd(255, 867 3 255) = gcd(255, 102) = gcd(255 2 102, 102) = gcd(51, 102) = gcd(51, 102 2 51) = gcd(51, 0) = 51 Number Theory 10

Implementation int gcd(int a, int b) { while(b){int r = a % b; a = b; b = r;} return a; } Running time: O(log(a + b)) Be careful: a % b follows the sign of a 5 % 3 == 2-5 % 3 == -2 Number Theory 11

Congruence & Modulo Operation x y (mod n) means x and y have the same remainder when divided by n Multiplicative inverse x 1 is the inverse of x modulo n if xx 1 1 (mod n) 5 1 3 (mod 7) because 5 3 15 1 (mod 7) May not exist (e.g., inverse of 2 mod 4) Exists if and only if gcd(x, n) = 1 Number Theory 12

Multiplicative Inverse All intermediate numbers computed by Euclidean algorithm are integer combinations of a and b Therefore, gcd(a, b) = ax + by for some integers x, y If gcd(a, n) = 1, then ax + ny = 1 for some x, y Taking modulo n gives ax 1 (mod n) We will be done if we can find such x and y Number Theory 13

Extended Euclidean Algorithm Main idea: keep the original algorithm, but write all intermediate numbers as integer combinations of a and b Exercise: implementation! Number Theory 14

Chinese Remainder Theorem Given a, b, m, n with gcd(m, n) = 1 Find x with x a (mod m) and x b (mod n) Solution: Let n 1 be the inverse of n modulo m Let m 1 be the inverse of m modulo n Set x = ann 1 + bmm 1 (check this yourself) Extension: solving for more simultaneous equations Number Theory 15

Outline Algebra Number Theory Combinatorics Geometry Combinatorics 16

Binomial Coefficients ( ) n is the number of ways to choose k objects out of n k distinguishable objects same as the coefficient of x k y n k in the expansion of (x + y) n Hence the name binomial coefficients Appears everywhere in combinatorics Combinatorics 17

Computing Binomial Coefficients Solution 1: Compute using the following formula: ( ) n n(n 1) (n k + 1) = k k! Solution 2: Use Pascal s triangle Can use either if both n and k are small Use Solution 1 carefully if n is big, but k or n k is small Combinatorics 18

Fibonacci Sequence Definition: F 0 = 0, F 1 = 1 F n = F n 1 + F n 2, where n 2 Appears in many different contexts Combinatorics 19

Closed Form F n = (1/ 5)(ϕ n ϕ n ) ϕ = (1 + 5)/2 ϕ = (1 5)/2 Bad because ϕ and 5 are irrational Cannot compute the exact value of F n for large n There is a more stable way to compute F n... and any other recurrence of a similar form Combinatorics 20

Better Closed Form [ Fn+1 F n ] = [ 1 1 1 0 ] [ Fn F n 1 ] = [ 1 1 1 0 ] n [ F1 F 0 ] Use fast exponentiation to compute the matrix power Can be extended to support any linear recurrence with constant coefficients Combinatorics 21

Outline Algebra Number Theory Combinatorics Geometry Geometry 22

Geometry In theory: not that hard In programming contests: more difficult than it looks Will cover basic stuff today Computational geometry in week 9 Geometry 23

When Solving Geometry Problems Precision, precision, precision! If possible, don t use floating-point numbers If you have to, always use double and never use float Avoid division whenever possible Introduce small constant ǫ in (in)equality tests e.g., Instead of if(x == 0), write if(abs(x) < EPS) No hacks! In most cases, randomization, probabilistic methods, small perturbations won t help Geometry 24

2D Vector Operations Have a vector (x, y) Norm (distance from the origin): x 2 + y 2 Counterclockwise rotation by θ: [ cos θ sin θ sin θ cos θ ] [ x y ] Make sure to use correct units (degrees, radians) Normal vectors: (y, x) and ( y, x) Memorize all of them! Geometry 25

Line-Line Intersection Have two lines: ax + by + c = 0 and dx + ey + f = 0 Write in matrix form: [ a b d e ] [ x y ] = [ c f ] Left-multiply by matrix inverse [ ] 1 a b = d e 1 ae bd [ e b d a ] Memorize this! Edge case: ae = bd The lines coincide or are parallel Geometry 26

Circumcircle of a Triangle Have three points A, B, C Want to compute P that is equidistance from A, B, C Don t try to solve the system of quadratic equations! Instead, do the following: Find the (equations of the) bisectors of AB and BC Compute their intersection Geometry 27

Area of a Triangle Have three points A, B, C Want to compute the area S of triangle ABC Use cross product: 2S = (B A) (C A) Cross product: (x 1, y 1 ) (x 2, y 2 ) = x 1 x 2 y 1 y 2 = x 1y 2 x 2 y 1 Very important in computational geometry. Memorize! Geometry 28

Area of a Simple Polygon Given vertices P 1, P 2,..., P n of polygon P Want to compute the area S of P If P is convex, we can decompose P into triangles: n 1 2S = (P i+1 P 1 ) (P i P 1 ) i=2 It turns out that the formula above works for non-convex polygons too Area is the absolute value of the sum of signed area Alternative formula (with x n+1 = x 1, y n+1 = y 1 ): n 2S = (x i y i+1 x i+1 y i ) i=1 Geometry 29

Conclusion No need to look for one-line closed form solutions Knowing how to compute (algorithms) is good enough Have fun with the exercise problems... and come to the practice contest if you can! Geometry 30