Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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