02-mathematics.pdf

download 02-mathematics.pdf

of 27

Transcript of 02-mathematics.pdf

  • 8/13/2019 02-mathematics.pdf

    1/27

    CS 97SI: INTRODUCTION TO

    PROGRAMMING CONTESTS

    Jaehyun Park

  • 8/13/2019 02-mathematics.pdf

    2/27

    Todays Lecture

    Algebra

    Number Theory

    Combinatorics

    (non-computational) Geometry

    Emphasis on how to compute

  • 8/13/2019 02-mathematics.pdf

    3/27

    Sum of Powers

    =

    ( 1)(2 1)

    1

    Pretty useful in many random situations

    Memorize above!

  • 8/13/2019 02-mathematics.pdf

    4/27

    Fast Exponentiation

    1, if 0 , if 1

    / , if is even

    ()/

    , if is odd

    Can be computed recursively

  • 8/13/2019 02-mathematics.pdf

    5/27

    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: log

  • 8/13/2019 02-mathematics.pdf

    6/27

    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

  • 8/13/2019 02-mathematics.pdf

    7/27

    Linear Algebra

    Gaussian Elimination

    Solve a system of linear equations

    Invert a matrix

    Find the rank of a matrix

    Compute the determinant of a matrix

    Cramers Rule

  • 8/13/2019 02-mathematics.pdf

    8/27

    Greatest Common Divisor (GCD)

    gcd(,): greatest integer divides both and

    Used very frequently in number theoretical

    problems

    Some facts:

    gcd , gcd(, )

    gcd , 0

    gcd(,)is the smallest positive number in {

    | , }

  • 8/13/2019 02-mathematics.pdf

    9/27

    Euclidean Algorithm

    Repeated use of gcd , gcd(, )

    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

  • 8/13/2019 02-mathematics.pdf

    10/27

    Implementation

    int gcd(int a, int b) {

    while(b){int r = a % b; a = b; b = r;}

    return a;

    }

    Running time: log

    Be careful: a % b follows the sign of a 5 % 3 == 2

    -5 % 3 == -2

  • 8/13/2019 02-mathematics.pdf

    11/27

    Congruence & Modulo Operation

    (mod )means and have the same

    remainder when divided by

    Multiplicative inverse

    is the inverse of modulo if 1 (mod )

    5 3 (mod 7)because 5 3 15 1 (mod 7)

    May not exist (e.g. Inverse of 2 mod 4) Exists iff gcd , 1

  • 8/13/2019 02-mathematics.pdf

    12/27

    Multiplicative Inverse

    All intermediate numbers computed by Euclidean

    algorithm are integer combinations of and

    Therefore, gcd , for some integers ,

    If gcd , 1, then 1for some ,

    Taking modulo gives 1 (mod )

    We will be done if we can find such and

  • 8/13/2019 02-mathematics.pdf

    13/27

    Extended Euclidean Algorithm

    Main idea: keep the original algorithm, but write all

    intermediate numbers as integer combinations of

    and

    Exercise: implementation!

  • 8/13/2019 02-mathematics.pdf

    14/27

    Chinese Remainder Theorem

    Given ,,,such that and are coprime

    Find such that mod , mod

    Solution:

    Let be the inverse of modulo

    Let be the inverse of modulo

    Set (check this yourself)

    Extension: solving for more simultaneous equations

  • 8/13/2019 02-mathematics.pdf

    15/27

    Binomial Coefficients

    is the number of ways to choose objects out

    of distinguishable objects

    or, the coefficient of in the expansion of

    Appears everywhere in combinatorics

  • 8/13/2019 02-mathematics.pdf

    16/27

    Computing

    Solution 1: Compute using the following formula

    ( 1) ( 1)

    !

    Solution 2: Use Pascals triangle

    Case 1: Both and are small

    Use either solution

    Case 2: is big, but or is small

    Use Solution 1 (carefully)

  • 8/13/2019 02-mathematics.pdf

    17/27

    Fibonacci sequence

    Definition:

    0, 1

    , where 2

    Appears in many different contexts

  • 8/13/2019 02-mathematics.pdf

    18/27

    Closed Form

    , where

    +

    Bad because and 5are irrational

    Cannot compute the exact value of for large

    There is a more stable way to compute and any other recurrence of a similar form

  • 8/13/2019 02-mathematics.pdf

    19/27

    Better Closed Form

    +

    1 1

    1 0

    1 1

    1 0

    Use fast exponentiation to compute the matrix

    power

    Can be extended to support any linear recurrencewith constant coefficients

  • 8/13/2019 02-mathematics.pdf

    20/27

    Geometry

    In theory: not that hard

    In programming contests: avoid if you can

    Will cover basic stuff today

    Computational geometry in week 9

  • 8/13/2019 02-mathematics.pdf

    21/27

    When Solving Geometry Problems

    Precision, precision, precision!

    If possible, dont 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 wont help

  • 8/13/2019 02-mathematics.pdf

    22/27

    2D Vector Operations

    Have a vector (,)

    Norm (distance from the origin):

    Counterclockwise rotation by :

    Left-multiply bycos sin

    sin cos

    Make sure to use correct units (degrees, radians)

    Normal vectors: (, ), ,

    Memorize all of them!

  • 8/13/2019 02-mathematics.pdf

    23/27

    Line-Line Intersection

    Have two lines: 0, 0

    Write in matrix form:

    Invert the matrix on the left using the following:

    Memorize this!

    Edge case:

  • 8/13/2019 02-mathematics.pdf

    24/27

    Circumcircle of a Triangle

    Have three points, ,

    Want to compute that is equidistance from, ,

    Dont try to solve the system of quadratic equations!

    Instead, do the following:

    Find the bisectors ofand

    Compute their intersection

  • 8/13/2019 02-mathematics.pdf

    25/27

    Area of a Triangle

    Have three points, ,

    Use cross product: 2

    Cross product:

    , ,

    Very important in computational geometry. Memorize!

  • 8/13/2019 02-mathematics.pdf

    26/27

    Area of a Simple Polygon

    Given , , , around perimeter of polygon

    If is convex, we can decompose into triangles:

    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:

    2 (+ +)=

  • 8/13/2019 02-mathematics.pdf

    27/27

    Conclusion

    No need to look for one-line closed form solutions

    Multi-step algorithms are good enough

    This is a CS class, after all

    Have fun with the exercise problems

    and come to the practice contest if you can!