You are on page 1of 41

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.
CS3233
C i i P i CompetitiveProgramming
Dr.StevenHalim
Week09 Mathematics
in Programming Contests inProgrammingContests
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
O tli Outline
MiniContest#7+Discussion+Break+Admins
Mathematics Related Problems & Algorithms MathematicsRelatedProblems&Algorithms
AdHocMathematicsProblems(quickoverview)
Those that do not need specific algorithm just basic coding/math skill Thosethatdonotneedspecificalgorithm,justbasiccoding/mathskill
JavaBigInteger Class
Number Theory, especially Prime Factors and Modulo Arithmetic NumberTheory,especiallyPrimeFactorsandModuloArithmetic
Manyothertopicsareforselfreadingathome(CP2.9)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
M th ti CS d ICPC/IOI (1) Mathematics,CS,andICPC/IOI(1)
ComputerScienceisdeeplyrootedinMaths
C t M th Compute=Math
ItisnotasurprisetoseemanyMathsproblems
inICPC(PS:IOItasksareusuallynot Mathsspecific)
Many of which, I do not have time to teach you Manyofwhich,Idonothavetimetoteachyou
Fewothers,IcannotteachyouasIdonotknowthemyet
CS3233 is NOT a pure Mathematics module CS3233isNOTapureMathematicsmodule
Only1week(1.5hours)isdevotedforMathematicsrelatedtopic
It i i if i k b l i Itisniceifwecanimproveourranksbysolving
somemathematicsproblemsinprogrammingcontest
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
Readmorereferencesaboutpowerfulmathalgorithms
and/orinterestingnumbertheories,etc
StudyC++<cmath>&Java.Util.Math/Java.MathLibrary
TrymathsproblemsinUVa/otherOJandatprojecteuler y p / p j
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Th L t Pl ( t h ) TheLecturePlan(moreathome)
Today,wewilldiscussasmallsubset ofthisbigdomain
Pl Plan:
Wewillskip/fastforwardthenotsointerestingstuffs
IwillgiveseveralMathsrelatedpopquizzesusingclicker
systemtoseehowfaryouknowthesetricks
Wewillfocusonseveralrelatedsubjects:
BigInteger,PrimeFactors,andModuloArithmetic g g , ,
AllinvolveBig(Large)Integers...
You will then have to read Chapter 5 of CP2.9 on your own YouwillthenhavetoreadChapter5ofCP2.9onyourown
(itisahugechapterbtw)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
MathematicsRelated Problems Mathematics RelatedProblems
(ascurrentlylistedinCP2.9)
1. AdHocMathematics
1. TheSimpleOnes
2. Mathematical Simulation (Brute Force)
4. NumberTheory
1. PrimeNumbers:SieveofEratosthenes
2. GCD & LCM 2. MathematicalSimulation(BruteForce)
3. FindingPatternorFormula
4. Grid
5. NumberSystemsorSequences
2. GCD&LCM
3. Factorial
4. PrimeFactors
5. WorkingwithPrimeFactors
6. Logarithm,Exponentiation,Power
7. Polynomial
8. BaseNumberVariant
9 Just Ad Hoc
6. FunctionsinvolvingPrimeFactors
7. ModifiedSieve
8. ModuloArithmetic
9 Extended Euclid/Linear Diophantine Equation 9. JustAdHoc
2. JavaBigInteger
1. BasicFeatures
2. BonusFeatures
9. ExtendedEuclid/LinearDiophantineEquation
10. Others
5. ProbabilityTheory
6 CycleFinding
3. Combinatorics
1. FibonacciNumbers
2. BinomialCoefficients
6. CycleFinding
1. FloydsTortoiseHareAlgorithm
7. GameTheory
1 Two Players Game Minimax
3. CatalanNumbers
4. Others
1. TwoPlayersGame,Minimax
2. Nim Game(SpragueGrundyTheorem)
8. AlsoseeChapter9
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Programmingproblemsthatarefromthedomainofmathematics,
butwedonotneedspecializeddatastructure(s)oralgorithm(s)tosolvethem
W ill d A QUICK SPLASH AND DASH L th d t il t h WewilldoAQUICKSPLASHANDDASHLearnthedetailsathome
Section5.2
ADHOCMATHEMATICS
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Th Si l O TheSimplerOnes
Nothingtoteach
Th t i l ll Theyaretoosimple,really
Youcanget~10ACsin<1hourifyousolveall g y
problemslistedinthiscategoryinCP2.9
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
M th ti l Si l ti (B t F ) MathematicalSimulation(BruteForce)
Nothingtoteachotherthantheonesalready
presented during iterative/recursive presentedduringiterative/recursive
CompleteSearchtopic
Justremembertoprunethesearchspace
wheneverpossible
Note:Problemsthatrequireothertechnique(like
number theory knowledge) and cannot be solved numbertheoryknowledge)andcannotbesolved
withbruteforceareNOTclassifiedinthiscategory
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Fi di P tt F l FindingPatternorFormula
Thisrequiresyourmathematicalinsightstoobtain
those patterns/formulas as soon as possible to thosepatterns/formulasassoonaspossible to
reducethetimepenalty(inICPCsetting)
Usefultrick:
Solvesomesmallinstancesbyhand y
Listthesolutionsandseeifthereis/areanypattern(s)?
Lets do a q ick e ercise Letsdoaquickexercise
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
G id Grid
Alsoaboutfindingpattern
It i ti it i l ti th id Itrequirescreativity onmanipulatingthegridor
convertingittosimplerones
Example:
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
N b S t S NumberSystemsorSequences
Nothingmuchtoteach:O
M t f th ti f ll f ll i th bl Mostofthetime,carefullyfollowingtheproblem
descriptionissufficient
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
L ith E ti ti P Logarithm,Exponentiation,Power
InC/C++<cmat h>,wehavel og(basee)and
l og10(base 10) l og10(base10)
InJava.lang.Math,weonlyhavel og(basee)
Todol og
b
( a) (baseb),wecanuse:
l og( a) / l og( b) l og( a) / l og( b)
Btw,whatdoesthiscodesnippetdo?
( i ) f l ( 1 l 10( ( d bl ) ) ) ( i nt ) f l oor ( 1 + l og10( ( doubl e) a) )
Andhowtocomputethenthrootofa? p
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
P l i l Polynomial
Representation:Usuallythecoefficientsoftheterms
in some sorted order (based on power) insomesortedorder(basedonpower)
Polynomialformatting,evaluation,derivation
(Hornersrule),division,remainder,roots(Ruffinis
rule) )
Thesolutionusuallyrequirescarefulloops
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
B N b V i t BaseNumberVariants
Doyouknowthatbasenumberconversionisnow
super easy with Java BigInteger? supereasy withJavaBigInteger?
However,forsomevariants,westillhavetogotothe
basicsmethod
Thesolutionusuallyusebase10(decimal) y ( )
asanintermediatestep
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
A d A F Oth AndAFewOthers
Bestwaytolearnthese:Viapractice
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
S ti 5 3 Section5.3
APowerfulAPIforProgrammingContests
JAVABIGINTEGERCLASS
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
J Bi I t JavaBigInteger
1. IamaJavauserbutIhave
never used it before neveruseditbefore
2. Iama(pure)C++userso
I never used it before Inever useditbefore
3. IamaJavauserandIhave
used it before useditbefore
4. Iambilingual(Java/C++)
d I h d it b f andIhaveuseditbefore
0 0 0 0
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
1 2 3 4
0 of 5
Bi I t (1) BigInteger(1)
Rangeofdefaultintegerdatatypes(C++)
unsigned int = unsigned long: 2
32
(9 10 digits) unsignedint=unsignedlong:2
32
(910digits)
unsignedlonglong:2
64
(1920digits)
Q ti Question:
Whatis777!,i.e.factorialof777?
Solution?
Big Integer: Use string to represent number BigInteger:Usestring torepresentnumber
~numbercanbeaslongascomputermemorypermits
FYI this is similar to how basic data types are stored in computer FYI,thisissimilartohowbasicdatatypesarestoredincomputer
memory.Justthatthistimewedonothavelimitationofthe
numberofbits(digits)used
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Bi I t (2) BigInteger(2)
OperationsonBigInteger
B i dd bt t lti l di id t Basic:add,subtract,multiply,divide,etc
Usehighschoolmethod
Someexamplesbelow:

