Introduction to Instructor: William W.Y. Hsu › Mathematics › Ad Hoc Mathematics CONTENTS

9/20/2016 PROGRAMMING IN C 2 Mathematics, CS, and ICPC/IOI › is deeply rooted in Math! – Compute = Math › It is not a surprise to see many Math problems in ICPC (PS: IOI tasks are usually not Math‐specific) – Many of which, I do not have time to teach you… – Few others, I cannot teach you as I do not know them yet… – It is nice if we can improve our ranks by solving some mathematics problems in programming contest.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 3 Mathematics, CS, and ICPC/IOI › Tips: – Revise your high school mathematics. – Read more references about powerful math and/or interesting number theories, etc › Discrete mathematics! – Study C++ & Java.Util.Math/Java.Math Library. – Try maths problems in UVa/other OJ and at projecteuler.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 4 Mathematics‐Related Problems

› Ad Hoc Mathematics › – The simple ones – Prime numbers: sieve of Eratosthenes – Mathematical simulation (brute force) – GCD & LCM – Finding pattern or formula – Factorial – Grid – Prime factors – Number systems or sequences – Working with prime factors – Polynomial – Functions involving prime factors – Base number variant – Modified sieve – Just Ad Hoc – Modulo arithmetic – Extended Euclid/Linear Diophantine › Java BigInteger equation – Basic features – Bonus features › Probability theory › › Cycle finding – Fibonacci numbers – Floyd’s Tortoise-Hare – Binomial coefficients › Game theory – Catalan numbers – Tow play game, minimax – Other – Num game (Sprague Grundy theorem)

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 5 Ad Hoc Mathematics Programming problems that are from the domain of mathematics, but we do not need specialized data structure(s) or algorithm(s) to solve them.

9/20/2016 PROGRAMMING IN C 6 The Simpler Ones › Nothing to teach › They are too simple, really… › You can get ~10 ACs in < 1 hour if you solve all problems listed in this category in CP2.9

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 7 Mathematical Simulation (Brute Force) › Nothing to teach other than the ones already presented during iterative/recursive “Complete Search” topic – Just remember to prune the search space whenever possible! › Note: Problems that require other technique (like number theory knowledge) and cannot be solved with brute‐force are NOT classified in this category.

› UVa 100: 3n+1, UVa 11150: Cola

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 8 Finding Pattern or Formula › This requires your mathematical insights to obtain those patterns/formulas as soon as possible to reduce the time penalty (in ICPC setting). › Useful trick: – Solve some small instances by hand!

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 9 Grid › Also about finding pattern. › It requires creativity on manipulating the grid or converting it to simpler ones.

14 13 5 4 6 12 1 3 7 11 2 10 8 9

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 10 Number Systems or Sequences › Nothing much to teach :O › Most of the time, carefully following the problem description is sufficient.

› UVa 100: 3n+1

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 11 Logarithm, Exponentiation, Power › In C/C++ , we have log (base e) and log10 (base 10) › In Java.lang.Math, we only have log (base e) › To do log ( ) (base b), we can use: – log(a)/log(b) 𝑏𝑏 𝑎𝑎 › Btw, what does this code snippet do? – (int)floor(1 + log10((double)a)) › How to compute the ‐th root of ?

𝑛𝑛 𝑎𝑎

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 12 Polynomial › Representation: Usually the coefficients of the terms in some sorted order (based on power) › Polynomial formatting, evaluation, derivation (Horner’s rule), division, remainder, roots (Ruffini’s rule)… › The solution usually requires careful loops…

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 13 Base Number Variants › Do you know that base number conversion is now super easy with Java BigInteger? – java.math.BigInteger.toString(int radix) › However, for some variants, we still have to go to the basics method. – The solution usually use base 10 (decimal) as an intermediate step.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 14 Java BigInteger Class

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 15 Java BigInteger Survey › Which class are you?

1. I am a Java user but I have never used it before. 2. I am a (pure) C++ user so I never used it before. 3. I am a Java user and I have used it before. 4. I am bilingual (Java/C++) and I have used it before.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 16 Big Integer › Range of default integer data types (C++) – unsigned int = unsigned long: 2 (9‐10 digits) – unsigned long long: 2 (19‐20 digits32 ) – __int128: 2 64 › Standard routines128 incomplete! › Question: – What is “777!”, i.e. factorial of 777?

› • Solution? Efficiency? – Big Integer: Use string or vector to represent number (in C/C++). – Number can be as long as computer memory permits. › FYI, this is similar to how basic data types are stored in computer memory. – Just that this time we do not have limitation of the number of bits (digits) used.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 17 Big Integer › Operations on Big Integer – Basic: add, subtract, multiply, divide, etc – Use “high school method” › Can be improved by using multi-digit arrays. – 3 digit example: 89423019

