├── APIO ├── 08-Beads.cpp ├── 09-ATM.cpp ├── 10-Commando.cpp ├── 12-Dispatching.cpp ├── 12-Kunai.cpp ├── 13-Robots.cpp ├── 13-Toll.cpp ├── 14-Beads.cpp ├── 14-Palindrome.cpp ├── 14-Sequence.cpp ├── 15-Bridge.cpp ├── 15-Sculpture.cpp ├── 16-Boat.cpp ├── 16-Fireworks.cpp ├── 17-Merchant.cpp ├── 17-Rainbow.cpp ├── 18-Circle_Selection.cpp ├── 18-Duathlon(1).cpp ├── 18-Duathlon(2).cpp ├── 18-New_Home.cpp ├── 20-Fun.cpp ├── 20-Paint.cpp └── 20-Swap.cpp ├── AtCoder ├── B112-C.cpp ├── G002-E.cpp ├── G003-E.cpp ├── G004-E.cpp ├── G005-E.cpp ├── G006-E.cpp ├── G007-E.cpp ├── G008-E.cpp ├── G009-E.cpp ├── G011-E.cpp ├── G018-C.cpp ├── G027-A.java ├── G027-B.java ├── G027-C.java ├── G028-A.java ├── G028-B.java ├── G028-C.java ├── G033-A.cpp ├── G033-B.cpp ├── G033-C.cpp ├── G037-B.cpp ├── G037-C.cpp ├── G037-D.cpp ├── G037-E.cpp ├── R067-F.java ├── R070-E.cpp ├── R079-F.java ├── R103-D.java ├── R103-E.java └── R103-F.java ├── BkOI ├── 11-2Circles.cpp ├── 11-Time_Is_Money.cpp ├── 12-Balls.cpp ├── 12-FanGroups.cpp ├── 17-Cats.cpp └── 18-Minmaxtree.cpp ├── BtOI ├── 07-Fence.cpp ├── 07-Sorting.cpp ├── 08-Game.cpp ├── 08-Magical.cpp ├── 09-Rectangle.cpp ├── 10-Bears.cpp ├── 10-Lego.cpp ├── 11-Meetings.cpp ├── 11-Mirroring.cpp ├── 13-Pipes.cpp ├── 13-Tracks.cpp ├── 13-Vim.cpp ├── 14-Cop_Robber.cpp ├── 14-Portals.cpp ├── 14-Postmen.cpp ├── 15-Bowling.cpp ├── 15-Tug.cpp ├── 16-Cities.cpp ├── 16-Park.cpp ├── 16-Swap.cpp └── 17W-Toast.cpp ├── CCO └── 18-Wrong_Answer.cpp ├── CEOI ├── 03-Trip.cpp ├── 04-Clouds.cpp ├── 04-Football.cpp ├── 04-Two.cpp ├── 06-Walk.cpp ├── 09-Boxes.cpp ├── 11-Hotel.cpp ├── 12-Circuit.cpp ├── 12-Highway.cpp ├── 12-Race.cpp ├── 13-Adriatic.cpp ├── 13-Board.cpp ├── 13-Tram.cpp ├── 13-Treasure.cpp ├── 14-007.cpp ├── 14-Fangorn.cpp ├── 14-Wall.cpp ├── 16-ICC.cpp ├── 16-Match.cpp ├── 16-Popeala.cpp ├── 16-Trick.cpp ├── 17-Building.cpp ├── 17-OneWay.cpp ├── 17-Sure.cpp ├── 18-Cloud.cpp ├── 18-Global.cpp ├── 18-Lottery.cpp ├── 18-Toy.cpp ├── 19-AmusementPark.cpp ├── 19-Cubeword.cpp ├── 19-Diameter.cpp ├── 19-MagicTree.cpp ├── 19-Skyscrapers.cpp ├── 20-Chessrush.cpp ├── 20-Cleaning.cpp ├── 20-Fancyfence.cpp ├── 20-Potion.cpp ├── 20-Roads.cpp └── 20-Startrek.cpp ├── COCI ├── 10-Aladin.cpp ├── 10-Pasijans.cpp ├── 15-Divljak.cpp ├── 16-Nekameleoni.cpp ├── 17-Osmosmjerka(1).cpp ├── 17-Osmosmjerka(2).cpp ├── 17-Rima.cpp └── 18-Deda.cpp ├── COI ├── 15-Cvenk.cpp ├── 15-Koviance.cpp ├── 16-Relay.cpp └── 18-Paprike.cpp ├── CSAcademy ├── 78-E.cpp ├── 82-E.java ├── 84-E.cpp ├── 84-F.cpp ├── 84-G.cpp ├── fii-code-2020-round-3-D.cpp ├── ioi-2016-training-round-1-farey_sequence.cpp ├── junior-challenge-2017-day-1-borland.cpp └── romanian-ioi-2017-selection-2-popcorn.cpp ├── CodeChef ├── ADIMAT.cpp ├── ADITREE.cpp ├── BINOFEV.cpp ├── BIPFAMIL.cpp ├── BJUDGE.java ├── CBFEAST.cpp ├── CHANGNUM.cpp ├── CHGCDG.cpp ├── CNTIT.cpp ├── COINDENO.cpp ├── COINPART.java ├── DOCSDEL.cpp ├── DOTIT.cpp ├── EXACTWAL.cpp ├── F2NDMAX.cpp ├── GCDMOD.cpp ├── GCDSUM.cpp ├── GHVSSI.cpp ├── INMAT.java ├── KCOMPRES.cpp ├── KPERFMAT.cpp ├── LONCYC.cpp ├── MATCH2.cpp ├── MDN.java ├── MKSTR.cpp ├── MYST │ ├── Checker │ │ ├── Checker$StdIn.class │ │ ├── Checker.class │ │ └── Checker.java │ ├── Judge │ │ ├── Judge$StdIn.class │ │ ├── Judge.class │ │ └── Judge.java │ ├── MultiTestGen │ │ ├── MultiTestGen$StdIn.class │ │ ├── MultiTestGen.class │ │ └── MultiTestGen.java │ ├── SingleTestGen │ │ ├── SingleTestGen$SCCFinder.class │ │ ├── SingleTestGen$StdIn.class │ │ ├── SingleTestGen.class │ │ └── SingleTestGen.java │ ├── Solutions │ │ ├── V1_0.cpp │ │ ├── V1_0.exe │ │ ├── V2_0.cpp │ │ ├── V2_0.exe │ │ ├── V3_0.cpp │ │ ├── V3_0.exe │ │ ├── V3_1.cpp │ │ ├── V3_1.exe │ │ ├── V4_0.cpp │ │ ├── V4_0.exe │ │ ├── V4_1.cpp │ │ ├── V4_1.exe │ │ ├── V5_0.cpp │ │ ├── V5_0.exe │ │ ├── V5_1.cpp │ │ ├── V5_1.exe │ │ ├── V5_2.cpp │ │ ├── V5_2.exe │ │ ├── V5_3.cpp │ │ ├── V5_3.exe │ │ ├── V5_4.cpp │ │ └── V5_4.exe │ ├── TestData │ │ ├── 30 │ │ │ ├── 30_2_0.txt │ │ │ ├── 30_3_0.txt │ │ │ ├── 30_4_0.txt │ │ │ └── 30_5_0.txt │ │ ├── main1 │ │ │ ├── 100_2_0.txt │ │ │ ├── 100_2_1.txt │ │ │ ├── 100_2_2.txt │ │ │ ├── 100_2_3.txt │ │ │ ├── 100_2_4.txt │ │ │ ├── 100_3_0.txt │ │ │ ├── 100_3_1.txt │ │ │ ├── 100_3_2.txt │ │ │ ├── 100_3_3.txt │ │ │ ├── 100_3_4.txt │ │ │ ├── 100_4_0.txt │ │ │ ├── 100_4_1.txt │ │ │ ├── 100_4_2.txt │ │ │ ├── 100_4_3.txt │ │ │ ├── 100_4_4.txt │ │ │ ├── 100_5_0.txt │ │ │ ├── 100_5_1.txt │ │ │ ├── 100_5_2.txt │ │ │ ├── 100_5_3.txt │ │ │ ├── 100_5_4.txt │ │ │ ├── 30_2_0.txt │ │ │ ├── 30_2_1.txt │ │ │ ├── 30_2_2.txt │ │ │ ├── 30_2_3.txt │ │ │ ├── 30_2_4.txt │ │ │ ├── 30_3_0.txt │ │ │ ├── 30_3_1.txt │ │ │ ├── 30_3_2.txt │ │ │ ├── 30_3_3.txt │ │ │ ├── 30_3_4.txt │ │ │ ├── 30_4_0.txt │ │ │ ├── 30_4_1.txt │ │ │ ├── 30_4_2.txt │ │ │ ├── 30_4_3.txt │ │ │ ├── 30_4_4.txt │ │ │ ├── 30_5_0.txt │ │ │ ├── 30_5_1.txt │ │ │ ├── 30_5_2.txt │ │ │ ├── 30_5_3.txt │ │ │ ├── 30_5_4.txt │ │ │ ├── 44_2_0.txt │ │ │ ├── 44_2_1.txt │ │ │ ├── 44_2_2.txt │ │ │ ├── 44_2_3.txt │ │ │ ├── 44_2_4.txt │ │ │ ├── 44_3_0.txt │ │ │ ├── 44_3_1.txt │ │ │ ├── 44_3_2.txt │ │ │ ├── 44_3_3.txt │ │ │ ├── 44_3_4.txt │ │ │ ├── 44_4_0.txt │ │ │ ├── 44_4_1.txt │ │ │ ├── 44_4_2.txt │ │ │ ├── 44_4_3.txt │ │ │ ├── 44_4_4.txt │ │ │ ├── 44_5_0.txt │ │ │ ├── 44_5_1.txt │ │ │ ├── 44_5_2.txt │ │ │ ├── 44_5_3.txt │ │ │ ├── 44_5_4.txt │ │ │ ├── 58_2_0.txt │ │ │ ├── 58_2_1.txt │ │ │ ├── 58_2_2.txt │ │ │ ├── 58_2_3.txt │ │ │ ├── 58_2_4.txt │ │ │ ├── 58_3_0.txt │ │ │ ├── 58_3_1.txt │ │ │ ├── 58_3_2.txt │ │ │ ├── 58_3_3.txt │ │ │ ├── 58_3_4.txt │ │ │ ├── 58_4_0.txt │ │ │ ├── 58_4_1.txt │ │ │ ├── 58_4_2.txt │ │ │ ├── 58_4_3.txt │ │ │ ├── 58_4_4.txt │ │ │ ├── 58_5_0.txt │ │ │ ├── 58_5_1.txt │ │ │ ├── 58_5_2.txt │ │ │ ├── 58_5_3.txt │ │ │ ├── 58_5_4.txt │ │ │ ├── 72_2_0.txt │ │ │ ├── 72_2_1.txt │ │ │ ├── 72_2_2.txt │ │ │ ├── 72_2_3.txt │ │ │ ├── 72_2_4.txt │ │ │ ├── 72_3_0.txt │ │ │ ├── 72_3_1.txt │ │ │ ├── 72_3_2.txt │ │ │ ├── 72_3_3.txt │ │ │ ├── 72_3_4.txt │ │ │ ├── 72_4_0.txt │ │ │ ├── 72_4_1.txt │ │ │ ├── 72_4_2.txt │ │ │ ├── 72_4_3.txt │ │ │ ├── 72_4_4.txt │ │ │ ├── 72_5_0.txt │ │ │ ├── 72_5_1.txt │ │ │ ├── 72_5_2.txt │ │ │ ├── 72_5_3.txt │ │ │ ├── 72_5_4.txt │ │ │ ├── 86_2_0.txt │ │ │ ├── 86_2_1.txt │ │ │ ├── 86_2_2.txt │ │ │ ├── 86_2_3.txt │ │ │ ├── 86_2_4.txt │ │ │ ├── 86_3_0.txt │ │ │ ├── 86_3_1.txt │ │ │ ├── 86_3_2.txt │ │ │ ├── 86_3_3.txt │ │ │ ├── 86_3_4.txt │ │ │ ├── 86_4_0.txt │ │ │ ├── 86_4_1.txt │ │ │ ├── 86_4_2.txt │ │ │ ├── 86_4_3.txt │ │ │ ├── 86_4_4.txt │ │ │ ├── 86_5_0.txt │ │ │ ├── 86_5_1.txt │ │ │ ├── 86_5_2.txt │ │ │ ├── 86_5_3.txt │ │ │ └── 86_5_4.txt │ │ ├── main2 │ │ │ ├── 100_2_0.txt │ │ │ ├── 100_2_1.txt │ │ │ ├── 100_2_10.txt │ │ │ ├── 100_2_11.txt │ │ │ ├── 100_2_12.txt │ │ │ ├── 100_2_13.txt │ │ │ ├── 100_2_14.txt │ │ │ ├── 100_2_15.txt │ │ │ ├── 100_2_16.txt │ │ │ ├── 100_2_17.txt │ │ │ ├── 100_2_18.txt │ │ │ ├── 100_2_19.txt │ │ │ ├── 100_2_2.txt │ │ │ ├── 100_2_20.txt │ │ │ ├── 100_2_21.txt │ │ │ ├── 100_2_22.txt │ │ │ ├── 100_2_23.txt │ │ │ ├── 100_2_24.txt │ │ │ ├── 100_2_3.txt │ │ │ ├── 100_2_4.txt │ │ │ ├── 100_2_5.txt │ │ │ ├── 100_2_6.txt │ │ │ ├── 100_2_7.txt │ │ │ ├── 100_2_8.txt │ │ │ ├── 100_2_9.txt │ │ │ ├── 100_3_0.txt │ │ │ ├── 100_3_1.txt │ │ │ ├── 100_3_10.txt │ │ │ ├── 100_3_11.txt │ │ │ ├── 100_3_12.txt │ │ │ ├── 100_3_13.txt │ │ │ ├── 100_3_14.txt │ │ │ ├── 100_3_15.txt │ │ │ ├── 100_3_16.txt │ │ │ ├── 100_3_17.txt │ │ │ ├── 100_3_18.txt │ │ │ ├── 100_3_19.txt │ │ │ ├── 100_3_2.txt │ │ │ ├── 100_3_20.txt │ │ │ ├── 100_3_21.txt │ │ │ ├── 100_3_22.txt │ │ │ ├── 100_3_23.txt │ │ │ ├── 100_3_24.txt │ │ │ ├── 100_3_3.txt │ │ │ ├── 100_3_4.txt │ │ │ ├── 100_3_5.txt │ │ │ ├── 100_3_6.txt │ │ │ ├── 100_3_7.txt │ │ │ ├── 100_3_8.txt │ │ │ ├── 100_3_9.txt │ │ │ ├── 100_4_0.txt │ │ │ ├── 100_4_1.txt │ │ │ ├── 100_4_10.txt │ │ │ ├── 100_4_11.txt │ │ │ ├── 100_4_12.txt │ │ │ ├── 100_4_13.txt │ │ │ ├── 100_4_14.txt │ │ │ ├── 100_4_15.txt │ │ │ ├── 100_4_16.txt │ │ │ ├── 100_4_17.txt │ │ │ ├── 100_4_18.txt │ │ │ ├── 100_4_19.txt │ │ │ ├── 100_4_2.txt │ │ │ ├── 100_4_20.txt │ │ │ ├── 100_4_21.txt │ │ │ ├── 100_4_22.txt │ │ │ ├── 100_4_23.txt │ │ │ ├── 100_4_24.txt │ │ │ ├── 100_4_3.txt │ │ │ ├── 100_4_4.txt │ │ │ ├── 100_4_5.txt │ │ │ ├── 100_4_6.txt │ │ │ ├── 100_4_7.txt │ │ │ ├── 100_4_8.txt │ │ │ ├── 100_4_9.txt │ │ │ ├── 100_5_0.txt │ │ │ ├── 100_5_1.txt │ │ │ ├── 100_5_10.txt │ │ │ ├── 100_5_11.txt │ │ │ ├── 100_5_12.txt │ │ │ ├── 100_5_13.txt │ │ │ ├── 100_5_14.txt │ │ │ ├── 100_5_15.txt │ │ │ ├── 100_5_16.txt │ │ │ ├── 100_5_17.txt │ │ │ ├── 100_5_18.txt │ │ │ ├── 100_5_19.txt │ │ │ ├── 100_5_2.txt │ │ │ ├── 100_5_20.txt │ │ │ ├── 100_5_21.txt │ │ │ ├── 100_5_22.txt │ │ │ ├── 100_5_23.txt │ │ │ ├── 100_5_24.txt │ │ │ ├── 100_5_3.txt │ │ │ ├── 100_5_4.txt │ │ │ ├── 100_5_5.txt │ │ │ ├── 100_5_6.txt │ │ │ ├── 100_5_7.txt │ │ │ ├── 100_5_8.txt │ │ │ ├── 100_5_9.txt │ │ │ ├── 30_2_0.txt │ │ │ ├── 30_2_1.txt │ │ │ ├── 30_2_10.txt │ │ │ ├── 30_2_11.txt │ │ │ ├── 30_2_12.txt │ │ │ ├── 30_2_13.txt │ │ │ ├── 30_2_14.txt │ │ │ ├── 30_2_15.txt │ │ │ ├── 30_2_16.txt │ │ │ ├── 30_2_17.txt │ │ │ ├── 30_2_18.txt │ │ │ ├── 30_2_19.txt │ │ │ ├── 30_2_2.txt │ │ │ ├── 30_2_20.txt │ │ │ ├── 30_2_21.txt │ │ │ ├── 30_2_22.txt │ │ │ ├── 30_2_23.txt │ │ │ ├── 30_2_24.txt │ │ │ ├── 30_2_3.txt │ │ │ ├── 30_2_4.txt │ │ │ ├── 30_2_5.txt │ │ │ ├── 30_2_6.txt │ │ │ ├── 30_2_7.txt │ │ │ ├── 30_2_8.txt │ │ │ ├── 30_2_9.txt │ │ │ ├── 30_3_0.txt │ │ │ ├── 30_3_1.txt │ │ │ ├── 30_3_10.txt │ │ │ ├── 30_3_11.txt │ │ │ ├── 30_3_12.txt │ │ │ ├── 30_3_13.txt │ │ │ ├── 30_3_14.txt │ │ │ ├── 30_3_15.txt │ │ │ ├── 30_3_16.txt │ │ │ ├── 30_3_17.txt │ │ │ ├── 30_3_18.txt │ │ │ ├── 30_3_19.txt │ │ │ ├── 30_3_2.txt │ │ │ ├── 30_3_20.txt │ │ │ ├── 30_3_21.txt │ │ │ ├── 30_3_22.txt │ │ │ ├── 30_3_23.txt │ │ │ ├── 30_3_24.txt │ │ │ ├── 30_3_3.txt │ │ │ ├── 30_3_4.txt │ │ │ ├── 30_3_5.txt │ │ │ ├── 30_3_6.txt │ │ │ ├── 30_3_7.txt │ │ │ ├── 30_3_8.txt │ │ │ ├── 30_3_9.txt │ │ │ ├── 30_4_0.txt │ │ │ ├── 30_4_1.txt │ │ │ ├── 30_4_10.txt │ │ │ ├── 30_4_11.txt │ │ │ ├── 30_4_12.txt │ │ │ ├── 30_4_13.txt │ │ │ ├── 30_4_14.txt │ │ │ ├── 30_4_15.txt │ │ │ ├── 30_4_16.txt │ │ │ ├── 30_4_17.txt │ │ │ ├── 30_4_18.txt │ │ │ ├── 30_4_19.txt │ │ │ ├── 30_4_2.txt │ │ │ ├── 30_4_20.txt │ │ │ ├── 30_4_21.txt │ │ │ ├── 30_4_22.txt │ │ │ ├── 30_4_23.txt │ │ │ ├── 30_4_24.txt │ │ │ ├── 30_4_3.txt │ │ │ ├── 30_4_4.txt │ │ │ ├── 30_4_5.txt │ │ │ ├── 30_4_6.txt │ │ │ ├── 30_4_7.txt │ │ │ ├── 30_4_8.txt │ │ │ ├── 30_4_9.txt │ │ │ ├── 30_5_0.txt │ │ │ ├── 30_5_1.txt │ │ │ ├── 30_5_10.txt │ │ │ ├── 30_5_11.txt │ │ │ ├── 30_5_12.txt │ │ │ ├── 30_5_13.txt │ │ │ ├── 30_5_14.txt │ │ │ ├── 30_5_15.txt │ │ │ ├── 30_5_16.txt │ │ │ ├── 30_5_17.txt │ │ │ ├── 30_5_18.txt │ │ │ ├── 30_5_19.txt │ │ │ ├── 30_5_2.txt │ │ │ ├── 30_5_20.txt │ │ │ ├── 30_5_21.txt │ │ │ ├── 30_5_22.txt │ │ │ ├── 30_5_23.txt │ │ │ ├── 30_5_24.txt │ │ │ ├── 30_5_3.txt │ │ │ ├── 30_5_4.txt │ │ │ ├── 30_5_5.txt │ │ │ ├── 30_5_6.txt │ │ │ ├── 30_5_7.txt │ │ │ ├── 30_5_8.txt │ │ │ ├── 30_5_9.txt │ │ │ ├── 44_2_0.txt │ │ │ ├── 44_2_1.txt │ │ │ ├── 44_2_10.txt │ │ │ ├── 44_2_11.txt │ │ │ ├── 44_2_12.txt │ │ │ ├── 44_2_13.txt │ │ │ ├── 44_2_14.txt │ │ │ ├── 44_2_15.txt │ │ │ ├── 44_2_16.txt │ │ │ ├── 44_2_17.txt │ │ │ ├── 44_2_18.txt │ │ │ ├── 44_2_19.txt │ │ │ ├── 44_2_2.txt │ │ │ ├── 44_2_20.txt │ │ │ ├── 44_2_21.txt │ │ │ ├── 44_2_22.txt │ │ │ ├── 44_2_23.txt │ │ │ ├── 44_2_24.txt │ │ │ ├── 44_2_3.txt │ │ │ ├── 44_2_4.txt │ │ │ ├── 44_2_5.txt │ │ │ ├── 44_2_6.txt │ │ │ ├── 44_2_7.txt │ │ │ ├── 44_2_8.txt │ │ │ ├── 44_2_9.txt │ │ │ ├── 44_3_0.txt │ │ │ ├── 44_3_1.txt │ │ │ ├── 44_3_10.txt │ │ │ ├── 44_3_11.txt │ │ │ ├── 44_3_12.txt │ │ │ ├── 44_3_13.txt │ │ │ ├── 44_3_14.txt │ │ │ ├── 44_3_15.txt │ │ │ ├── 44_3_16.txt │ │ │ ├── 44_3_17.txt │ │ │ ├── 44_3_18.txt │ │ │ ├── 44_3_19.txt │ │ │ ├── 44_3_2.txt │ │ │ ├── 44_3_20.txt │ │ │ ├── 44_3_21.txt │ │ │ ├── 44_3_22.txt │ │ │ ├── 44_3_23.txt │ │ │ ├── 44_3_24.txt │ │ │ ├── 44_3_3.txt │ │ │ ├── 44_3_4.txt │ │ │ ├── 44_3_5.txt │ │ │ ├── 44_3_6.txt │ │ │ ├── 44_3_7.txt │ │ │ ├── 44_3_8.txt │ │ │ ├── 44_3_9.txt │ │ │ ├── 44_4_0.txt │ │ │ ├── 44_4_1.txt │ │ │ ├── 44_4_10.txt │ │ │ ├── 44_4_11.txt │ │ │ ├── 44_4_12.txt │ │ │ ├── 44_4_13.txt │ │ │ ├── 44_4_14.txt │ │ │ ├── 44_4_15.txt │ │ │ ├── 44_4_16.txt │ │ │ ├── 44_4_17.txt │ │ │ ├── 44_4_18.txt │ │ │ ├── 44_4_19.txt │ │ │ ├── 44_4_2.txt │ │ │ ├── 44_4_20.txt │ │ │ ├── 44_4_21.txt │ │ │ ├── 44_4_22.txt │ │ │ ├── 44_4_23.txt │ │ │ ├── 44_4_24.txt │ │ │ ├── 44_4_3.txt │ │ │ ├── 44_4_4.txt │ │ │ ├── 44_4_5.txt │ │ │ ├── 44_4_6.txt │ │ │ ├── 44_4_7.txt │ │ │ ├── 44_4_8.txt │ │ │ ├── 44_4_9.txt │ │ │ ├── 44_5_0.txt │ │ │ ├── 44_5_1.txt │ │ │ ├── 44_5_10.txt │ │ │ ├── 44_5_11.txt │ │ │ ├── 44_5_12.txt │ │ │ ├── 44_5_13.txt │ │ │ ├── 44_5_14.txt │ │ │ ├── 44_5_15.txt │ │ │ ├── 44_5_16.txt │ │ │ ├── 44_5_17.txt │ │ │ ├── 44_5_18.txt │ │ │ ├── 44_5_19.txt │ │ │ ├── 44_5_2.txt │ │ │ ├── 44_5_20.txt │ │ │ ├── 44_5_21.txt │ │ │ ├── 44_5_22.txt │ │ │ ├── 44_5_23.txt │ │ │ ├── 44_5_24.txt │ │ │ ├── 44_5_3.txt │ │ │ ├── 44_5_4.txt │ │ │ ├── 44_5_5.txt │ │ │ ├── 44_5_6.txt │ │ │ ├── 44_5_7.txt │ │ │ ├── 44_5_8.txt │ │ │ ├── 44_5_9.txt │ │ │ ├── 58_2_0.txt │ │ │ ├── 58_2_1.txt │ │ │ ├── 58_2_10.txt │ │ │ ├── 58_2_11.txt │ │ │ ├── 58_2_12.txt │ │ │ ├── 58_2_13.txt │ │ │ ├── 58_2_14.txt │ │ │ ├── 58_2_15.txt │ │ │ ├── 58_2_16.txt │ │ │ ├── 58_2_17.txt │ │ │ ├── 58_2_18.txt │ │ │ ├── 58_2_19.txt │ │ │ ├── 58_2_2.txt │ │ │ ├── 58_2_20.txt │ │ │ ├── 58_2_21.txt │ │ │ ├── 58_2_22.txt │ │ │ ├── 58_2_23.txt │ │ │ ├── 58_2_24.txt │ │ │ ├── 58_2_3.txt │ │ │ ├── 58_2_4.txt │ │ │ ├── 58_2_5.txt │ │ │ ├── 58_2_6.txt │ │ │ ├── 58_2_7.txt │ │ │ ├── 58_2_8.txt │ │ │ ├── 58_2_9.txt │ │ │ ├── 58_3_0.txt │ │ │ ├── 58_3_1.txt │ │ │ ├── 58_3_10.txt │ │ │ ├── 58_3_11.txt │ │ │ ├── 58_3_12.txt │ │ │ ├── 58_3_13.txt │ │ │ ├── 58_3_14.txt │ │ │ ├── 58_3_15.txt │ │ │ ├── 58_3_16.txt │ │ │ ├── 58_3_17.txt │ │ │ ├── 58_3_18.txt │ │ │ ├── 58_3_19.txt │ │ │ ├── 58_3_2.txt │ │ │ ├── 58_3_20.txt │ │ │ ├── 58_3_21.txt │ │ │ ├── 58_3_22.txt │ │ │ ├── 58_3_23.txt │ │ │ ├── 58_3_24.txt │ │ │ ├── 58_3_3.txt │ │ │ ├── 58_3_4.txt │ │ │ ├── 58_3_5.txt │ │ │ ├── 58_3_6.txt │ │ │ ├── 58_3_7.txt │ │ │ ├── 58_3_8.txt │ │ │ ├── 58_3_9.txt │ │ │ ├── 58_4_0.txt │ │ │ ├── 58_4_1.txt │ │ │ ├── 58_4_10.txt │ │ │ ├── 58_4_11.txt │ │ │ ├── 58_4_12.txt │ │ │ ├── 58_4_13.txt │ │ │ ├── 58_4_14.txt │ │ │ ├── 58_4_15.txt │ │ │ ├── 58_4_16.txt │ │ │ ├── 58_4_17.txt │ │ │ ├── 58_4_18.txt │ │ │ ├── 58_4_19.txt │ │ │ ├── 58_4_2.txt │ │ │ ├── 58_4_20.txt │ │ │ ├── 58_4_21.txt │ │ │ ├── 58_4_22.txt │ │ │ ├── 58_4_23.txt │ │ │ ├── 58_4_24.txt │ │ │ ├── 58_4_3.txt │ │ │ ├── 58_4_4.txt │ │ │ ├── 58_4_5.txt │ │ │ ├── 58_4_6.txt │ │ │ ├── 58_4_7.txt │ │ │ ├── 58_4_8.txt │ │ │ ├── 58_4_9.txt │ │ │ ├── 58_5_0.txt │ │ │ ├── 58_5_1.txt │ │ │ ├── 58_5_10.txt │ │ │ ├── 58_5_11.txt │ │ │ ├── 58_5_12.txt │ │ │ ├── 58_5_13.txt │ │ │ ├── 58_5_14.txt │ │ │ ├── 58_5_15.txt │ │ │ ├── 58_5_16.txt │ │ │ ├── 58_5_17.txt │ │ │ ├── 58_5_18.txt │ │ │ ├── 58_5_19.txt │ │ │ ├── 58_5_2.txt │ │ │ ├── 58_5_20.txt │ │ │ ├── 58_5_21.txt │ │ │ ├── 58_5_22.txt │ │ │ ├── 58_5_23.txt │ │ │ ├── 58_5_24.txt │ │ │ ├── 58_5_3.txt │ │ │ ├── 58_5_4.txt │ │ │ ├── 58_5_5.txt │ │ │ ├── 58_5_6.txt │ │ │ ├── 58_5_7.txt │ │ │ ├── 58_5_8.txt │ │ │ ├── 58_5_9.txt │ │ │ ├── 72_2_0.txt │ │ │ ├── 72_2_1.txt │ │ │ ├── 72_2_10.txt │ │ │ ├── 72_2_11.txt │ │ │ ├── 72_2_12.txt │ │ │ ├── 72_2_13.txt │ │ │ ├── 72_2_14.txt │ │ │ ├── 72_2_15.txt │ │ │ ├── 72_2_16.txt │ │ │ ├── 72_2_17.txt │ │ │ ├── 72_2_18.txt │ │ │ ├── 72_2_19.txt │ │ │ ├── 72_2_2.txt │ │ │ ├── 72_2_20.txt │ │ │ ├── 72_2_21.txt │ │ │ ├── 72_2_22.txt │ │ │ ├── 72_2_23.txt │ │ │ ├── 72_2_24.txt │ │ │ ├── 72_2_3.txt │ │ │ ├── 72_2_4.txt │ │ │ ├── 72_2_5.txt │ │ │ ├── 72_2_6.txt │ │ │ ├── 72_2_7.txt │ │ │ ├── 72_2_8.txt │ │ │ ├── 72_2_9.txt │ │ │ ├── 72_3_0.txt │ │ │ ├── 72_3_1.txt │ │ │ ├── 72_3_10.txt │ │ │ ├── 72_3_11.txt │ │ │ ├── 72_3_12.txt │ │ │ ├── 72_3_13.txt │ │ │ ├── 72_3_14.txt │ │ │ ├── 72_3_15.txt │ │ │ ├── 72_3_16.txt │ │ │ ├── 72_3_17.txt │ │ │ ├── 72_3_18.txt │ │ │ ├── 72_3_19.txt │ │ │ ├── 72_3_2.txt │ │ │ ├── 72_3_20.txt │ │ │ ├── 72_3_21.txt │ │ │ ├── 72_3_22.txt │ │ │ ├── 72_3_23.txt │ │ │ ├── 72_3_24.txt │ │ │ ├── 72_3_3.txt │ │ │ ├── 72_3_4.txt │ │ │ ├── 72_3_5.txt │ │ │ ├── 72_3_6.txt │ │ │ ├── 72_3_7.txt │ │ │ ├── 72_3_8.txt │ │ │ ├── 72_3_9.txt │ │ │ ├── 72_4_0.txt │ │ │ ├── 72_4_1.txt │ │ │ ├── 72_4_10.txt │ │ │ ├── 72_4_11.txt │ │ │ ├── 72_4_12.txt │ │ │ ├── 72_4_13.txt │ │ │ ├── 72_4_14.txt │ │ │ ├── 72_4_15.txt │ │ │ ├── 72_4_16.txt │ │ │ ├── 72_4_17.txt │ │ │ ├── 72_4_18.txt │ │ │ ├── 72_4_19.txt │ │ │ ├── 72_4_2.txt │ │ │ ├── 72_4_20.txt │ │ │ ├── 72_4_21.txt │ │ │ ├── 72_4_22.txt │ │ │ ├── 72_4_23.txt │ │ │ ├── 72_4_24.txt │ │ │ ├── 72_4_3.txt │ │ │ ├── 72_4_4.txt │ │ │ ├── 72_4_5.txt │ │ │ ├── 72_4_6.txt │ │ │ ├── 72_4_7.txt │ │ │ ├── 72_4_8.txt │ │ │ ├── 72_4_9.txt │ │ │ ├── 72_5_0.txt │ │ │ ├── 72_5_1.txt │ │ │ ├── 72_5_10.txt │ │ │ ├── 72_5_11.txt │ │ │ ├── 72_5_12.txt │ │ │ ├── 72_5_13.txt │ │ │ ├── 72_5_14.txt │ │ │ ├── 72_5_15.txt │ │ │ ├── 72_5_16.txt │ │ │ ├── 72_5_17.txt │ │ │ ├── 72_5_18.txt │ │ │ ├── 72_5_19.txt │ │ │ ├── 72_5_2.txt │ │ │ ├── 72_5_20.txt │ │ │ ├── 72_5_21.txt │ │ │ ├── 72_5_22.txt │ │ │ ├── 72_5_23.txt │ │ │ ├── 72_5_24.txt │ │ │ ├── 72_5_3.txt │ │ │ ├── 72_5_4.txt │ │ │ ├── 72_5_5.txt │ │ │ ├── 72_5_6.txt │ │ │ ├── 72_5_7.txt │ │ │ ├── 72_5_8.txt │ │ │ ├── 72_5_9.txt │ │ │ ├── 86_2_0.txt │ │ │ ├── 86_2_1.txt │ │ │ ├── 86_2_10.txt │ │ │ ├── 86_2_11.txt │ │ │ ├── 86_2_12.txt │ │ │ ├── 86_2_13.txt │ │ │ ├── 86_2_14.txt │ │ │ ├── 86_2_15.txt │ │ │ ├── 86_2_16.txt │ │ │ ├── 86_2_17.txt │ │ │ ├── 86_2_18.txt │ │ │ ├── 86_2_19.txt │ │ │ ├── 86_2_2.txt │ │ │ ├── 86_2_20.txt │ │ │ ├── 86_2_21.txt │ │ │ ├── 86_2_22.txt │ │ │ ├── 86_2_23.txt │ │ │ ├── 86_2_24.txt │ │ │ ├── 86_2_3.txt │ │ │ ├── 86_2_4.txt │ │ │ ├── 86_2_5.txt │ │ │ ├── 86_2_6.txt │ │ │ ├── 86_2_7.txt │ │ │ ├── 86_2_8.txt │ │ │ ├── 86_2_9.txt │ │ │ ├── 86_3_0.txt │ │ │ ├── 86_3_1.txt │ │ │ ├── 86_3_10.txt │ │ │ ├── 86_3_11.txt │ │ │ ├── 86_3_12.txt │ │ │ ├── 86_3_13.txt │ │ │ ├── 86_3_14.txt │ │ │ ├── 86_3_15.txt │ │ │ ├── 86_3_16.txt │ │ │ ├── 86_3_17.txt │ │ │ ├── 86_3_18.txt │ │ │ ├── 86_3_19.txt │ │ │ ├── 86_3_2.txt │ │ │ ├── 86_3_20.txt │ │ │ ├── 86_3_21.txt │ │ │ ├── 86_3_22.txt │ │ │ ├── 86_3_23.txt │ │ │ ├── 86_3_24.txt │ │ │ ├── 86_3_3.txt │ │ │ ├── 86_3_4.txt │ │ │ ├── 86_3_5.txt │ │ │ ├── 86_3_6.txt │ │ │ ├── 86_3_7.txt │ │ │ ├── 86_3_8.txt │ │ │ ├── 86_3_9.txt │ │ │ ├── 86_4_0.txt │ │ │ ├── 86_4_1.txt │ │ │ ├── 86_4_10.txt │ │ │ ├── 86_4_11.txt │ │ │ ├── 86_4_12.txt │ │ │ ├── 86_4_13.txt │ │ │ ├── 86_4_14.txt │ │ │ ├── 86_4_15.txt │ │ │ ├── 86_4_16.txt │ │ │ ├── 86_4_17.txt │ │ │ ├── 86_4_18.txt │ │ │ ├── 86_4_19.txt │ │ │ ├── 86_4_2.txt │ │ │ ├── 86_4_20.txt │ │ │ ├── 86_4_21.txt │ │ │ ├── 86_4_22.txt │ │ │ ├── 86_4_23.txt │ │ │ ├── 86_4_24.txt │ │ │ ├── 86_4_3.txt │ │ │ ├── 86_4_4.txt │ │ │ ├── 86_4_5.txt │ │ │ ├── 86_4_6.txt │ │ │ ├── 86_4_7.txt │ │ │ ├── 86_4_8.txt │ │ │ ├── 86_4_9.txt │ │ │ ├── 86_5_0.txt │ │ │ ├── 86_5_1.txt │ │ │ ├── 86_5_10.txt │ │ │ ├── 86_5_11.txt │ │ │ ├── 86_5_12.txt │ │ │ ├── 86_5_13.txt │ │ │ ├── 86_5_14.txt │ │ │ ├── 86_5_15.txt │ │ │ ├── 86_5_16.txt │ │ │ ├── 86_5_17.txt │ │ │ ├── 86_5_18.txt │ │ │ ├── 86_5_19.txt │ │ │ ├── 86_5_2.txt │ │ │ ├── 86_5_20.txt │ │ │ ├── 86_5_21.txt │ │ │ ├── 86_5_22.txt │ │ │ ├── 86_5_23.txt │ │ │ ├── 86_5_24.txt │ │ │ ├── 86_5_3.txt │ │ │ ├── 86_5_4.txt │ │ │ ├── 86_5_5.txt │ │ │ ├── 86_5_6.txt │ │ │ ├── 86_5_7.txt │ │ │ ├── 86_5_8.txt │ │ │ └── 86_5_9.txt │ │ ├── sample │ │ │ └── 4_3_0.txt │ │ └── test.txt │ └── out.txt ├── ORMATRIX.cpp ├── PAINTREE.cpp ├── PAPER.cpp ├── PLANT.cpp ├── PRESTIGE.cpp ├── PRINDRAG.cpp ├── RECOVER.cpp ├── RIVER.cpp ├── SAFPAR.cpp ├── SPECTAC.cpp ├── STICKS2.cpp ├── STRTF.cpp ├── TBGRAPH.cpp ├── TREEBAL.cpp ├── TREEQ1.cpp ├── TWOARR.cpp ├── TWOTREES.cpp ├── UPDOTR.cpp ├── WORDGRID.cpp └── XORSORT2.cpp ├── Codeforces ├── 0071C.cpp ├── 0079D.cpp ├── 0087D.cpp ├── 0150C.java ├── 0165B.cpp ├── 0190E.cpp ├── 0191E.cpp ├── 0240E.cpp ├── 0258E.cpp ├── 0297C.cpp ├── 0317D.cpp ├── 0324D.cpp ├── 0341D.cpp ├── 0349D(1).java ├── 0349D(2).java ├── 0351C.cpp ├── 0360E.cpp ├── 0365E.cpp ├── 0374D.cpp ├── 0403C.java ├── 0416D.cpp ├── 0436E.cpp ├── 0436F.cpp ├── 0449B.java ├── 0453E.cpp ├── 0518E.cpp ├── 0536C.cpp ├── 0631E.cpp ├── 0633E(1).cpp ├── 0633E(2).cpp ├── 0641D.cpp ├── 0653G.cpp ├── 0682E.cpp ├── 0697E.cpp ├── 0742E.cpp ├── 0758F.cpp ├── 0787D.java ├── 0789D.cpp ├── 0789E.java ├── 0813D.cpp ├── 0832E.cpp ├── 0833C.cpp ├── 0833D.cpp ├── 0875F.cpp ├── 0938G.cpp ├── 0981E.cpp ├── 0981F.cpp ├── 0981G.java ├── 0985F.cpp ├── 0993D.cpp ├── 0993E.cpp ├── 0997E.cpp ├── 1000E.cpp ├── 1000F.cpp ├── 1000G.cpp ├── 1004F.cpp ├── 1007D.cpp ├── 1008D.cpp ├── 1008E.cpp ├── 1010E.cpp ├── 1012D.cpp ├── 1012E.cpp ├── 1023D.cpp ├── 1023F(1).cpp ├── 1023F(2).cpp ├── 1030G.cpp ├── 1031E.cpp ├── 1031F.cpp ├── 1032F.cpp ├── 1032G.cpp ├── 1033E.cpp ├── 1033F.cpp ├── 1034C.cpp ├── 1034D.cpp ├── 1036F.cpp ├── 1037G.cpp ├── 1037H.cpp ├── 1042F.cpp ├── 1043F.cpp ├── 1044D.cpp ├── 1044E.cpp ├── 1044F.cpp ├── 1051E.cpp ├── 1054F.cpp ├── 1054G.cpp ├── 1056G.cpp ├── 1056H.cpp ├── 1060G.cpp ├── 1061E.cpp ├── 1062F.cpp ├── 1063D.cpp ├── 1063E.cpp ├── 1063F.cpp ├── 1065F.cpp ├── 1065G.cpp ├── 1067C.cpp ├── 1067D.cpp ├── 1071E.cpp ├── 1073G.cpp ├── 1078E.cpp ├── 1080E.cpp ├── 1081G.cpp ├── 1083C.cpp ├── 1083D.cpp ├── 1083F.cpp ├── 1085G.cpp ├── 1086F.cpp ├── 1091E.cpp ├── 1091F.cpp ├── 1091G.java ├── 1091H.cpp ├── 1093E.cpp ├── 1096E.cpp ├── 1096G.cpp ├── 1096G.java ├── 1097E.cpp ├── 1097F.cpp ├── 1097G.cpp ├── 1098D.cpp ├── 1098E.cpp ├── 1103C.cpp ├── 1103D.cpp ├── 1105E.cpp ├── 1106F.cpp ├── 1107F.cpp ├── 1107G.cpp ├── 1109C.cpp ├── 1109D.cpp ├── 1109E.cpp ├── 1111E.cpp ├── 1118F2.cpp ├── 1129D.cpp ├── 1129E.cpp ├── 1131G.cpp ├── 1132G.cpp ├── 1147D.cpp ├── 1147E.cpp ├── 1149C.cpp ├── 1149D.cpp ├── 1172C.cpp ├── 1172D.cpp ├── 1172E.cpp ├── 1172F.cpp ├── 1175G.cpp ├── 1179C.cpp ├── 1179D.cpp ├── 1179E.cpp ├── 1181E.cpp ├── 1182E.cpp ├── 1182F.cpp ├── 1187G.cpp ├── 1190E.cpp ├── 1194G.cpp ├── 1197F.cpp ├── 1198E.cpp ├── 1198F.cpp ├── 1205D.cpp ├── 1205E.cpp ├── 1208G.cpp ├── 1225F.cpp ├── 1225G.cpp ├── 1227D.cpp ├── 1227F.cpp ├── 1227G.cpp ├── 1254D.cpp ├── 1254E.cpp ├── 1261F.cpp ├── 1303G.cpp ├── 1327G.cpp ├── G102012E.cpp ├── G102052D.cpp ├── G102052E.cpp ├── G102056C.cpp ├── G102056E.cpp ├── G102056G.cpp └── G102056J.cpp ├── FacebookHackerCup ├── 18-0-A.java ├── 18-0-B.java ├── 18-0-C.java ├── 18-1-A.java ├── 18-1-B.java ├── 18-1-C.java ├── 18-1-D.java ├── 18-2-A.java ├── 18-2-B.java ├── 18-2-C.java ├── 18-2-D.java ├── 19-0-A.java ├── 19-0-B.java ├── 19-0-C.java ├── 19-0-D.java ├── 19-1-A.java ├── 19-1-B.java ├── 19-1-C.java ├── 19-1-D.java ├── 19-2-A.java ├── 19-2-B.java ├── 19-2-C.java ├── 19-2-D.java ├── 19-3-A.java ├── 19-3-B.java ├── 19-3-C.java ├── 19-3-D.java ├── 20-2-A.java ├── 20-2-B.java ├── 20-2-C.java ├── 20-2-D.cpp └── 21-2-D.cpp ├── FunctionCup ├── 19-Chairs │ ├── king.cpp │ └── vassal.cpp ├── 19-Cross.cpp ├── 19-Hiccup.cpp ├── 19-Museum.cpp └── 19-Unique.cpp ├── GoogleCodeJam ├── 08-AMER-D.cpp ├── 18-2-A.java ├── 18-2-B(1).java ├── 18-2-B(2).java ├── 18-2-C.java ├── 18-2-D.java ├── 18-3-A.java ├── 18-3-B.java ├── 19-0-A.cpp ├── 19-0-B.cpp ├── 19-0-C.java ├── 19-0-D.cpp ├── 19-1A-A.cpp ├── 19-1A-B.cpp ├── 19-1A-C.cpp ├── 19-1B-A.cpp ├── 19-1B-B.cpp ├── 19-1B-C.cpp ├── 19-1C-A.cpp ├── 19-1C-B.cpp └── 19-1C-C.cpp ├── GoogleKickStart ├── 19A-A.cpp ├── 19A-B.cpp ├── 19A-C.cpp └── 19E-C.cpp ├── HackerRank ├── alien-languages.cpp ├── beautiful-pairs.cpp ├── hourrank-28 │ └── xorry-queries.cpp ├── hourrank-30 │ └── diverse-strings.java ├── hourrank-31 │ └── basketball-tournament-1.cpp ├── ioi-2014-practice-contest-1 │ └── bounce-bounce-bounce-ioi14.cpp ├── ioi-2014-practice-contest-2 │ ├── family-ioi14.cpp │ └── guardians-lunatics-ioi14.cpp ├── mining.cpp ├── noi-ph-2019-finals-1-mirror │ └── sumbong-centers.cpp ├── noi-ph-2019-practice-2 │ └── kono-subarashii-sekai-ni-shukufuku-wo.java ├── noi-ph-2019 │ ├── almost-original.java │ ├── anion-find.java │ ├── aswang-immortal-trivia.java │ ├── dagohoy-rock-lapu-lapu-boogie.java │ ├── evening-gown.java │ ├── exchange-gift.java │ ├── lots-of-cookies.java │ ├── ngc.java │ ├── remotion.java │ ├── saishuu-shinpan.java │ ├── skyscaping.java │ ├── spratly-islands-tour.java │ ├── super-rangers.java │ └── yet-another-packing-problem.java ├── pwshpc-online-round │ └── pwsh-tokens.cpp ├── university-codesprint-4 │ └── drawing-rectangles.java ├── w38 │ ├── cargo-delivery.cpp │ ├── neighborhood-queries(1).cpp │ └── neighborhood-queries(2).cpp └── world-codesprint-13 │ ├── balanced-sequence.cpp │ ├── competitive-teams.cpp │ ├── dynamic-trees.cpp │ └── landslide.cpp ├── IOI ├── 02-Bus.cpp ├── 03-Code.cpp ├── 03-Guess.cpp ├── 03-Maintain.cpp ├── 04-Empodia.cpp ├── 06-Points.cpp ├── 07-Pairs.cpp ├── 08-Pyramid.cpp ├── 09-Archery.cpp ├── 09-Mecho.cpp ├── 11-Elephants.cpp ├── 12-City.cpp ├── 12-Rings.cpp ├── 12-Scrivener.cpp ├── 12-Supper │ ├── advisor.cpp │ └── assistant.cpp ├── 13-Robots.cpp ├── 13-Wombats.cpp ├── 14-Friend.cpp ├── 14-Game.cpp ├── 14-Holiday.cpp ├── 14-Rail.cpp ├── 14-Wall.cpp ├── 15-Boxes.cpp ├── 15-Horses.cpp ├── 15-Scales.cpp ├── 15-Sorting.cpp ├── 15-Teams.cpp ├── 15-Towns.cpp ├── 16-Aliens.cpp ├── 16-Messy.cpp ├── 16-Molecules.cpp ├── 16-Paint.cpp ├── 16-Railroad.cpp ├── 16-Shortcut.cpp ├── 17-Books.cpp ├── 17-Prize.cpp ├── 17-Simurgh.cpp ├── 17-Train.cpp ├── 17-Wiring.cpp ├── 18-Combo.cpp ├── 18-Doll.cpp ├── 18-Highway.cpp ├── 18-Meetings.cpp ├── 18-Seats.cpp ├── 18-Werewolf.cpp ├── 19-Line.cpp ├── 19-Rect.cpp ├── 19-Split.cpp ├── 19-Walk.cpp ├── 19P-Cycle.cpp ├── 19P-Job.cpp ├── 19P-Packing.cpp └── 19P-Transfer.cpp ├── IZhO ├── 13-Burrow.cpp ├── 13-Trading.cpp ├── 14-Divide.cpp ├── 14-Shymbulak.cpp ├── 17-Bomb.cpp └── 19-Xoractive.cpp ├── Index_of_Cancerous_Codes.md ├── Info1Cup ├── 18-Del13.cpp ├── 18-Hidden.cpp └── 18-Maxcomp.cpp ├── InfoArena ├── Invtree.cpp ├── Pitici3.cpp ├── Regat.cpp └── Tricolor.cpp ├── JOI ├── 13F-BubbleSort.cpp ├── 13O-Synchronization(1).cpp ├── 13O-Synchronization(2).cpp ├── 16O-Selling_RNA.cpp ├── 16O-Skyscraper.cpp ├── 17O-Amusement_Park │ ├── Ioi.cpp │ └── Joi.cpp ├── 17O-Bulldozer.cpp ├── 17S-City │ ├── Device.cpp │ └── Encoder.cpp ├── 17S-Coach.cpp ├── 17S-Park.cpp ├── 17S-Port_Facility.cpp ├── 17S-Railway(1).cpp ├── 17S-Railway(2).cpp ├── 18F-Art.cpp ├── 18F-Commuter_Pass.cpp ├── 18F-Dango_Maker.cpp ├── 18F-Snake.cpp ├── 18F-Stove.cpp ├── 18S-Asceticism.cpp ├── 18S-Bitaro.cpp ├── 18S-Construction.cpp ├── 18S-Fences.cpp ├── 18S-Wild_Boar.cpp ├── 19F-Bitaro.cpp ├── 19F-Coin.cpp ├── 19F-Exhibition.cpp ├── 19F-Growing.cpp ├── 19O-Jumps.cpp ├── 19O-Remittance.cpp ├── 19O-Virus.cpp ├── 19S-Antennas.cpp ├── 19S-Cake3.cpp ├── 19S-Dishes.cpp ├── 19S-Examination.cpp ├── 19S-Meetings.cpp ├── 19S-Mergers.cpp ├── 19S-Minerals.cpp ├── 19S-Naan.cpp ├── 19S-Transportations │ ├── Azer.cpp │ └── Baijan.cpp ├── 20S-Building4.cpp ├── 20S-Constellation3.cpp ├── 20S-Hamburg.cpp ├── 20S-Harvest.cpp ├── 20S-Joitter2.cpp ├── 20S-Sweeping.cpp └── 20S-Treatment.cpp ├── Kattis ├── hanoi18 │ └── grabagraph.cpp └── open │ └── stringmultimatching.cpp ├── LiveArchive ├── 5820.cpp └── 5854.cpp ├── PKU ├── 1738.cpp └── 3709.cpp ├── POI ├── 18-Lightning_Conductor.cpp ├── 18T-Strongbox.cpp ├── 19-Warehouse_Store.cpp ├── 20-Price_List.cpp ├── 22-Gluttons.cpp ├── 22-Squares.cpp ├── 23-Hydro.cpp ├── 23-Stutter.cpp └── 25-Polynomial.cpp ├── README.md ├── SGNOI └── 19-Feast.cpp ├── SPOJ ├── BAABO.cpp ├── DYNACON1.cpp ├── DYNALCA.cpp ├── MATCHING.cpp ├── ORDERSET.cpp ├── SARRAY.cpp └── SFLIP.cpp ├── TJU └── temp.txt ├── TOKI └── 18-Group.cpp ├── Templates ├── Main.cpp ├── Mnt.cpp ├── Segtree.cpp ├── combo.cpp ├── digraph.cpp ├── hashfactory.cpp └── sieve.cpp ├── Timus └── temp.txt ├── Topcoder ├── Open19-1B-D1-1000.java ├── Open19-1B-D1-250.java ├── Open19-1B-D1-500.java ├── Open20-2A-D1-1000.cpp ├── Open20-2A-D1-300.cpp ├── Open20-2A-D1-400.cpp ├── SRM416-D1-500.java ├── SRM515-D2-1000.java ├── SRM739-D2-1000.java ├── SRM739-D2-250.java ├── SRM739-D2-500.java ├── SRM777-D1-500.cpp ├── SRM778-D1-250.cpp ├── SRM778-D1-600.cpp ├── SRM778-D1-900.cpp ├── SRM779-D1-250.cpp ├── SRM779-D1-450.cpp ├── SRM779-D1-900.cpp ├── SRM780-D1-1000.cpp ├── SRM780-D1-250.cpp └── SRM780-D1-450.cpp ├── USACO ├── Contests │ ├── 1112_1G │ │ ├── Bsudoku.java │ │ ├── Median.java │ │ ├── Steeple(1).java │ │ ├── Steeple(2).java │ │ └── Steeple(3).java │ ├── 1112_2G │ │ ├── Grassplant(1).java │ │ ├── Grassplant(2).java │ │ ├── Photo.cpp │ │ └── Simplify.cpp │ ├── 1213_1G │ │ ├── Bbreeds.java │ │ ├── Btree.java │ │ └── Cbs.java │ ├── 1516_1P │ │ ├── Cardgame.java │ │ ├── Haybales.java │ │ └── Maxflow.java │ ├── 1617_1P │ │ ├── Roboherd.cpp │ │ ├── Team.java │ │ └── Triangles.java │ ├── 1617_2P │ │ ├── Promote.java │ │ ├── Subrev.java │ │ └── Tallbarn.java │ ├── 1617_3P │ │ ├── Friendcross.java │ │ ├── Mincross.java │ │ └── Nocross.java │ ├── 1617_4P │ │ ├── art.cpp │ │ └── grass.cpp │ ├── 1718_1G │ │ ├── Barnpainting.java │ │ ├── Hayfeast.java │ │ └── Piepie.java │ ├── 1718_1P │ │ ├── Greedy.java │ │ ├── Pushabox.cpp │ │ └── Standingout.cpp │ ├── 1718_2P │ │ ├── Atlarge.java │ │ ├── Lifeguards.java │ │ └── Sprinklers.java │ ├── 1718_3P │ │ ├── Gymnasts.java │ │ ├── Newbarn(1).java │ │ ├── Newbarn(2).java │ │ └── Slingshot.java │ ├── 1718_4G │ │ └── Sort.cpp │ ├── 1718_4P │ │ ├── Disrupt.java │ │ ├── Sort.cpp │ │ └── Train │ │ │ ├── Train.cpp │ │ │ ├── compile.bat │ │ │ ├── grader.cpp │ │ │ ├── grader.h │ │ │ └── run.bat │ ├── 1819_1B │ │ ├── Backforth.cpp │ │ ├── Backforth.java │ │ ├── Blist.cpp │ │ ├── Blist.java │ │ ├── MixMilk.java │ │ └── Mixmilk.cpp │ ├── 1819_1G │ │ ├── Cowpatibility.cpp │ │ ├── Cowpatibility.java │ │ ├── Dining.cpp │ │ ├── Dining.java │ │ ├── Teamwork.cpp │ │ └── Teamwork.java │ ├── 1819_1P │ │ ├── Balance.cpp │ │ ├── Gathering.cpp │ │ ├── Gathering.java │ │ ├── Itout.cpp │ │ └── Itout.java │ ├── 1819_1S │ │ ├── Convention.cpp │ │ ├── Convention.java │ │ ├── Convention2.cpp │ │ ├── Convention2.java │ │ ├── Mooyomooyo.cpp │ │ └── Mooyomooyo.java │ ├── 1819_2P │ │ ├── Exercise.cpp │ │ ├── Redistricting.cpp │ │ └── Tracking2.cpp │ ├── 1819_3P │ │ ├── Cowdate.cpp │ │ ├── Mooriokart.cpp │ │ └── Mowing.cpp │ ├── 1920_1P │ │ ├── Pieaters.cpp │ │ ├── Snowcow.cpp │ │ └── Treedepth.cpp │ ├── 1920_3P │ │ ├── Deleg.cpp │ │ ├── Help.cpp │ │ └── Triangles.cpp │ └── 1920_4P │ │ ├── Circus.cpp │ │ ├── Exercise.cpp │ │ └── Sprinklers2.cpp └── Training Pages │ ├── Section 1.1 │ ├── Beads.java │ ├── Friday.java │ ├── Gift1.java │ ├── Ride.java │ └── Test.java │ ├── Section 1.2 │ ├── Dualpal.java │ ├── Milk2.java │ ├── Namenum.java │ ├── Palsquare.java │ └── Transform.java │ ├── Section 1.3 │ ├── Barn1.java │ ├── Combo.java │ ├── Crypt1.java │ ├── Milk.java │ ├── Skidesign.java │ └── Wormhole.java │ ├── Section 1.4 │ ├── Ariprog.java │ └── Milk3.java │ ├── Section 1.5 │ ├── Numtri.java │ ├── Pprime.java │ └── Sprime.java │ ├── Section 2.1 │ ├── Castle.java │ ├── Frac1.java │ ├── Hamming.java │ ├── Holstein.java │ └── Sort3.java │ ├── Section 2.2 │ ├── Lamps.java │ ├── Preface.java │ ├── Runround.java │ └── Subset.java │ ├── Section 2.3 │ ├── Concom.java │ ├── Money.java │ ├── Nocows.java │ ├── Prefix.java │ └── Zerosum.java │ ├── Section 2.4 │ ├── Comehome.java │ ├── Cowtour.java │ ├── Fracdec.java │ ├── Maze1.java │ └── Ttwo.java │ ├── Section 3.1 │ ├── Agrinet.java │ ├── Contact.java │ ├── Humble.java │ ├── Inflate.java │ └── Stamps.java │ ├── Section 3.2 │ ├── Butter.java │ ├── Fact4.java │ ├── Kimbits.java │ ├── Msquare.java │ ├── Ratios.java │ └── Spin.java │ ├── Section 3.3 │ ├── Camelot.cpp │ ├── Camelot.java │ ├── Fence.java │ ├── Game1.java │ ├── Range.java │ └── Shopping.java │ ├── Section 3.4 │ ├── Fence9.java │ ├── Heritage.java │ └── Rockers.java │ ├── Section 3.5 │ ├── Fence6.java │ └── Nuggets.java │ ├── Section 4.2 │ ├── Ditch.java │ ├── Job.java │ └── Stall4.java │ ├── Section 4.3 │ ├── Buylow.java │ ├── Lgame.java │ └── Race3.java │ ├── Section 4.4 │ ├── Frameup.java │ ├── Milk6.java │ └── Shuttle.java │ ├── Section 5.1 │ ├── Fc.java │ └── Theme.java │ ├── Section 5.3 │ ├── Bigbrn.java │ ├── Milk4.java │ ├── Schlnet.java │ └── Window.java │ └── Section 5.4 │ ├── Charrec.java │ ├── Telecow.java │ └── Tour.java └── UVA ├── 1427.cpp └── 529.cpp /APIO/08-Beads.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lib1739.h" 3 | using namespace std; 4 | 5 | const int mxN=3e5; 6 | int n, m, q, a[mxN]; 7 | vector> pos[mxN]; 8 | 9 | int main() { 10 | scanf("%d%d", &n, &m); 11 | for(int i=0; isecond); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /APIO/10-Commando.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e6; 7 | int n; 8 | ll a, b, c, xs, qum[mxN+1], qub[mxN+1], qh=0, qt=1, ans; 9 | 10 | inline void upd(ll mi, ll bi) { 11 | while(qt-qh>=2&&(qub[qt-1]-qub[qt-2])*(qum[qt-1]-mi)>=(qum[qt-2]-qum[qt-1])*(bi-qub[qt-1])) 12 | --qt; 13 | qum[qt]=mi, qub[qt]=bi, ++qt; 14 | } 15 | 16 | int main() { 17 | ios_base::sync_with_stdio(0); 18 | cin.tie(0); 19 | 20 | cin >> n >> a >> b >> c; 21 | for(int i=0; i> xi, xs+=xi; 24 | ans=(a*xs+b)*xs+c; 25 | while(qh+1 3 | using namespace std; 4 | 5 | const int mxN=1e5; 6 | int d[mxN]; 7 | bool e[mxN], f[mxN]; 8 | vector v[mxN]; 9 | 10 | int minimumInstructions(int n, int m, int k, vector c, vector a, vector> b) { 11 | for(int i=0; i=m) 17 | e[i]=1; 18 | f[(j-i%m+m)%m]=1; 19 | } 20 | if(i) 21 | for(int j : v[c[i-1]]) 22 | if(!f[(j-i%m+1+m)%m]) 23 | d[(j-i%m+1+m)%m]=0; 24 | for(int j : v[c[i]]) 25 | f[(j-i%m+m)%m]=0; 26 | } 27 | int i=n-1, x=0, j; 28 | for(; i>=0; ++x) { 29 | for(j=i; j=i+m) 31 | return -1; 32 | i=j-m; 33 | } 34 | return x; 35 | } 36 | -------------------------------------------------------------------------------- /AtCoder/G002-E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - http://agc002.contest.atcoder.jp/data/agc/002/editorial.pdf 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | const int mxN=1e5; 9 | int n, a[mxN]; 10 | 11 | int main() { 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> n; 16 | for(int i=0; i> a[i]; 18 | sort(a, a+n, greater()); 19 | int i=0; 20 | for(; i())-a-i+1&1?"Second":"First"); 22 | } 23 | -------------------------------------------------------------------------------- /AtCoder/G003-E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Consider the operations in reverse order 3 | - We start out with array of 1s, and we consider all i such that i >= q and we move them to a[i%q] 4 | - We can compress with array by storing differences 5 | - If we use a map and don't go through the elements i < q, each element will only be considered O(logq) times 6 | - If x >= y, x%y < x/2 7 | - O(qlogq+n) 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | #define ll long long 14 | 15 | int q; 16 | ll a[100001]; 17 | map mp; 18 | 19 | int main() { 20 | ios::sync_with_stdio(0); 21 | cin.tie(0); 22 | 23 | cin >> a[0] >> q; 24 | for(int i=1; i<=q; ++i) 25 | cin >> a[i]; 26 | mp[a[q]]=1; 27 | for(int i=q-1; ~i; --i) { 28 | auto it=mp.end(); 29 | while((--it)->first>a[i]) { 30 | mp[a[i]]+=it->first/a[i]*it->second; 31 | mp[it->first%a[i]]+=it->second; 32 | it=mp.erase(it); 33 | } 34 | } 35 | for(int i=a[0]; i; --i) 36 | mp[i-1]+=mp[i]; 37 | for(int i=1; i<=a[0]; ++i) 38 | cout << mp[i] << "\n"; 39 | } 40 | -------------------------------------------------------------------------------- /AtCoder/G005-E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - http://agc005.contest.atcoder.jp/data/agc/005/editorial.pdf 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | const int mxN=2e5; 9 | int n, r[2], d[2][mxN], p[mxN], ans; 10 | vector adj[2][mxN]; 11 | 12 | void dfs(int u, int k, int pu=-1) { 13 | if(k) 14 | p[u]=pu; 15 | if(!k&&d[0][u]>=d[1][u]) 16 | return; 17 | for(int v : adj[k][u]) { 18 | if(v^pu) { 19 | d[k][v]=d[k][u]+1; 20 | dfs(v, k, u); 21 | } 22 | } 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | 29 | cin >> n >> r[0] >> r[1], --r[0], --r[1]; 30 | for(int k : {0, 1}) { 31 | for(int i=1, a, b; i> a >> b, --a, --b; 33 | adj[k][a].push_back(b); 34 | adj[k][b].push_back(a); 35 | } 36 | } 37 | memset(d, 0x3f, 4*n); 38 | d[0][r[0]]=0; 39 | for(int k : {1, 0}) 40 | dfs(r[k], k); 41 | for(int i=0; i=d[1][i]) 45 | continue; 46 | for(int j : adj[0][i]) { 47 | if(p[i]^j&&p[j]^i&&p[i]^p[j]&&(p[i]<0||p[p[i]]^j)&&(p[j]<0||p[p[j]]^i)) { 48 | cout << -1; 49 | return 0; 50 | } 51 | } 52 | } 53 | cout << ans; 54 | } 55 | -------------------------------------------------------------------------------- /AtCoder/G006-E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, a[3][mxN], c[2], ft[mxN+1]; 6 | 7 | void fk() { 8 | cout << "No"; 9 | exit(0); 10 | } 11 | 12 | void upd(int i) { 13 | for(; i<=n; i+=i&-i) 14 | ++ft[i]; 15 | } 16 | 17 | int qry(int i) { 18 | int r=0; 19 | for(; i; i-=i&-i) 20 | r+=ft[i]; 21 | return r; 22 | } 23 | 24 | int main() { 25 | ios::sync_with_stdio(0); 26 | cin.tie(0); 27 | 28 | cin >> n; 29 | for(int i=0; i<3; ++i) 30 | for(int j=0; j> a[i][j]; 32 | for(int k : {0, 1}) { 33 | for(int i=k; i1||abs(a[2][i]-a[1][i])>1||(a[1][i]/3^i)&1) 35 | fk(); 36 | c[k]+=a[0][i]>a[2][i]; 37 | c[k^1]+=qry(n-a[1][i]/3); 38 | upd(n-a[1][i]/3); 39 | } 40 | memset(ft+1, 0, 4*n); 41 | } 42 | if(c[0]&1||c[1]&1) 43 | fk(); 44 | cout << "Yes"; 45 | } 46 | -------------------------------------------------------------------------------- /AtCoder/G011-E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string s; 5 | int a[500001], b, c; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> s; 12 | for(int i=0; ic; ++c) { 20 | for(int i=0; ; ++i) { 21 | if(a[i]>8) { 22 | a[i]=0; 23 | b-=9; 24 | } else { 25 | ++a[i]; 26 | ++b; 27 | break; 28 | } 29 | } 30 | } 31 | cout << (c+8)/9; 32 | } 33 | -------------------------------------------------------------------------------- /AtCoder/G027-A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | 7 | public static void main(String[] args) { 8 | int n=in.nextInt(), x=in.nextInt(); 9 | int[] a = new int[n]; 10 | for(int i=0; i0?Math.min(ans, n-1):ans); 21 | } 22 | 23 | static class Reader { 24 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 25 | StringTokenizer st; 26 | String next() { 27 | while(st==null||!st.hasMoreTokens()) { 28 | try { 29 | st = new StringTokenizer(br.readLine()); 30 | } catch(Exception e) {} 31 | } 32 | return st.nextToken(); 33 | } 34 | int nextInt() { 35 | return Integer.parseInt(next()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /AtCoder/G027-B.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | 7 | public static void main(String[] args) { 8 | int n=in.nextInt(); 9 | long x=in.nextInt(), ans=Long.MAX_VALUE; 10 | long[] ps = new long[n+1]; 11 | for(int i=0; i0; ++j) { 16 | long b=(2*j+3)*(ps[n-i*j]-ps[Math.max(n-i*(j+1), 0)]); 17 | if(Long.MAX_VALUE-b0&&(b%=a)>0); 22 | return a^b; 23 | } 24 | 25 | static class Reader { 26 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 27 | StringTokenizer st; 28 | String next() { 29 | while(st==null||!st.hasMoreTokens()) { 30 | try { 31 | st = new StringTokenizer(br.readLine()); 32 | } catch(Exception e) {} 33 | } 34 | return st.nextToken(); 35 | } 36 | int nextInt() { 37 | return Integer.parseInt(next()); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /AtCoder/G028-B.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | static final long M=(long)1e9+7; 7 | 8 | public static void main(String[] args) { 9 | int n=in.nextInt(); 10 | long[] ps = new long[n+1]; 11 | long nf=1; 12 | for(int i=1; i<=n; ++i) { 13 | ps[i]=(ps[i-1]+in.nextInt())%M; 14 | nf=nf*i%M; 15 | } 16 | long ans=nf*ps[n]%M, s=0; 17 | for(int i=1; i<=n-1; ++i) { 18 | s=(s+ps[n-i]-ps[i]+M)%M; 19 | ans=(ans+nf*modI((long)(i+1)*(i>=n-1?1:i+2), M)%M*2*s+nf*modI(i+1, M)%M*(ps[i]+ps[n]-ps[n-i]+M))%M; 20 | } 21 | System.out.println(ans); 22 | } 23 | 24 | static long modI(long a, long m) { 25 | return (a%=m)<=1?1:(1-modI(m%a, a)*m)/a+m; 26 | } 27 | 28 | static class Reader { 29 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 30 | StringTokenizer st; 31 | String next() { 32 | while(st==null||!st.hasMoreTokens()) { 33 | try { 34 | st = new StringTokenizer(br.readLine()); 35 | } catch(Exception e) {} 36 | } 37 | return st.nextToken(); 38 | } 39 | int nextInt() { 40 | return Integer.parseInt(next()); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /AtCoder/G033-A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e3, di[4]={1, 0, -1, 0}, dj[4]={0, 1, 0, -1}; 5 | int h, w, d[mxN][mxN], ans; 6 | string a[mxN]; 7 | queue> qu; 8 | 9 | int main() { 10 | ios::sync_with_stdio(0); 11 | cin.tie(0); 12 | 13 | cin >> h >> w; 14 | memset(d, 0x3f, sizeof(d)); 15 | for(int i=0; i> a[i]; 17 | for(int j=0; j u=qu.front(); 26 | qu.pop(); 27 | ans=max(ans, d[u[0]][u[1]]); 28 | for(int k=0; k<4; ++k) { 29 | int ni=u[0]+di[k], nj=u[1]+dj[k]; 30 | if(ni>=0&&ni=0&&nj1e9) { 31 | d[ni][nj]=d[u[0]][u[1]]+1; 32 | qu.push(array{ni, nj}); 33 | } 34 | } 35 | } 36 | cout << ans; 37 | } 38 | -------------------------------------------------------------------------------- /AtCoder/G033-B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool solve(int n, int a, string sa, string sb, char ca, char cb, int s) { 5 | int l=0, r=s+1; 6 | for(int i=n-1; ~i; --i) { 7 | if(sb[i]==ca) 8 | r = min(r+1, s+1); 9 | if(sb[i]==cb) 10 | l = max(l-1, 0); 11 | if(sa[i]==ca) 12 | ++l; 13 | if(sa[i]==cb) 14 | --r; 15 | if(l+1>=r) 16 | return 0; 17 | } 18 | return l> h >> w >> n >> sr >> sc >> sa >> sb; 28 | cout << (solve(n, sr, sa, sb, 'U', 'D', h)&&solve(n, sc, sa, sb, 'L', 'R', w)?"YES":"NO"); 29 | } 30 | -------------------------------------------------------------------------------- /AtCoder/G033-C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | vector adj[200000]; 6 | 7 | array dfs(int u, int p=-1, int d=1) { 8 | array r={d, u}; 9 | for(int v : adj[u]) 10 | if(v^p) 11 | r=max(dfs(v, u, d+1), r); 12 | return r; 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> n; 20 | for(int i=1, a, b; i> a >> b, --a, --b; 22 | adj[a].push_back(b); 23 | adj[b].push_back(a); 24 | } 25 | cout << (dfs(dfs(0)[1])[0]%3==2?"Second":"First"); 26 | } 27 | -------------------------------------------------------------------------------- /AtCoder/G037-B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5, M=998244353; 5 | int n, a[3*mxN], d[4]; 6 | string s; 7 | map mp{{'R', 0}, {'G', 1}, {'B', 2}}; 8 | vector v[3]; 9 | long long ans=1; 10 | 11 | int main() { 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> n >> s; 16 | for(int i=0; i<3*n; ++i) 17 | v[mp[s[i]]].push_back(i); 18 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=2e5; 5 | int n, a[mxN], b[mxN]; 6 | priority_queue> pq; 7 | long long ans; 8 | 9 | void fk() { 10 | cout << -1; 11 | exit(0); 12 | } 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); 16 | cin.tie(0); 17 | 18 | cin >> n; 19 | for(int i=0; i> a[i]; 21 | for(int i=0; i> b[i]; 23 | pq.push({b[i], i}); 24 | } 25 | while(pq.size()) { 26 | int i=pq.top()[1], c=b[(i+1)%n]+b[(i+n-1)%n], t=(b[i]-a[i])/c; 27 | pq.pop(); 28 | if(a[i]==b[i]) 29 | continue; 30 | if(t<=0) 31 | fk(); 32 | b[i]-=t*c; 33 | pq.push({b[i], i}); 34 | ans+=t; 35 | } 36 | cout << ans; 37 | } 38 | -------------------------------------------------------------------------------- /AtCoder/R103-E.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | static final PrintWriter out = new PrintWriter(System.out); 7 | 8 | public static void main(String[] args) { 9 | char[] s=in.next().toCharArray(); 10 | int n=s.length; 11 | if(s[0]=='0'||s[n-1]=='1') 12 | fk(); 13 | for(int i=0; il; --j) 20 | out.println(j+" "+(i+1)); 21 | l=i; 22 | } 23 | out.println(n+" "+(n-1)); 24 | out.close(); 25 | } 26 | 27 | static void fk() { 28 | System.out.println(-1); 29 | System.exit(0); 30 | } 31 | 32 | static class Reader { 33 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 34 | StringTokenizer st; 35 | String next() { 36 | while(st==null||!st.hasMoreTokens()) { 37 | try { 38 | st = new StringTokenizer(br.readLine()); 39 | } catch(Exception e) {} 40 | } 41 | return st.nextToken(); 42 | } 43 | int nextInt() { 44 | return Integer.parseInt(next()); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /BkOI/12-Balls.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - The change in score for a right obstacle (l, r) is -s[l-1]+l*(-a[r])+r*a[r]+s[r-1] 3 | - We can use CHT with binary search to find the max for each r 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | #define ll long long 10 | 11 | const int mxN=3e5; 12 | int n, qt; 13 | ll c[mxN], s[mxN+1], a[mxN], b[mxN]; 14 | 15 | void al(ll ai, ll bi) { 16 | while(qt>1&&(bi-b[qt-1])/(a[qt-1]-ai)<(b[qt-1]-b[qt-2])/(a[qt-2]-a[qt-1])) 17 | --qt; 18 | a[qt]=ai; 19 | b[qt]=bi; 20 | ++qt; 21 | } 22 | 23 | ll qry(ll x) { 24 | int l=0, r=qt-1; 25 | while(la[m+1]*x+b[m+1]) 28 | r=m; 29 | else 30 | l=m+1; 31 | } 32 | return a[l]*x+b[l]; 33 | } 34 | 35 | void gg() { 36 | for(int i=0; i> n; 52 | for(int i=0; i> c[i]; 54 | gg(); 55 | reverse(c, c+n); 56 | gg(); 57 | } 58 | -------------------------------------------------------------------------------- /BtOI/09-Rectangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - For each pair of points, compute the vector and also the projection of the first point onto the vector 3 | - If 2 pairs have both of these same, then they can form a rectangle 4 | - Compute the minimum and maximum projection for all pairs that can form a rectangle 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define ll long long 11 | 12 | const int mxN=1500; 13 | int n; 14 | ll x[mxN], y[mxN], ans; 15 | pair, ll> a[mxN*mxN]; 16 | 17 | int main() { 18 | ios::sync_with_stdio(0); 19 | cin.tie(0); 20 | 21 | cin >> n; 22 | for(int i=0; i> x[i] >> y[i]; 24 | for(int i=0; i{x[j]-x[i], y[j]-y[i], (x[j]-x[i])*x[i]+(y[j]-y[i])*y[i]}, (y[i]-y[j])*x[i]+(x[j]-x[i])*y[i]); 27 | sort(a, a+n*n); 28 | for(int i=0, j=0; i 2 | using namespace std; 3 | 4 | long long dp[50*3000+1], n, p, v; 5 | 6 | int main() { 7 | ios::sync_with_stdio(0); 8 | cin.tie(0); 9 | 10 | cin >> n >> p >> v; 11 | for(int i=0; ; ++i) { 12 | dp[i]=1; 13 | for(int j=2; i-v-j*p>=0; ++j) 14 | dp[i]=max(j*dp[i-v-j*p], dp[i]); 15 | if(dp[i]>=n) { 16 | cout << i; 17 | break; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BtOI/17W-Toast.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Basically finding the radius when the angle and chord length are known 3 | - Law of Cosines 4 | - A few other edge cases to watch out for 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define PI 3.1415926535897 11 | 12 | int main() { 13 | int n, d, t; 14 | cin >> n >> d >> t; 15 | if(t%n) { 16 | cout << "0 " << d; 17 | return 0; 18 | } 19 | t/=n; 20 | double th1=2*PI/n*t, th2=2*PI/n*(t+1); 21 | cout << fixed << setprecision(9) << (2*(t+1)>n?0:d*sqrt(2/(1-cos(th2)))) << " " << d*sqrt(2/(1-cos(th1))); 22 | } 23 | -------------------------------------------------------------------------------- /CCO/18-Wrong_Answer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | cout << "100\n"; 9 | for(int i=0; i<99; ++i) 10 | cout << "1 "; 11 | cout << "\n"; 12 | for(int i=98; i>0; --i) { 13 | cout << (i&1?100:1); 14 | if(i>1) 15 | cout << " 100"; 16 | for(int j=0; j 8 | using namespace std; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | int n; 15 | cin >> n; 16 | cout << n-2 << "\n"; 17 | for(int i=0; i 6 | using namespace std; 7 | 8 | const int mxN=2e4; 9 | int n, w[mxN+2], d[mxN+2], ans, dp[4][mxN+2], a[mxN], b[mxN], qh, qt; 10 | 11 | void al(int ai, int bi) { 12 | while(qt-qh>1&&(bi-b[qt-1])/(a[qt-1]-ai)>(b[qt-1]-b[qt-2])/(a[qt-2]-a[qt-1])) 13 | --qt; 14 | a[qt]=ai; 15 | b[qt]=bi; 16 | ++qt; 17 | } 18 | 19 | int qry(int x) { 20 | while(qt-qh>1&&a[qh+1]*x+b[qh+1]>a[qh]*x+b[qh]) 21 | ++qh; 22 | return a[qh]*x+b[qh]; 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | 29 | cin >> n; 30 | for(int i=0; i> w[i+1] >> d[i]; 32 | w[n+1]=1; 33 | for(int i=n-1; ~i; --i) { 34 | d[i]+=d[i+1]; 35 | ans+=w[i+1]*d[i]; 36 | } 37 | for(int i=0; i<=n; ++i) 38 | w[i+1]+=w[i]; 39 | for(int i=1; i<4; ++i) { 40 | qh=qt=0; 41 | for(int j=1; j<=n+1; ++j) { 42 | al(-w[j-1], dp[i-1][j-1]); 43 | dp[i][j]=qry(d[j-1])+w[j]*d[j-1]; 44 | } 45 | } 46 | cout << ans-dp[3][n+1]; 47 | } 48 | -------------------------------------------------------------------------------- /CEOI/11-Hotel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=5e5; 5 | int n, m, o, c[mxN], p[mxN], p2[mxN], a[mxN]; 6 | vector va[mxN], vb[mxN+1]; 7 | priority_queue pq; 8 | 9 | int main() { 10 | ios::sync_with_stdio(0); 11 | cin.tie(0); 12 | 13 | cin >> n >> m >> o; 14 | for(int i=0; i> c[i] >> p[i]; 16 | memcpy(p2, p, 4*n); 17 | sort(p2, p2+n); 18 | for(int i=0; i> d >> v; 24 | vb[lower_bound(p2, p2+n, v)-p2].push_back(d); 25 | } 26 | for(int i=0; iva[i][j]; ++j) { 32 | a[k++]=pq.top()-va[i][j]; 33 | pq.pop(); 34 | } 35 | } 36 | nth_element(a, a+o, a+min(n, m), greater()); 37 | cout << accumulate(a, a+o, 0ll); 38 | } 39 | -------------------------------------------------------------------------------- /CEOI/12-Highway.cpp: -------------------------------------------------------------------------------- 1 | #include "office.h" 2 | #include 3 | using namespace std; 4 | 5 | vector a[2]; 6 | 7 | int ol(int b) { 8 | return isOnLine(a[1][0], a[1][0]+1, b); 9 | } 10 | 11 | void ans(int b, int c) { 12 | Answer(a[1][0], a[1][0]+1, b, c); 13 | } 14 | 15 | int main() { 16 | int n=GetN(); 17 | for(int i=1; i+2<=n; i+=3) 18 | a[isOnLine(i, i+1, i+2)].push_back(i); 19 | if(a[0].size()>=2) { 20 | int b[2]; 21 | for(int i=0; i<2; ++i) 22 | for(b[i]=a[0][i]; ol(b[i]); ++b[i]); 23 | ans(b[0], b[1]); 24 | } 25 | for(int i=1; i+1 b; 42 | for(int i=0; i<3; ++i) 43 | if(!ol(a[0][0]+i)) 44 | b.push_back(a[0][0]+i); 45 | b.push_back(n); 46 | ans(b[0], b[1]); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CEOI/13-Treasure.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - The query rectangles must have both sides >= n/2 3 | */ 4 | 5 | #include "treasure.h" 6 | #include 7 | using namespace std; 8 | 9 | const int mxN=100; 10 | short a[mxN+1][mxN+1][mxN+1][mxN+1], b[mxN+1][mxN+1]; 11 | 12 | inline int qry(int r1, int c1, int r2, int c2) { 13 | if(!a[r1][c1][r2][c2]) 14 | a[r1][c1][r2][c2]=countTreasure(r1, c1, r2, c2)+1; 15 | return a[r1][c1][r2][c2]-1; 16 | } 17 | 18 | void findTreasure(int n) { 19 | memset(a, 0, sizeof(a)); 20 | for(int i=1; i<=n/2; ++i) { 21 | for(int j=1; j<=n/2; ++j) 22 | b[i][j]=qry(1, 1, n, n)-qry(i+1, 1, n, n)-qry(1, j+1, n, n)+qry(i+1, j+1, n, n); 23 | for(int j=n/2+1; j<=n; ++j) 24 | b[i][j]=qry(1, 1, n, j)-qry(i+1, 1, n, j); 25 | } 26 | for(int i=n/2+1; i<=n; ++i) { 27 | for(int j=1; j<=n/2; ++j) 28 | b[i][j]=qry(1, 1, i, n)-qry(1, j+1, i, n); 29 | for(int j=n/2+1; j<=n; ++j) 30 | b[i][j]=qry(1, 1, i, j); 31 | } 32 | for(int i=n; i>=1; --i) { 33 | for(int j=1; j<=n; ++j) 34 | b[i][j]-=b[i-1][j]; 35 | for(int j=n; j>=1; --j) 36 | if(b[i][j]-=b[i][j-1]) 37 | Report(i, j); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /CEOI/16-Match.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - https://codeforces.com/blog/entry/46065?#comment-306472 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | const int mxN=1e5; 9 | string s, a; 10 | int n, st[mxN+1], c[mxN+1][26], p[mxN+1], ts=1; 11 | vector occ[mxN+1]; 12 | 13 | inline int gc(int i, int j) { 14 | if(!c[i][j]) { 15 | c[i][j]=ts++; 16 | p[c[i][j]]=i; 17 | } 18 | return c[i][j]; 19 | } 20 | 21 | void rec(int l, int r) { 22 | if(l>=r) 23 | return; 24 | int m=*--upper_bound(occ[st[l+1]].begin(), occ[st[l+1]].end(), r-1); 25 | a[l]='('; 26 | a[m]=')'; 27 | rec(l+1, m); 28 | rec(m+1, r); 29 | } 30 | 31 | int main() { 32 | ios_base::sync_with_stdio(0); 33 | cin.tie(0); 34 | 35 | cin >> s; 36 | n=s.size(); 37 | occ[0].push_back(0); 38 | for(int i=0, j=0; i 6 | using namespace std; 7 | 8 | const int mxN=1234567; 9 | int t, r, n, m; 10 | bool a[2*mxN+1]; 11 | 12 | int main() { 13 | ios::sync_with_stdio(0); 14 | cin.tie(0); 15 | 16 | cin >> t >> r; 17 | while(t--) { 18 | cin >> n, m=2*n+1; 19 | vector> p{{0, 2*n}, {0, 1}, {2*n-1, n}, {2, n}, {n+1, 4}, {n+1, 2*n-3}}; 20 | if(n&1) 21 | for(int i=2; i<6; ++i) 22 | swap(p[i][0], p[i][1]); 23 | for(int i=1; i> b, s+=b, a[b]=1; 30 | for(array c : p) { 31 | if(a[(c[0]+s)%m]&&a[(c[1]+s)%m]) { 32 | cout << (c[0]+s)%m << " " << (c[1]+s)%m << "\n"; 33 | break; 34 | } 35 | } 36 | } else { 37 | int s=0; 38 | for(int i=0, b0, b1; i<2; ++i) { 39 | cin >> b0 >> b1; 40 | for(array c : p) 41 | if((b0-c[0]+m)%m==(b1-c[1]+m)%m) 42 | s+=b0-c[0]+m; 43 | } 44 | cout << (4*m-s)%m << "\n"; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CEOI/17-Sure.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - If you know which type of outcome you want to bet for, choose the highest price for that outcome 3 | - If the profit for one of the outcomes is lower, the minimum profit will only increase if more bets for that outcome are bought 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | const int mxN=1e5; 10 | int n; 11 | double a[mxN+1], b[mxN+1], ans, sa, sb; 12 | 13 | int main() { 14 | ios_base::sync_with_stdio(0); 15 | cin.tie(0); 16 | 17 | cin >> n; 18 | for(int i=0; i> a[i] >> b[i]; 20 | sort(a, a+n+1); 21 | sort(b, b+n+1); 22 | for(int i1=n, i2=n; i1>=0&&i2>=0; ) { 23 | if(sa 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2e3, mxC=50; 7 | int n, m, ci, fi, vi; 8 | ll dp[mxN+1][mxC*2+1], ans; 9 | vector> v1, v2; 10 | 11 | int main() { 12 | ios_base::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> n; 16 | for(int i=0; i> ci >> fi >> vi; 18 | v1.push_back({fi, ci, vi}); 19 | } 20 | sort(v1.begin(), v1.end(), greater>()); 21 | cin >> m; 22 | for(int i=0; i> ci >> fi >> vi; 24 | v2.push_back({fi, ci, vi}); 25 | } 26 | sort(v2.begin(), v2.end(), greater>()); 27 | memset(dp, 0x96, sizeof(dp)); 28 | dp[0][50]=0; 29 | for(int i=0; i<=n; ++i) { 30 | for(int j=0; j<=m; ++j) { 31 | for(int k=2*mxC; k>=0; --k) { 32 | if(k=v2[j-1][0]) 33 | dp[j][k+v1[i][1]]=max(dp[j][k]-v1[i][2], dp[j][k+v1[i][1]]); 34 | if(k>=mxC&&j 2 | using namespace std; 3 | 4 | const int mxN=2e5; 5 | int n, x, t[mxN], ps[mxN], ft[2][mxN+1]; 6 | 7 | inline void upd(int k, int i, int x) { 8 | for(++i; i<=n; i+=i&-i) 9 | ft[k][i]=max(x, ft[k][i]); 10 | } 11 | 12 | inline int qry(int k, int i) { 13 | int r=0; 14 | for(; i; i-=i&-i) 15 | r=max(ft[k][i], r); 16 | return r; 17 | } 18 | 19 | int main() { 20 | ios_base::sync_with_stdio(0); 21 | cin.tie(0); 22 | 23 | cin >> n >> x; 24 | for(int i=0; i> t[i], ps[i]=t[i]; 26 | sort(ps, ps+n); 27 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=1e4, mxQ=100; 5 | int n, l, a[mxN], ps[mxN+1], ans[mxN][mxQ+1], b[mxN+1], q, k[mxQ], k2[mxQ+1]; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n >> l; 12 | for(int i=0; i> a[i]; 14 | cin >> q; 15 | for(int i=0; i> k[i], k2[i]=k[i]; 17 | sort(k2, k2+q); 18 | k2[q]=l; 19 | for(int i=0, p=-1; i<=q; ++i) { 20 | for(int j=p+1; j<=k2[i]; ++j) 21 | b[j]=i; 22 | p=k2[i]; 23 | } 24 | for(int i=1; i 9 | using namespace std; 10 | 11 | int n; 12 | vector p, ans; 13 | 14 | void dfs(int x, vector &p, int i, int s) { 15 | ans.push_back(s+x-1); 16 | vector p2; 17 | for(; i=p2[i]) 22 | dfs(x/p2[i], p2, i, s+p2[i]-1); 23 | } 24 | 25 | int main() { 26 | ios_base::sync_with_stdio(0); 27 | cin.tie(0); 28 | 29 | cin >> n; 30 | for(int i=2; i*i<=n; ++i) 31 | if(n%i==0) 32 | p.push_back(i); 33 | dfs(n, p, 0, 0); 34 | sort(ans.begin(), ans.end()); 35 | ans.resize(unique(ans.begin(), ans.end())-ans.begin()); 36 | cout << ans.size() << "\n"; 37 | for(int ai : ans) 38 | cout << ai << " "; 39 | } 40 | -------------------------------------------------------------------------------- /CEOI/19-AmusementPark.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=18, M=998244353, I2=499122177; 5 | int n, m, adj[mxN], ans, dp[1<> v[1<> n >> m; 13 | for(int i=0, a, b; i> a >> b, --a, --b; 15 | adj[a]|=1< a : v[i]) { 21 | dp[a[0]]+=a[1]; 22 | if(dp[a[0]]>=M) 23 | dp[a[0]]-=M; 24 | if(!i) { 25 | ans+=a[1]; 26 | if(ans>=M) 27 | ans-=M; 28 | } 29 | } 30 | vector>().swap(v[i]); 31 | for(int j=i, k=n; ~j; j=j?(j-1)&i:-1) { 32 | if(!dp[j]) 33 | continue; 34 | if(j) { 35 | while(j>>k&1^1) 36 | --k; 37 | v[i^1<=M) 40 | dp[j^1< 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e5, M=1e9+7; 7 | int n, l[mxN], r[mxN]; 8 | ll h[mxN], w[mxN+1], ans; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n; 15 | for(int i=0; i> h[i]; 17 | l[i]=i-1; 18 | while(~l[i]&&h[l[i]]>h[i]) 19 | l[i]=l[l[i]]; 20 | } 21 | for(int i=0; i> w[i+1]; 23 | w[i+1]+=w[i]; 24 | } 25 | for(int i=n-1; ~i; --i) { 26 | r[i]=i+1; 27 | while(r[i]=h[i]) 28 | r[i]=r[r[i]]; 29 | ll cw=(w[r[i]]-w[l[i]+1])%M, lh=max(~l[i]?h[l[i]]:0, r[i] 8 | using namespace std; 9 | 10 | const int mxS=3e6+1; 11 | int n, m=1, c[mxS][26], dp[mxS], ans; 12 | string s; 13 | bool a[mxS]; 14 | 15 | int main() { 16 | ios::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> n; 20 | while(n--) { 21 | cin >> s; 22 | int u=0; 23 | for(int i=s.size()-1; i>=0; u=c[u][s[i]-'a'], --i) 24 | if(!c[u][s[i]-'a']) 25 | c[u][s[i]-'a']=m++; 26 | a[u]=1; 27 | } 28 | while(m--) { 29 | int nc=0; 30 | for(int i=0; i<26; ++i) 31 | nc+=a[c[m][i]]; 32 | dp[m]=a[m]; 33 | for(int i=0; i<26; ++i) { 34 | if(!a[c[m][i]]) 35 | continue; 36 | ans=max(dp[c[m][i]]+dp[m]+nc-2, ans); 37 | dp[m]=max(dp[c[m][i]]+a[m], dp[m]); 38 | } 39 | dp[m]=max(a[m]+nc, dp[m]+nc-1); 40 | ans=max(dp[m], ans); 41 | } 42 | cout << ans; 43 | } 44 | -------------------------------------------------------------------------------- /COCI/18-Deda.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Create a segment tree with the indicies as the numbers of the children 3 | - Maintain minimum stops rode for each segment 4 | - Kind of like doing a binary search on a segment tree to find the minimum r with smaller than y number of stops 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | const int mxN=2e5; 11 | int n, q, a, b, st[1<<19]; 12 | char qt; 13 | 14 | void upd(int l1, int x, int i=1, int l2=0, int r2=n-1) { 15 | st[i]=min(x, st[i]); 16 | if(l2==r2) 17 | return; 18 | int m2=(l2+r2)/2; 19 | if(l1<=m2) 20 | upd(l1, x, 2*i, l2, m2); 21 | else 22 | upd(l1, x, 2*i+1, m2+1, r2); 23 | } 24 | 25 | int qry(int l1, int x, int i=1, int l2=0, int r2=n-1) { 26 | if(st[i]>x) 27 | return -2; 28 | if(l2==r2) 29 | return l2; 30 | int m2=(l2+r2)/2; 31 | int r=-2; 32 | if(l1<=m2) 33 | r=qry(l1, x, 2*i, l2, m2); 34 | if(r==-2) 35 | r=qry(l1, x, 2*i+1, m2+1, r2); 36 | return r; 37 | } 38 | 39 | int main() { 40 | ios_base::sync_with_stdio(0); 41 | cin.tie(0); 42 | 43 | cin >> n >> q; 44 | memset(st, 0x3f, sizeof(st)); 45 | while(q--) { 46 | cin >> qt >> a >> b, --b; 47 | if(qt=='M') 48 | upd(b, a); 49 | else 50 | cout << qry(b, a)+1 << "\n"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /COI/18-Paprike.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, k, ans; 6 | long long h[mxN]; 7 | vector adj[mxN]; 8 | 9 | void dfs(int u=0, int p=-1) { 10 | if(~p) 11 | adj[u].erase(find(adj[u].begin(), adj[u].end(), p)); 12 | for(int v : adj[u]) { 13 | dfs(v, u); 14 | h[u]+=h[v]; 15 | } 16 | sort(adj[u].begin(), adj[u].end(), [](const int &i, const int &j) { 17 | return h[i]>h[j]; 18 | }); 19 | for(int i=0; ik; ++i, ++ans) 20 | h[u]-=h[adj[u][i]]; 21 | } 22 | 23 | int main() { 24 | ios::sync_with_stdio(0); 25 | cin.tie(0); 26 | 27 | cin >> n >> k; 28 | for(int i=0; i> h[i]; 30 | for(int i=1, u, v; i> u >> v, --u, --v; 32 | adj[u].push_back(v); 33 | adj[v].push_back(u); 34 | } 35 | dfs(); 36 | cout << ans; 37 | } 38 | -------------------------------------------------------------------------------- /CSAcademy/78-E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - https://csacademy.com/contest/round-78/task/xor-transform/solution/ 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | int n, m, q, dp[1<<23]; 9 | unsigned long long x, ha; 10 | vector> a; 11 | 12 | unsigned long long nextInt() { 13 | unsigned long long z=(x+=0x9E3779B97F4A7C15ULL); 14 | z=(z^z>>30)*0xBF58476D1CE4E5B9ULL; 15 | z=(z^z>>27)*0x94D049BB133111EBULL; 16 | return z^z>>31; 17 | } 18 | 19 | int rng() { 20 | return 1+(nextInt()>>33); 21 | } 22 | 23 | int main() { 24 | cin >> n >> m >> q >> x; 25 | for(int i=0; i>k&1) 31 | dp[i]^=dp[i^1< 2 | using namespace std; 3 | 4 | const int mxN=2e6; 5 | int n, p[2*mxN+2], ft[mxN+1]; 6 | string s; 7 | char s2[2*mxN+3]; 8 | vector ta[mxN]; 9 | 10 | void upd(int i, int x) { 11 | for(++i; i<=n; i+=i&-i) 12 | ft[i]+=x; 13 | } 14 | 15 | int qry(int i) { 16 | int r=0; 17 | for(; i; i-=i&-i) 18 | r+=ft[i]; 19 | return r; 20 | } 21 | 22 | int main() { 23 | ios::sync_with_stdio(0); 24 | cin.tie(0); 25 | 26 | cin >> s; 27 | n=s.size(); 28 | s2[0]='!', s2[1]='#', s2[2*n+2]='@'; 29 | for(int i=0; i=i) 35 | p[i]=min(r-i, p[2*c-i]); 36 | while(s2[i+p[i]]==s2[i-p[i]]) 37 | ++p[i]; 38 | --p[i]; 39 | if(i+p[i]>r) { 40 | c=i; 41 | r=i+p[i]; 42 | } 43 | } 44 | long long ans=0; 45 | for(int i=n-1; ~i; --i) { 46 | ans+=qry(i+1+p[2*i+3]/2); 47 | upd(i, 1); 48 | if(i-p[2*i+3]/2>=0) 49 | ta[i-p[2*i+3]/2].push_back(i); 50 | for(int j : ta[i]) 51 | upd(j, -1); 52 | } 53 | cout << ans; 54 | } 55 | -------------------------------------------------------------------------------- /CSAcademy/ioi-2016-training-round-1-farey_sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2e5; 7 | int n, c[mxN+1]; 8 | ll lb, rb, k, a1, a2=1; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n >> k; 15 | rb=(ll)n*n; 16 | while(lba1*i) { 33 | a1=c; 34 | a2=i; 35 | } 36 | } 37 | cout << a1 << " " << a2 << endl; 38 | } 39 | -------------------------------------------------------------------------------- /CSAcademy/junior-challenge-2017-day-1-borland.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Given an element, we can also find the previous element 3 | - Divide and conquer on the array 4 | - Use 2 pointers starting from the middle to count the answer 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define ll long long 11 | 12 | const int M=1e9+7; 13 | int n; 14 | ll s, a, b, c, ia, ans; 15 | 16 | ll iv(ll a, ll m) { 17 | return a<=1?a:(1-iv(m%a, a)*m)/a+m; 18 | } 19 | 20 | void dc(int l=0, int r=n-1, ll t=s) { 21 | if(l>r) 22 | return; 23 | int m=(l+r)/2; 24 | ll vl=t; 25 | for(int i=l; il||irl?(vl-b+c)*ia%c:2e9; 38 | ans+=ml*(ir-m)%M; 39 | } else { 40 | mr=max(vr, mr); 41 | vr=++ir> n >> s >> a >> b >> c; 52 | ia=iv(a, c); 53 | dc(); 54 | cout << ans%M; 55 | } 56 | -------------------------------------------------------------------------------- /CodeChef/ADIMAT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=550, M=1e9+7; 7 | int n, m; 8 | ll iv[mxN+1], f1[mxN+1], f2[mxN+1], p2[mxN+1], ans, dp[mxN+1]; 9 | vector a; 10 | 11 | void dfs(int s, int t, ll o) { 12 | if(!s) { 13 | memset(dp+1, 0, 8*m); 14 | dp[0]=o; 15 | for(int i=1; i<=m; ++i) { 16 | for(int j=1; j<=i; ++j) { 17 | ll f=dp[i-j]*f1[i-1]%M*f2[i-j]%M; 18 | for(int ai : a) 19 | f=f*p2[__gcd(ai, j)]%M; 20 | dp[i]=(dp[i]+f)%M; 21 | } 22 | } 23 | ans+=dp[m]; 24 | } else if(t) { 25 | for(int i=0; i*t<=s; ++i, o=o*iv[t]%M) { 26 | dfs(s-i*t, t-1, o*f2[i]%M); 27 | a.push_back(t); 28 | } 29 | while(a.size()&&a.back()==t) 30 | a.pop_back(); 31 | } 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(0); 36 | cin.tie(0); 37 | 38 | cin >> n >> m; 39 | if(n>m) 40 | swap(n, m); 41 | iv[1]=f1[0]=f2[0]=p2[0]=1; 42 | for(int i=2; i<=m; ++i) 43 | iv[i]=M-M/i*iv[M%i]%M; 44 | for(int i=1; i<=m; ++i) { 45 | f1[i]=f1[i-1]*i%M; 46 | f2[i]=f2[i-1]*iv[i]%M; 47 | p2[i]=p2[i-1]*2%M; 48 | } 49 | dfs(n, n, f2[m]); 50 | cout << ans%M; 51 | } 52 | -------------------------------------------------------------------------------- /CodeChef/BIPFAMIL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2.5e3, M=1e9+7; 7 | int t, n, m; 8 | ll f1[mxN+1], f2[mxN+1], dp[mxN+1][mxN+1]; 9 | 10 | ll exp(ll b, int p) { 11 | ll r=1; 12 | while(p) { 13 | if(p&1) 14 | r=r*b%M; 15 | b=b*b%M; 16 | p/=2; 17 | } 18 | return r; 19 | } 20 | 21 | int main() { 22 | ios_base::sync_with_stdio(0); 23 | cin.tie(0); 24 | 25 | f1[0]=f2[0]=1; 26 | for(int i=1; i<=mxN; ++i) { 27 | f1[i]=f1[i-1]*i%M; 28 | f2[i]=exp(f1[i], M-2); 29 | } 30 | for(int i=0; i<=mxN; ++i) 31 | dp[1][i]=f2[i]; 32 | for(int i=2; i<=mxN; ++i) { 33 | for(int j=1; j<=mxN/i; ++j) { 34 | for(int k=1; k> t; 41 | while(t--) { 42 | cin >> n >> m; 43 | cout << dp[m][n]*f1[n]%M << "\n"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CodeChef/CHGCDG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5, mxC=1e6; 5 | int t, n, a[mxN]; 6 | vector b[mxC+1]; 7 | 8 | inline void solve() { 9 | cin >> n; 10 | for(int i=0; i> a[i]; 12 | for(int j=2; j*j<=a[i]; ++j) { 13 | if(a[i]%j==0) 14 | b[j].push_back(0); 15 | while(a[i]%j==0) { 16 | a[i]/=j; 17 | ++b[j].back(); 18 | } 19 | } 20 | if(a[i]>1) 21 | b[a[i]].push_back(1); 22 | } 23 | int ans=1; 24 | for(int i=1; i<=mxC; ++i) { 25 | for(int j=1; ; ++j) { 26 | int d=0; 27 | for(int c : b[i]) { 28 | if(c>j) 29 | d+=(c-j)/2+j; 30 | else 31 | d+=c; 32 | } 33 | if(d> t; 48 | while(t--) 49 | solve(); 50 | } 51 | -------------------------------------------------------------------------------- /CodeChef/COINDENO.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - http://codeforces.com/blog/entry/60701?#comment-446462 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | #define ll long long 9 | 10 | const int mxN=100, mxQ=1e6; 11 | const ll M=1e9+7, INF=1e18; 12 | int c, n, m; 13 | ll w[mxN+1], dp[mxQ+1]; 14 | 15 | int main() { 16 | ios_base::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> c >> n >> m; 20 | for(int i=0; i<=n; ++i) 21 | w[i]=INF; 22 | while(c--) { 23 | int ai, wi; 24 | cin >> ai >> wi; 25 | w[ai]=wi; 26 | } 27 | for(int i=1; i<=mxQ; ++i) { 28 | dp[i]=INF; 29 | for(int j=1; j<=min(n, i); ++j) 30 | dp[i]=min(dp[i-j]+w[j], dp[i]); 31 | } 32 | while(m--) { 33 | ll qi; 34 | cin >> qi; 35 | if(qi>mxQ) { 36 | __int128 ans=(__int128)INF*INF; 37 | for(int i=1; i<=n; ++i) { 38 | ll j=mxQ/i*i+qi%i-i; 39 | if(w[i]>=INF||dp[j]>=INF) 40 | continue; 41 | ans=min(dp[j]+(__int128)w[i]*((qi-j)/i), ans); 42 | } 43 | cout << (ans>=(__int128)INF*INF?-1:(ll)(ans%M)) << "\n"; 44 | } else 45 | cout << (dp[qi]>=INF?-1:dp[qi]%M) << "\n"; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CodeChef/EXACTWAL.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - http://codeforces.com/blog/entry/60701?#comment-446462 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | const int mxN=100; 9 | int n, m, a[mxN]; 10 | bool adj[2][mxN][mxN], vis[mxN]; 11 | 12 | int dfs(int u) { 13 | int s=1; 14 | vis[u]=1; 15 | for(int v=0; v> n >> m; 26 | while(m--) { 27 | int u, v; 28 | cin >> u >> v, --u, --v; 29 | adj[0][u][v]=adj[0][v][u]=1; 30 | } 31 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=3e5; 5 | int t, n, m, din[mxN], dout[mxN], cc; 6 | vector adj[mxN]; 7 | 8 | inline void solve() { 9 | cin >> n >> m; 10 | memset(din, 0, 4*n); 11 | memset(dout, 0, 4*n); 12 | for(int i=0; i> u >> v, --u, --v; 17 | adj[u].push_back(v); 18 | ++din[v]; 19 | } 20 | for(int i=0; i, greater> pq; 25 | for(int i=0; i1) { 31 | int a=pq.top(); 32 | pq.pop(); 33 | int b=pq.top(); 34 | pq.pop(); 35 | pq.push(max(a, b)+1); 36 | } 37 | cout << cc+pq.top()-2 << "\n"; 38 | } 39 | 40 | int main() { 41 | ios_base::sync_with_stdio(0); 42 | cin.tie(0); 43 | 44 | cin >> t; 45 | while(t--) 46 | solve(); 47 | } 48 | -------------------------------------------------------------------------------- /CodeChef/GCDMOD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int M=1e9+7; 7 | int t; 8 | ll a, b, n, m, pa, pb, s; 9 | 10 | inline ll gcd(ll a, ll b) { 11 | if(a&&b) 12 | while((a%=b)&&(b%=a)); 13 | return a^b; 14 | } 15 | 16 | inline ll exp(ll b, ll p, ll m) { 17 | ll r=1; 18 | while(p) { 19 | if(p&1) 20 | r=(__int128)r*b%m; 21 | b=(__int128)b*b%m; 22 | p/=2; 23 | } 24 | return r; 25 | } 26 | 27 | inline void solve() { 28 | cin >> a >> b >> n; 29 | m=a==b?M:a-b; 30 | s=(exp(a, n, m)+exp(b, n, m))%m; 31 | cout << gcd(a-b, s)%M << "\n"; 32 | } 33 | 34 | int main() { 35 | ios_base::sync_with_stdio(0); 36 | cin.tie(0); 37 | 38 | cin >> t; 39 | while(t--) 40 | solve(); 41 | } 42 | -------------------------------------------------------------------------------- /CodeChef/GCDSUM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=20, mxM=1e5, M=1e9+7; 7 | int n, m, aij, a[mxN][mxM+1]; 8 | ll a1[mxM+1], a2; 9 | 10 | int main() { 11 | ios_base::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n >> m; 15 | for(int i=0; i> aij, ++a[i][aij]; 18 | for(int i=mxM; i; --i) { 19 | a1[i]=1; 20 | for(int j=0; j 2 | using namespace std; 3 | 4 | const int mxN=300; 5 | int t, n, ai, c[4], dp[mxN+1][mxN+1][mxN+1][4]; 6 | 7 | void solve() { 8 | cin >> n; 9 | c[0]=c[1]=c[2]=c[3]=0; 10 | for(int i=0; i> ai, ++c[ai%4]; 12 | cout << (dp[c[1]][c[2]][c[3]][0]^c[0]&1?"Ghayeeth\n":"Siroj\n"); 13 | } 14 | 15 | int main() { 16 | ios_base::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | for(int i=0; i<=mxN; ++i) { 20 | for(int j=0; j<=mxN-i; ++j) { 21 | for(int k=0; k<=mxN-i-j; ++k) { 22 | for(int l=0; l<4; ++l) { 23 | if(l==2) { 24 | dp[i][j][k][l]=9; 25 | continue; 26 | } 27 | int a=i?dp[i-1][j][k][(l+1)&3]:9, b=j?dp[i][j-1][k][(l+2)&3]:9, c=k?dp[i][j][k-1][(l+3)&3]:9; 28 | for(int m=0; ; ++m) { 29 | if(a!=m&&b!=m&&c!=m) { 30 | dp[i][j][k][l]=m; 31 | break; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | } 38 | cin >> t; 39 | while(t--) 40 | solve(); 41 | } 42 | -------------------------------------------------------------------------------- /CodeChef/MYST/Checker/Checker$StdIn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Checker/Checker$StdIn.class -------------------------------------------------------------------------------- /CodeChef/MYST/Checker/Checker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Checker/Checker.class -------------------------------------------------------------------------------- /CodeChef/MYST/Judge/Judge$StdIn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Judge/Judge$StdIn.class -------------------------------------------------------------------------------- /CodeChef/MYST/Judge/Judge.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Judge/Judge.class -------------------------------------------------------------------------------- /CodeChef/MYST/MultiTestGen/MultiTestGen$StdIn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/MultiTestGen/MultiTestGen$StdIn.class -------------------------------------------------------------------------------- /CodeChef/MYST/MultiTestGen/MultiTestGen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/MultiTestGen/MultiTestGen.class -------------------------------------------------------------------------------- /CodeChef/MYST/SingleTestGen/SingleTestGen$SCCFinder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/SingleTestGen/SingleTestGen$SCCFinder.class -------------------------------------------------------------------------------- /CodeChef/MYST/SingleTestGen/SingleTestGen$StdIn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/SingleTestGen/SingleTestGen$StdIn.class -------------------------------------------------------------------------------- /CodeChef/MYST/SingleTestGen/SingleTestGen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/SingleTestGen/SingleTestGen.class -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V1_0.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V1_0.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V2_0.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V2_0.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V3_0.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V3_0.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V3_1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V3_1.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V4_0.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V4_0.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V4_1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V4_1.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V5_0.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V5_0.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V5_1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V5_1.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V5_2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V5_2.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V5_3.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V5_3.exe -------------------------------------------------------------------------------- /CodeChef/MYST/Solutions/V5_4.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmwilliamlin168/CompetitiveProgramming/e91d7f69f394a21aa04a7e00e9fe95a262c2dfcf/CodeChef/MYST/Solutions/V5_4.exe -------------------------------------------------------------------------------- /CodeChef/MYST/TestData/sample/4_3_0.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 4 3 | 1 4 4 4 | 2 1 3 3 1 5 | 3 1 2 1 4 1 2 6 | 3 4 4 3 1 2 3 -------------------------------------------------------------------------------- /CodeChef/MYST/TestData/test.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 10 3 | 4 9 5 8 6 9 8 3 10 4 | 1 1 4 5 | 2 9 6 6 2 6 | 3 3 10 6 1 6 5 7 | 1 7 7 8 | 4 5 1 2 6 10 4 9 1 9 | 2 4 6 8 3 10 | 2 5 6 5 7 11 | 3 3 6 6 4 5 6 12 | 1 3 2 13 | -------------------------------------------------------------------------------- /CodeChef/ORMATRIX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int t, n, m, ans[1000][1000]; 5 | string g[1000]; 6 | bool b[1000], c[1000]; 7 | 8 | inline void solve() { 9 | cin >> n >> m; 10 | bool a=0; 11 | memset(b, 0, n); 12 | memset(c, 0, m); 13 | for(int i=0; i> g[i]; 15 | for(int j=0; j> t; 35 | while(t--) 36 | solve(); 37 | } 38 | -------------------------------------------------------------------------------- /CodeChef/SPECTAC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=50; 7 | int n, m, k, M; 8 | ll dp[mxN+1][mxN+1][mxN+1], dp2[mxN+1][mxN+1]; 9 | 10 | int main() { 11 | ios_base::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n >> m >> k >> M; 15 | dp[0][0][0]=1; 16 | for(int a=1; a<=n; ++a) { 17 | memset(dp2, 0, (a+1)*sizeof(dp2[0])); 18 | for(int b=1; b<=a; ++b) { 19 | for(int c=1; c<=b; ++c) { 20 | for(int d=a; d<=n; ++d) { 21 | for(int e=m; e; --e) { 22 | dp[a][c][e]=(dp[b-1][c-1][e-1]+dp[a][c][e-1]+dp[a][c][e])%M; 23 | if(d>a) 24 | dp2[c][e]=(dp[b-1][c-1][e-1]+dp2[c][e-1]+dp2[c][e])%M; 25 | } 26 | } 27 | } 28 | } 29 | for(int b=0; b<=a; ++b) 30 | for(int c=0; c<=m; ++c) 31 | dp[a][b][c]=(dp[a][b][c]-dp2[b][c]+M+dp[a-1][b][c])%M; 32 | } 33 | cout << dp[n][k][m]; 34 | } 35 | -------------------------------------------------------------------------------- /CodeChef/STRTF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1<<18, MG1=1<<9; 5 | int n, q, a[MG1][mxN], ki, xi; 6 | 7 | int dfs(int i, int j) { 8 | if(i>n) 9 | return 0; 10 | if(j> n >> q; 21 | for(int i=0; i> a[0][i]; 23 | for(int i=1; i> ki >> xi, --xi; 28 | cout << dfs(xi, ki%mxN) << "\n"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CodeChef/TWOTREES.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int t, n[2], p[2][mxN], h[2][mxN]; 6 | vector adj[2][mxN]; 7 | bool d[mxN]; 8 | 9 | void solve() { 10 | cin >> n[0] >> n[1]; 11 | map, int> mp[2]; 12 | for(int k=0; k<2; ++k) { 13 | for(int i=1; i> p[k][i], --p[k][i]; 15 | adj[k][p[k][i]].push_back(i); 16 | } 17 | for(int i=n[k]-1; i>=0; --i) { 18 | vector v; 19 | for(int j : adj[k][i]) 20 | v.push_back(h[k][j]); 21 | sort(v.begin(), v.end()); 22 | if(mp[k].find(v)==mp[k].end()) 23 | mp[k].insert({v, mp[k].size()}); 24 | h[k][i]=mp[k][v]; 25 | } 26 | } 27 | int a1=0; 28 | memset(d, 0, n[0]); 29 | d[0]=1; 30 | for(int i=0; i s; 34 | for(int j : adj[0][i]) 35 | d[j]=s.insert(h[0][j]).second; 36 | a1+=!adj[0][i].size(); 37 | } 38 | cout << (long long)a1*mp[1].size() << "\n"; 39 | for(int k=0; k<2; ++k) 40 | for(int i=0; i> t; 49 | while(t--) 50 | solve(); 51 | } 52 | -------------------------------------------------------------------------------- /CodeChef/UPDOTR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e6; 5 | int t, n, a[mxN], anc[mxN][20], b[mxN][20]; 6 | vector adj[mxN]; 7 | 8 | void dfs(int u, int p=-1) { 9 | anc[u][0]=p; 10 | for(int i=1; i<20; ++i) 11 | anc[u][i]=anc[u][i-1]==-1?-1:anc[anc[u][i-1]][i-1]; 12 | b[u][0]=p==-1||a[u]>a[p]; 13 | for(int i=1; i<20; ++i) 14 | b[u][i]=b[u][i-1]+(anc[u][i-1]==-1?0:b[anc[u][i-1]][i-1]); 15 | a[u]=max(a[p], a[u]); 16 | for(int v : adj[u]) 17 | dfs(v, u); 18 | } 19 | 20 | void solve() { 21 | cin >> n; 22 | for(int i=0; i> a[i]; 24 | adj[i].clear(); 25 | } 26 | int pi, q, ans=0, vi, wi; 27 | for(int i=1; i> pi, --pi; 29 | adj[pi].push_back(i); 30 | } 31 | dfs(0); 32 | cin >> q; 33 | while(q--) { 34 | cin >> vi >> wi, vi=(vi^ans)-1, wi^=ans; 35 | ans=0; 36 | for(int i=19; i>=0; --i) { 37 | if(anc[vi][i]!=-1&&a[anc[vi][i]]>wi) { 38 | ans+=b[vi][i]; 39 | vi=anc[vi][i]; 40 | } 41 | } 42 | if(a[vi]>wi) 43 | ans+=b[vi][0]; 44 | cout << ans << "\n"; 45 | } 46 | } 47 | 48 | int main() { 49 | ios_base::sync_with_stdio(0); 50 | cin.tie(0); 51 | 52 | cin >> t; 53 | while(t--) 54 | solve(); 55 | } 56 | -------------------------------------------------------------------------------- /Codeforces/0071C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, a[mxN]; 6 | 7 | void chk(int x) { //x = side length 8 | //iterate starting index 9 | for(int si=0; si> n; 25 | for(int i=0; i> a[i]; 27 | //iterate through divisors of n 28 | //O(sqrt(n)) iterations, O(n) check, so O(nsqrt(n)) overall 29 | for(int i=1; i*i<=n; ++i) { 30 | if(n%i) //side length must divide n 31 | continue; 32 | if(n/i>=3) //number of points must be >= 3 to form a nondegenerate polygon 33 | chk(i); 34 | if(i>=3) 35 | chk(n/i); //if i = sqrt(n) then this will check x = sqrt(n) again, but we don't care since checking again doesn't do anything 36 | } 37 | cout << "NO"; 38 | } 39 | -------------------------------------------------------------------------------- /Codeforces/0165B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int n, k; 9 | cin >> n >> k; 10 | int lb=1, rb=1e9; //Left bound and right bound for answer, inclusive 11 | while(lb=n) 18 | rb=mb; //We want the minimum, all v>mb aren't minimal since v=mb works 19 | else 20 | lb=mb+1; //mb doesn't work, so we need at least mb+1 21 | } 22 | cout << lb; 23 | } 24 | -------------------------------------------------------------------------------- /Codeforces/0191E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Binary search, implement the function that returns how many subarrays have sum >= x 3 | - Binary indexed tree to answer how many prefix sums are <= x 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | #define ll long long 10 | 11 | const int mxN=1e5; 12 | int n; 13 | ll k, ps[mxN+1], sps[mxN+1], ft[mxN+2], lb=-1e14, rb=1e14, mb, k2; 14 | 15 | inline void upd(int i) { 16 | for(++i; i<=n+1; i+=i&-i) 17 | ++ft[i]; 18 | } 19 | 20 | inline int qry(int i) { 21 | int r=0; 22 | for(; i; i-=i&-i) 23 | r+=ft[i]; 24 | return r; 25 | } 26 | 27 | int main() { 28 | ios_base::sync_with_stdio(0); 29 | cin.tie(0); 30 | 31 | cin >> n >> k; 32 | for(int i=1; i<=n; ++i) { 33 | cin >> ps[i]; 34 | sps[i]=ps[i]+=ps[i-1]; 35 | } 36 | sort(sps, sps+n+1); 37 | while(lb<=rb) { 38 | mb=(lb+rb)/2, k2=0; 39 | memset(ft, 0, sizeof(ft)); 40 | upd(lower_bound(sps, sps+n+1, 0)-sps); 41 | for(int i=1; i<=n; ++i) { 42 | k2+=qry(upper_bound(sps, sps+n+1, ps[i]-mb)-sps); 43 | upd(lower_bound(sps, sps+n+1, ps[i])-sps); 44 | } 45 | if(k2>=k) 46 | lb=mb+1; 47 | else 48 | rb=mb-1; 49 | } 50 | cout << rb; 51 | } 52 | -------------------------------------------------------------------------------- /Codeforces/0297C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - http://codeforces.com/blog/entry/7437 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | #define fi first 9 | #define se second 10 | 11 | const int mxN=1e5; 12 | int n, a[mxN], b[mxN]; 13 | pair ps[mxN]; 14 | 15 | int main() { 16 | ios_base::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> n; 20 | for(int i=0; i> ps[i].fi, ps[i].se=i; 22 | sort(ps, ps+n); 23 | for(int i=0; i<(n+2)/3; ++i) { 24 | a[ps[i].se]=i; 25 | b[ps[i].se]=ps[i].fi-i; 26 | } 27 | for(int i=(n+2)/3; i<(2*n+2)/3; ++i) { 28 | b[ps[i].se]=i; 29 | a[ps[i].se]=ps[i].fi-i; 30 | } 31 | for(int i=(2*n+2)/3; i 6 | using namespace std; 7 | 8 | #define ll long long 9 | 10 | const int mxN=1e3; 11 | int n, m, qt, x0, y0, x1, y1; 12 | ll vi, ft[4][mxN+1][mxN+1]; 13 | 14 | void upd(int x, int y, ll v) { 15 | int k=x&1|(y&1)<<1; 16 | for(int i=x+1; i<=n; i+=i&-i) 17 | for(int j=y+1; j<=n; j+=j&-j) 18 | ft[k][i][j]^=v; 19 | } 20 | 21 | ll qry(int x, int y) { 22 | int k=x&1|(y&1)<<1; 23 | ll r=0; 24 | for(int i=x; i; i-=i&-i) 25 | for(int j=y; j; j-=j&-j) 26 | r^=ft[k^3][i][j]; 27 | return r; 28 | } 29 | 30 | int main() { 31 | ios_base::sync_with_stdio(0); 32 | cin.tie(0); 33 | 34 | cin >> n >> m; 35 | while(m--) { 36 | cin >> qt >> x0 >> y0 >> x1 >> y1, --x0, --y0; 37 | if(qt==2) { 38 | cin >> vi; 39 | upd(x1, y1, vi); 40 | upd(x1, y0, vi); 41 | upd(x0, y1, vi); 42 | upd(x0, y0, vi); 43 | } else 44 | cout << (qry(x1, y1)^qry(x1, y0)^qry(x0, y1)^qry(x0, y0)) << "\n"; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Codeforces/0365E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Only 5000 possible inputs, so just test them locally (because it is hard to prove that a solution works) 3 | - This solution iterates over the number of prime factors to use 4 | - It then constructs all numbers <=2n^2 with these prime factors 5 | - The larger numbers have more prime factors, so if we create over n numbers, we choose the n largest ones 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | const int primes[]={2, 3, 5, 7, 11}; 12 | int n; 13 | vector ans={1}; 14 | 15 | int main() { 16 | ios_base::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> n; 20 | for(int i=0; ; ++i) { 21 | for(int j=0; j=n) 25 | break; 26 | } 27 | for(int i=0; i 6 | using namespace std; 7 | 8 | const int mxN=2e5; 9 | int n; 10 | long long a[mxN]; 11 | 12 | int main() { 13 | ios_base::sync_with_stdio(0); 14 | cin.tie(0); 15 | 16 | cin >> n; 17 | for(int i=0; i> a[i]; 19 | int i=0, p1, p2, ans=0; 20 | while(1) { 21 | ++ans; 22 | int j=i; 23 | while(j=n) 26 | break; 27 | p1=j++; 28 | while(j=n) 31 | break; 32 | p2=j++; 33 | if((a[p2]-a[p1])%(p2-p1)) { 34 | i=p2; 35 | continue; 36 | } 37 | int d=(a[p2]-a[p1])/(p2-p1); 38 | for(int k=p1-1; k>=i; --k) 39 | a[k]=a[k+1]-d; 40 | if(a[i]<=0) { 41 | i=p2; 42 | continue; 43 | } 44 | bool c=0; 45 | for(int k=p2+1; k 10 | using namespace std; 11 | 12 | const int mxN=1e5; 13 | int n; 14 | double smx[mxN+1], smn[mxN+1], s1, pa[mxN+1], pb[mxN+1]; 15 | 16 | int main() { 17 | ios_base::sync_with_stdio(0); 18 | cin.tie(0); 19 | 20 | cin >> n; 21 | for(int i=0; i> smx[i+1], smx[i+1]+=smx[i]; 23 | cout << fixed << setprecision(9); 24 | for(int i=0; i> smn[i+1], smn[i+1]+=smn[i]; 26 | s1=smx[i+1]+smn[i+1]; 27 | pa[i+1]=(s1-sqrt(max(s1*s1-4*smx[i+1], 0.0)))/2; 28 | pb[i+1]=(s1+sqrt(max(s1*s1-4*smx[i+1], 0.0)))/2; 29 | cout << pa[i+1]-pa[i] << " "; 30 | } 31 | cout << "\n"; 32 | for(int i=0; i 6 | using namespace std; 7 | 8 | #define ll long long 9 | 10 | const int mxN=3e5, M=1e9+7; 11 | int n, ti; 12 | ll iv[mxN], f1[mxN], f2[mxN], a1, b1[mxN+1]; 13 | map> mp; 14 | 15 | int main() { 16 | ios_base::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> n; 20 | for(int i=0; i> ti; 22 | for(int j=2; j*j<=ti; ++j) { 23 | if(ti%j) 24 | continue; 25 | int c=0; 26 | while(ti%j==0) { 27 | ++c; 28 | ti/=j; 29 | } 30 | mp[j].push_back(c); 31 | } 32 | if(ti>1) 33 | mp[ti].push_back(1); 34 | } 35 | iv[1]=1; 36 | for(int i=2; i &v=it->second; 47 | sort(v.begin(), v.end()); 48 | for(int i=1; i<=v.size(); ++i) 49 | a1+=v[v.size()-i]*(b1[n-i]-b1[i-1]+M)%M; 50 | } 51 | cout << a1%M; 52 | } 53 | -------------------------------------------------------------------------------- /Codeforces/0742E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - We can make the problem stricter by pairing up consecutive elements and forcing them to be different 3 | - It becomes a classic bipartite coloring problem 4 | - Kind of intuitive that there are no odd cycles in the graph, so I didn't bother to prove it 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | const int mxN=1e5; 11 | int n, a[mxN], b[mxN], op[mxN*2], ans[mxN*2]; 12 | 13 | int main() { 14 | ios_base::sync_with_stdio(0); 15 | cin.tie(0); 16 | 17 | cin >> n; 18 | for(int i=0; i> a[i] >> b[i], --a[i], --b[i]; 20 | op[a[i]]=b[i]; 21 | op[b[i]]=a[i]; 22 | } 23 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=1e7; 5 | int n, l, r, a[mxN+1], b[mxN+1]; 6 | long long ans; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n >> l >> r; 13 | if(n==1) { 14 | cout << r-l+1; 15 | return 0; 16 | } 17 | for(int i=1; ; ++i) { 18 | int pi=1; 19 | for(int j=0; jr) 21 | break; 22 | for(int j=0; (j+=pi)<=r; ) 23 | a[j]=pi; 24 | b[pi]=i; 25 | } 26 | for(int i=1; i<=r; ++i) 27 | b[i]=b[i]?b[i]:b[i-1]; 28 | for(int i=l; i<=r; ++i) 29 | ans+=b[r/(i/a[i])]-b[a[i]]; 30 | cout << ans*2; 31 | } 32 | -------------------------------------------------------------------------------- /Codeforces/0789D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - All nodes that have edges need to be in one connected component 3 | - Any combination of 2 self-loops work 4 | - Any combination of a self-loop and a non-self-loop works 5 | - A combination of 2 edges works iff they share a common node 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define ll long long 12 | 13 | const int mxN=1e6; 14 | int n, m, nv; 15 | ll sls, ans; 16 | vector adj[mxN]; 17 | bool sl[mxN], vis[mxN]; 18 | 19 | void dfs(int u) { 20 | vis[u]=1; 21 | ++nv; 22 | for(int v : adj[u]) 23 | if(!vis[v]) 24 | dfs(v); 25 | } 26 | 27 | int main() { 28 | ios_base::sync_with_stdio(0); 29 | cin.tie(0); 30 | 31 | cin >> n >> m; 32 | for(int i=0; i> u >> v, --u, --v; 35 | adj[u].push_back(v); 36 | if(u==v) { 37 | sl[u]=1; 38 | ++sls; 39 | } else 40 | adj[v].push_back(u); 41 | } 42 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=500, mxQ=300, M=1e9+7; 5 | int mi[5]={0, 1, 3, 2, 4}, n, m, q; 6 | vector a[mxN]; 7 | long long ans=1; 8 | 9 | void rs(int n) { 10 | for(int i=0; i> s; 13 | for(int j=0; j> n >> m; 23 | rs(n); 24 | cin >> q; 25 | rs(q); 26 | int ei=0; 27 | for(int j=0; j=m||!a[ei][j]) { 32 | ans=ans*5%M; 33 | continue; 34 | } 35 | for(int i=n+q-1; i>=j; --i) 36 | a[ei][i]=a[ei][i]*mi[a[ei][j]]%5; 37 | for(int i=ei+1; i=j; --k) 39 | a[i][k]=(a[i][k]+4*a[ei][k]*a[i][j])%5; 40 | ++ei; 41 | } 42 | for(int i=0; i 2 | using namespace std; 3 | 4 | long long l, r; 5 | int al[19], ar[19], c[10]; 6 | 7 | bool e(int i=0, bool b1=0, bool b2=0) { 8 | if(b1&&b2) 9 | return 1; 10 | if(i>18) 11 | return 1; 12 | for(int j=0; j<10; ++j) { 13 | if(!c[j]||!b1&&jar[i]) 14 | continue; 15 | --c[j]; 16 | bool b3=e(i+1, b1||j>al[i], b2||j18) 26 | return e(); 27 | int d=0; 28 | for(; b<10; ++b) { 29 | ++c[b]; 30 | d+=a(i+1, b); 31 | --c[b]; 32 | } 33 | return d; 34 | } 35 | 36 | int main() { 37 | ios::sync_with_stdio(0); 38 | cin.tie(0); 39 | 40 | cin >> l >> r; 41 | for(int i=18; i>=0; --i, l/=10) 42 | al[i]=l%10; 43 | for(int i=18; i>=0; --i, r/=10) 44 | ar[i]=r%10; 45 | cout << a(); 46 | } 47 | -------------------------------------------------------------------------------- /Codeforces/0875F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=2e5; 5 | int n, m, ans, p[mxN]; 6 | array e[mxN]; 7 | bool c[mxN]; 8 | 9 | int find(int x) { 10 | return x==p[x]?x:(p[x]=find(p[x])); 11 | } 12 | 13 | bool join(int x, int y) { 14 | if((x=find(x))==(y=find(y))||c[x]&&c[y]) { 15 | if(c[x]) 16 | return 0; 17 | return c[x]=1; 18 | } 19 | p[y]=x; 20 | c[x]|=c[y]; 21 | return 1; 22 | } 23 | 24 | int main() { 25 | ios::sync_with_stdio(0); 26 | cin.tie(0); 27 | 28 | cin >> n >> m; 29 | for(int i=0; i> e[i][1] >> e[i][2] >> e[i][0], --e[i][1], --e[i][2]; 31 | sort(e, e+m); 32 | iota(p, p+n, 0); 33 | while(m--) 34 | if(join(e[m][1], e[m][2])) 35 | ans+=e[m][0]; 36 | cout << ans; 37 | } 38 | -------------------------------------------------------------------------------- /Codeforces/1008E.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - log^2 worst case but who cares 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | #define ll long long 9 | 10 | inline int qry(ll x, ll y) { 11 | cout << x << " " << y << endl; 12 | int ans; 13 | cin >> ans; 14 | if(!ans) 15 | exit(0); 16 | return ans; 17 | } 18 | 19 | int main() { 20 | ios_base::sync_with_stdio(0); 21 | cin.tie(0); 22 | 23 | ll n; 24 | cin >> n; 25 | ll a=1, b=1, c=n, d=n; 26 | while(aa&&f>b) { 37 | if(e-a>f-b) { 38 | ll m=(a+e)/2; 39 | ans=qry(m, f); 40 | if(ans==1) 41 | a=m+1; 42 | else if(ans==2) { 43 | b=f+1; 44 | break; 45 | } else 46 | e=m; 47 | } else { 48 | ll m=(b+f)/2; 49 | ans=qry(e, m); 50 | if(ans==1) { 51 | a=e+1; 52 | break; 53 | } else if(ans==2) 54 | b=m+1; 55 | else 56 | f=m; 57 | } 58 | } 59 | if(e<=a) 60 | d=f-1; 61 | else if(f<=b) 62 | c=e-1; 63 | } 64 | qry(a, b); 65 | } 66 | -------------------------------------------------------------------------------- /Codeforces/1023D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=2e5; 5 | int n, q, a[18][mxN], fo[mxN+1], lo[mxN+1]; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n >> q; 12 | bool b=0; 13 | for(int i=0; i> a[0][i]; 15 | for(int i=0; i=0; --i) { 34 | if(!a[0][i]) 35 | a[0][i]=a[0][i+1]; 36 | fo[a[0][i]]=i; 37 | } 38 | for(int i=0; i 3 | using namespace std; 4 | 5 | #define ll long long 6 | 7 | const int mxW=12, mx3W=531441; 8 | int w, n, m, a, c[1< mp{{'A', 3}, {'O', 1}, {'X', 5}, {'a', 4}, {'o', 6}, {'x', 2}}; 12 | 13 | ll rec(int i, int a) { 14 | if(i>=w) 15 | return d[a]; 16 | ll r=0; 17 | for(int j=0; j<3; ++j) 18 | if(e[i]>>j&1) 19 | r+=rec(i+1, a+j*p3[w-1-i]); 20 | return r; 21 | } 22 | 23 | int main() { 24 | ios_base::sync_with_stdio(0); 25 | cin.tie(0); 26 | 27 | cin >> w >> n >> m; 28 | while(n--) { 29 | cin >> a; 30 | ++b[a]; 31 | } 32 | p3[0]=1; 33 | for(int i=1; i>j)%2*p3[j]; 38 | for(int i=0; i<1<> g; 43 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e6, M=1e9+7; 7 | int n, p[mxN]; 8 | ll a[mxN], b[mxN+1], c[mxN+1]; 9 | 10 | int main() { 11 | ios_base::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n; 15 | for(int i=0; i> a[i]; 17 | for(int i=1; i> p[i], --p[i]; 19 | for(int i=n-1; i; --i) 20 | a[p[i]]+=a[i]; 21 | for(int i=0; i=i) { 31 | c[i]=1; 32 | for(int j=i; (j+=i)<=n; ) 33 | c[i]=(c[j]+c[i])%M; 34 | } 35 | } 36 | cout << c[1]; 37 | } 38 | -------------------------------------------------------------------------------- /Codeforces/1036F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | int t, mc[60]; 7 | ll n, rb; 8 | 9 | ll a(int p) { 10 | ll lb=1; 11 | while(lb> t; 38 | while(t--) { 39 | cin >> n; 40 | ll ans=n-1; 41 | rb=(ll)1e9; 42 | for(int i=2; i<60; ++i) 43 | if(mc[i]) 44 | ans+=mc[i]*a(i); 45 | cout << ans << "\n"; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Codeforces/1063D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | ll n, k, l, r, ans=-1; 7 | 8 | int main() { 9 | ios_base::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n >> l >> r >> k, r=(r-l+n)%n; 13 | if(n<3e5) { 14 | auto c=[&](ll i, int a, int b) { 15 | ll j=(k-r-a+n+i+b)%(n+i+b); 16 | if(j<=min(i, r)&&i<=j+n-1-r&&k>=r+a+j) 17 | ans=max(i+b, ans); 18 | }; 19 | for(ll i=0; i<=n-1; ++i) { 20 | c(i, 1, 0); 21 | c(i, 1, 1); 22 | c(i, 2, 1); 23 | } 24 | } else { 25 | for(int i=1; i<=2; ++i) 26 | for(int j=0; j 2 | using namespace std; 3 | 4 | const int mxN=1e3; 5 | int n, a[mxN], a1, lcc=-1, r; 6 | bool vis[mxN]; 7 | vector cs[mxN]; 8 | 9 | int main() { 10 | ios_base::sync_with_stdio(0); 11 | cin.tie(0); 12 | 13 | cin >> n; 14 | for(int i=0; i> a[i], --a[i]; 16 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=5e5, B=69; 7 | int n, dp[mxN], ans; 8 | ll pB[mxN+1], ps[mxN+1]; 9 | string s; 10 | unordered_set us; 11 | 12 | ll qry(int l, int r) { 13 | return ps[r+1]-ps[l]*pB[r-l+1]; 14 | } 15 | 16 | int main() { 17 | ios_base::sync_with_stdio(0); 18 | cin.tie(0); 19 | 20 | cin >> n >> s; 21 | pB[0]=1; 22 | for(int i=0; i=0; ans=max(dp[i], ans), --i) { 27 | dp[i]=i1&&us.find(qry(i, i+dp[i]-2))==us.end()&&us.find(qry(i+1, i+dp[i]-1))==us.end()) { 29 | --dp[i]; 30 | for(int j=dp[i+dp[i]]; j&&us.insert(qry(i+dp[i], i+dp[i]+j-1)).second; --j); 31 | } 32 | } 33 | cout << ans; 34 | } 35 | -------------------------------------------------------------------------------- /Codeforces/1065G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=200; 7 | int n, m; 8 | ll k, dp[mxN+1]; 9 | string fp[mxN+1], fs[mxN+1]; 10 | 11 | int main() { 12 | ios_base::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> n >> k >> m; 16 | fp[0]=fs[0]="0"; 17 | fp[1]=fs[1]="1"; 18 | for(int i=2; i<=n; ++i) { 19 | fp[i]=fp[i-2]+fp[i-1]; 20 | fp[i]=fp[i].substr(0, m); 21 | } 22 | for(int i=2; i<=n; ++i) { 23 | fs[i]=fs[i-2]+fs[i-1]; 24 | fs[i]=fs[i].substr(max((int)fs[i].size()-m, 0)); 25 | } 26 | string cs=""; 27 | ++k; 28 | for(int i=1; i<=m; ++i) { 29 | if(cs==fs[n].substr(max((int)fs[n].size()-i+1, 0))) 30 | --k; 31 | if(!k) 32 | break; 33 | cs+='0'; 34 | dp[0]=i==1; 35 | for(int j=2; j<=n; ++j) { 36 | dp[j]=min(dp[j-2]+dp[j-1], (ll)1e18); 37 | for(int l=1; l<=i-1; ++l) { 38 | bool ok=fs[j-2].size()>=l&&fp[j-1].size()>=i-l; 39 | for(int o=1; o<=l&&ok; ++o) 40 | ok=fs[j-2][fs[j-2].size()-o]==cs[l-o]; 41 | for(int o=l; odp[n]) { 47 | k-=dp[n]; 48 | cs.back()='1'; 49 | } 50 | } 51 | cout << cs; 52 | } 53 | -------------------------------------------------------------------------------- /Codeforces/1067C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int n, x, y; 9 | cin >> n; 10 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | auto c=[&](string a, int b=1) { 6 | while(b--) 7 | cout << a; 8 | }; 9 | auto cl=[&](int a, int b) { 10 | c("l", a); 11 | c("r10lt", b); 12 | }; 13 | c("d"); 14 | c("d0usut0dtl", 31); 15 | c("uuu"); 16 | c("ru0dsdt0ut", 31); 17 | c("drrr0llld"); 18 | for(int i=0; i<31; ++i) { 19 | cl(i, i); 20 | c("u"); 21 | cl(i, i+1); 22 | c("d010ltutr10ltr010ltut"); 23 | cl(0, 31-i); 24 | c("l", 31-i); 25 | c("100ltutll100ltutd10utrr10utl10ltrtu10dtu10dtr10ltdlll"); 26 | } 27 | c("rrrr"); 28 | } 29 | -------------------------------------------------------------------------------- /Codeforces/1081G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e5; 7 | int n, k, q, l1, l2, c1, c2; 8 | ll iv[mxN+1], pi[mxN+1], ans; 9 | 10 | void ms(int l=0, int r=n-1, int h=k) { 11 | if(h<2||l==r) { 12 | if(!l1||r-l+1==l1) { 13 | l1=r-l+1; 14 | ++c1; 15 | } else { 16 | l2=r-l+1; 17 | ++c2; 18 | } 19 | return; 20 | } 21 | int m=(l+r)/2; 22 | ms(l, m, h-1); 23 | ms(m+1, r, h-1); 24 | } 25 | 26 | int main() { 27 | ios::sync_with_stdio(0); 28 | cin.tie(0); 29 | 30 | cin >> n >> k >> q; 31 | iv[1]=1; 32 | for(int i=2; i<=n; ++i) 33 | iv[i]=(q-q/i)*iv[q%i]%q; 34 | for(int i=1; i<=n; ++i) 35 | pi[i]=(pi[i-1]+iv[i])%q; 36 | ms(); 37 | if(c1>1) 38 | for(int i=1; i<=l1; ++i) 39 | ans+=(pi[l1+i]-pi[i]+q)*c1%q*(c1-1)%q; 40 | if(c2>1) 41 | for(int i=1; i<=l2; ++i) 42 | ans+=(pi[l2+i]-pi[i]+q)*c2%q*(c2-1)%q; 43 | for(int i=1; i<=l1; ++i) 44 | ans+=(pi[l2+i]-pi[i]+q)*c1%q*c2%q*2; 45 | cout << (iv[2]*n%q*(n-1)%q-ans%q+q)*iv[2]%q; 46 | } 47 | -------------------------------------------------------------------------------- /Codeforces/1091H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxA=2e5; 5 | int n, f, bi, wi, ri, ans, g[mxA]; 6 | bitset c, t, d[99]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n >> f; 13 | for(int i=2; i<=mxA; ++i) { 14 | if(c[i]) 15 | continue; 16 | for(long long j=(long long)i*i; j<=mxA; j+=i) 17 | c[j]=1; 18 | for(int j=1; j<=i&&i*j<=mxA; ++j) 19 | if(!c[j]) 20 | t[i*j]=1; 21 | } 22 | t[f]=0; 23 | for(int i=0; i> bi >> wi >> ri; 30 | ans^=g[wi-bi-1]^g[ri-wi-1]; 31 | } 32 | for(int i=0; i<2; ++i) 33 | cout << ((ans>0)^i?"Alice\n":"Bob\n"); 34 | } 35 | -------------------------------------------------------------------------------- /Codeforces/1096E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | int p, s, r, M=998244353; 6 | ll iv[5100], f1[5100], f2[5100], ans; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | iv[1]=f1[0]=f1[1]=f2[0]=f2[1]=1; 13 | for(int i=2; i<5100; ++i) { 14 | iv[i]=(M-M/i)*iv[M%i]%M; 15 | f1[i]=f1[i-1]*i%M; 16 | f2[i]=f2[i-1]*iv[i]%M; 17 | } 18 | cin >> p >> s >> r; 19 | for(int i=1; i<=p&&s-r*i>=0; ++i) 20 | ans+=(i&1?1:-1)*f1[s-r*i+p-1]*f2[s-r*i]%M*f2[i]%M*f2[p-i]%M; 21 | ans=(ans%M+M)*f1[p-1]%M*f1[s-r]%M*f2[s-r+p-1]%M; 22 | cout << ans; 23 | } 24 | -------------------------------------------------------------------------------- /Codeforces/1097F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5, mxV=7000; 5 | int n, q, qt, xi, yi, zi; 6 | bitset in[mxV+1], cq[mxV+1], ms[mxN+1], c; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | for(int i=1; i<=mxV; ++i) { 13 | for(int j=0; (j+=i)<=mxV; ) 14 | in[j][i]=1; 15 | for(int j=i; j<=mxV; ++j) { 16 | if(j==i?!c[j]:c[j]) { 17 | for(int k=0; (k+=j)<=mxV; ) 18 | c.flip(k); 19 | cq[i][j]=1; 20 | } 21 | } 22 | c.reset(); 23 | } 24 | cin >> n >> q; 25 | while(q--) { 26 | cin >> qt; 27 | if(qt==1) { 28 | cin >> xi >> yi, --xi; 29 | ms[xi]=in[yi]; 30 | } else if(qt==2) { 31 | cin >> xi >> yi >> zi, --xi, --yi, --zi; 32 | ms[xi]=ms[yi]^ms[zi]; 33 | } else if(qt==3) { 34 | cin >> xi >> yi >> zi, --xi, --yi, --zi; 35 | ms[xi]=ms[yi]&ms[zi]; 36 | } else { 37 | cin >> xi >> yi, --xi; 38 | cout << ((ms[xi]&cq[yi]).count()&1); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Codeforces/1098D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | int q, x, f; 7 | char qt; 8 | ll ss[30]; 9 | priority_queue, greater> p1[30], p2[30]; 10 | 11 | int main() { 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> q; 16 | while(q--) { 17 | cin >> qt >> x; 18 | int k=31-__builtin_clz(x), ans=0; 19 | if(qt=='+') { 20 | ss[k]+=x; 21 | p1[k].push(x); 22 | ++f; 23 | } else { 24 | ss[k]-=x; 25 | p2[k].push(x); 26 | --f; 27 | } 28 | ll ps=0; 29 | for(int i=0; i<30; ps+=ss[i++]) { 30 | while(p1[i].size()&&p2[i].size()&&p1[i].top()==p2[i].top()) { 31 | p1[i].pop(); 32 | p2[i].pop(); 33 | } 34 | if(p1[i].size()) 35 | ans+=p1[i].top()>2*ps; 36 | } 37 | cout << f-ans << "\n"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Codeforces/1105E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | int n, m, qt, dp[1<<20]; 7 | map mp; 8 | vector v; 9 | ll b[40]; 10 | 11 | int a() { 12 | string s; 13 | cin >> s; 14 | if(mp.find(s)==mp.end()) 15 | mp.insert({s, mp.size()}); 16 | return mp[s]; 17 | } 18 | 19 | int cdp(ll i) { 20 | int ans=i<1<<20?dp[i]:0; 21 | if(!ans&&i) { 22 | int j=63-__builtin_clzll(i); 23 | ans=max(cdp(i^1ll<> n >> m; 35 | for(int i=0; i> qt; 37 | if(qt==1) 38 | v.push_back(0); 39 | else 40 | v.back()|=1ll<>i&1&&vi>>j&1) 46 | b[i]|=1ll< 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=500; 7 | int n, p[mxN]; 8 | ll a[mxN], b[mxN], c[mxN], dp[mxN+1][mxN+1]; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n; 15 | for(int i=0; i> a[i] >> b[i] >> c[i]; 17 | iota(p, p+n, 0); 18 | sort(p, p+n, [&](const int &i, const int &j) { 19 | return b[i]>b[j]; 20 | }); 21 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=3e5; 7 | int n, x, p[mxN]; 8 | ll c[mxN+1], d[mxN], ans, ms[mxN], mp[mxN], ts[mxN]; 9 | array e[mxN]; 10 | 11 | int find(int x) { 12 | return x==p[x]?x:(p[x]=find(p[x])); 13 | } 14 | 15 | void join(int x, int y, ll d) { 16 | x=find(x); 17 | y=find(y); 18 | ans=max(ms[x]+mp[y]+d, ans); 19 | p[y]=x; 20 | ms[x]=max(ms[y], ts[y]+ms[x]); 21 | mp[x]=max(mp[x], ts[x]+mp[y]); 22 | ts[x]+=ts[y]; 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | 29 | cin >> n >> x; 30 | iota(p, p+n, 0); 31 | for(int i=0; i> d[i] >> c[i]; 33 | ms[i]=mp[i]=ts[i]=c[i]=x-c[i]; 34 | ans=max(c[i], ans); 35 | } 36 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e6, M=1e9+7; 7 | int n, m; 8 | ll iv[mxN+1], f1[mxN+1], f2[mxN+1], pn[mxN+1], pm[mxN+1], ans; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n >> m; 15 | iv[1]=1; 16 | for(int i=2; i<=mxN; ++i) 17 | iv[i]=M-M/i*iv[M%i]%M; 18 | f1[0]=f2[0]=pn[0]=pm[0]=1; 19 | for(int i=1; i<=mxN; ++i) { 20 | f1[i]=f1[i-1]*i%M; 21 | f2[i]=f2[i-1]*iv[i]%M; 22 | pn[i]=pn[i-1]*n%M; 23 | pm[i]=pm[i-1]*m%M; 24 | } 25 | for(int i=1; i<=min(n-1, m); ++i) 26 | ans+=f1[m-1]*f2[i-1]%M*f2[m-i]%M*f1[n-2]%M*f2[n-1-i]%M*pm[n-1-i]%M*(i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2.5e5, mxM=1e7; 7 | int n, m, q, h[mxM], l[mxM], r[mxM]; 8 | vector> a[mxN]; 9 | ll c[mxM], dp[mxM+1], rm[mxM]; 10 | 11 | int main() { 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> n >> m; 16 | for(int i=0, k; i> k; 18 | a[i].resize(k); 19 | for(int b : {0, 1}) 20 | for(int j=0; j> a[i][j][b]; 22 | } 23 | cin >> q; 24 | for(int i=0, x, y; q--; ) { 25 | cin >> x >> y, --x; 26 | for(int j=0; j=0&&i-l[i]=0; --i) { 36 | rm[i]=dp[i+1]; 37 | r[i]=i+1; 38 | while(r[i] 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e6; 7 | int n, k, a[mxN], l[mxN], p[mxN], ld[mxN], lb[mxN]; 8 | 9 | int find(int x) { 10 | return x==p[x]?x:(p[x]=find(p[x])); 11 | } 12 | 13 | void join(int x, int y) { 14 | p[x]=y; 15 | lb[y]=lb[x]; 16 | ld[y]+=ld[x]; 17 | } 18 | 19 | void upd1(int i, int j) { 20 | i=find(i); 21 | ++ld[i]; 22 | if(j=0&&lb[i]) 25 | join(lb[i]-1, i); 26 | } 27 | 28 | void upd2(int i) { 29 | if(ii-k) 33 | join(lb[j]-1, j); 34 | } 35 | 36 | int main() { 37 | ios::sync_with_stdio(0); 38 | cin.tie(0); 39 | 40 | cin >> n >> k; 41 | for(int i=0; i> a[i]; 43 | iota(p, p+n, 0); 44 | iota(lb, lb+n, 0); 45 | for(int i=0; i=0&&a[l[i]]=k-1) 52 | cout << ld[find(0)] << " "; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Codeforces/1172C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2e5, mxM=3e3, M=998244353; 7 | int n, m, a[mxN], w[mxN], wt[2]; 8 | ll dp[mxM+1][mxM+1], b[2]; 9 | 10 | ll iv(ll a, ll m) { 11 | return a<=1?1:(1-iv(m%a, a)*m)/a+m; 12 | } 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); 16 | cin.tie(0); 17 | 18 | cin >> n >> m; 19 | for(int i=0; i> a[i]; 21 | for(int i=0; i> w[i], wt[a[i]]+=w[i]; 23 | dp[0][0]=1; 24 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=1e3; 5 | int n, r[mxN], c[mxN]; 6 | vector> ans; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n; 13 | for(int i=0, a; i> a, r[a-1]=i; 15 | for(int i=0, a; i> a, c[a-1]=i; 17 | for(int i=n-1; i; --i) { 18 | int j1=find(r, r+i+1, i)-r, j2=find(c, c+i+1, i)-c; 19 | swap(r[i], r[j1]); 20 | swap(c[i], c[j2]); 21 | if(i^j1||i^j2) 22 | ans.push_back({j1, i, i, j2}); 23 | } 24 | cout << ans.size() << "\n"; 25 | for(auto a : ans) 26 | cout << a[0]+1 << " " << a[1]+1 << " " << a[2]+1 << " " << a[3]+1 << "\n"; 27 | } 28 | -------------------------------------------------------------------------------- /Codeforces/1182F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int B=3e4; 5 | int t, a, b, p, q; 6 | array c[B]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> t; 13 | while(t--) { 14 | cin >> a >> b >> p >> q; 15 | for(int i=0; i ans{q, b}; 22 | for(; a+B<=b; a+=B) { 23 | int x=(3ll*q-2ll*p*a%(2*q))%(2*q), p1=(lower_bound(c, c+B, array{x})-c)%B, p2=(upper_bound(c, c+B, array{x})-c+B-1)%B; 24 | ans=min({array{(int)abs(q-(c[p1][0]+2ll*p*a)%(2*q)), c[p1][1]+a}, array{(int)abs(q-(c[p2][0]+2ll*p*a)%(2*q)), c[p2][1]+a}, ans}); 25 | } 26 | for(; a<=b; ++a) 27 | ans=min(array{(int)abs(q-2ll*p*a%(2*q)), a}, ans); 28 | cout << ans[1] << "\n"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Codeforces/1205E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e5, M=1e9+7; 7 | int n, k; 8 | ll pk[mxN], mb[mxN], ans; 9 | bool c[mxN]; 10 | 11 | ll iv(ll a, ll m) { 12 | return a<2?1:(1-iv(m%a, a)*m)/a+m; 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | cin >> n >> k; 20 | pk[1]=iv(k, M); 21 | for(int i=2; i 2 | using namespace std; 3 | 4 | const int mxN=1e6; 5 | int n, k, p[mxN+1]; 6 | bool c[mxN+1]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n >> k; 13 | iota(p, p+n+1, 0); 14 | for(int i=2; i<=n; ++i) { 15 | if(c[i]) 16 | continue; 17 | for(int j=i; j<=n; j+=i) { 18 | c[j]=1; 19 | p[j]=p[j]/i*(i-1); 20 | } 21 | } 22 | sort(p+1, p+n+1); 23 | cout << (k<2?3:accumulate(p+1, p+k+3, 0ll)); 24 | } 25 | -------------------------------------------------------------------------------- /Codeforces/1225F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, p[mxN], a1[mxN], a1i; 6 | vector adj[mxN], a2; 7 | array d[mxN]; 8 | 9 | void dfs(int u=0) { 10 | d[u][1]=u; 11 | for(int v : adj[u]) { 12 | d[v][0]=d[u][0]+1; 13 | dfs(v); 14 | } 15 | for(int v : adj[u]) 16 | d[u]=max(d[v], d[u]); 17 | sort(adj[u].begin(), adj[u].end(), [](const int &i, const int &j) { 18 | return d[i]> n; 27 | for(int i=1; i> p[i]; 29 | adj[p[i]].push_back(i); 30 | } 31 | dfs(); 32 | int u=d[0][1]; 33 | a1[--(a1i=n)]=u; 34 | while(u) { 35 | if(adj[p[u]].size()<2) { 36 | u=p[u]; 37 | a1[--a1i]=u; 38 | } else { 39 | adj[p[u]].pop_back(); 40 | p[u]=adj[p[u]].back(); 41 | adj[p[u]].push_back(u); 42 | a2.push_back(u); 43 | } 44 | } 45 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=16, mxA=2000; 5 | int n, k, a[mxN]; 6 | bitset dp[1< v[151]; 8 | 9 | int main() { 10 | ios::sync_with_stdio(0); 11 | cin.tie(0); 12 | 13 | cin >> n >> k; 14 | for(int i=0; i> a[i]; 16 | dp[0][0]=1; 17 | for(int i=0; i<1<>j&1^1) 22 | dp[i^1<>m&1^1||j 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 8 | 9 | const int mxN=2e5; 10 | int n, a[mxN], m, ans[mxN]; 11 | array b[mxN]; 12 | vector> c[mxN]; 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); 16 | cin.tie(0); 17 | 18 | cin >> n; 19 | for(int i=0; i> a[i]; 21 | b[i]={-a[i], i}; 22 | } 23 | sort(b, b+n); 24 | cin >> m; 25 | for(int i=0, k, p; i> k >> p, --k, --p; 27 | c[k].push_back({p, i}); 28 | } 29 | oset s; 30 | for(int i=0; i d : c[i]) 33 | ans[d[1]]=a[*s.find_by_order(d[0])]; 34 | } 35 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=1e3; 5 | int n, ans[mxN+1][mxN]; 6 | array a[mxN]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n; 13 | for(int i=0; i> a[i][0]; 15 | a[i][1]=i; 16 | } 17 | sort(a, a+n, greater>()); 18 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=200; 5 | int n, m, c1[mxN+1][mxN+1][mxN+1], c2[mxN+1][mxN+1][mxN+1]; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n >> m; 12 | vector oa(n), ob(m), a, b; 13 | for(int i=0; i> oa[i]; 15 | for(int i=0; i> ob[i]; 17 | int lb=-m, rb=n-1; 18 | while(lb0) { 21 | a=vector(oa.begin(), oa.end()-mb); 22 | b=ob; 23 | } else { 24 | a=oa; 25 | b=vector(ob.begin(), ob.end()+mb); 26 | } 27 | bool ok; 28 | for(int i=0; i<=a.size(); ++i) { 29 | for(int j=0; j<=b.size(); ++j) { 30 | for(int k=0; k<=b.size(); ++k) { 31 | int dp1=i?c2[i-1][j][min(k+a[a.size()-i], (int)b.size())]:0, dp2=j?max(c1[i][j-1][k]-b[b.size()-j], 0):n; 32 | c1[i][j][k]=min(dp1, i?c1[i-1][j][k]+1:n); 33 | if(i==a.size()&&j==b.size()&&!k) 34 | ok=dp1; 35 | c2[i][j][k]=max(dp2, j&&k?c2[i][j-1][k-1]:0); 36 | } 37 | } 38 | } 39 | if(ok) 40 | lb=mb; 41 | else 42 | rb=mb-1; 43 | } 44 | if(lb>=0) 45 | cout << "Gandalf " << lb; 46 | else 47 | cout << "Saruman " << -lb-1; 48 | } 49 | -------------------------------------------------------------------------------- /FacebookHackerCup/18-0-B.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | static final PrintWriter out = new PrintWriter(System.out); 7 | 8 | public static void main(String[] args) { 9 | for(int tests=in.nextInt(), ti=1; ti<=tests; ++ti) { 10 | int n=in.nextInt(); 11 | for(int i=0; i2&&k>2) { 13 | int m=Math.min(n-1, k-1), s=-k; 14 | for(int i=0; i=s.length-1?"Y":"N")); 16 | } 17 | out.close(); 18 | } 19 | 20 | static class Reader { 21 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 22 | StringTokenizer st; 23 | String next() { 24 | while(st==null||!st.hasMoreTokens()) { 25 | try { 26 | st = new StringTokenizer(br.readLine()); 27 | } catch(Exception e) {} 28 | } 29 | return st.nextToken(); 30 | } 31 | int nextInt() { 32 | return Integer.parseInt(next()); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FacebookHackerCup/19-0-B.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class B { 5 | static final Reader in = new Reader(); 6 | static final PrintWriter out = new PrintWriter(System.out); 7 | 8 | public static void main(String[] args) { 9 | int t=in.nextInt(); 10 | for(int _=1; _<=t; ++_) { 11 | char[] s=in.next().toCharArray(); 12 | int x=0; 13 | for(char c : s) 14 | x+=c=='B'?1:0; 15 | out.println("Case #"+_+": "+(x1||s.length<4&&x>0)?"Y":"N")); 16 | } 17 | out.close(); 18 | } 19 | 20 | static class Reader { 21 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 22 | StringTokenizer st; 23 | String next() { 24 | while(st==null||!st.hasMoreTokens()) { 25 | try { 26 | st = new StringTokenizer(br.readLine()); 27 | } catch(Exception e) {} 28 | } 29 | return st.nextToken(); 30 | } 31 | int nextInt() { 32 | return Integer.parseInt(next()); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FacebookHackerCup/19-2-A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class A { 5 | static final Reader in = new Reader(); 6 | static final PrintWriter out = new PrintWriter(System.out); 7 | 8 | public static void main(String[] args) { 9 | int t=in.nextInt(); 10 | for(int _=1; _<=t; ++_) { 11 | int n=in.nextInt(), m=in.nextInt(), k=in.nextInt(), a=in.nextInt(), b=in.nextInt(); 12 | int[] r = new int[k], c = new int[k]; 13 | for(int i=0; i1&&(a+b)%2==(r[0]+c[0])%2&&(a+b)%2==(r[1]+c[1])%2?"Y":"N")); 18 | } 19 | out.close(); 20 | } 21 | 22 | static class Reader { 23 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 24 | StringTokenizer st; 25 | String next() { 26 | while(st==null||!st.hasMoreTokens()) { 27 | try { 28 | st = new StringTokenizer(br.readLine()); 29 | } catch(Exception e) {} 30 | } 31 | return st.nextToken(); 32 | } 33 | int nextInt() { 34 | return Integer.parseInt(next()); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /FunctionCup/19-Chairs/king.cpp: -------------------------------------------------------------------------------- 1 | #include "king.h" 2 | #include 3 | using namespace std; 4 | 5 | long long SendInfo(vector w, vector c) { 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /FunctionCup/19-Chairs/vassal.cpp: -------------------------------------------------------------------------------- 1 | #include "vassal.h" 2 | #include 3 | using namespace std; 4 | 5 | map> mp; 6 | void Init(long long b, vector c) { 7 | for(int i=0; isecond.back(); 16 | it->second.pop_back(); 17 | if(it->second.empty()) 18 | mp.erase(it); 19 | return a; 20 | } 21 | -------------------------------------------------------------------------------- /FunctionCup/19-Cross.cpp: -------------------------------------------------------------------------------- 1 | #include "cross.h" 2 | #include 3 | using namespace std; 4 | 5 | array a[200000]; 6 | 7 | long long SelectCross(int k, vector x, vector y) { 8 | long long ans=0; 9 | for(int i=0; i()); 12 | priority_queue, greater> pq; 13 | for(int i=0; i 3 | using namespace std; 4 | 5 | int a[1000000], lb=0, rb=1000000, mb; 6 | 7 | bool chk(string &s, int l, int r, int b=0) { 8 | if(l>r) 9 | return 1; 10 | for(; s[r]=='!'; --r, ++b); 11 | return b t; 22 | for(int i=0; i 3 | using namespace std; 4 | 5 | long long CountSimilarPairs(vector b, vector t, vector g) { 6 | long long ans=0; 7 | for(int i=1; i<8; ++i) { 8 | unordered_map mp; 9 | for(int j=0; j 3 | using namespace std; 4 | 5 | vector PickUnique(int n) { 6 | vector a(1), b(1); 7 | for(int i=0; i c(n); 12 | for(int i=0; i0&&b[n-i]-b[n-1-i]>0; 14 | return c; 15 | } 16 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-0-A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int t; 9 | cin >> t; 10 | for(int ti=1; ti<=t; ++ti) { 11 | string s; 12 | cin >> s; 13 | string t(s.size(), '0'); 14 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int t, n; 9 | cin >> t; 10 | for(int ti=1; ti<=t; ++ti) { 11 | string s; 12 | cin >> n >> s; 13 | cout << "Case #" << ti << ": "; 14 | for(int i=0; i<2*(n-1); ++i) 15 | cout << (char)(s[i]^'E'^'S'); 16 | cout << "\n"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-0-D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int t, n, b, f; 9 | cin >> t; 10 | for(int ti=1; ti<=t; ++ti) { 11 | cin >> n >> b >> f; 12 | vector v{b}; 13 | for(int bs=16, pb=1024; bs; pb=bs, bs/=2) { 14 | string s(1024, '0'), t; 15 | for(int i=1; i*bs<1024; i+=2) 16 | for(int j=i*bs; j<(i+1)*bs; ++j) 17 | s[j]='1'; 18 | cout << s.substr(0, n) << endl; 19 | cin >> t; 20 | for(int i=n; i<1024; ++i) 21 | t+=s[i]; 22 | vector w; 23 | for(int i1=0, i2=0, j=0; i1<1024/pb; ++i1, i2=j) { 24 | if(pb<1024) { 25 | for(int k : {0, 1}) { 26 | w.push_back(bs); 27 | for(; j> n; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-1A-A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int t, n, m; 5 | vector> g; 6 | bool u[20][20]; 7 | 8 | bool rec(int s=0, int li=0, int lj=0) { 9 | if(s>=n*m) 10 | return 1; 11 | vector> v; 12 | for(int i=0; i vi : v) { 18 | if(s&&(li==vi[0]||lj==vi[1]||li+lj==vi[0]+vi[1]||li-lj==vi[0]-vi[1])) 19 | continue; 20 | g.push_back(vi); 21 | u[vi[0]][vi[1]]=1; 22 | if(rec(s+1, vi[0], vi[1])) 23 | return 1; 24 | g.pop_back(); 25 | u[vi[0]][vi[1]]=0; 26 | } 27 | return 0; 28 | } 29 | 30 | int main() { 31 | ios::sync_with_stdio(0); 32 | cin.tie(0); 33 | 34 | cin >> t; 35 | for(int i=1; i<=t; ++i) { 36 | cout << "Case #" << i << ": "; 37 | cin >> n >> m; 38 | if(rec()) { 39 | cout << "POSSIBLE\n"; 40 | for(array x : g) 41 | cout << x[0]+1 << " " << x[1]+1 << "\n"; 42 | } else 43 | cout << "IMPOSSIBLE\n"; 44 | g.clear(); 45 | memset(u, 0, sizeof(u)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-1A-B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int M[7]={16,9,5,7,11,13,17}; 5 | int t, n, m; 6 | bool ans[1000001]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> t >> n >> m; 13 | while(t--) { 14 | memset(ans, 0, m+1); 15 | for(int i=0; i<7; ++i) { 16 | for(int j=0; j<18; ++j) 17 | cout << M[i] << " "; 18 | cout << endl; 19 | int s=0; 20 | for(int j=0, a; j<18; ++j) 21 | cin >> a, s=(s+a)%M[i]; 22 | for(int j=1; j<=m; ++j) 23 | ans[j]|=j%M[i]^s; 24 | } 25 | for(int i=1; i<=m; ++i) 26 | if(!ans[i]) 27 | cout << i << endl; 28 | cin >> n; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-1A-C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxTS=5e4; 5 | int t, n, ts, d[mxTS], c[mxTS][26]; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> t; 12 | for(int i=1; i<=t; ++i) { 13 | cout << "Case #" << i << ": "; 14 | cin >> n; 15 | ts=1; 16 | memset(d, 0, sizeof(d)); 17 | memset(c, 0, sizeof(c)); 18 | for(int i=0; i> w; 21 | int u=0; 22 | for(int i=w.size()-1; ~i; --i) { 23 | if(!c[u][w[i]-'A']) { 24 | c[u][w[i]-'A']=ts++; 25 | } 26 | u=c[u][w[i]-'A']; 27 | } 28 | ++d[u]; 29 | } 30 | int ans=0; 31 | for(int i=ts-1; i; --i) { 32 | for(int j=0; j<26; ++j) 33 | if(c[i][j]) 34 | d[i]+=d[c[i][j]]; 35 | if(d[i]>=2) { 36 | ans+=2; 37 | d[i]-=2; 38 | } 39 | } 40 | cout << ans << "\n"; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-1B-A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | auto f=[](vector a, vector b) { 9 | sort(a.begin(), a.end()); 10 | sort(b.begin(), b.end()); 11 | array r{}; 12 | for(int i1=0, i2=0; i1{i1+b.size()-i2, -a[i1]}, r); 16 | } 17 | return -r[1]; 18 | }; 19 | int t, p, q; 20 | cin >> t; 21 | for(int ti=1; ti<=t; ++ti) { 22 | cin >> p >> q; 23 | vector vn{0}, vs{q+1}, ve{0}, vw{q+1}; 24 | for(int xi, yi; p--; ) { 25 | char ci; 26 | cin >> xi >> yi >> ci; 27 | if(ci=='N') 28 | vn.push_back(yi+1); 29 | else if(ci=='S') 30 | vs.push_back(yi-1); 31 | else if(ci=='E') 32 | ve.push_back(xi+1); 33 | else 34 | vw.push_back(xi-1); 35 | } 36 | cout << "Case #" << ti << ": " << f(ve, vw) << " " << f(vn, vs) << "\n"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-1B-B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int t, w, a[6]; 9 | cin >> t >> w; 10 | for(long long n; t--; ) { 11 | cout << 224 << endl; 12 | cin >> n; 13 | a[3]=n>>56; 14 | a[4]=n>>44&127; 15 | a[5]=n>>37&127; 16 | cout << 56 << endl; 17 | cin >> n; 18 | n-=a[3]<<14; 19 | n-=a[4]<<11; 20 | n-=a[5]<<9; 21 | a[0]=n>>56; 22 | a[1]=n>>28&127; 23 | a[2]=n>>18&127; 24 | for(int i=0; i<6; ++i) 25 | cout << a[i] << " "; 26 | cout << endl; 27 | cin >> n; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /GoogleCodeJam/19-1C-A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int t, n; 9 | cin >> t; 10 | for(int ti=1; ti<=t; ++ti) { 11 | cin >> n; 12 | string a[n], b[n]; 13 | for(int i=0; i> b[i]; 15 | while(a[i].size() 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int t, f; 9 | cin >> t >> f; 10 | for(string ans, ve; t--; ) { 11 | vector v(119); 12 | iota(v.begin(), v.end(), 0); 13 | bool u[5]={}; 14 | for(int i=0; i<5; ++i) { 15 | vector w[5]; 16 | for(int vi : v) { 17 | cout << vi*5+i+1 << endl; 18 | cin >> ve; 19 | w[ve[0]-'A'].push_back(vi); 20 | } 21 | int mi=-1; 22 | for(int j=0; j<5; ++j) 23 | if(!u[j]&&(mi==-1||w[j].size()> ve; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /GoogleKickStart/19A-A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int t, n, p, s[100000]; 5 | 6 | int main() { 7 | ios::sync_with_stdio(0); 8 | cin.tie(0); 9 | 10 | cin >> t; 11 | for(int ti=1; ti<=t; ++ti) { 12 | cin >> n >> p; 13 | for(int i=0; i> s[i]; 15 | sort(s, s+n); 16 | int a=0; 17 | for(int i=0; i 2 | using namespace std; 3 | 4 | int t, l, r; 5 | vector v; 6 | 7 | int s(int r) { 8 | return upper_bound(v.begin(), v.end(), r)-v.begin(); 9 | } 10 | 11 | int solve() { 12 | cin >> l >> r; 13 | return (l<=1)+(l<=8&&8<=r)+(l<=4&&4<=r)+s(r)-s(l-1)+s(r/4)-s((l+3)/4-1)+r/2-(l-1)/2-r/4+(l-1)/4; 14 | } 15 | 16 | struct segmentedsieve { 17 | void ac(int n, auto f) { 18 | int s=1; 19 | while(s*s c(s+1), v; 22 | for(int i=2; i<=s; ++i) { 23 | if(c[i]) 24 | continue; 25 | f(i); 26 | v.push_back(i); 27 | for(int j=i*i; j<=s; j+=i) 28 | c[j]=1; 29 | } 30 | for(int i1=s+1, i2=2*s; i1<=n; i1+=s, i2+=s) { 31 | c=vector(s); 32 | for(int p : v) 33 | for(int j=(i1+p-1)/p; j*p<=i2; ++j) 34 | c[j*p-i1]=1; 35 | for(int j=i1; j<=i2; ++j) 36 | if(!c[j-i1]&&j<=n) 37 | f(j); 38 | } 39 | } 40 | } ss; 41 | 42 | int main() { 43 | ios::sync_with_stdio(0); 44 | cin.tie(0); 45 | 46 | ss.ac(1e9, [](const int &i) { 47 | v.push_back(i); 48 | }); 49 | v.erase(v.begin()); 50 | 51 | cin >> t; 52 | for(int i=1; i<=t; ++i) 53 | cout << "Case #" << i << ": " << solve() << "\n"; 54 | } 55 | -------------------------------------------------------------------------------- /HackerRank/beautiful-pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1000; 5 | int n, a[mxN], b[mxN], c1[mxN+1], c2[mxN+1]; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n; 12 | for(int i=0; i> a[i], ++c1[a[i]]; 14 | for(int i=0; i> b[i], ++c2[b[i]]; 16 | bool ch=0; 17 | for(int i=1; i<=mxN&&!ch; ++i) { 18 | if(c2[i] 7 | using namespace std; 8 | 9 | inline int gcd(int a, int b) { 10 | while((a%=b)&&(b%=a)); 11 | return a^b; 12 | } 13 | 14 | int n, a; 15 | 16 | int main() { 17 | ios_base::sync_with_stdio(0); 18 | cin.tie(0); 19 | 20 | cin >> n; 21 | ++n; 22 | for(int i=1; i 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int ax, ay, bx, by, cx, cy; 9 | cin >> ax >> ay >> bx >> by >> cx >> cy; 10 | array d[3]={{ax+bx-2*cx, ay+by-2*cy}, {ax+cx-2*bx, ay+cy-2*by}, {bx+cx-2*ax, by+cy-2*ay}}; 11 | sort(d, d+3); 12 | for(int i=0; i<3; ++i) 13 | cout << d[i][0] << " " << d[i][1] << "\n"; 14 | } 15 | -------------------------------------------------------------------------------- /HackerRank/ioi-2014-practice-contest-2/guardians-lunatics-ioi14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=8000; 7 | int n, k; 8 | ll ps[mxN+1], dp[2][mxN+1]; 9 | 10 | inline void cdp(int a, int l1, int r1, int l2, int r2) { 11 | if(l1>r1) 12 | return; 13 | int m1=(l1+r1)/2, mi; 14 | for(int i=l2; i<=m1&&i<=r2; ++i) { 15 | if(dp[a^1][i]+(m1-i)*(ps[m1]-ps[i])> n >> k; 29 | for(int i=0; i> ps[i+1], ps[i+1]+=ps[i]; 31 | memset(dp[0], 0x3F, 8*(n+1)); 32 | dp[0][0]=0; 33 | for(int i=1; i<=k; ++i) { 34 | memset(dp[i&1], 0x3F, 8*(n+1)); 35 | cdp(i&1, 0, n, 0, n); 36 | } 37 | cout << dp[k&1][n]; 38 | } 39 | -------------------------------------------------------------------------------- /HackerRank/noi-ph-2019/aswang-immortal-trivia.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | static final PrintWriter out = new PrintWriter(System.out); 7 | 8 | public static void main(String[] args) { 9 | int n=in.nextInt(), m=in.nextInt(); 10 | TreeSet s = new TreeSet(); 11 | s.add(0); 12 | s.add(n+1); 13 | long ans=(long)n*(n+1)/2; 14 | while(m-->0) { 15 | int a=in.nextInt(), b=s.lower(a), c=s.higher(a); 16 | ans-=(long)(c-a)*(a-b); 17 | s.add(a); 18 | out.println(ans); 19 | } 20 | out.close(); 21 | } 22 | 23 | static class Reader { 24 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 25 | StringTokenizer st; 26 | String next() { 27 | while(st==null||!st.hasMoreTokens()) { 28 | try { 29 | st = new StringTokenizer(br.readLine()); 30 | } catch(Exception e) {} 31 | } 32 | return st.nextToken(); 33 | } 34 | int nextInt() { 35 | return Integer.parseInt(next()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /HackerRank/noi-ph-2019/lots-of-cookies.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | static final Reader in = new Reader(); 6 | static final PrintWriter out = new PrintWriter(System.out); 7 | 8 | public static void main(String[] args) { 9 | int q=in.nextInt(); 10 | long a=in.nextLong(), b=in.nextLong(); 11 | long[] c = new long[1000001]; 12 | for(int i=1; i<=1000000; ++i) 13 | for(int j=1; i*j<=1000000; ++j) { 14 | long x=a*j*j^b*j; 15 | c[Math.min(1000000, i*(j+1)-1)]+=x; 16 | c[i*j-1]-=x; 17 | } 18 | for(int i=1000000; i>1; --i) 19 | c[i-1]+=c[i]; 20 | while(q-->0) { 21 | int d=in.nextInt(); 22 | out.println(c[d]); 23 | } 24 | out.close(); 25 | } 26 | 27 | static class Reader { 28 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 29 | StringTokenizer st; 30 | String next() { 31 | while(st==null||!st.hasMoreTokens()) { 32 | try { 33 | st = new StringTokenizer(br.readLine()); 34 | } catch(Exception e) {} 35 | } 36 | return st.nextToken(); 37 | } 38 | int nextInt() { 39 | return Integer.parseInt(next()); 40 | } 41 | long nextLong() { 42 | return Long.parseLong(next()); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /HackerRank/noi-ph-2019/ngc.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | int[] d={0, 0, 0, 0, 2}; 8 | String[][] a={{"23", "25", "27"}, {"segmented", "plain", "reeded"}, {"waling-waling", "tayabak", "kapa-kapa"}}; 9 | String[] b={"1", "5", "10", "APPRECIATE YOUR MONEY"}; 10 | for(int i=0; i<3; ++i) { 11 | String s=in.next(); 12 | for(int j=0; j<3; ++j) 13 | if(a[i][j].equals(s)) 14 | ++d[j+1]; 15 | } 16 | for(int i=0; d[i]<2; ++i) 17 | if(d[i+1]>1) 18 | System.out.println(b[i]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HackerRank/world-codesprint-13/balanced-sequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - You can prove that the maximum answer is 2, so you just need to check if the answer is 0 or 1 3 | - b = sum('('->+1, ')'->-1), mb = min(b over all prefixes) 4 | - If b = mb = 0, the string is already balanced 5 | - If b = 0 and mb < 0, the answer is 2 6 | - If b > 0, the answer is 1 if mb == 0 7 | - If mb < 0 but b>0, the answer can't be 1 because a flip will only increase both or decrease both 8 | - If b < 0, the answer is 1 if mb == b 9 | - 1 flip can only increase b and mb by the same amount 10 | */ 11 | 12 | #include 13 | using namespace std; 14 | 15 | int main() { 16 | int n, b=0, mb=0; 17 | string s; 18 | cin >> n >> s; 19 | for(int i=0; i=0?0:(b>0&&mb>=0||b<0&&mb>=b?1:2)); 24 | } 25 | -------------------------------------------------------------------------------- /IOI/03-Maintain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ar array 5 | 6 | const int mxN=200; 7 | int n, w, p[mxN]; 8 | vector> e; 9 | 10 | int find(int x) { 11 | return x^p[x]?p[x]=find(p[x]):x; 12 | } 13 | 14 | bool join(int x, int y) { 15 | if((x=find(x))==(y=find(y))) 16 | return 0; 17 | p[x]=y; 18 | return 1; 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(0); 23 | cin.tie(0); 24 | 25 | cin >> n >> w; 26 | for(int a, b, c; w--; ) { 27 | cin >> a >> b >> c, --a, --b; 28 | e.push_back({c, a, b}); 29 | sort(e.begin(), e.end()); 30 | vector> e2; 31 | int ans=0; 32 | iota(p, p+n, 0); 33 | for(auto a : e) { 34 | if(join(a[1], a[2])) { 35 | ans+=a[0]; 36 | e2.push_back(a); 37 | } 38 | } 39 | e=e2; 40 | cout << (e.size()x) 33 | u=anc[u][i]; 34 | return c[u]; 35 | } 36 | -------------------------------------------------------------------------------- /IOI/12-Supper/advisor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - n bits max 3 | */ 4 | 5 | #include "advisor.h" 6 | #include 7 | using namespace std; 8 | 9 | const int mxN=1e5; 10 | int a[2*mxN], nxt[2*mxN], d[mxN]; 11 | bool b[2*mxN]; 12 | 13 | void ComputeAdvice(int *c, int n, int k, int m) { 14 | iota(a, a+k, 0); 15 | memcpy(a+k, c, 4*n); 16 | map mp; 17 | for(int i=0; i> s; 24 | for(int i=0; ii) { 28 | array c=*--s.end(); 29 | s.erase(--s.end()); 30 | b[c[1]]=1; 31 | } else 32 | s.erase(s.begin()); 33 | s.insert({nxt[i], i}); 34 | } 35 | memset(d, -1, 4*n); 36 | vector v; 37 | for(int i=0; i 3 | using namespace std; 4 | 5 | const int mxN=1e5; 6 | bool e[mxN]; 7 | 8 | void Assist(unsigned char *a, int n, int k, int r) { 9 | vector v; 10 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | 7 | const int mxN1=5e4, mxN2=1e6; 8 | int a, b, t, x[mxN1], y[mxN1]; 9 | pair ty[mxN2]; 10 | priority_queue pq; 11 | 12 | inline bool can(int m) { 13 | pq = priority_queue(); 14 | for(int i1=0, i2=0; i1=0&&ty[i].fi>=(a?x[a-1]:0); pq.push(ty[i--].se)); 19 | for(int i1=b-1; i1>=0&&!(!pq.empty()&&pq.top()>=y[i1]); --i1) 20 | for(int j=0; j> a >> b >> t; 29 | for(int i=0; i> x[i]; 31 | sort(x, x+a); 32 | for(int i=0; i> y[i]; 34 | sort(y, y+b); 35 | for(int i=0; i> ty[i].fi >> ty[i].se; 37 | sort(ty, ty+t); 38 | int l=1, r=t+1; 39 | while(l<=r) { 40 | int m=(l+r)/2; 41 | if(can(m)) 42 | r=m-1; 43 | else 44 | l=m+1; 45 | } 46 | cout << (l>t?-1:l); 47 | } 48 | -------------------------------------------------------------------------------- /IOI/14-Friend.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - I'm not entirely sure what I did, it just works :/ 3 | - Use a dp for maximum weighted independent set on a tree 4 | - If p[i]=1, just add dp[i] to dp[h[i]] as you can consider them as being the same 5 | - The edges with p[i]=2 form cliques 6 | - In each clique, you can only take one node 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | const int mxN=1e5; 13 | int dp[mxN][2]; 14 | 15 | int findSample(int n, int c[], int h[], int p[]) { 16 | for(int i=0; i 3 | using namespace std; 4 | 5 | const int mxN=1.5e3; 6 | int n, d[mxN]; 7 | bool adj[mxN][mxN], c[mxN]={1}; 8 | 9 | void initialize(int n) { 10 | ::n=n; 11 | for(int i=0; i 3 | using namespace std; 4 | 5 | int stl[1<<22], str[1<<22]; 6 | 7 | void app(int i, int xl, int xr) { 8 | stl[i]=min(max(stl[i], xl), xr); 9 | str[i]=min(max(str[i], xl), xr); 10 | } 11 | 12 | void psh(int i) { 13 | app(2*i, stl[i], str[i]); 14 | app(2*i+1, stl[i], str[i]); 15 | stl[i]=0; 16 | str[i]=1e5; 17 | } 18 | 19 | void upd(int l1, int r1, int xl, int xr, int i, int l2, int r2) { 20 | if(l1<=l2&&r2<=r1) { 21 | app(i, xl, xr); 22 | return; 23 | } 24 | int m2=(l2+r2)/2; 25 | psh(i); 26 | if(l1<=m2) 27 | upd(l1, r1, xl, xr, 2*i, l2, m2); 28 | if(m2 3 | using namespace std; 4 | 5 | long long dp[10000002], ans; 6 | 7 | long long delivery(int n, int k, int l, int p[]) { 8 | int m=upper_bound(p, p+n, l/2)-p; 9 | for(int i=1; i<=m; ++i) { 10 | dp[i]=2*p[i-1]; 11 | if(i>k) 12 | dp[i]+=dp[i-k]; 13 | } 14 | for(int i=n; i>m; --i) { 15 | dp[i]=2*(l-p[i-1]); 16 | if(i+k<=n) 17 | dp[i]+=dp[i+k]; 18 | } 19 | ans=dp[m]+dp[m+1]; 20 | for(int i=m+1; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | pair w[200001]; 7 | 8 | int main() { 9 | ios_base::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | int n, i; 13 | ll l, u, sum=0; 14 | cin >> n >> l >> u; 15 | pair *w = new pair[n+1]; 16 | for(i=0; i> w[i].first, w[i].second=i; 18 | sort(w, w+n); 19 | w[n]=make_pair(1LL<<31, -1); 20 | for(i=0; sum<=u; sum+=w[i].first, ++i); 21 | sum-=w[--i].first; 22 | for(int j=0; j<=min(i, n-i); sum+=w[n-1-j].first-w[j].first, ++j) { 23 | if(sum>=l) { 24 | cout << i << "\n"; 25 | for(int k=j; k 3 | using namespace std; 4 | 5 | const int mxN=2e5, mxK=100; 6 | int n, k, a[mxN+1], b1[mxN], b2[mxN+1]; 7 | bool dp1[mxN+1][mxK+1], dp2[mxN+1][mxK+1]; 8 | 9 | string solve_puzzle(string s, vector c) { 10 | n=s.size(); 11 | k=c.size(); 12 | for(int i=0; i0&&s[i-c[j-1]-1]^'X'&&a[i]==a[i-c[j-1]]) 28 | dp2[i-c[j-1]-1][j-1]|=dp2[i][j]; 29 | } 30 | } 31 | for(int i=0; i 3 | using namespace std; 4 | 5 | const int mxN=4e5; 6 | int a[mxN], p[mxN]; 7 | array e[mxN]; 8 | 9 | int find(int x) { 10 | return x^p[x]?p[x]=find(p[x]):x; 11 | } 12 | 13 | bool join(int x, int y) { 14 | if((x=find(x))==(y=find(y))) 15 | return 0; 16 | p[x]=y; 17 | return 1; 18 | } 19 | 20 | long long plan_roller_coaster(vector s, vector t) { 21 | int n=s.size(); 22 | vector x=s; 23 | x.insert(x.end(), t.begin(), t.end()); 24 | sort(x.begin(), x.end()); 25 | iota(p, p+x.size(), 0); 26 | for(int i=0; i 3 | using namespace std; 4 | 5 | const int mxN=2e5; 6 | int n, a, ft[mxN+1]; 7 | vector v; 8 | 9 | void upd(int i) { 10 | for(++i; i<=n; i+=i&-i) 11 | ++ft[i]; 12 | } 13 | 14 | int qry(int i) { 15 | int r=0; 16 | for(; i; i-=i&-i) 17 | r+=ft[i]; 18 | return r; 19 | } 20 | 21 | int dc(int l=0, int r=n-1, int sl=0, int sr=a) { 22 | if(l>r) 23 | return -1; 24 | int ml=(l+r)/2, mr=ml; 25 | vector b; 26 | for(; ml>=l; --ml) { 27 | if(qry(r+1)-qry(l)>=sr-sl) 28 | return -1; 29 | if(qry(ml+1)-qry(ml)) 30 | continue; 31 | b=ask(ml); 32 | int c=b[0]+b[1]; 33 | if(!c) 34 | return ml; 35 | if(c>a) { 36 | a=c; 37 | for(int vi : v) 38 | upd(vi); 39 | v.clear(); 40 | return dc(); 41 | } 42 | if(c==a) { 43 | v.push_back(ml); 44 | b[1]=b[0]+mr-ml; 45 | break; 46 | } 47 | if(c b=ask(n-1); 59 | a=b[0]+b[1]; 60 | return a?dc():n-1; 61 | } 62 | -------------------------------------------------------------------------------- /IOI/18-Combo.cpp: -------------------------------------------------------------------------------- 1 | #include "combo.h" 2 | #include 3 | using namespace std; 4 | 5 | string guess_sequence(int n) { 6 | string a="ABXY", b="AB", d, s; 7 | if(!press("AB")) 8 | b="XY"; 9 | char fc=b[0]; 10 | if(!press(b.substr(0, 1))) 11 | fc=b[1]; 12 | for(char c : a) 13 | if(c!=fc) 14 | d+=c; 15 | s+=fc; 16 | if(n<=1) 17 | return s; 18 | while(s.size()s.size()) { 21 | if(press(s+d[0])>s.size()) 22 | return s+d[0]; 23 | return s+d[1]; 24 | } 25 | return s+d[2]; 26 | } 27 | -------------------------------------------------------------------------------- /IOI/18-Doll.cpp: -------------------------------------------------------------------------------- 1 | #include "doll.h" 2 | #include 3 | using namespace std; 4 | 5 | int n, p=1, sti=1; 6 | vector x(1<<19), y(1<<19); 7 | bool b[1<<19]; 8 | 9 | int bld(int l, int r) { 10 | if(l>=r) 11 | return 0; 12 | if(r a) { 30 | n=a.size(); 31 | while(p c(m+1, -1); 40 | c[0]=a[0]; 41 | x.resize(sti); 42 | y.resize(sti); 43 | answer(c, x, y); 44 | } 45 | -------------------------------------------------------------------------------- /IOI/19P-Cycle.cpp: -------------------------------------------------------------------------------- 1 | #include "cycle.h" 2 | #include 3 | using namespace std; 4 | 5 | void escape(int n) { 6 | if(n<3) { 7 | jump(1); 8 | return; 9 | } 10 | int a=jump(0)?0:n/2; 11 | for(int i=28; ~i; --i) { 12 | int l=min(1< 3 | using namespace std; 4 | 5 | vector get_attachment(vector s) { 6 | int x=0; 7 | for(int i=0; i v{__builtin_popcount(x)&1}; 11 | for(; x; x/=2) 12 | v.push_back(x&1); 13 | return v; 14 | } 15 | 16 | vector retrieve(vector d) { 17 | int n=d.size()>=255?255:63, x=0; 18 | for(int i=d.size()-1; i>n; --i) 19 | x=x*2+d[i]; 20 | vector v=vector(d.begin(), d.begin()+n); 21 | if((__builtin_popcount(x)^d[n])&1) 22 | return v; 23 | for(int i=0; i 6 | using namespace std; 7 | 8 | const int mxN=3e5; 9 | int n, m, st[2*mxN], l, r, x; 10 | 11 | int main() { 12 | ios_base::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> n >> m; 16 | while(m--) { 17 | cin >> l >> r >> x, --l, x+=n-l; 18 | for(l+=n, r+=n; l 9 | using namespace std; 10 | 11 | #define ll long long 12 | 13 | const int mxN=1e5; 14 | int n, x[mxN]; 15 | ll pg[mxN+1], pd[mxN+1], pts[mxN], ft[mxN+1], ans; 16 | 17 | inline void upd(int i, ll x) { 18 | for(++i; i<=n; i+=i&-i) 19 | ft[i]=max(x, ft[i]); 20 | } 21 | 22 | inline ll qry(int i) { 23 | ll r=0; 24 | for(; i; i-=i&-i) 25 | r=max(ft[i], r); 26 | return r; 27 | } 28 | 29 | int main() { 30 | ios_base::sync_with_stdio(0); 31 | cin.tie(0); 32 | 33 | cin >> n; 34 | for(int i=0; i> x[i] >> pg[i+1] >> pd[i+1], pg[i+1]+=pg[i], pd[i+1]+=pd[i]; 36 | pts[i]=x[i]-pd[i+1]; 37 | } 38 | sort(pts, pts+n); 39 | for(int i=n-1; i>=0; --i) { 40 | upd(lower_bound(pts, pts+n, x[i]-pd[i+1])-pts, pg[i+1]); 41 | ans=max(qry(upper_bound(pts, pts+n, x[i]-pd[i])-pts)-pg[i], ans); 42 | } 43 | cout << ans; 44 | } 45 | -------------------------------------------------------------------------------- /IZhO/19-Xoractive.cpp: -------------------------------------------------------------------------------- 1 | #include "interactive.h" 2 | #include 3 | using namespace std; 4 | 5 | vector guess(int n) { 6 | int k=-1; 7 | while(n>1<<++k); 8 | vector ans, c[k]; 9 | ans.push_back(ask(1)); 10 | for(int j=0; j a; 12 | for(int i=0; i>j&1) 14 | a.push_back(i+1); 15 | vector b=get_pairwise_xor(a); 16 | a.push_back(1); 17 | c[j]=get_pairwise_xor(a); 18 | c[j].erase(c[j].begin()); 19 | for(int d : b) 20 | c[j].erase(find(c[j].begin(), c[j].end(), d)); 21 | for(int &d : c[j]) 22 | d^=ans[0]; 23 | } 24 | for(int i=1; i v; 26 | for(int j=0; j>j&1^1) 28 | continue; 29 | if(v.size()) { 30 | vector w; 31 | for(int b : c[j]) 32 | if(find(v.begin(), v.end(), b)!=v.end()) 33 | w.push_back(b); 34 | v=w; 35 | } else 36 | v=c[j]; 37 | } 38 | for(int j=0; j>j&1^1) 40 | for(int b : c[j]) 41 | if(find(v.begin(), v.end(), b)!=v.end()) 42 | v.erase(find(v.begin(), v.end(), b)); 43 | ans.push_back(v[0]); 44 | } 45 | return ans; 46 | } 47 | -------------------------------------------------------------------------------- /Info1Cup/18-Del13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int t, n, q, a[mxN+2], dp[mxN+2][3]; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> t; 12 | while(t--) { 13 | cin >> n >> q; 14 | for(int i=1; i<=q; ++i) 15 | cin >> a[i]; 16 | a[q+1]=n+1; 17 | memset(dp, -1, (q+2)*sizeof(dp[0])); 18 | dp[0][0]=-2; 19 | for(int i=0; i<=q; ++i) { 20 | for(int j=0; j<3; ++j) { 21 | if(dp[i][j]==-1||a[i+1]-a[i]-1-j<0) 22 | continue; 23 | if((a[i+1]-a[i]-1-j)&1^1) { 24 | if(!(a[i+1]-a[i]-1-j)||j) 25 | dp[i+1][0]=j; 26 | if(a[i+1]-a[i]-1-j) 27 | dp[i+1][2]=j; 28 | } else 29 | dp[i+1][1]=j; 30 | } 31 | } 32 | if(dp[q+1][0]==-1) { 33 | cout << "-1\n"; 34 | continue; 35 | } 36 | vector ans; 37 | for(int i=q, j=0; ~i; j=dp[i+1][j], --i) 38 | for(int k=a[i+1]-a[i]-1-j-dp[i+1][j]; k; k-=2) 39 | ans.push_back((a[i]+a[i+1])/2); 40 | for(int i=q, j=0; ~i; j=dp[i+1][j], --i) 41 | for(int k=j; k; --k) 42 | ans.push_back(a[i+1]); 43 | cout << ans.size() << "\n"; 44 | for(int ai : ans) 45 | cout << ai << " "; 46 | cout << "\n"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Info1Cup/18-Hidden.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "grader.h" 3 | using namespace std; 4 | 5 | bool qry(int a, int b, int c) { 6 | vector v(a, c); 7 | while(b--) 8 | v.push_back(c^1); 9 | return isSubsequence(v); 10 | } 11 | 12 | vector findSequence(int n) { 13 | int c=0; 14 | for(int i=1; i<=n; ++i) { 15 | vector v(i>n/2?n-i+1:i, i>n/2); 16 | if(qry(i>n/2?n-i+1:i, 0, i>n/2)^i<=n/2) 17 | break; 18 | c=i; 19 | } 20 | vector ans; 21 | for(int i=1, j=0; i<=c; ++i) { 22 | for(; j 2 | using namespace std; 3 | 4 | const int mxN=1e3; 5 | int n, m, a[mxN][mxN], b1[mxN+1][mxN+1], b2[mxN+1][mxN+1], b3[mxN+1][mxN+1], b4[mxN+1][mxN+1], ans; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n >> m; 12 | memset(b1, 0xbf, sizeof(b1)); 13 | memset(b2, 0xbf, sizeof(b2)); 14 | memset(b3, 0xbf, sizeof(b3)); 15 | memset(b4, 0xbf, sizeof(b4)); 16 | for(int i=0; i> a[i][j]; 19 | b1[i+1][j+1]=max({b1[i][j+1], b1[i+1][j], -a[i][j]+i+j}); 20 | } 21 | for(int j=m-1; ~j; --j) 22 | b2[i+1][j]=max({b2[i][j], b2[i+1][j+1], -a[i][j]+i-j}); 23 | } 24 | for(int i=n-1; ~i; --i) { 25 | for(int j=0; j 2 | using namespace std; 3 | 4 | const int mxN=2e3; 5 | int n, h[mxN], l[mxN], d, p[mxN], dp[mxN+1][mxN+1]; 6 | 7 | int main() { 8 | ifstream cin("pitici3.in"); 9 | ofstream cout("pitici3.out"); 10 | 11 | cin >> n; 12 | memset(dp, 0xc0, sizeof(dp)); 13 | dp[0][0]=0; 14 | for(int i=0; i> h[i] >> l[i]; 16 | dp[0][0]+=h[i]; 17 | } 18 | cin >> d; 19 | iota(p, p+n, 0); 20 | sort(p, p+n, [](const int &i, const int &j) { 21 | return h[i]+l[i]=d) 26 | dp[i+1][j+1]=dp[i][j]-h[p[i]]; 27 | dp[i+1][j]=max(dp[i][j], dp[i+1][j]); 28 | } 29 | } 30 | for(int i=n; ; --i) { 31 | if(dp[n][i]>=0) { 32 | cout << i; 33 | break; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InfoArena/Tricolor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=5e3; 5 | int t, n, dp[mxN][mxN], td[mxN], ui, vi; 6 | vector adj[mxN]; 7 | 8 | int dfs(int u=0, int p=-1) { 9 | int ts=0, aa=0; 10 | dp[u][0]=0; 11 | for(int v : adj[u]) { 12 | if(v==p) 13 | continue; 14 | int cs=dfs(v, u), ab=0; 15 | memset(td, 0xc0, 4*(ts+cs+1)); 16 | for(int i=0; i<=ts; ++i) 17 | for(int j=0; j<=cs; ++j) 18 | td[i+j]=max(dp[u][i]+dp[v][j]+i*j, td[i+j]); 19 | ts+=cs; 20 | memcpy(dp[u], td, 4*(ts+1)); 21 | for(int j=0; j<=cs; ++j) 22 | ab=max(dp[v][j]+j, ab); 23 | aa+=ab; 24 | } 25 | dp[u][1]=max(aa, dp[u][1]); 26 | adj[u].clear(); 27 | return ts+1; 28 | } 29 | 30 | int main() { 31 | ifstream cin("tricolor.in"); 32 | ofstream cout("tricolor.out"); 33 | 34 | cin >> t; 35 | while(t--) { 36 | cin >> n; 37 | for(int i=0; i> ui >> vi, --ui, --vi; 39 | adj[ui].push_back(vi); 40 | adj[vi].push_back(ui); 41 | } 42 | memset(dp, 0xc0, n*sizeof(dp[0])); 43 | dfs(); 44 | int ans=0; 45 | for(int i=1; i 6 | using namespace std; 7 | 8 | #define ll long long 9 | 10 | const int mxN=100, mxM=1e3, M=1e9+7; 11 | int n, m, a[mxN]; 12 | ll dp[mxN][4][mxN+1][mxM+1], ans; 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); 16 | cin.tie(0); 17 | 18 | cin >> n >> m; 19 | if(n<2) { 20 | cout << 1; 21 | return 0; 22 | } 23 | for(int i=0; i> a[i]; 25 | sort(a, a+n); 26 | dp[0][0][1][0]=1; 27 | dp[0][1][1][0]=2; 28 | for(int i=0; i+1m) 34 | break; 35 | dp[i][j][k][l]%=M; 36 | dp[i+1][j][k+1][nl]+=dp[i][j][k][l]; 37 | dp[i+1][j+1][k+1][nl]+=(2-j)*dp[i][j][k][l]; 38 | dp[i+1][j][k][nl]+=(2*k-j)*dp[i][j][k][l]; 39 | dp[i+1][j+1][k][nl]+=(2-j)*(k-j)*dp[i][j][k][l]; 40 | dp[i+1][j][k-1][nl]+=(k-j)*(k-1)*dp[i][j][k][l]; 41 | if(i+2==n&&(j==1&&k==1||j==2&&k==2)) 42 | ans+=dp[i][j][k][l]; 43 | } 44 | } 45 | } 46 | } 47 | cout << ans%M; 48 | } 49 | -------------------------------------------------------------------------------- /JOI/17S-City/Device.cpp: -------------------------------------------------------------------------------- 1 | #include "Device.h" 2 | #include 3 | using namespace std; 4 | 5 | void InitDevice() {} 6 | 7 | int Answer(long long s, long long t) { 8 | auto decrange=[](int x) { 9 | int l=1, r=1<<11; 10 | while(l{x-r*(r-1)/2, r}; 18 | }; 19 | auto inr=[](int x, array r) { 20 | return r[0]<=x&&x>26&&t>>26) { 23 | if((s>>15^t>>15)&(1<<11)-1) 24 | return 2; 25 | array rs=decrange(s&(1<<15)-1), rt=decrange(t&(1<<15)-1); 26 | return inr(rt[0], rs)?1:(inr(rs[0], rt)?0:2); 27 | } 28 | if(s>>26) 29 | return inr(s>>15&(1<<11)-1, decrange(t&(1<<21)-1))?0:2; 30 | if(t>>26) 31 | return inr(t>>15&(1<<11)-1, decrange(s&(1<<21)-1))?1:2; 32 | int sd=s>>21, td=t>>21; 33 | array rs=decrange(s&(1<<21)-1), rt=decrange(t&(1<<21)-1); 34 | return td>sd&&inr(rt[0], rs)?1:(sd>td&&inr(rs[0], rt)?0:2); 35 | } 36 | -------------------------------------------------------------------------------- /JOI/18F-Art.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | #define pll pair 6 | #define fi first 7 | #define se second 8 | 9 | const int mxN=5e5; 10 | int n; 11 | ll mx, ans; 12 | pll ps[mxN+1]; 13 | 14 | int main() { 15 | ios_base::sync_with_stdio(0); 16 | cin.tie(0); 17 | 18 | cin >> n; 19 | for(int i=1; i<=n; ++i) 20 | cin >> ps[i].fi >> ps[i].se; 21 | sort(ps, ps+n+1); 22 | for(int i=1; i<=n; ++i) { 23 | ps[i].se+=ps[i-1].se; 24 | mx=max(ps[i].fi-ps[i-1].se, mx); 25 | ans=max(ps[i].se-ps[i].fi+mx, ans); 26 | } 27 | cout << ans; 28 | } 29 | -------------------------------------------------------------------------------- /JOI/18F-Dango_Maker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Let (i, j) be the position of 'R' in a triple, 2 triples could intersect only if i1+j1 = i2+j2 3 | - For each possible i+j, use a dp to find the maximal independent set 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | const int mxN=3e3; 10 | int n, m, dp[mxN+1][3], ans; 11 | string g[mxN]; 12 | 13 | inline bool c(int i, int j, char c) { 14 | return i>=0&&i=0&&j> n >> m; 22 | for(int i=0; i> g[i]; 24 | for(int i=0; i<=n+m-4; ++i) { 25 | int jm=min(m, i+2); 26 | for(int j=0; j 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, k, t1, t2, ans, d[mxN-1]; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n >> k; 12 | for(int i=0; i> t2; 14 | if(i) 15 | d[i-1]=t2-t1-1; 16 | t1=t2; 17 | } 18 | ans=n; 19 | sort(d, d+n-1); 20 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=3e3; 7 | int h, w, a[mxN]; 8 | string g[mxN]; 9 | ll ans; 10 | 11 | int main() { 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | cin >> h >> w; 16 | for(int i=0; i> g[i]; 18 | for(int i=h-1; i>=0; --i) { 19 | for(int j=w-1, b=0; j>=0; --j) { 20 | if(g[i][j]=='J') 21 | ans+=a[j]*b; 22 | else if(g[i][j]=='O') 23 | ++b; 24 | else 25 | ++a[j]; 26 | } 27 | } 28 | cout << ans; 29 | } 30 | -------------------------------------------------------------------------------- /JOI/19F-Coin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e5; 7 | int n, c[mxN][2]; 8 | ll a1; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | 14 | cin >> n; 15 | for(int i=0, x, y; i<2*n; ++i) { 16 | cin >> x >> y; 17 | if(x<1) { 18 | a1+=1-x; 19 | x=1; 20 | } 21 | if(x>n) { 22 | a1+=x-n; 23 | x=n; 24 | } 25 | if(y<1) { 26 | a1+=1-y; 27 | y=1; 28 | } 29 | if(y>2) { 30 | a1+=y-2; 31 | y=2; 32 | } 33 | ++c[x-1][y-1]; 34 | } 35 | for(int i1=0, i2=0, j=0; i1 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, m, b[mxN]; 6 | array a[mxN]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n >> m; 13 | for(int i=0; i> a[i][1] >> a[i][0]; 15 | for(int i=0; i> b[i]; 17 | sort(a, a+n); 18 | sort(b, b+m); 19 | int ans=m-1; 20 | for(int i=n-1; i>=0; --i) 21 | if(ans>=0&&a[i][1]<=b[ans]) 22 | --ans; 23 | cout << m-1-ans; 24 | } 25 | -------------------------------------------------------------------------------- /JOI/19F-Growing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=400; 5 | int n, dp[mxN][8][mxN+1][4], b[mxN][7]; 6 | string s; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | 12 | cin >> n >> s; 13 | for(char &c : s) 14 | c=(c>'G')+(c>'R'); 15 | for(int i=0; i>s[i]&1) 20 | b[i][j]=s[i]; 21 | } 22 | memset(dp, 0x3f, sizeof(dp)); 23 | dp[0][1<>s[i]&1) 33 | continue; 34 | if(l!=s[i]) 35 | tm(dp[i][j][k][s[i]], dp[i-1][j][k][l]+k); 36 | if(k>1) 37 | tm(dp[i][j][k-1][s[i]], dp[i-1][j][k][l]+k-1); 38 | else 39 | tm(dp[i][1<=1e9?-1:ans); 49 | } 50 | -------------------------------------------------------------------------------- /JOI/19O-Remittance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e6; 5 | int n, a[mxN], b[mxN]; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | 11 | cin >> n; 12 | for(int i=0; i> a[i] >> b[i]; 14 | for(bool ch=1; ch; ) { 15 | ch=0; 16 | for(int i=0; i 3 | using namespace std; 4 | 5 | bool qry(int x) { 6 | static int lst=0; 7 | int ans=Query(x), c=lst^ans; 8 | lst=ans; 9 | return c; 10 | } 11 | 12 | void solve(vector l, vector r, bool f) { 13 | int n=l.size(), m=floor(0.35*n); 14 | if(n<2) { 15 | Answer(l[0], r[0]); 16 | return; 17 | } 18 | if(f) { 19 | m=n-m-2; 20 | for(int i=n-1; i>m; --i) 21 | qry(l[i]); 22 | } else 23 | for(int i=0; i<=m; ++i) 24 | qry(l[i]); 25 | vector v[2]; 26 | random_shuffle(r.begin(), r.end()); 27 | for(int x : r) 28 | v[v[0].size()>m||v[1].size()(l.begin(), l.begin()+m+1), v[0], 1); 30 | solve(vector(l.begin()+m+1, l.end()), v[1], 0); 31 | } 32 | 33 | void Solve(int n) { 34 | vector v[2]; 35 | for(int i=1; i<=2*n; ++i) 36 | v[qry(i)].push_back(i); 37 | solve(v[0], v[1], 1); 38 | } 39 | -------------------------------------------------------------------------------- /JOI/19S-Naan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2e3; 7 | int n, l, m, p[mxN], b[mxN]; 8 | ll v[mxN][mxN]; 9 | array a[mxN][mxN+1], a1[mxN]; 10 | 11 | bool cmp(array a, array b) { 12 | return a[0]^b[0]?a[0]> n >> l; 20 | for(int i=0; i> v[i][j], s+=v[i][j]; 24 | a[i][0]={0, 0, 1}; 25 | for(int j=0, k=1; j=s) { 27 | a[i][k++]={j, s-s2, n*v[i][j]}; 28 | s2-=s; 29 | } 30 | s2+=n*v[i][j]; 31 | } 32 | b[i]=n; 33 | } 34 | array ls={l, 0, 1}; 35 | for(int i=0; i 6 | using namespace std; 7 | 8 | const int mxN=3000; 9 | int n, ps[mxN+1], dp[mxN][mxN], h[mxN][mxN]; 10 | 11 | int main() { 12 | ios_base::sync_with_stdio(0); 13 | cin.tie(0); 14 | 15 | while(1) { 16 | cin >> n; 17 | if(!n) 18 | break; 19 | for(int i=0; i> ps[i+1], ps[i+1]+=ps[i]; 21 | for(int i=1; i1?h[j][j+i-1]:j; 24 | for(int k=h[j][j+i]+1; k<=(i>1?h[j+1][j+i]:j); ++k) 25 | if(dp[j][k]+dp[k+1][j+i] 8 | using namespace std; 9 | 10 | #define ll long long 11 | 12 | const int mxN=5e5; 13 | int t, n, k, qh, qt; 14 | ll a[mxN], ps[mxN+1], dp[mxN+1], m[mxN], b[mxN]; 15 | 16 | inline void al(ll mi, ll bi) { 17 | if(qt-qh>=1&&m[qt-1]==mi) { 18 | if(bi=2&&(b[qt-1]-b[qt-2])/(m[qt-2]-m[qt-1])>=(bi-b[qt-1])/(m[qt-1]-mi)) 24 | --qt; 25 | m[qt]=mi, b[qt]=bi; 26 | ++qt; 27 | } 28 | 29 | int main() { 30 | ios_base::sync_with_stdio(0); 31 | cin.tie(0); 32 | 33 | cin >> t; 34 | while(t--) { 35 | cin >> n >> k; 36 | for(int i=0; i> a[i]; 38 | ps[i+1]=a[i]+ps[i]; 39 | } 40 | qh=qt=0; 41 | for(int i=1; i=2&&m[qh]*i+b[qh]>=m[qh+1]*i+b[qh+1]) 46 | ++qh; 47 | dp[i]=ps[i]+m[qh]*i+b[qh]; 48 | } 49 | cout << dp[n] << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /POI/18-Lightning_Conductor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Any two functions only intersect once, so we can use CHT 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | const int mxN=5e5; 9 | int n, h[mxN], a1[mxN], a2[mxN], sr[1000001], qu[mxN]; 10 | 11 | double ix(int i, int j) { 12 | double a=(j-i)/2.0/(h[j]-h[i])-(h[j]-h[i])/2.0; 13 | return a*a+j; 14 | } 15 | 16 | void solve(int a[mxN]) { 17 | for(int i=0, qh=0, qt=0; iqh&&h[i]>=h[qu[qt-1]]+sr[i-qu[qt-1]]||qt-qh>1&&ix(qu[qt-2], qu[qt-1])>ix(qu[qt-1], i)) 20 | --qt; 21 | qu[qt++]=i; 22 | } 23 | while(qt-qh>1&&i>ix(qu[qh], qu[qh+1])) 24 | ++qh; 25 | a[i]=h[qu[qh]]+sr[i-qu[qh]]; 26 | } 27 | } 28 | 29 | int main() { 30 | ios_base::sync_with_stdio(0); 31 | cin.tie(0); 32 | 33 | for(int i=1; i<=1000; ++i) 34 | for(int j=(i-1)*(i-1)+1; j<=i*i; ++j) 35 | sr[j]=i; 36 | cin >> n; 37 | for(int i=0; i> h[i]; 39 | solve(a1); 40 | for(int i=0; i 8 | using namespace std; 9 | 10 | #define ll long long 11 | 12 | ll n, m, ans=1e18, p[12], n2; 13 | int k, ps; 14 | unordered_set v; 15 | 16 | void dfs(ll u, bool ua=0) { 17 | if(ua) 18 | ans=min(u, ans); 19 | v.insert(u); 20 | for(int i=0; i> n >> k; 30 | n2=n; 31 | for(ll i=2; i*i0) 39 | p[ps++]=n; 40 | for(int i=0; i> m, dfs(__gcd(m, n2), i==k-1); 42 | cout << n2/ans; 43 | } 44 | -------------------------------------------------------------------------------- /POI/19-Warehouse_Store.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Process everything in order 3 | - Always complete orders if possible 4 | - If a certain order can not be completed and an order with a higher demand was completed, undo that order and complete the new order instead 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | const int mxN=2.5e5; 11 | int n, a[mxN], bi; 12 | priority_queue> pq; 13 | long long s=0; 14 | vector ans; 15 | 16 | int main() { 17 | ios_base::sync_with_stdio(0); 18 | cin.tie(0); 19 | 20 | cin >> n; 21 | for(int i=0; i> a[i]; 23 | for(int i=0; i> bi; 26 | if(sbi) { 27 | s+=pq.top()[0]; 28 | pq.pop(); 29 | } 30 | if(s>=bi) { 31 | pq.push({bi, i}); 32 | s-=bi; 33 | } 34 | } 35 | while(!pq.empty()) { 36 | ans.push_back(pq.top()[1]); 37 | pq.pop(); 38 | } 39 | cout << ans.size() << "\n"; 40 | sort(ans.begin(), ans.end()); 41 | for(int b : ans) 42 | cout << b+1 << " "; 43 | } 44 | -------------------------------------------------------------------------------- /POI/22-Gluttons.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Fix the cakes that the first 2 gluttons choose 3 | - Run DP on the rest 4 | - https://github.com/mostafa-saad/MyCompetitiveProgramming/blob/master/Olympiad/POI/official/2015/EDITORIAL/las.pdf 5 | - A solution is always possible 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | const int mxN=1e6; 12 | int n, c[mxN], dp[mxN][4], a[mxN]; 13 | 14 | bool cg(int i, bool g0, bool g1, bool g2) { 15 | return g1?c[i]*(1+!g2)<=c[(i+1)%n]*(1+g0):c[i]*(1+!g2)>=c[(i+1)%n]*(1+g0); 16 | } 17 | 18 | int main() { 19 | ios::sync_with_stdio(0); 20 | cin.tie(0); 21 | 22 | cin >> n; 23 | for(int i=0; i> c[i]; 25 | for(int i=0; i<4; ++i) { 26 | memset(dp, -1, sizeof(dp)); 27 | dp[1][i]=i%2*2; 28 | for(int j=1; j 7 | using namespace std; 8 | 9 | #define ll long long 10 | 11 | const int S=651; 12 | int dp[S]; 13 | ll n, lb=1, rb=2e6, a; 14 | 15 | int main() { 16 | ios::sync_with_stdio(0); 17 | cin.tie(0); 18 | 19 | memset(dp, 1, 4*S); 20 | dp[0]=0; 21 | for(int i=1; i<14; ++i) 22 | for(int j=i*i; ji&&dp[j-i*i]> n; 26 | for(int i=S-1, mn=INT_MAX; i; --i) { 27 | if(i<=n) 28 | a+=dp[i]>mn; 29 | mn=min(dp[i], mn); 30 | } 31 | if(n13) 33 | cout << "-"; 34 | else 35 | cout << dp[n]; 36 | cout << " " << a; 37 | return 0; 38 | } 39 | while(lb=n) 42 | rb=mb; 43 | else 44 | lb=mb+1; 45 | } 46 | ll b=lb*(lb+1)*(2*lb+1)/6-n; 47 | cout << lb+(b<144&&dp[S-1-b]==13) << " "; 48 | a+=(lb-12)*31; 49 | for(int i=S; i>S-min(144ll, b); --i) 50 | a-=dp[i-1]==13; 51 | cout << a; 52 | } 53 | -------------------------------------------------------------------------------- /POI/25-Polynomial.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Write a slightly modified version FFT 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | #define ll long long 9 | 10 | int n, m, q, a2; 11 | 12 | vector solve(vector a, ll p) { 13 | if(a.size()<=1) 14 | return {a[0]%m}; 15 | vector b1, b2; 16 | for(int i=0; i c1=solve(b1, p*p%m), c2=solve(b2, p*p%m), c(a.size()); 21 | ll p1=p, p2=p; 22 | for(int i=0; i> n >> m >> q; 35 | vector a(n); 36 | for(int i=0; i> a[i]; 38 | vector a1=solve(a, q); 39 | for(int i=0; i 7 | using namespace std; 8 | 9 | #define ll long long 10 | #define pli pair 11 | 12 | const int mxN=3e5; 13 | int n, k; 14 | ll a[mxN+1], lb, rb=3e14; 15 | pli dp[mxN+1]; 16 | 17 | pli cdp(ll x) { 18 | memset(dp, 0, sizeof(dp)); 19 | pli ms{}; 20 | for(int i=0; ims.first:cs.seconddp[i].first:nd.second> n >> k; 35 | for(int i=0; i> a[i+1], a[i+1]+=a[i]; 37 | while(lb iv, f1, f2; 2 | void comboInit(int n) { 3 | vti(iv, 1, n+1); 4 | vti(f1, 1, n+1); 5 | vti(f2, 1, n+1); 6 | FOR(i, 2, n+1) 7 | iv[i]=-M/i*iv[M%i]; 8 | FOR(i, 1, n+1) { 9 | f1[i]=f1[i-1]*i; 10 | f2[i]=f2[i-1]*iv[i]; 11 | } 12 | } 13 | Mnt nck(int n, int k) { 14 | return f1[n]*f2[k]*f2[n-k]; 15 | } 16 | Mnt nck2(int n, int k) { 17 | umin(k, n-k); 18 | Mnt r=f2[k]; 19 | FOR(k) 20 | r*=n-i; 21 | return r; 22 | } 23 | vt gp(Mnt b, int n) { 24 | vt v(n+1, 1); 25 | FOR(n) 26 | v[i+1]=v[i]*b; 27 | return v; 28 | } 29 | -------------------------------------------------------------------------------- /Templates/digraph.cpp: -------------------------------------------------------------------------------- 1 | struct digraph { 2 | int n; 3 | vt> adj, adj2; 4 | digraph(int _n, vt> &e) { 5 | n=_n; 6 | adj=vt>(n); 7 | adj2=vt>(n); 8 | EACH(a, e) { 9 | adj[a[0]].pb(a[1]); 10 | adj2[a[1]].pb(a[0]); 11 | } 12 | } 13 | vt toposort() { 14 | vt d(n); 15 | queue qu; 16 | FOR(n) { 17 | d[i]=sz(adj2[i]); 18 | if(!d[i]) 19 | qu.push(i); 20 | } 21 | vt ans; 22 | while(qu.size()) { 23 | int u=qu.front(); 24 | qu.pop(); 25 | EACH(v, adj[u]) { 26 | --d[v]; 27 | if(!d[v]) 28 | qu.push(v); 29 | } 30 | ans.pb(u); 31 | } 32 | if(sz(ans) struct sieve { 2 | int lp[N+1]; 3 | vt p; 4 | sieve() { 5 | FOR(i, 2, N+1) { 6 | if(!lp[i]) { 7 | lp[i]=i; 8 | p.pb(i); 9 | } 10 | for(int j=0; j>p[i]&1)>0) { 11 | if(k==p[i]) 12 | dp[i+1][j][k]=Math.max(dp[i][j][k]+1, dp[i+1][j][k]); 13 | } else { 14 | dp[i+1][j|1<=0&&h[j]<=h[j+1]; --j) 6 | ans=Math.max(i-j+1, ans); 7 | for(int j=i+1; jnh) 21 | rb=mb-1; 22 | else 23 | lb=mb; 24 | } 25 | f[i]=lb==-1?-1:c[lb].b; 26 | } 27 | int ans=0; 28 | boolean[] vis = new boolean[n]; 29 | for(int i=0; i { 43 | long a; 44 | int b; 45 | Pair(long a, int b) { 46 | this.a=a; 47 | this.b=b; 48 | } 49 | public int compareTo(Pair o) { 50 | return a==o.a?b-o.b:Long.compare(a, o.a); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Topcoder/Open20-2A-D1-1000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class PairsOfHouses { 5 | public: 6 | vector design(int p) { 7 | int r=-1, c; 8 | for(int i=1; i<=25&&r<0; ++i) { 9 | for(int j=1; j<=25&&r<0; ++j) { 10 | int a=j*i*(i-1)/2; 11 | if(a+i*(j-1)<=p&&p<=a+i*j*(j-1)/2) { 12 | r=i; 13 | c=j; 14 | } 15 | } 16 | } 17 | vector> v(r, vector(c, 1)); 18 | for(int i=0; i1; --j1) { 24 | if(a+j1-1<=p) { 25 | v[i][j1]+=j1*(j1-1)/2; 26 | a+=j1-1; 27 | } else if(a<=p) { 28 | for(int j=j1; ~j; --j) 29 | v[i][j]+=j1*(j1-1)/2; 30 | for(int j2=j1-2; a ans={r, c}; 38 | for(int i=0; i 2 | using namespace std; 3 | 4 | class GridSpiral { 5 | public: 6 | long long findCell(int d) { 7 | if(d%2==0) 8 | return -1; 9 | if(d<7) 10 | return 0; 11 | long long a=(d-7)/2; 12 | return (a+1)/2*(a/2+1); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Topcoder/Open20-2A-D1-400.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class PrimeSubstrings { 5 | public: 6 | string construct(int n, int l) { 7 | vector s={"", "2", "11", "113", "1193", "11939", "193939", "13339913"}; 8 | string t; 9 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | #define ar array 6 | 7 | class KRectangleIntersection { 8 | public: 9 | ll solve(vector> &v, int m) { 10 | ll ans=0; 11 | sort(v.begin(), v.end()); 12 | priority_queue, greater> pq; 13 | for(int i=0; im) 16 | pq.pop(); 17 | if(pq.size()>=m) 18 | ans=max(ans, pq.top()-v[i][0]); 19 | } 20 | return ans; 21 | } 22 | ll maxIntersection(vector xl, vector yl, vector xh, vector yh, int m) { 23 | int n=xl.size(); 24 | ll ans=0; 25 | for(int i=0; i> v; 29 | for(int k=0; k=xh[j]) 31 | v.push_back({yl[k], yh[k]}); 32 | ans=max(ans, solve(v, m)*(xh[j]-xl[i])); 33 | } 34 | } 35 | } 36 | return ans; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Topcoder/SRM778-D1-600.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | class CollectingCoins { 7 | public: 8 | vector v, d; 9 | vector a; 10 | vector f(int m, int k) { 11 | if((m+k)%2==1&&m) { 12 | vector b=f(m-1, k); 13 | b.push_back(0); 14 | for(int i=1; i<=k; ++i) 15 | b.back()=max(b[2*k-i]+a[i], b.back()); 16 | return vector(b.begin()+1, b.end()); 17 | } else if((m+k)%2==0&&m>=k) { 18 | vector b=f((m+k)/2-k, k), c(2*k-1); 19 | for(int i=1; i(m+2*k); 28 | for(int i=1; i(a.begin()+m+1, a.end()); 36 | } 37 | } 38 | ll maxValue(int m, int k, vector v, vector d) { 39 | this->v=v; 40 | this->d=d; 41 | return f(m+1, k)[0]; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /Topcoder/SRM779-D1-250.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ar array 5 | 6 | class ArraySorting { 7 | public: 8 | int dp[2001][2001]; 9 | vector arraySort(vector a) { 10 | int n=a.size(); 11 | for(int i=n-1; ~i; --i) { 12 | int mn=1e9; 13 | for(int j=2000; j; --j) { 14 | mn=min(mn, dp[i+1][j]); 15 | dp[i][j]=mn+(j!=a[i]); 16 | } 17 | } 18 | vector v(n); 19 | int s=1; 20 | for(int i=0; i b{(int)1e9}; 22 | for(int j=s; j<=2000; ++j) 23 | b=min(b, ar{dp[i][j], j}); 24 | v[i]=b[1]; 25 | s=b[1]; 26 | } 27 | return v; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Topcoder/SRM779-D1-450.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | class SubstringQueries { 7 | public: 8 | int p[5000], c[26], p2[5000], s[5000]; 9 | ll solve(string s2, ll k) { 10 | int n=s2.size(), la=0; 11 | for(int i=0; i<2*n; ++i) 12 | s[i]=s2[i%n]-'a'; 13 | iota(p, p+n+n, 0); 14 | ll ans=0; 15 | for(int i=0; i=n) 27 | ++j; 28 | ans+=p[j]; 29 | la=p[j]; 30 | } 31 | if(k>1) { 32 | ans+=(ll)la*(k-2)*n; 33 | for(int i=n-1; ~i; --i) { 34 | int j=0; 35 | while(p[j]>i) 36 | ++j; 37 | ans+=p[j]; 38 | } 39 | } 40 | return ans; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /Topcoder/SRM779-D1-900.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | class ParadePlanner { 7 | public: 8 | bool g[500][500]; 9 | int p[500], d[500]; 10 | ll count(int n, int seed, int threshold, vector toggle) { 11 | for(int x=0; x 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e3, M=1e9+7; 7 | int n; 8 | ll dp[mxN][8], ndp[8]; 9 | vector adj[mxN]; 10 | 11 | void ad(ll &a, ll b) { 12 | a+=b; 13 | if(a>=M) 14 | a-=M; 15 | } 16 | 17 | class RestrictedLeaves { 18 | public: 19 | void dfs(int u=0) { 20 | if(adj[u].size()) { 21 | dfs(adj[u][0]); 22 | for(int i=0; i<8; ++i) { 23 | ad(dp[u][i&3], dp[adj[u][0]][i]); 24 | if(i<4) 25 | ad(dp[u][4|i], dp[adj[u][0]][i]); 26 | } 27 | adj[u].erase(adj[u].begin()); 28 | for(int v : adj[u]) { 29 | dfs(v); 30 | memset(ndp, 0, sizeof(ndp)); 31 | for(int i=0; i<8; ++i) 32 | for(int j=0; j<8; ++j) 33 | if(!(i&4&&j&4)&&!(i&1&&j&2)) 34 | ad(ndp[(i&4)|(i&2)|(j&1)], dp[u][i]*dp[v][j]%M); 35 | memcpy(dp[u], ndp, sizeof(dp[0])); 36 | } 37 | } else 38 | dp[u][0]=dp[u][7]=1; 39 | } 40 | int count(vector p) { 41 | n=p.size(); 42 | for(int i=0; i 2 | using namespace std; 3 | 4 | class BeatTheStar { 5 | public: 6 | long double dp[101][10000], ans; 7 | double doesItMatter(int n, int g) { 8 | dp[0][0]=1; 9 | for(int i=1; i<=n; ++i) { 10 | for(int j=0; j<=i*(i-1)/2; ++j) { 11 | if(i^g) { 12 | dp[i][j]+=0.5*dp[i-1][j]; 13 | dp[i][j+i]+=0.5*dp[i-1][j]; 14 | } else 15 | dp[i][j]=dp[i-1][j]; 16 | } 17 | } 18 | for(int i=0; i<=n*(n+1)/2; ++i) 19 | if((i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e6; 7 | ll h[mxN], ans[mxN], l[mxN], lm[mxN], a2; 8 | 9 | class Prominence { 10 | public: 11 | void solve(int n) { 12 | for(int i=0; i=h[l[i]]; l[i]=l[l[i]]) 14 | lm[i]=min(lm[i], lm[l[i]]); 15 | if((i==0||h[i]>h[i-1])&&(i==n-1||h[i]>h[i+1])&&~l[i]) 16 | ans[i]=min(ans[i], h[i]-lm[i]); 17 | } 18 | } 19 | ll sumOfProminences(int n, vector coef, vector idx, vector val) { 20 | for(int i=0; ih[i-1])&&(i==n-1||h[i]>h[i+1])) 26 | ans[i]=h[i]; 27 | solve(n); 28 | reverse(h, h+n); 29 | reverse(ans, ans+n); 30 | solve(n); 31 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | 8 | int n; 9 | cin >> n; 10 | int *a = new int[n]; 11 | unordered_map p[5]; 12 | for(int k=0; k<5; ++k) { 13 | for(int i=0; i> ai; 16 | p[k][ai]=i; 17 | if(!k) 18 | a[i]=ai; 19 | } 20 | } 21 | sort(a, a+n, [&p](const int &a, const int &b) { 22 | int c=0; 23 | for(int k=0; k<5; ++k) 24 | c+=p[k][a]>p[k][b]?1:-1; 25 | return c<0; 26 | }); 27 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n; 6 | array a[mxN]; 7 | priority_queue, greater> pq; 8 | 9 | int main() { 10 | ifstream cin("sort.in"); 11 | ofstream cout("sort.out"); 12 | 13 | cin >> n; 14 | for(int i=0; i> a[i][0]; 16 | a[i][1]=i; 17 | } 18 | sort(a, a+n); 19 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, ft[mxN+1]; 6 | array a[mxN], b[mxN]; 7 | long long ans=0; 8 | 9 | void upd(int i) { 10 | for(++i; i<=n; i+=i&-i) 11 | ++ft[i]; 12 | } 13 | 14 | int qry(int i) { 15 | int r=0; 16 | for(; i; i-=i&-i) 17 | r+=ft[i]; 18 | return r; 19 | } 20 | 21 | int main() { 22 | ifstream cin("sort.in"); 23 | ofstream cout("sort.out"); 24 | 25 | cin >> n; 26 | for(int i=0; i> a[i][0]; 28 | a[i][1]=i; 29 | } 30 | memcpy(b, a, sizeof(a[0])*n); 31 | sort(b, b+n); 32 | for(int i=n-1, j=n-1; i>=0; --i) { 33 | upd(b[i][1]); 34 | while(j>=0&&a[j]>=b[i]) 35 | --j; 36 | ans+=max(qry(max(b[i][1], j+1)), 1); 37 | } 38 | cout << (n>1?ans:0); 39 | } 40 | -------------------------------------------------------------------------------- /USACO/Contests/1718_4P/Train/compile.bat: -------------------------------------------------------------------------------- 1 | g++ -std=c++14 -o Train grader.cpp Train.cpp 2 | pause -------------------------------------------------------------------------------- /USACO/Contests/1718_4P/Train/grader.h: -------------------------------------------------------------------------------- 1 | void helpBessie(int ID); 2 | int get(int index); 3 | void set(int index, int value); 4 | void shoutMinimum(int output); 5 | int getTrainLength(); 6 | int getWindowLength(); 7 | int getCurrentCarIndex(); 8 | int getCurrentPassIndex(); 9 | -------------------------------------------------------------------------------- /USACO/Contests/1718_4P/Train/run.bat: -------------------------------------------------------------------------------- 1 | Train 2 | pause -------------------------------------------------------------------------------- /USACO/Contests/1819_1B/Blist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, a[1001], si, ti, bi, ans; 5 | 6 | int main() { 7 | ifstream cin("blist.in"); 8 | ofstream cout("blist.out"); 9 | 10 | //input 11 | cin >> n; 12 | for(int i=0; i> si >> ti >> bi; 14 | //add bi to all moments of time [si, ti] 15 | for(int j=si; j<=ti; ++j) 16 | a[j]+=bi; 17 | } 18 | //answer is maximum of all moments 19 | for(int i=1; i<=1000; ++i) 20 | ans=max(a[i], ans); 21 | //output 22 | cout << ans; 23 | } 24 | -------------------------------------------------------------------------------- /USACO/Contests/1819_1B/Mixmilk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ifstream cin("mixmilk.in"); 6 | ofstream cout("mixmilk.out"); 7 | 8 | //input 9 | int c[3], m[3]; 10 | for(int i=0; i<3; ++i) 11 | cin >> c[i] >> m[i]; 12 | //Do the 100 pours 13 | for(int it=0; it<100; ++it) { 14 | int from=it%3, to=(it+1)%3; 15 | int amount=m[from]; 16 | //Make sure milk doesn't overflow 17 | amount=min(c[to]-m[to], amount); 18 | m[from]-=amount; 19 | m[to]+=amount; 20 | } 21 | //output 22 | for(int i=0; i<3; ++i) 23 | cout << m[i] << "\n"; 24 | } 25 | -------------------------------------------------------------------------------- /USACO/Contests/1819_1G/Cowpatibility.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll unsigned long long 5 | 6 | const ll base=1e6+3; 7 | ll n, ans; 8 | unordered_map cnt[6]; 9 | 10 | int main() { 11 | ifstream cin("cowpatibility.in"); 12 | ofstream cout("cowpatibility.out"); 13 | 14 | cin >> n; 15 | for(int i=0; i> b[j]; 19 | sort(b, b+5); 20 | //Iterate over all subsets 21 | for(int j=1; j<32; ++j) { 22 | int c=__builtin_popcount(j); 23 | //pray to cow gods for no collisions 24 | ll hash=0; 25 | for(int k=0; k<5; ++k) 26 | if(j>>k&1) 27 | hash=hash*base+b[k]; 28 | //add for odd-sized subsets and subtract for even-sized 29 | ans+=cnt[c][hash]*(c&1?1:-1); 30 | ++cnt[c][hash]; 31 | } 32 | } 33 | //we found number of compatible, now take the complement 34 | cout << n*(n-1)/2-ans; 35 | } 36 | -------------------------------------------------------------------------------- /USACO/Contests/1819_1G/Teamwork.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mxN=1e4, mxK=1e3; 5 | int n, k, s[mxN], dp[mxN+1]; 6 | 7 | int main() { 8 | ifstream cin("teamwork.in"); 9 | ofstream cout("teamwork.out"); 10 | 11 | //input 12 | cin >> n >> k; 13 | for(int i=0; i> s[i]; 15 | //calc the dp 16 | for(int i=1; i<=n; ++i) { 17 | int runningmax=0; 18 | for(int j=0; jj; ++j) { 19 | runningmax=max(s[i-j-1], runningmax); 20 | dp[i]=max(dp[i-j-1]+runningmax*(j+1), dp[i]); 21 | } 22 | } 23 | cout << dp[n]; 24 | } 25 | -------------------------------------------------------------------------------- /USACO/Contests/1819_1P/Balance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | #define ar array 6 | 7 | const int mxN=1e5; 8 | int n, a[mxN+2]; 9 | vector> ch; 10 | 11 | ll cross(ar o, ar a, ar b) { 12 | return (a[0]-o[0])*(b[1]-o[1])-(a[1]-o[1])*(b[0]-o[0]); 13 | } 14 | 15 | int main() { 16 | ifstream cin("balance.in"); 17 | ofstream cout("balance.out"); 18 | 19 | //input 20 | cin >> n; 21 | for(int i=1; i<=n; ++i) 22 | cin >> a[i]; 23 | //find the convex hull 24 | //note we only need the upper part 25 | for(int i=0; i<=n+1; ++i) { 26 | while(ch.size()>1&&cross(ch[ch.size()-2], ch.back(), {i, a[i]})>0) 27 | ch.pop_back(); 28 | ch.push_back({i, a[i]}); 29 | } 30 | //two pointers to traverse the convex hull 31 | for(int i1=1, i2=0; i1<=n; ++i1) { 32 | while(i2+1 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, m, c, t[mxN]; 6 | 7 | int main() { 8 | ifstream cin("convention.in"); 9 | ofstream cout("convention.out"); 10 | 11 | //input 12 | cin >> n >> m >> c; 13 | for(int i=0; i> t[i]; 15 | sort(t, t+n); 16 | //do the binary search 17 | int lb=0, rb=(int)1e9; 18 | while(lb 2 | using namespace std; 3 | 4 | const int mxN=1e5; 5 | int n, a[mxN], t[mxN], sortedbyA[mxN], ans; 6 | priority_queue, greater> waiting; 7 | 8 | int main() { 9 | ifstream cin("convention2.in"); 10 | ofstream cout("convention2.out"); 11 | 12 | //input 13 | cin >> n; 14 | for(int i=0; i> a[i] >> t[i]; 16 | sortedbyA[i]=i; 17 | } 18 | sort(sortedbyA, sortedbyA+n, [&](const int &i, const int &j) { 19 | return a[i]=n) 25 | break; 26 | //update time to when next one arrives 27 | time=a[sortedbyA[i]]; 28 | //add next cow to arrive 29 | waiting.push(sortedbyA[i++]); 30 | } 31 | int next=waiting.top(); 32 | waiting.pop(); 33 | //update ans according to wait time 34 | ans=max(time-a[next], ans); 35 | //update time 36 | time+=t[next]; 37 | //add the cows that arrived 38 | while(i 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=1e5, M=1e9+7; 7 | int n, k, a[mxN+1]; 8 | 9 | ll dp(ll b, int m) { 10 | vector dp{1, 1}; 11 | for(int i=1; i> n >> k; 26 | for(int i=1; i<=n-k+1; ++i) 27 | cin >> a[i], a[i]=M-6-a[i]; 28 | a[0]=a[n-k+2]=M; 29 | ll ans=1; 30 | for(int i=1; i<=n-k+1; ) { 31 | int j=i; 32 | while(++j<=n-k+1&&a[j]==a[i]); 33 | ans=ans*dp(a[i], j-i-((a[i-1] 2 | using namespace std; 3 | 4 | const int mxN=1e6; 5 | int n; 6 | double p[mxN], ans, a, b=1; 7 | 8 | int main() { 9 | ifstream cin("cowdate.in"); 10 | ofstream cout("cowdate.out"); 11 | 12 | cin >> n; 13 | for(int i=0; i> p[i], p[i]/=1e6; 15 | for(int i1=0, i2=0; i1 2 | using namespace std; 3 | 4 | const int mxN=300; 5 | int n, ans, a[mxN][mxN]; 6 | string g[mxN], g2[mxN]; 7 | 8 | void solve() { 9 | memset(a, 0, sizeof(a)); 10 | for(int i=0; i=k; ++k) { 20 | if(g[i][j+k]=='.'||g[i+k][j]=='.') 21 | continue; 22 | int k2=min(k, j); 23 | ans+=a[i-k+k2][j-k2]; 24 | if(i>=k) 25 | ans-=a[i-k][j]; 26 | } 27 | } 28 | } 29 | } 30 | 31 | void rot() { 32 | for(int i=0; i> n; 44 | for(int i=0; i> g[i]; 46 | g2[i].resize(n); 47 | } 48 | for(int k=0; k<4; ++k) { 49 | solve(); 50 | rot(); 51 | } 52 | cout << ans; 53 | } 54 | -------------------------------------------------------------------------------- /USACO/Contests/1920_4P/Sprinklers2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | const int mxN=2e3, M=1e9+7, I2=5e8+4; 7 | int n; 8 | string g[mxN]; 9 | ll dp1[mxN+1][mxN+1], dp2[mxN+1][mxN+1]; 10 | 11 | int main() { 12 | ifstream cin("sprinklers2.in"); 13 | ofstream cout("sprinklers2.out"); 14 | 15 | cin >> n; 16 | for(int i=0; i> g[i]; 18 | dp1[0][0]=dp2[0][0]=1; 19 | for(int i=0; i<=n; ++i) { 20 | for(int j=0; j<=n; ++j) { 21 | if(i 8 | using namespace std; 9 | 10 | int n, ans1, ans2[24], tmp[24]={1, 2}; 11 | 12 | void dfs(int d) { 13 | if(ans2[0]) 14 | return; 15 | int n2=tmp[d-1]; 16 | if(n2==n) 17 | memcpy(ans2, tmp, sizeof(ans2)); 18 | else { 19 | if(d>=ans1||n2>n) 20 | return; 21 | for(int i=0; i=0; --i) { 26 | for(int j=d-1; j>=i&&tmp[i]+tmp[j]>tmp[d-1]; --j) { 27 | tmp[d]=tmp[i]+tmp[j]; 28 | dfs(d+1); 29 | } 30 | } 31 | } 32 | } 33 | 34 | int main() { 35 | ios_base::sync_with_stdio(0); 36 | cin.tie(0); 37 | 38 | while(1) { 39 | cin >> n; 40 | if(!n) 41 | break; 42 | ans2[0]=0; 43 | for(ans1=1; !ans2[0]; ++ans1) 44 | dfs(1); 45 | cout << ans2[0]; 46 | for(int i=1; i