Asyouknow,anundirectedconnectedgraphwithnnodesandn - 1edgesiscalledatree.Youaregivenanintegerdandatreeconsistingofnnodes.Eachnodeihasavalueaiassociatedwithit.WecallasetSoftreenodesvalidiffollowingconditionsaresatisfied:Sisnon-empty.Sisconnected.Inotherwords,ifnodesuandvareinS,thenallnodeslyingonthesimplepathbetweenuandvshouldalsobepresentedinS..Yourtaskistocountthenumberofvalidsets.S...
Namisplayingwithastringonhiscomputer.ThestringconsistsofnlowercaseEnglishletters.Itismeaningless,soNamdecidedtomakethestringmorebeautiful,thatistomakeitbeapalindromebyusing4arrowkeys:left,right,up,down.Thereisacursorpointingatsomesymbolofthestring.Supposethatcursorisatpositioni(1 ≤ i ≤ n,thestringuses1-basedindexing)now.Leftandrightarrowkeysareusedtomovecursoraroundthestring.Thestri...
Let'sdefinelogicalORasanoperationontwologicalvalues(i.e.valuesthatbelongtotheset{0, 1})thatisequalto1ifeitherorbothofthelogicalvaluesissetto1,otherwiseitis0.WecandefinelogicalORofthreeormorelogicalvaluesinthesamemanner:whereisequalto1ifsomeai = 1,otherwiseitisequalto0.NamhasamatrixAconsistingofmrowsandncolumns.Therowsarenumberedfrom1tom,columnsarenumberedfrom1ton.Elementatrowi(1 ≤ ...
Forapositiveintegernlet'sdefineafunctionf:f(n) = - 1 + 2 - 3 + .. + ( - 1)nnYourtaskistocalculatef(n)foragivenintegern.InputThesinglelinecontainsthepositiveintegern(1 ≤ n ≤ 1015).OutputPrintf(n)inasingleline.Sampletest(s)input[crayon-67ae0d95e8ee4900679544/]output[crayon-67ae0d95e8eec993670351/]input[crayon-67ae0d95e8ef0735851917/]output[crayon-67ae0d95e8ef3471694843/]N...
近期评论