You are on page 1of 2

26/03/2017 AlgorithmsWeeklybyPetrMitrichev:XXISt.

PetersburgStateUniversityChampionshipproblemC

0 More NextBlog madhavsharma0@gmail.com Dashboard SignOut

ALGORITHMSWEEKLYBYPETR
MITRICHEV

commentspoweredby
W E D N E S D AY, O C T O B E R 2 2 , 2 0 0 8
HyperComments
XXISt.PetersburgStateUniversityChampionship
problemC
SUBSCRIBE
I'veparticipatedinacontestthisweekendinSt.Petersburg,andI'dliketoexplain
(atleast)oneproblemfromthatcontestIthinkitmaybeinterestingevenfor Posts
peoplenotverymuchintoprogrammingcompetitions.
Comments

Theproblemis:howmanyparallelogramsaretherewithverticeshavinginteger
coordinates,xfrom0ton,yfrom0tom(bothinclusive)?nandmaregivenas
input,andbothdon'texceed2000(soanO(n^2)algorithmisalmostsurelyOK, SubscribeviaEmail
whileO(n^3)ormoreisalmostsurelytoomuch).

BLOG ARCHIVE
Thisiskindofclassicalproblemifwereplaceparallelogramswithtriangles.Butit
turnsoutthatparallelogramsallowforsomemoreinterestingsolutions.There'san 2017(9)
approachthatisthesameforbothparallelogramsandtriangles.What'sthe 2016(54)
problemwithcountingthoseparallelogramsdirectly?Well,theremaybetoomuch
2015(56)
ontheorderofn^6(theparallelogramisbasicallydefinedby3ofitsvertices)so
weneedtosomehowcountmanyatonce.Howcanwedothat?Themostobvious 2014(60)
waywouldbetocountparallelogramsthatdifferonlybyashifttogether.Buteven 2013(31)
thatwouldonlygetusn^4variants.Todobetter,weneedtosomehowgroup 2012(28)
differentparallelogramstogether.Themoststraightforwardwaytodothisisto
2011(14)
countallparralelogramsthathavethesameboundingboxtogether.Why?Because
allthoseparallelogramshavethesameamountofpossibleshifts(ifthebounding 2010(10)
boxis[x1,x2]*[y1,y2],thenwehave(nx2+x1+1)*(my2+y1+1)possibleshifts),so 2009(19)
theanswerfortheproblemcanbecalculatedas:sum(wfrom0ton)ofsum(hfrom
2008(11)
0tom)ofnum(0,0,w,h)*(nw+1)*(mh+1),wherenum(x1,x2,y1,y2)isthenumber
November(4)
ofdifferentparallelogramswiththegivenboundingbox.Ifwecangetthatnumber
inO(1),thenwe'redonewehaveaO(n^2)solution! October(5)
Solutionsforprevious
ForaparallelogramABCDtohavethegivenrectangle(wtimesh)asabounding problems
box,theparallelogrammusthaveavertexoneachofthesidesofthisrectangle. XXISt.PetersburgState
There'retwochoices: University
1)oneoftheverticesoftheparallelogram(say,A)coincideswithavertexofthe Championship...
rectangle.Thenit'seasytoseethattheoppositevertexCoftheparallelogram Ashortmathproblem
coincideswiththeoppositevertexoftherectangle,andtherestoftheparallelogram insteadofalong
isdefinedbythepositionofthethirdvertexBoftheparallelogram.Thenumberof philosophic...
possibilitiesforBisjustthenumberofpointsinsidetherectangle,minusthe2
IOI2002:Frog
pointsalreadytaken,andminusthepointsthatlieonthesegmentAC.Andweneed

http://petrmitrichev.blogspot.com/2008/10/xxistpetersburgstateuniversity.html 1/2
26/03/2017 AlgorithmsWeeklybyPetrMitrichev:XXISt.PetersburgStateUniversityChampionshipproblemC

todividetheresultingnumberby2toaccountforBandDbeinginterchangeable, Screencastand
andmultiplyitby2toaccountfortwopossiblechoicesforwhichverticesofthe challenging
rectangleAandCgoto.Thus,weget:((w+1)*(h+1)gcd(w,h)1)/2*2=(wh+w+h
September(1)
gcd(w,h)).(Thereasonwhygcdappearshereislefttothereader)
2)Eachsideoftherectanglehasonevertexofparralleogramstrictlyinsideit.Then, May(1)
theparallelogramisdefinedbytwoofitsvertices,AandB,asCandDwillbe
locatedinsymmetricpositions,sothere're(w1)*(h1)choiceshere.
ABOU T ME
So,we'dhaveaO(n^2)solutionifweknewthevaluesforgcd(w,h).But,wecan
calculateallsuchgcd'sinO(n^2)usingDynamicProgrammingandthefactthat
gcd(w,h)=gcd(wh,h)forw>h.Sowe'redone!

There'sanotherO(n^2)solutionthatgroupsparallelogramsinadifferentmanner:
wecangroupthembythelocationoftheircenter,whichalwayshashalfinteger P E TR M I TRI CH E V
coordinates.Giventhelocationofthecenter,theparallelogramisdefinedbytwoof Follow
itsvertices,andthosetwoverticescanbeplacedarbitrarilyinsidesomerectangle,
minusthecaseswheretheylieonthesamelinewiththecenter. V I EW MY CO MP LET E P R O FI LE


Butexplainingthosesolutionsisnotthemainpointofthispost.I'mwritingthis
because,whenIwasaskedrecentlyataTCSpotlightSessionwhatkindof
mathematicalbackgroundisneededtosucceedinprogrammingcompetitions,my
answerwas"Onethingthatisabsolutelycrucialistobeabletothink
'mathematically'",andIhaddifficultyexplainingwhatthatmeans.Ibelievethis
problem,andthissolution,isanexcellentexample,andexplanation,ofthat
phrase.It'scrucialtobeabletodoseveralquiteeasyreductionstepsuntilyour
problemsplitsintoseveralquiteeasyproblems,andtocarefullydothereductions
withoutomittinganyimportantcases(likewhenthegcdappearedintheabove
solution).Tobeableto'explore'somegivendirectiondeeply,andcheckifityieldsa
solutionornot(insteadofjusttryingtoapplysomeknownideasonebyone).

Doesthismakesense?

Andalso,canyousolvetheaboveproblemfasterthanO(n^2)?

P O S T ED B Y P ET R MI T R I CH EV AT 10: 59 P M

NewerPost Home OlderPost

Subscribeto:PostComments(Atom)

http://petrmitrichev.blogspot.com/2008/10/xxistpetersburgstateuniversity.html 2/2

You might also like