89 423 19

– printf(“%03d”, digit[a] ); can be used for middle terms to fill in zeros.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 18 Big Integer › Writing these “high school methods” during stressful contest environment is not a good strategy! › Fortunately, Java has BigInteger library – They are allowed to be used in contests (ICPC). – Note: IOI does not allow Java yet, and anyway, I have not see BigInteger‐related tasks in IOI… › Or, if you insist, build your own BigInt library and bring its hardcopy to future contests!

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 19 So Use it!

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 20 Java BigInteger Class › This class is rather powerful: – Not just it allows for basic mathematical operations involving big integers (addition, subtraction, multiplication, division, mod or remainder, and power)… – It also provides support for: › Finding GCD of big numbers. › Finding the solution of (modulo arithmetic). › Very Easy Base Number Conversion, quite useful. 𝑥𝑥𝑥𝑥 𝑚𝑚𝑚𝑚𝑚𝑚 𝑚𝑚 › NEW : IsProbablePrime

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 21 Java BigDecimal Class › BigDecimal is for large floating point numbers. › The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. › The toString() method provides a canonical representation of a BigDecimal. It gives the user complete control over rounding behavior. › Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations decimal point motion operations. › This class and its iterator implement all of the optional methods of the Comparable interfaces.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 22 UVa 748 - Exponentiation Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of where is a real number (0.0 < < 99.999) and𝑛𝑛 is an integer such that 0 < 25. 𝑅𝑅 𝑅𝑅 𝑅𝑅 𝑛𝑛 𝑛𝑛 ≤

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 23 UVa 748 - Exponentiation Input The input will consist of a set of pairs of values for and . The value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. 𝑅𝑅 𝑛𝑛 𝑅𝑅 Output The output will consist of one line for each line of input giving the exact value of . Leading zeros and insignificant trailing zeros should𝑛𝑛 be suppressed in the output. 𝑅𝑅

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 24 UVa 748 - Exponentiation Sample Input 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 Sample Output 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.12682503013196972066120

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 25 Approach › C/C++ – Array? (Oh no~~~) – std::vectors<> (doesn’t seem better) › Java – BigDecimal! › C# – Similar solution as Java.

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 26 Java Solution import java.util.Scanner; import java.math.BigDecimal; public class Main {

public static void main(String[] args) { Scanner cin = new Scanner(System.in);

while (cin.hasNext()) { BigDecimal num = cin.nextBigDecimal(); int n = cin.nextInt(); This is it! num = num.pow(n);

String s = num.toPlainString(); if (s.charAt(0) == '0') s = s.substring(1);

int end = s.length(); while (s.charAt(end - 1) == '0') end--; if (s.charAt(end - 1) == '.') end--; s = s.substring(0, end); System.out.println(s); } } } 9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 27 Combinatorics Discrete mathematics *gasp*

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 28 Combinatorics › Given problem description, find some nice formula to count something. – Coding is (usually very) short – Finding the formula is not straightforward. › If formula has overlapping sub problems use DP

› If formula yield huge numbers use Java→ BigInteger. → › Memorize/study the basic ones: Fibonacci‐based formulas, Binomial Coefficients, Catalan Numbers… › PS: On‐Line Encyclopedia of Integer Sequences can be a good reference: http://oeis.org/

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 29 Number Theory Programming problems that requires the knowledge of number theory, otherwise you will likely get Time Limit Exceeded (TLE) response for solving them naively. Prime Numbers › First prime and the only even prime: 2 › First 10 primes: {2, 3, 5, 7, 11, 13, 17, 19, 23, 29} › Primes in range: – 1 to 100 : 25 primes – 1 to 1,000 : 168 primes – 1 to 7,919 : 1,000 primes – 1 to 10,000 : 1,229 primes › Largest prime in signed 32‐bit int = 2,147,483,647 › Used/appear in: – Factoring – Cryptography – Many other problems in ICPC, etc

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 31 Optimized Prime Testing › Algorithms for testing if is prime: isPrime(N) – First try: check if is divisible by [2, . . . , 1]? › ( ) 𝑁𝑁 𝑁𝑁 𝑖𝑖 ∈ 𝑁𝑁 − Improved 1: Is divisible by [2, … , ]? – 𝑂𝑂 𝑁𝑁 › ( ) 𝑁𝑁 𝑖𝑖 ∈ 𝑁𝑁 Improved 2: Is divisible by [3,5, . . . , ]? – 𝑂𝑂 𝑛𝑛 › One test for = 2, no need to test other even numbers! 𝑁𝑁 𝑖𝑖 ∈ 𝑁𝑁 › ( ) = 𝑖𝑖( ) 𝑁𝑁 – Improved𝑂𝑂 2 3: Is𝑂𝑂 N divisible𝑁𝑁 by [primes ]? › ( ( )) = ( /log( )) 𝑖𝑖 ∈ ≤ 𝑁𝑁 › ( ) = number of primes up to . 𝑂𝑂 𝜋𝜋 𝑁𝑁 𝑂𝑂 𝑁𝑁 𝑁𝑁 › For this, we need to generate smaller primes beforehand. 𝜋𝜋 𝑀𝑀 𝑀𝑀

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 32 Prime Generation › What if we want to generate a list of prime numbers between [0 … ]?

