
Introduction to Competitive Programming Instructor: William W.Y. Hsu › Mathematics › Ad Hoc Mathematics CONTENTS 9/20/2016 PROGRAMMING IN C 2 Mathematics, CS, and ICPC/IOI › Computer Science 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 algorithms and/or interesting number theories, etc › Discrete mathematics! – Study C++ <cmath> & 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 › Number theory – 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 › Combinatorics › Cycle finding – Fibonacci numbers – Floyd’s Tortoise-Hare algorithm – 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++ <cmath>, 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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages41 Page
-
File Size-