218
1 car r y
218
45
218
45
- - - 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 contest Writingthese highschoolmethods duringstressfulcontest
environmentisnotagoodstrategy!
Fortunately,JavahasBigIntegerlibrary y, g g y
Theyareallowedtobeusedincontests(ICPCandCS3233)
Souseit
Note:IOIdoesnotallowJavayet,
andanyway,IhavenotseeBigIntegerrelatedtasksinIOI
Or,ifyouinsist,buildyourownBigIntlibraryandbringits
hardcopytofuturecontests! py
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
J Bi I t Cl JavaBigIntegerClass
Thisclassisratherpowerful
N t j t it ll f b i th ti l ti Notjustitallowsforbasicmathematicaloperations
involvingbigintegers(addition,subtraction,multiplication,
division mod or remainder and power) division,modorremainder,andpower)
Italsoprovidessupportfor:
FindingGCDofbignumbers
Findingthesolutionofx
y
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 i Combinatorics
Givenproblemdescription,
find some nice formula to count something findsomeniceformulatocountsomething
Codingis(usuallyvery)short
Findingtheformulaisnotstraightforward
IfformulahasoverlappingsubproblemsuseDP
IfformulayieldhugenumbersuseJavaBigInteger
Memorize/studythebasicones:Fibonaccibased / y
formulas,BinomialCoefficients,CatalanNumbers
PS: On Line Encyclopedia of Integer Sequences PS:OnLineEncyclopediaofIntegerSequences
Canbeagoodreference:http://oeis.org/
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Programmingproblemsthatrequirestheknowledgeofnumbertheory,
otherwiseyouwilllikelygetTimeLimitExceeded(TLE)response
f l i th i l forsolvingthemnaively
Section5.5
NUMBERTHEORY
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
P i N b PrimeNumbers
Firstprimeandtheonlyevenprime:2
Fi 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,647
U d/ i Used/appearin:
Factoring
Cryptography
ManyotherproblemsinICPC,etc
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
O ti i d P i T ti OptimizedPrimeTesting
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)=numofprimesuptoM
F thi d ll i b f h d Forthis,weneedsmallerprimesbeforehand
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
P i G ti PrimeGeneration
Whatifwewanttogeneratealistof
prime numbers between [0 N]? primenumbersbetween[0N]?
Slownavealgorithm:
Loop i f r om[ 0 N]
i f ( i sPr i me( i ) )
pr i nt i
Canwedobetter?
Yes:SieveofEratosthenes
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Si f E t th Al ith SieveofEratosthenesAlgorithm
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 i Code:sieve&isPrime
#i ncl ude <bi t set > / / compact STL f or Si eve, bet t er t han vect or <bool >!
l l _si eve_si ze; / / l l i s def i ned as: t ypedef l ong l ong l l ;
bi t set <10000010> bs; / / 10^7 shoul d be enough f or most cases
vi pr i mes; / / compact l i st of pr i mes i n f or mof vect or <i nt >
voi d si eve( l l upper bound) { / / cr eat e l i st of pr i mes i n [ 0. . upper bound]
si eve si ze = upper bound + 1; / / add 1 t o i ncl ude upper bound _si eve_si ze = upper bound + 1; / / add 1 t o i ncl ude upper bound
bs. set ( ) ; / / set al l bi t s t o 1
bs[ 0] = bs[ 1] = 0; / / except i ndex 0 and 1
f or ( l l i = 2; i <= _si eve_si ze; i ++) i f ( bs[ i ] ) {
/ / cr oss out mul t i pl es of i st ar t i ng f r omi * i !
f or ( l l j = i * i ; j <= _si eve_si ze; j += i ) bs[ j ] = 0;
pr i mes. push_back( ( i nt ) i ) ; / / add t hi s pr i me t o t he l i st of pr i mes
} } / / cal l t hi s met hod i n mai n met hod } } / / cal l t hi s met hod i n mai n met hod
bool i sPr i me( l l N) { / / a good enough det er mi ni st i c pr i me t est er
i f ( N <= _si eve_si ze) r et ur n bs[ N] ; / / O( 1) f or smal l pr i mes _ _
f or ( i nt i = 0; i < ( i nt ) pr i mes. si ze( ) ; i ++)
i f ( N %pr i mes[ i ] == 0) r et ur n f al se;
r et ur n t r ue; / / i t t akes l onger t i me i f N i s a l ar ge pr i me!
} / / t l k f N < ( l t i i i " i " ) ^2 } / / not e: onl y wor k f or N <= ( l ast pr i me i n vi " pr i mes" ) ^2
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
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&CEuclidalgorithm etter & Famous algorithm: & C uclid algorithm
GCD(a,0)=a
GCD(a b) GCD(b a % b) // problem size decreases a lot!! GCD(a,b)=GCD(b,a%b)//problemsizedecreasesalot!!
Itsrecursivecodeiseasytowrite:
i nt gcd( i nt a, i nt b) { r et ur n ( 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)
i nt l cm( i nt a, i nt b) { r et ur n ( a / gcd( a, b) * b) ; }
/ / Q: why we wr i t e t he l cmcode t hi s way?
Noteforgcd/lcmofmorethan2numbers:
gcd(a,b,c)=gcd(a,gcd(b,c));
BothgcdandlcmrunsinO(log
10
n) g ( g
10
)
wheren=max(a,b)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
F t i l Factorial
Whatisthehighestn sothatfactorial(n) stillfitsin
64 bits unsigned long long? 64bitsunsignedlonglong?
Answer:n=20
20! = 2432902008176640000
ul l = 18446744073709551615
21! 51090942171709440000 21! = 51090942171709440000
Hm...soalmostallfactorialrelatedquestionsrequire
JavaBigInteger?
Not really, if you use prime power factorization Notreally,ifyouuseprimepowerfactorization
Example:7!=2x3x4x5x6x7=2x3x2x2x5x2x3x7=2
4
x3
2
x5x7
Now: is 7! divisible by 35? (Revisited Soon) Now:is7!divisibleby35?(RevisitedSoon)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
P i F t PrimeFactors
Directalgorithm:Generatelistofprimes(usesieve),
check how many of them can divide integer N checkhowmanyofthemcandivideintegerN
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 t Code:PrimeFactors
vi pr i meFact or s( l l N) { / / r emember : vi i s vect or <i nt >, l l i s l ong l ong
vi f act or s;
l l PF i dx = 0 PF = pr i mes[ PF i dx] ; / / PF = 2 t hen 3 5 7 i s al so ok l l PF_i dx 0, PF pr i mes[ PF_i dx] ; / / PF 2, t hen 3, 5, 7, . . . i s al so ok
whi l e ( N ! = 1 && ( PF * PF <= N) ) { / / st op at sqr t ( N) ; N can get smal l er
whi l e ( N %PF == 0) { N / = PF; f act or s. push_back( PF) ; } / / r emove PF
PF = pr i mes[ ++PF i dx] ; / / onl y consi der pr i mes! PF = pr i mes[ ++PF_i dx] ; / / onl y consi der pr i mes!
}
i f ( N ! = 1) f act or s. push_back( N) ; / / speci al case i f N i s a pr i me
r et ur n f act or s; / / i f N does not f i t i n 32- bi t i nt eger and i s a pr i me r et ur n f act or s; / / i f N does not f i t i n 32 bi t i nt eger and i s a pr i me
} / / t hen `f act or s' wi l l have t o be changed t o vect or <l l >
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Okthatsenoughforthissemester
THEOTHERMATHSPROBLEMS
IN PROGRAMMING CONTEST INPROGRAMMINGCONTEST
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
N t C d i L t thi S NotCoveredinLecturethisSem
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 read Theyaregoodread
Readthemonyourown
These problems will not appear as problem A or B in mini contest 8 TheseproblemswillnotappearasproblemAorBinminicontest8
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
M M Still N t i CP2 9 Y t ManyMoreStillNotinCP2.9Yet
Mathematicsisalargefield
Pollards Rho integer factoring algorithm Pollard 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 t ThePaceofthisLecture
1. Tooslow,Ialready
k ll th knowallthese
Iwanttoknowmore
2. Fine;weareusedto
itnow
3 Crazy as always 3. Crazyasalways
andwestillhave
0 0 0
lotsofreading
material at home
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
1 2 3
materialathome
0 of 30
SSummary
Wehaveseensome mathematicsrelatedproblems
and algorithms andalgorithms
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 f References
CP2.9,Chapter5andpartsofChapter9
I 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

You might also like