ebook img

Competitive Programming 4: The Lower Bound of Programming Contests in the 2020s PDF

352 Pages·2020·11.615 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Competitive Programming 4: The Lower Bound of Programming Contests in the 2020s

This is the 100% identical eBook (PDF) version of CP4 Book 2 that was released on 19 July 2020 Please read https://cpbook.net/errata for the latest known updates to this PDF c Steven, Felix, Suhendry � ii Contents Authors’ Profiles vii 5 Mathematics 273 5.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 5.2 Ad Hoc Mathematical Problems . . . . . . . . . . . . . . . . . . . . . . . . . 274 5.3 Number Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 5.3.1 Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 5.3.2 Probabilistic Prime Testing (Java Only) . . . . . . . . . . . . . . . . 284 5.3.3 Finding Prime Factors with Optimized Trial Divisions. . . . . . . . . 284 5.3.4 Functions Involving Prime Factors . . . . . . . . . . . . . . . . . . . 286 5.3.5 Modified Sieve. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 5.3.6 Greatest Common Divisor & Least Common Multiple . . . . . . . . . 288 5.3.7 Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 5.3.8 Working with Prime Factors . . . . . . . . . . . . . . . . . . . . . . . 289 5.3.9 Modular Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 5.3.10 Extended Euclidean Algorithm . . . . . . . . . . . . . . . . . . . . . 292 5.3.11 Number Theory in Programming Contests . . . . . . . . . . . . . . . 293 5.4 Combinatorics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 5.4.1 Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 5.4.2 Binomial Coe�cients . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 5.4.3 Catalan Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 5.4.4 Combinatorics in Programming Contests . . . . . . . . . . . . . . . . 301 5.5 Probability Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 5.6 Cycle-Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 5.6.1 Problem Description . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 5.6.2 Solutions using E�cient Data Structures . . . . . . . . . . . . . . . . 308 5.6.3 Floyd’s Cycle-Finding Algorithm . . . . . . . . . . . . . . . . . . . . 309 5.7 Game Theory (Basic) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 5.8 Matrix Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 5.8.1 Some Definitions and Sample Usages . . . . . . . . . . . . . . . . . . 315 5.8.2 E�cient Modular Power (Exponentiation) . . . . . . . . . . . . . . . 316 5.8.3 E�cient Matrix Modular Power (Exponentiation) . . . . . . . . . . . 317 5.8.4 DP Speed-up with Matrix Power . . . . . . . . . . . . . . . . . . . . 318 5.9 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 321 5.10 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 6 String Processing 325 6.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 6.2 Ad Hoc String (Harder) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 6.3 String Processing with DP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 iii CONTENTS c Steven, Felix, Suhendry � 6.3.1 String Alignment (Edit Distance) . . . . . . . . . . . . . . . . . . . . 329 6.3.2 Longest Common Subsequence. . . . . . . . . . . . . . . . . . . . . . 331 6.3.3 Non Classical String Processing with DP . . . . . . . . . . . . . . . . 331 6.4 String Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 6.4.1 Library Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 6.4.2 Knuth-Morris-Pratt (KMP) Algorithm . . . . . . . . . . . . . . . . . 333 6.4.3 String Matching in a 2D Grid . . . . . . . . . . . . . . . . . . . . . . 336 6.5 Su�x Trie/Tree/Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 6.5.1 Su�x Trie and Applications . . . . . . . . . . . . . . . . . . . . . . . 338 6.5.2 Su�x Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 6.5.3 Applications of Su�x Tree . . . . . . . . . . . . . . . . . . . . . . . . 341 6.5.4 Su�x Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 6.5.5 Applications of Su�x Array . . . . . . . . . . . . . . . . . . . . . . . 351 6.6 String Matching with Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . 355 6.6.1 Hashing a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 6.6.2 Rolling Hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 6.6.3 Rabin-Karp String Matching Algorithm. . . . . . . . . . . . . . . . . 357 6.6.4 Collisions Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 6.7 Anagram and Palindrome . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 6.7.1 Anagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 6.7.2 Palindrome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 6.8 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 363 6.9 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 7 (Computational) Geometry 365 7.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 7.2 Basic Geometry Objects with Libraries . . . . . . . . . . . . . . . . . . . . . 368 7.2.1 0D Objects: Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 7.2.2 1D Objects: Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 7.2.3 2D Objects: Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 7.2.4 2D Objects: Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . 378 7.2.5 2D Objects: Quadrilaterals. . . . . . . . . . . . . . . . . . . . . . . . 381 7.3 Algorithms on Polygon with Libraries . . . . . . . . . . . . . . . . . . . . . . 384 7.3.1 Polygon Representation . . . . . . . . . . . . . . . . . . . . . . . . . 384 7.3.2 Perimeter of a Polygon . . . . . . . . . . . . . . . . . . . . . . . . . . 384 7.3.3 Area of a Polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 7.3.4 Checking if a Polygon is Convex . . . . . . . . . . . . . . . . . . . . . 386 7.3.5 Checking if a Point is Inside a Polygon . . . . . . . . . . . . . . . . . 387 7.3.6 Cutting Polygon with a Straight Line . . . . . . . . . . . . . . . . . . 388 7.3.7 Finding the Convex Hull of a Set of Points . . . . . . . . . . . . . . . 390 7.4 3D Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 7.5 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 398 7.6 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 8 More Advanced Topics 401 8.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 8.2 More Advanced Search Techniques . . . . . . . . . . . . . . . . . . . . . . . 402 8.2.1 Backtracking with Bitmask . . . . . . . . . . . . . . . . . . . . . . . 402 8.2.2 State-Space Search with BFS or Dijkstra’s . . . . . . . . . . . . . . . 405 8.2.3 Meet in the Middle . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 iv CONTENTS c Steven, Felix, Suhendry � 8.3 More Advanced DP Techniques . . . . . . . . . . . . . . . . . . . . . . . . . 411 8.3.1 DP with Bitmask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 8.3.2 Compilation of Common (DP) Parameters . . . . . . . . . . . . . . . 412 8.3.3 Handling Negative Parameter Values with O↵set . . . . . . . . . . . . 412 8.3.4 MLE/TLE? Use Better State Representation . . . . . . . . . . . . . . 414 8.3.5 MLE/TLE? Drop One Parameter, Recover It from Others . . . . . . 415 8.3.6 Multiple Test Cases? No Memo Table Re-initializations . . . . . . . . 416 8.3.7 MLE? Use bBST or Hash Table as Memo Table . . . . . . . . . . . . 417 8.3.8 TLE? Use Binary Search Transition Speedup . . . . . . . . . . . . . . 417 8.3.9 Other DP Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 8.4 Network Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 8.4.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . 420 8.4.2 Ford-Fulkerson Method . . . . . . . . . . . . . . . . . . . . . . . . . . 420 8.4.3 Edmonds-Karp Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 422 8.4.4 Dinic’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 8.4.5 Flow Graph Modeling - Classic . . . . . . . . . . . . . . . . . . . . . 428 8.4.6 Flow Graph Modeling - Non Classic . . . . . . . . . . . . . . . . . . . 432 8.4.7 Network Flow in Programming Contests . . . . . . . . . . . . . . . . 433 8.5 Graph Matching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 8.5.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . 435 8.5.2 Graph Matching Variants . . . . . . . . . . . . . . . . . . . . . . . . 435 8.5.3 Unweighted MCBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 8.5.4 Weighted MCBM and Unweighted/Weighted MCM . . . . . . . . . . 439 8.6 NP-hard/complete Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 8.6.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 8.6.2 Pseudo-Polynomial: Knapsack, Subset-Sum, Coin-Change . . . 442 8.6.3 Traveling-Salesman-Problem (TSP) . . . . . . . . . . . . . . . 443 8.6.4 Hamiltonian-Path/Tour . . . . . . . . . . . . . . . . . . . . . . . 445 8.6.5 Longest-Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 8.6.6 Max-Independent-Set and Min-Vertex-Cover . . . . . . . . . 447 8.6.7 Min-Set-Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 8.6.8 Min-Path-Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 8.6.9 Satisfiability (SAT) . . . . . . . . . . . . . . . . . . . . . . . . . . 455 8.6.10 Steiner-Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 8.6.11 Graph-Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 8.6.12 Min-Clique-Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 8.6.13 Other NP-hard/complete Problems . . . . . . . . . . . . . . . . . . . 461 8.6.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 8.7 Problem Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 8.7.1 Two Components: Binary Search the Answer and Other . . . . . . . 465 8.7.2 Two Components: Involving E�cient Data Structure . . . . . . . . . 467 8.7.3 Two Components: Involving Geometry . . . . . . . . . . . . . . . . . 468 8.7.4 Two Components: Involving Graph . . . . . . . . . . . . . . . . . . . 468 8.7.5 Two Components: Involving Mathematics . . . . . . . . . . . . . . . 468 8.7.6 Two Components: Graph Preprocessing and DP . . . . . . . . . . . . 469 8.7.7 Two Components: Involving 1D Static RSQ/RMQ . . . . . . . . . . 470 8.7.8 Three (or More) Components . . . . . . . . . . . . . . . . . . . . . . 470 8.8 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 478 8.9 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 v CONTENTS c Steven, Felix, Suhendry � 9 Rare Topics 481 9.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 9.2 Sliding Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 9.3 Sparse Table Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 9.4 Square Root Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 9.5 Heavy-Light Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 9.6 Tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 9.7 Matrix Chain Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 9.8 Lowest Common Ancestor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 9.9 Tree Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 9.10 De Bruijn Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 9.11 Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 9.12 Pollard’s rho Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 9.13 Chinese Remainder Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 9.14 Lucas’ Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534 9.15 Rare Formulas or Theorems . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 9.16 Combinatorial Game Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 9.17 Gaussian Elimination Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 543 9.18 Art Gallery Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546 9.19 Closest Pair Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 9.20 A* and IDA*: Informed Search . . . . . . . . . . . . . . . . . . . . . . . . . 548 9.21 Pancake Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 9.22 Egg Dropping Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 9.23 Dynamic Programming Optimization . . . . . . . . . . . . . . . . . . . . . . 558 9.24 Push-Relabel Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566 9.25 Min Cost (Max) Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 9.26 Hopcroft-Karp Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 9.27 Kuhn-Munkres Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 9.28 Edmonds’ Matching Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 577 9.29 Chinese Postman Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 9.30 Constructive Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582 9.31 Interactive Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 9.32 Linear Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 9.33 Gradient Descent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 9.34 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590 Bibliography 593 vi Authors’ Profiles Steven Halim, PhD1 [email protected] StevenHalimisaseniorlecturerinSchoolofComput- ing,NationalUniversityofSingapore(SoC,NUS).He teaches several programming courses in NUS, rang- ing from basic programming methodology, intermedi- ate to hard data structures and algorithms, web pro- gramming, and also the ‘Competitive Programming’ module that uses this book. He is the coach of both the NUS ICPC teams and the Singapore IOI team. He participated in several ICPC Re- gionals as a student (Singapore 2001, Aizu 2003, Shanghai 2004). So far, he and other trainers @ NUS have successfully groomed various ICPC teams that won ten di↵erent ICPC Regionals (see below), advanced to ICPC World Finals eleven times (2009-2010; 2012-2020) with current best result of Joint-14th in ICPC World Finals Phuket 2016 (see below), as well as seven gold, nineteen silver, and fifteen bronze IOI medalists (2009-2019). He is also the Regional Contest Director of ICPC Asia Singapore 2015+2018 and is the Deputy Di- rector+International Committee member for the IOI 2020+2021 in Singapore. He has been invited to give international workshops about ICPC/IOI at various countries, e.g., Bolivia ICPC/IOI camp in 2014, Saudi Arabia IOI camp in 2019, Cambodia NOI camp in 2020. Steven is happily married to Grace Suryani Tioso and has two daughters and one son: Jane Angelina Halim, Joshua Ben Halim, and Jemimah Charissa Halim. ICPC Regionals # Year(s) Asia Jakarta 5 2013 (ThanQ), 2014 (ThanQ+), 2015 (RRwatameda), 2017 (DomiNUS), 2019 (Send Bobs to Alice) Asia Manila 2 2017 (Pandamiao), 2019 (7 Halim) Asia Nakhon Pathom 1 2018 (Pandamiao) Asia Yangon 1 2018 (3body2) Asia Kuala Lumpur 1 2019 (3body3) Table 1: NUS ICPC Regionals Wins in 2010s ICPC World Finals Team Name Rank Year Phuket, Thailand RRwatameda Joint-14/128 2016 Ekaterinburg, Russia ThanQ+ Joint-19/122 2014 Rapid City, USA TeamTam Joint-20/133 2017 Table 2: NUS ICPC World Finals Top 3 Results in 2010s 1PhD Thesis: “An Integrated White+Black Box Approach for Designing and Tuning Stochastic Local Search Algorithms”, 2009. vii AUTHORS’ PROFILES c Steven, Felix, Suhendry � Felix Halim, PhD2 [email protected] Felix Halim is a senior software engineer at Google. While in Google, he worked on distributed system problems, data analysis, indexing, internal tools, and database related stu↵. Felix has a passion for web development. He created uHunt to help UVa on- line judge users find the next problems to solve. He also developed a crowdsourcing website, https: //kawalpemilu.org, to let the Indonesian public to oversee and actively keep track of the Indonesia gen- eral election in 2014 and 2019. Asacontestant,FelixparticipatedinIOI2002Ko- rea(representingIndonesia),ICPCManila2003-2005, Kaohsiung 2006, and World Finals Tokyo 2007 (rep- resenting Bina Nusantara University). He was also one of Google India Code Jam 2005 and 2006 final- ists. Asaproblemsetter,FelixsetproblemsforICPC Jakarta 2010, 2012, 2013, ICPC Kuala Lumpur 2014, and several Indonesian national contests. Felix is happily married to Siska Gozali. The picture on the right is one of their Europe honeymoon travel photos (in Switzerland) after ICPC World Finals @ Porto 2019. For more information about Felix, visit his website at https://felix-halim.net. Suhendry E↵endy, PhD3 suhendry.e↵[email protected] Suhendry E↵endy is a research fellow in the School of Computing of the National University of Singa- pore (SoC, NUS). He obtained his bachelor degree inComputer SciencefromBinaNusantaraUniversity (BINUS), Jakarta, Indonesia, and his PhD degree in Computer Science from National University of Singa- pore, Singapore. Before completing his PhD, he was a lecturer in BINUS specializing in algorithm anal- ysis and served as the coach for BINUS competitive programming team (nicknamed as “Jollybee”). Suhendry is a recurring problem setter for the ICPC Asia Jakarta since the very first in 2008. From 2010 to 2016, he served as the chief judge for the ICPC Asia Jakarta collaborating with many other problem setters. He also set problems in many other contests, such as the ICPC Asia Kuala Lumpur, the ICPC Asia Singapore, and Olimpiade Sains Nasional bidang Komputer (Indonesia National Science Olympiad in Informatic) to name but a few. 2PhD Thesis: “Solving Big Data Problems: from Sequences to Tables and Graphs”, 2012. 3PhD Thesis: “Graph Properties and Algorithms in Social Networks: Privacy, Sybil Attacks, and the Computer Science Community”, 2017. viii Chapter 5 Mathematics We all use math every day; to predict weather, to tell time, to handle money. Math is more than formulas or equations; it’s logic, it’s rationality, it’s using your mind to solve the biggest mysteries we know. — TV show NUMB3RS 5.1 Overview and Motivation The appearance of mathematics-related problems in programming contests is not surprising sinceComputerScienceisdeeplyrootedinMathematics. Manyinterestingreallifeproblems can be modeled as mathematical problems as you will frequently see in this chapter. Recent ICPC problem sets (based on our experience in Asian Regionals) usually contain one or two mathematical problems. Recent IOIs usually do not contain pure mathematics tasks, but many tasks do require mathematical insights. This chapter aims to prepare contestants in dealing with many of these mathematical problems. We are aware that di↵erent countries place di↵erent emphases in mathematics training in pre-University education. Thus, some contestants are familiar with the mathematical terms listed in Table 5.1. But for others, these mathematical terms do not ring a bell, perhaps because the contestant has not learnt it before, or perhaps the term is di↵erent in the contestant’s native language. In this chapter, we want to make a more level-playing field for the readers by listing as many common mathematical terminologies, definitions, problems, and algorithms that frequently appear in programming contests as possible. Arithmetic Progression Geometric Progression Polynomial Algebra Logarithm/Power Big Integer Number Theory Prime Number Sieve of Eratosthenes Miller-Rabin Greatest Common Divisor Lowest Common Multiple Factorial Euler Phi Modified Sieve Extended Euclidean Linear Diophantine Modular Inverse Combinatorics Fibonacci Golden Ratio Binet’s Formula Zeckendorf’s Theorem Pisano Period Binomial Coe�cients Fermat’s little theorem Lucas’ Theorem Catalan Numbers Inclusion-Exclusion Probability Theory Cycle-Finding Game Theory Zero-Sum Game Decision Tree Perfect Play Minimax Nim Game Sprague-Grundy Theorem Matrix Power Table 5.1: List of some mathematical terms discussed in this chapter 273 5.2. AD HOC MATHEMATICAL PROBLEMS c Steven, Felix, Suhendry � 5.2 Ad Hoc Mathematical Problems We start this chapter with something light: the Ad Hoc mathematical problems. These are programming contest problems that require no more than basic programming skills and some fundamental mathematics. As there are still too many problems in this category, we further divide them into sub-categories, as shown below. These problems are not placed in Book1astheyareAdHocproblemswith(heavier)mathematicalflavor. Butrememberthat many of these Ad Hoc mathematical problems are the easier ones. To do well in the actual programming contests, contestants must also master the other sections of this chapter. Finding (Simple) Formula or Pattern • These problems require the problem solver to read the problem description carefully to get a simplified formula or to spot the pattern. Attacking them directly will usually result in a TLE verdict. The actual solutions are usually short and do not require loops or recursions. Example: Let set S be an infinite set of square integers: 1, 4, 9, { 16, 25, ... . Given an integer X (1 X 1018), count how many integers in S are }   less than X. The answer is simply: pX 1 . This is an O(1) solution. b � c Note that in Section 5.4, we will discuss Combinatorics problems that will also end up with some (not necessarily simple) formula. We also have Section 9.15 where we discuss a few known but very rare mathematical formulas. Base Number Conversion or Variants • Thesearethemathematicalproblemsinvolvingbasenumbers. Themostfrequenttype involves the standard conversion problems that can be easily solved manually or with C/C++/Python/OCaml (limited) or Java Integer/BigInteger (most generic) library. For example, to convert 132 in base 8 (octal) into base 2 (binary), we can use base 10 (decimal)astheintermediatestep: (132) is1 82+3 81+2 80 = 64+24+2 = (90) 8 10 ⇥ ⇥ ⇥ and (90) is 90 45(0) 22(1) 11(0) 5(1) 2(1) 1(0) 0(1) = 10 ! ! ! ! ! ! ! (1011010) (that is, divide by 2 until 0, then read the remainders from backwards). 2 However, we can also use built-in libraries: – C/C++: int v; scanf("%o", &v); // read v in octal bitset<32> bin(v); // use bitset printf("%s\n", bin.to_string().c_str()); // print in binary – Python: print("{0:b}".format(int(str(input()), 8))) # octal to binary – OCaml: Printf.sprintf "%X" (int_of_string "0o374");; # octal to hexa – Java: If we know Java Integer/BigInteger class, we can actually construct an instance of Integer/BigInteger class in any base (radix) and use its toString(int radix) methodtoprintthevalueofthatinstanceinanybase(radix). Thisisamuchmore flexible library solution than C/C++ or Python solutions earlier that are limited to popular bases = 2/8/10/16. See an example below for Kattis - basicremains (also available at UVa 10551 - Basic Remains). Given a base b and two non- negative integers p and m—both in base b, compute p % m and print the result as a base b integer. The solution is as follows: 274

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.