› Slow naïve algorithm:𝑁𝑁 Loop from [0 … ] if (isPrime(i)) print𝑖𝑖 i 𝑁𝑁 › Can we do better? – Yes: Sieve of Eratosthenes

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 33 Sieve of Eratosthenes Algorithm › Generate primes between [0 … ]: – Use bitset of size , set all true except index 0 & 1 – Start from = 2 until × > 𝑁𝑁 › If bitset at index𝑁𝑁 is on, cross all multiple of (i.e. turn off bit at index i) starting from × 𝑖𝑖 𝑘𝑘 𝑖𝑖 𝑁𝑁 𝑖𝑖 𝐼𝐼 › – Finally,𝑖𝑖 whatever𝑖𝑖 not crossed are primes › Example:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, 51, 52, 53, 54, 55, …, 75, 76, 77, … 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, 51, 52, 53, 54, 55, …, 75, 76, 77, … 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, 51, 52, 53, 54, 55, …, 75, 76, 77, … 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, 51, 52, 53, 54, 55, …, 75, 76, 77, … 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, 51, 52, 53, 54, 55, …, 75, 76, 77, …

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 34 Code: sieve & isPrime

#include // compact STL for Sieve, better than vector! ll _sieve_size; // ll is defined as: typedef long long ll; bitset<10000010> bs; // 10 should be enough for most cases vi primes; // compact7 list of primes in form of vector void sieve(ll upperbound) { // create list of primes in [0..upperbound] _sieve_size = upperbound + 1; // add 1 to include upperbound bs.set(); // set all bits to 1 bs[0] = bs[1] = 0; // except index 0 and 1

for (ll i = 2; i <= _sieve_size; i++) if (bs[i]) { // cross out multiples of i starting from i * i! for (ll j = i * i; j <= _sieve_size; j += i) bs[j] = 0; primes.push_back((int)i); // add this prime to the list of primes } } // call this method in main method bool isPrime(ll N) { // a good enough deterministic prime tester if (N <= _sieve_size) return bs[N]; // O(1) for small primes for (int i = 0; i < (int)primes.size(); i++) if (N % primes[i] == 0) return false; return true; // it takes longer time if N is a large prime! } // note: only work for N (last prime in vi "primes")^2

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 35 Greatest Common Divisor (GCD) › Naïve Algorithm: – Find all divisors of and (slow). – Find those that are common. – Pick the greatest one.𝑎𝑎 𝑏𝑏 › Better & Famous algorithm: D & C Euclid algorithm – GCD(a, 0) = a – GCD(a, b) = GCD(b, a % b) // problem size decreases a lot!! › Its recursive code is easy to write: int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 36 Lowest Common Multiple (LCM) › lcm(a, b) = a*b / gcd(a, b) int lcm(int a, int b) { return (a / gcd(a, b) * b); } // Q: why we write the lcm code this way? › Note for gcd/lcm of more than 2 numbers: – gcd(a, b, c) = gcd(a, gcd(b, c)); › Both gcd and lcm runs in (log ) where = max( , ). 𝑂𝑂 10𝑛𝑛 𝑛𝑛 𝑎𝑎 𝑏𝑏

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 37 Factorial › What is the highest n so that factorial(n) still fits in 64‐bits unsigned long long? – Answer: = 20 – 20! = 2432902008176640000 𝑛𝑛 – ull = 18446744073709551615 – 21! = 51090942171709440000 › Almost all factorial related questions require Java BigInteger?

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 38 Prime Factors › Direct algorithm: Generate list of primes (use sieve), check how many of them can divide integer . – This can be improved! 𝑁𝑁 › Better algorithm: Divide and Conquer! – An integer can be expressed as: – = PF 𝑁𝑁 › PF = Prime′ factor 𝑁𝑁› =another∗ 𝑁𝑁 number which is / . – If ′= 1, stop; otherwise, repeat. 𝑁𝑁 𝑁𝑁 𝑃𝑃𝑃𝑃 – is ′reduced every time we find a divisor. 𝑁𝑁 𝑁𝑁

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 39 The Other Math Problems in Programming Contest

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 40 Topics › Linear Diophantine Equation › Probability Theory › Cycle‐Finding › Game Theory › Gaussian Elimination › Matrix Power › Roman Numerals

9/20/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 41