Week09 Maths

Preview:

DESCRIPTION

Competitive Programming

Citation preview

  • This course material is now made available for public usage.Special acknowledgement to School of Computing, National University of Singapore

    for allowing Steven to prepare and distribute these teaching materials.

    CS3233C i i P iCompetitiveProgramming

    Dr.StevenHalimWeek09 Mathematicsin Programming ContestsinProgrammingContests

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • O tliOutline

    MiniContest#7+Discussion+Break+Admins Mathematics Related Problems & Algorithms MathematicsRelatedProblems&Algorithms

    AdHocMathematicsProblems(quickoverview) Those that do not need specific algorithm just basic coding/math skillThosethatdonotneedspecificalgorithm,justbasiccoding/mathskill

    JavaBigInteger Class Number Theory, especially Prime Factors and Modulo ArithmeticNumberTheory,especiallyPrimeFactorsandModuloArithmetic Manyothertopicsareforselfreadingathome(CP2.9)

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • M th ti CS d ICPC/IOI (1)Mathematics,CS,andICPC/IOI(1)

    ComputerScienceisdeeplyrootedinMathsC t M th Compute=Math

    ItisnotasurprisetoseemanyMathsproblemsinICPC(PS:IOItasksareusuallynotMathsspecific) Many of which, I do not have time to teach youManyofwhich,Idonothavetimetoteachyou Fewothers,IcannotteachyouasIdonotknowthemyetCS3233 is NOT a pure Mathematics module CS3233isNOTapureMathematicsmodule

    Only1week(1.5hours)isdevotedforMathematicsrelatedtopic

    It i i if i k b l i Itisniceifwecanimproveourranksbysolvingsomemathematicsproblemsinprogrammingcontest

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • M th ti CS d ICPC/IOI (2)Mathematics,CS,andICPC/IOI(2)

    Tips:R i hi h h l th ti Reviseyourhighschoolmathematics

    (InNUS):TakeMAXXXXmodulesasCFM:D Readmorereferencesaboutpowerfulmathalgorithmsand/orinterestingnumbertheories,etc

    StudyC++&Java.Util.Math/Java.MathLibrary TrymathsproblemsinUVa/otherOJandatprojecteulery p / p j

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Th L t Pl ( t h )TheLecturePlan(moreathome)

    Today,wewilldiscussasmallsubset ofthisbigdomainPl Plan: Wewillskip/fastforwardthenotsointerestingstuffs IwillgiveseveralMathsrelatedpopquizzesusingclickersystemtoseehowfaryouknowthesetricks

    Wewillfocusonseveralrelatedsubjects:BigInteger,PrimeFactors,andModuloArithmeticg g , ,

    AllinvolveBig(Large)Integers...

    You will then have to read Chapter 5 of CP2.9 on your ownYouwillthenhavetoreadChapter5ofCP2.9onyourown(itisahugechapterbtw)

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • MathematicsRelated ProblemsMathematics RelatedProblems(ascurrentlylistedinCP2.9)

    1. AdHocMathematics1. TheSimpleOnes2. Mathematical Simulation (Brute Force)

    4. NumberTheory1. PrimeNumbers:SieveofEratosthenes2. GCD & LCM2. MathematicalSimulation(BruteForce)

    3. FindingPatternorFormula4. Grid5. NumberSystemsorSequences

    2. GCD&LCM3. Factorial4. PrimeFactors5. WorkingwithPrimeFactors

    6. Logarithm,Exponentiation,Power7. Polynomial8. BaseNumberVariant9 Just Ad Hoc

    6. FunctionsinvolvingPrimeFactors7. ModifiedSieve8. ModuloArithmetic9 Extended Euclid/Linear Diophantine Equation9. JustAdHoc

    2. JavaBigInteger1. BasicFeatures2. BonusFeatures

    9. ExtendedEuclid/LinearDiophantineEquation10. Others

    5. ProbabilityTheory6 CycleFinding

    3. Combinatorics1. FibonacciNumbers2. BinomialCoefficients

    6. CycleFinding1. FloydsTortoiseHareAlgorithm

    7. GameTheory1 Two Players Game Minimax

    3. CatalanNumbers4. Others

    1. TwoPlayersGame,Minimax2. Nim Game(SpragueGrundyTheorem)

    8. AlsoseeChapter9

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Programmingproblemsthatarefromthedomainofmathematics,butwedonotneedspecializeddatastructure(s)oralgorithm(s)tosolvethemW ill d A QUICK SPLASH AND DASH L th d t il t h WewilldoAQUICKSPLASHANDDASHLearnthedetailsathomeSection5.2

    ADHOCMATHEMATICS

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Th Si l OTheSimplerOnes

    NothingtoteachTh t i l ll Theyaretoosimple,really

    Youcanget~10ACsin

  • M th ti l Si l ti (B t F )MathematicalSimulation(BruteForce)

    Nothingtoteachotherthantheonesalreadypresented during iterative/recursivepresentedduringiterative/recursiveCompleteSearchtopic Justremembertoprunethesearchspacewheneverpossible

    Note:Problemsthatrequireothertechnique(likenumber theory knowledge) and cannot be solvednumbertheoryknowledge)andcannotbesolvedwithbruteforceareNOTclassifiedinthiscategory

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Fi di P tt F lFindingPatternorFormula

    Thisrequiresyourmathematicalinsightstoobtainthose patterns/formulas as soon as possible tothosepatterns/formulasassoonaspossible toreducethetimepenalty(inICPCsetting)

    Usefultrick: Solvesomesmallinstancesbyhandy Listthesolutionsandseeifthereis/areanypattern(s)?

    Lets do a q ick e ercise Letsdoaquickexercise

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • G idGrid

    AlsoaboutfindingpatternIt i ti it i l ti th id Itrequirescreativity onmanipulatingthegridorconvertingittosimplerones

    Example:

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • N b S t SNumberSystemsorSequences

    Nothingmuchtoteach:OM t f th ti f ll f ll i th bl Mostofthetime,carefullyfollowingtheproblemdescriptionissufficient

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • L ith E ti ti PLogarithm,Exponentiation,Power

    InC/C++,wehavelog (basee)andlog10 (base 10)log10 (base10)

    InJava.lang.Math,weonlyhavelog (basee) Todologb(a) (baseb),wecanuse:log(a) / log(b)log(a) / log(b)

    Btw,whatdoesthiscodesnippetdo?(i )fl (1 l 10((d bl ) ))(int)floor(1 + log10((double)a))

    Andhowtocomputethenthrootofa?p

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • P l i lPolynomial

    Representation:Usuallythecoefficientsofthetermsin some sorted order (based on power)insomesortedorder(basedonpower)

    Polynomialformatting,evaluation,derivation(Hornersrule),division,remainder,roots(Ruffinisrule))

    Thesolutionusuallyrequirescarefulloops

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • B N b V i tBaseNumberVariants

    Doyouknowthatbasenumberconversionisnowsuper easy with Java BigInteger?supereasy withJavaBigInteger?

    However,forsomevariants,westillhavetogotothebasicsmethod Thesolutionusuallyusebase10(decimal)y ( )asanintermediatestep

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • A d A F OthAndAFewOthers

    Bestwaytolearnthese:Viapractice

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • S ti 5 3Section5.3APowerfulAPIforProgrammingContests

    JAVABIGINTEGERCLASS

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • J Bi I tJavaBigInteger

    1. IamaJavauserbutIhavenever used it beforeneveruseditbefore

    2. Iama(pure)C++usersoI never used it beforeInever useditbefore

    3. IamaJavauserandIhaveused it beforeuseditbefore

    4. Iambilingual(Java/C++)d I h d it b f andIhaveuseditbefore

    0 000

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

    1 2 3 40 of 5

  • Bi I t (1)BigInteger(1)

    Rangeofdefaultintegerdatatypes(C++)unsigned int = unsigned long: 232 (9 10 digits) unsignedint=unsignedlong:232(910digits)

    unsignedlonglong:264 (1920digits)

    Q ti Question: Whatis777!,i.e.factorialof777?

    Solution? Big Integer: Use string to represent numberBigInteger:Usestring torepresentnumber

    ~numbercanbeaslongascomputermemorypermits FYI this is similar to how basic data types are stored in computerFYI,thisissimilartohowbasicdatatypesarestoredincomputermemory.Justthatthistimewedonothavelimitationofthenumberofbits(digits)used

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Bi I t (2)BigInteger(2)

    OperationsonBigIntegerB i dd bt t lti l di id t Basic:add,subtract,multiply,divide,etc

    Usehighschoolmethod Someexamplesbelow:

    2181 carry21845

    21845--- x

    --- +263

    1090 (218*5)872 (218*4)*10----- +9810

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Bi I t (3)BigInteger(3)

    Note: Writing these high school methods during stressful contestWritingthese highschoolmethods duringstressfulcontest

    environmentisnotagoodstrategy!

    Fortunately,JavahasBigIntegerlibraryy, g g y Theyareallowedtobeusedincontests(ICPCandCS3233)

    Souseit

    Note:IOIdoesnotallowJavayet,andanyway,IhavenotseeBigIntegerrelatedtasksinIOI

    Or,ifyouinsist,buildyourownBigIntlibraryandbringitshardcopytofuturecontests!py

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • J Bi I t ClJavaBigIntegerClass

    ThisclassisratherpowerfulN t j t it ll f b i th ti l ti Notjustitallowsforbasicmathematicaloperationsinvolvingbigintegers(addition,subtraction,multiplication,division mod or remainder and power)division,modorremainder,andpower)

    Italsoprovidessupportfor: FindingGCDofbignumbers Findingthesolutionofxy modm(moduloarithmetic)V E B N b C i it f l VeryEasyBaseNumberConversion,quiteuseful

    NEWinCP2.9:IsProbablePrime See various examples in the book Seevariousexamplesinthebook

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Section5.4

    COMBINATORICS

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • C bi t iCombinatorics

    Givenproblemdescription,find some nice formula to count somethingfindsomeniceformulatocountsomething Codingis(usuallyvery)short Findingtheformulaisnotstraightforward

    Ifformulahasoverlappingsubproblems useDP Ifformulayieldhugenumbers useJavaBigInteger

    Memorize/studythebasicones:Fibonaccibased/ yformulas,BinomialCoefficients,CatalanNumbers

    PS: On Line Encyclopedia of Integer Sequences PS:OnLineEncyclopediaofIntegerSequences Canbeagoodreference:http://oeis.org/

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Programmingproblemsthatrequirestheknowledgeofnumbertheory,otherwiseyouwilllikelygetTimeLimitExceeded(TLE)responsef l i th i lforsolvingthemnaivelySection5.5

    NUMBERTHEORY

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • P i N bPrimeNumbers

    Firstprimeandtheonlyevenprime:2Fi t 10 i {2 3 5 7 11 13 17 19 23 29} First10primes:{2,3,5,7,11,13,17,19,23,29}

    Primesinrange:g 1to100:25primes 1to1,000:168primes 1to7,919:1,000primes 1to10,000:1,229primes

    Largestprimeinsigned32bitint=2,147,483,647U d/ i Used/appearin: Factoring Cryptography ManyotherproblemsinICPC,etc

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • O ti i d P i T tiOptimizedPrimeTesting

    AlgorithmsfortestingifNisprime:isPrime(N)Fi t t h k if N i di i ibl b i [2 N 1]? Firsttry:checkifNisdivisiblebyi [2..N1]?

    O(N)

    I d 1 I N di i ibl b i [2 (N)]? Improved1:IsNdivisiblebyi [2..sqrt(N)]? O(sqrt(N))

    Improved2:IsNdivisiblebyi [3,5,..sqrt(N)]? Onetestfori=2,noneedtotestotherevennumbers! O(sqrt(N)/2)=O(sqrt(N))

    Improved3:IsNdivisiblebyi primessqrt(N) O((sqrt(N)))=O(sqrt(N)/log(sqrt(N)))

    (M)=numofprimesuptoMF thi d ll i b f h d Forthis,weneedsmallerprimesbeforehand

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • P i G tiPrimeGeneration

    Whatifwewanttogeneratealistofprime numbers between [0 N]?primenumbersbetween[0N]?

    Slownavealgorithm:Loop i from [0 N]if (isPrime(i))

    print i Canwedobetter?

    Yes:SieveofEratosthenes

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Si f E t th Al ithSieveofEratosthenesAlgorithm

    Generateprimesbetween[0N]:f Usebitset ofsizeN,setalltrueexceptindex0&1

    Startfromi=2untilk*i>N Ifbitsetatindexiison,crossallmultipleofI(i.e.turnoffbitatindexi)startingfromi*i

    Finally,whatevernotcrossedareprimes

    Example: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,

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • C d i & i P iCode:sieve&isPrime#include // compact STL for Sieve, better than vector!ll _sieve_size; // ll is defined as: typedef long long ll;bitset bs; // 10^7 should be enough for most casesvi primes; // compact 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_sieve_size = upperbound + 1; // add 1 to include upperbound

    bs.set(); // set all bits to 1bs[0] = bs[1] = 0; // except index 0 and 1for (ll i = 2; i

  • G t t C Di i (GCD)GreatestCommonDivisor(GCD)

    NaveAlgorithm:Fi d ll di i f d b ( l ) Findalldivisorsofaandb(slow)

    Findthosethatarecommon Pickthegreatestone

    Better&Famousalgorithm:D&CEuclidalgorithmetter & Famous algorithm: & C uclid algorithm GCD(a,0)=aGCD(a b) GCD(b a % b) // problem size decreases a lot!! GCD(a,b)=GCD(b,a%b)//problemsizedecreasesalot!!

    Itsrecursivecodeiseasytowrite:int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • L t C M lti l (LCM)LowestCommonMultiple(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?

    Noteforgcd/lcmofmorethan2numbers: gcd(a,b,c)=gcd(a,gcd(b,c));

    BothgcdandlcmrunsinO(log10 n)g ( g10 )wheren=max(a,b)

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • F t i lFactorial

    Whatisthehighestn sothatfactorial(n) stillfitsin64 bits unsigned long long?64bitsunsignedlonglong? Answer:n=20

    20! = 2432902008176640000 ull = 1844674407370955161521! 51090942171709440000 21! = 51090942171709440000

    Hm...soalmostallfactorialrelatedquestionsrequireJavaBigInteger? Not really, if you use prime power factorizationNotreally,ifyouuseprimepowerfactorization Example:7!=2x3x4x5x6x7=2x3x2x2x5x2x3x7=24x32x5x7Now: is 7! divisible by 35? (Revisited Soon) Now:is7!divisibleby35?(RevisitedSoon)

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • P i F tPrimeFactors

    Directalgorithm:Generatelistofprimes(usesieve),check how many of them can divide integer NcheckhowmanyofthemcandivideintegerN Thiscanbeimproved!

    Betteralgorithm:DivideandConquer! An integer N can be expressed as:AnintegerNcanbeexpressedas:

    N=PF*N' PF=aprimefactor

    But if integer I is a large prime, then thi i till l N'=anothernumberwhichisN/PF

    IfN'=1,stop;otherwise,repeat

    this is still slow.

    This fact is the basis Nisreducedeverytimewefindadivisor for cryptography

    techniques

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • C d P i F tCode:PrimeFactorsvi primeFactors(ll N) { // remember: vi is vector, ll is long longvi factors;ll PF idx = 0 PF = primes[PF idx]; // PF = 2 then 3 5 7 is also okll PF_idx 0, PF primes[PF_idx]; // PF 2, then 3,5,7,... is also okwhile (N != 1 && (PF * PF
  • Okthatsenoughforthissemester

    THEOTHERMATHSPROBLEMSIN PROGRAMMING CONTESTINPROGRAMMINGCONTEST

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • N t C d i L t thi SNotCoveredinLecturethisSem

    LinearDiophantineEquation(Section5.5) Probability Theory (Section 5 6) ProbabilityTheory(Section5.6) CycleFinding(Section5.7)

    h ( ) GameTheory(Section5.8) GaussianElimination(Section9.4) MatrixPower(Section9.13) RomanNumerals(Section9.20)( ) TheyarealreadywritteninCP2.9

    They are good readTheyaregoodread Readthemonyourown These problems will not appear as problem A or B in mini contest 8TheseproblemswillnotappearasproblemAorBinminicontest8

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • M M Still N t i CP2 9 Y tManyMoreStillNotinCP2.9Yet

    Mathematicsisalargefield Pollards Rho integer factoring algorithmPollard sRhointegerfactoringalgorithm ManyotherPrimetheorems,hypotheses,conjectures ChineseRemainderTheorem LotsofDivisibilityProperties CombinatorialGames,etc,

    AgainCS3233!=MathModule Chapter 5 of CP2 9 has a collection of ~372 UVa programming Chapter5ofCP2.9hasacollectionof 372 UVa programming

    exercisesthehighestamongthe9chaptersinCP2.9!

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • Th P f thi L tThePaceofthisLecture

    1. Tooslow,Ialreadyk ll thknowalltheseIwanttoknowmore

    2. Fine;weareusedtoitnow

    3 Crazy as always3. Crazyasalwaysandwestillhave

    0 00lotsofreadingmaterial at home

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

    1 2 3

    materialathome0 of 30

  • SSummary

    Wehaveseensomemathematicsrelatedproblemsand algorithmsandalgorithms Toomanytobelearnedinonenight Evenso,manyothersareleftuncovered(someareinsideCP2.9foryoutoreadonyourownpace)

    Bestwaytolearn:Lotsofpractice In the next two weeks, two more new topics:Inthenexttwoweeks,twomorenewtopics:

    Week10:StringProcessing(FocusonSA)W k 11 (C t ti l) G t (F P l ) Week11:(Computational)Geometry(FocusonPolygons)

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

  • R fReferences

    CP2.9,Chapter5 andpartsofChapter9I t d ti t Al ith Ch 31 A di A/B/C IntroductiontoAlgorithms,Ch31,AppendixA/B/C

    ProjectEuler,http://projecteuler.net/j , p //p j /

    CS3233 CompetitiveProgramming,StevenHalim,SoC,NUS

Recommended