├── .gitignore ├── Brute_Force └── all_combinations.cpp ├── C++_Utils └── printf_scanf.cpp ├── Data_Structures ├── C++ STL │ ├── array.cpp │ ├── bitset.cpp │ ├── deque.cpp │ ├── iterators.cpp │ ├── list.cpp │ ├── map & multimap.cpp │ ├── ordered_set.cpp │ ├── pairs & tuples.cpp │ ├── priority_queue.cpp │ ├── queue & stack.cpp │ ├── set & multiset.cpp │ ├── unordered_map&multimap.cpp │ ├── unordered_set&multiset.cpp │ └── vector.cpp ├── WaveletTree.cpp ├── fenwicktree.cpp ├── fenwicktree2D.cpp ├── segment-tree-lazy.cpp ├── segment-tree.cpp ├── sparse-table.cpp └── unionfind.cpp ├── Divide&Conquer └── mergesort.cpp ├── Dynamic_Programming ├── Divide&ConquerOptimization.cpp ├── Knapsack.cpp ├── LIS.cpp ├── TSP.cpp └── fibonnacci.cpp ├── Geometry ├── 2D.cpp ├── 3D.cpp ├── ConvexHull.cpp ├── GreensTheorem.cpp ├── SmallestEnclosingCircle.cpp └── Trigonometry.cpp ├── Graphs ├── BFS.cpp ├── BellmanFord.cpp ├── CentroidDecomposition.cpp ├── DFS.cpp ├── Dijkstra.cpp ├── Dinic.cpp ├── EdmondsKarp.cpp ├── FloydWarshall.cpp ├── LA.cpp ├── LCA.cpp ├── MinimumSpanningTree.cpp ├── SCC.cpp ├── TopoSort.cpp ├── articulation-points,cut-edges,biconnected-components.cpp └── diameter_of_tree.cpp ├── Mathematics ├── binary_modular_exponentiation.cpp ├── chinese_remainder_theorem.cpp ├── euclidean_algorithm.cpp ├── integer_root_square.cpp ├── modular_binomial_coefficient.cpp ├── modular_fibonacci.cpp ├── modular_multinomial_coefficient.cpp ├── primality_test.cpp ├── prime_factorization.cpp └── sieve_of_eratosthenes.cpp ├── Parsers └── ArithmeticParser.cpp ├── Search ├── BinarySearch.cpp └── TernarySearch.cpp ├── Solved problems ├── AtCoder │ ├── abc128_d_equeue.cpp │ ├── abc128_e_Roadwork.cpp │ ├── abc159_f_KnapsackForAllSegments.cpp │ └── arc139_a_TrailingZeroes.cpp ├── Codechef │ ├── AMCS03_RaceTime!.cpp │ ├── GEEK04_BridgeCrossing.cpp │ ├── GEEK04_BridgeCrossing_v2.cpp │ ├── ICM2003_IsThisJEE.cpp │ └── SPREAD_Gravel.cpp ├── Codeforces │ ├── 1015B_ObtainingTheString.cpp │ ├── 1015C_SongsCompression.cpp │ ├── 101E_CandiesAndStones.cpp │ ├── 1042B_Vitamins.cpp │ ├── 1043A_Elections.cpp │ ├── 1070A_FindANumber.cpp │ ├── 1105D_KilaniAndTheGame.cpp │ ├── 1163B2_CatParty(HardEdition).cpp │ ├── 1175E_MinimalSegmentCover.cpp │ ├── 1180C_ValeriyAndDeque.cpp │ ├── 1189C_Candies!.cpp │ ├── 119E_AlternativeReality.cpp │ ├── 1209E2_RotateColumns(HardVersion).cpp │ ├── 1209F_KoalaAndNotebook │ │ ├── canonical.cpp │ │ ├── gen.py │ │ └── sol.cpp │ ├── 1243A_MaximumSquare.cpp │ ├── 1243B1_CharacterSwap(EasyVersion).cpp │ ├── 1243B1_CharacterSwap(HardVersion).cpp │ ├── 1243C_TilePainting.cpp │ ├── 1270B_InterestingSubarrays.cpp │ ├── 1270C_MakeGood.cpp │ ├── 1270D_StrangeDevice.cpp │ ├── 1270E_DivideThePoints.cpp │ ├── 1279B_VerseForSanta.cpp │ ├── 1279C_StackOfPresents.cpp │ ├── 1279D_Santa'sBot.cpp │ ├── 1279E_NewYearPermutations.cpp │ ├── 1282A_TemporarilyUnavailable.cpp │ ├── 1282B1_KforthePriceofOne(HardVersion).cpp │ ├── 1282B2_KforthePriceofOne(HardVersion).cpp │ ├── 1282C_PetyaAndExam.cpp │ ├── 1282D_EnchantedArtifact.cpp │ ├── 1282E_TheCakeIsALie.cpp │ ├── 1284A_NewYearAndNaming.cpp │ ├── 1284B_NewYearAndAscentSequence.cpp │ ├── 1284C_NewYearAndPermutation.cpp │ ├── 1284D_NewYearAndConference.cpp │ ├── 1284E_NewYearAndCastleConstruction.cpp │ ├── 1285D_Dr.EvilUnderscores.cpp │ ├── 1285E_DeleteASegment.cpp │ ├── 1287B_Hyperset.cpp │ ├── 1287B_Hyperset.py │ ├── 1287C_Garland.cpp │ ├── 12D_Ball.cpp │ ├── 1324F_MaximumWhiteSubtree.cpp │ ├── 1326E_Bombs.cpp │ ├── 1359F_RCKaboomShow.cpp │ ├── 1359F_RCKaboomShow_v2.cpp │ ├── 1365A_MatrixGame.cpp │ ├── 1365B_TroubleSort.cpp │ ├── 1365C_RotationMatching.cpp │ ├── 1365D_SolveTheMaze.cpp │ ├── 1365E_MaximumSubsequenceValue.cpp │ ├── 1365F_SwapsAgain.cpp │ ├── 1372C_OmkarAndBaseball.cpp │ ├── 1372D_OmkarAndCircle.cpp │ ├── 1385D_a-GoodString.cpp │ ├── 1385E_DirectingEdges.cpp │ ├── 1385F_RemovingLeaves.cpp │ ├── 1428E_CarrotsForRabbits.cpp │ ├── 14C_FourSegments.cpp │ ├── 1516C_BabyEhabPartitionsAgain.cpp │ ├── 1519E_OffByOne.cpp │ ├── 1523D_Love-Hate.cpp │ ├── 1523D_Love-Hate.py │ ├── 160D_EdgesInMST.cpp │ ├── 163E_e-Government.cpp │ ├── 166E_Tetrahedron.cpp │ ├── 182E_WoodenFence.cpp │ ├── 230B_T-primes.cpp │ ├── 234G_Practice.cpp │ ├── 235A_LCM_Challenge.cpp │ ├── 245H_QueriesForNumberOfPalindromes.cpp │ ├── 24E_BerlandCollider.cpp │ ├── 269B_GreenhouseEffect.cpp │ ├── 26A_AlmostPrime.cpp │ ├── 276D_LittleGirlAndMaximumXOR.cpp │ ├── 289B_PoloThePenguinAndMatrix.cpp │ ├── 295A_GregAndArray.cpp │ ├── 300C_BeautifulNumbers.cpp │ ├── 339D_XeniaAndBitOperations.cpp │ ├── 342E_XeniaAndTree.cpp │ ├── 346B_LuckyCommonSubsequence.cpp │ ├── 350D_LookingForOwls.cpp │ ├── 354E_LuckyNumberRepresentation.cpp │ ├── 373B_MakingSequencesIsFun.cpp │ ├── 379F_NewYearTree.cpp │ ├── 418D_BigProblemsForOrganizers.cpp │ ├── 427C_Checkposts.cpp │ ├── 427D_Match&Catch.cpp │ ├── 427E_PolicePatrol.cpp │ ├── 429B_WorkingOut.cpp │ ├── 432D_PrefixesAndSuffixes.cpp │ ├── 445A_DZY_LovesChessboard.py │ ├── 446B_DZYLovesModification.cpp │ ├── 448C_PaintingFence.cpp │ ├── 448C_PaintingFence_v2.cpp │ ├── 452E_ThreeStrings.cpp │ ├── 453A_LittlePonyAndExpectedMaximum.cpp │ ├── 455A_Boredom.cpp │ ├── 459B_PashmakAndFlowers.cpp │ ├── 466C_NumberOfWays.cpp │ ├── 467C_GeorgeAndJob.cpp │ ├── 471D_MUHandCubeWalls.cpp │ ├── 474B_Worms.py │ ├── 492B_VanyaAndLanterns.cpp │ ├── 49B_Sum.cpp │ ├── 513C_SecondPriceAuction.cpp │ ├── 514A_ChewbaccaAndNumber.cpp │ ├── 520B_TwoButtons.cpp │ ├── 524C_TheArtOfDealingWithATMs.cpp │ ├── 528D_FuzzySearch.cpp │ ├── 543A_WritingCode.cpp │ ├── 579E_WeaknessAndPoorness.cpp │ ├── 579E_WeaknessAndPoorness_v2.cpp │ ├── 580A_KefaAndFirstSteps.cpp │ ├── 580C_KefaAndPark.cpp │ ├── 5B_CenterAlignment.cpp │ ├── 5B_CenterAlignment.py │ ├── 600B_QueriesAboutLessOrEqualNumbers.py │ ├── 609E_MinimumSpanningTreeForEachEdge.cpp │ ├── 616C_TheLabyrinth.cpp │ ├── 622F_TheSumOfTheK-thPowers.cpp │ ├── 645C_EnduringExodus.cpp │ ├── 652E_PursuitForArtifacts.cpp │ ├── 659C_TanyaAndToys.cpp │ ├── 675A_InfiniteSequence.cpp │ ├── 681E_RunawayToAShadow.cpp │ ├── 682C_AlyonaAndTheTree.cpp │ ├── 687B_RemaindersGame.cpp │ ├── 701C_TheyAreEverywhere.cpp │ ├── 709A_Juicer.cpp │ ├── 709B_Checkpoints.cpp │ ├── 709C_LetterCyclicShift.cpp │ ├── 709D_RecoverTheString.cpp │ ├── 709E_Centroids.cpp │ ├── 712B_MemoryAndTrident.cpp │ ├── 732D_Exams.cpp │ ├── 740A_AlyonaAndCopybooks.cpp │ ├── 740B_AlyonaAndFlowers.cpp │ ├── 766B_MahamoudAndTriangle.cpp │ ├── 767B_TheQueue.cpp │ ├── 768B_CodeFor1.cpp │ ├── 769C_CycleInMaze.cpp │ ├── 780B_TheMeetingPlaceCannotBeChanged.cpp │ ├── 811E_VladikAndEntertainingFlags.cpp │ ├── 811E_VladikAndEntertainingFlags_v2.cpp │ ├── 839C_Journey.cpp │ ├── 863E_TurnOffTheTV.py │ ├── 86D_PowerfulArray.cpp │ ├── 873D_MergeSort.cpp │ ├── 895C_SquareSubsets.cpp │ ├── 8C_LookingForOrder.cpp │ ├── 919D_Substring.cpp │ ├── 958A2_DeathStars(medium).cpp │ ├── 97E_Leaders.cpp │ ├── 982E_Billiard.cpp │ ├── 993A_TwoSquares.cpp │ ├── 999E_ReachabilityFromTheCapital.cpp │ ├── gym_100570D_TROYQuery.cpp │ ├── gym_100733F_CosmicTimeline.cpp │ ├── gym_100733H_DesignationInTheMafia.cpp │ ├── gym_100829A_ChasingTheCheetahs.cpp │ ├── gym_100960G_YounglingTournament.cpp │ ├── gym_101612_Fygon2.0.cpp │ ├── gym_101873G_WaterTesting.cpp │ ├── gym_101875D_Checkerbaord.cpp │ ├── gym_101875G_TrafficManagement.cpp │ ├── gym_102263E_LongestPathProblem.cpp │ ├── gym_103185B_BeautifulMountains.cpp │ ├── gym_103185E_ExcellentViews.cpp │ ├── gym_103185M_MayIAddALetter_v1.cpp │ ├── gym_103640E_ExpeditionPlans │ │ ├── generator.py │ │ ├── gym_103640E_ExpeditionPlans(slow).cpp │ │ └── gym_103640E_ExpeditionPlans.cpp │ ├── gym_103640G_GeneratorTree_v1.cpp │ ├── gym_103640G_GeneratorTree_v2.cpp │ ├── outputs_comparators.py │ └── random_input_generator.py ├── Codejam │ ├── 2017 │ │ ├── round_0 │ │ │ ├── A_OversizedPancakeFlipper.cpp │ │ │ ├── B_TidyNumbers.cpp │ │ │ ├── C_BathroomStalls.cpp │ │ │ ├── C_BathroomStalls_v2.cpp │ │ │ └── D_FashionShow.cpp │ │ └── round_1B │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 2021 │ │ └── round0 │ │ │ ├── MoonsAndUmbrellas.cpp │ │ │ └── ReverseSort.cpp │ └── 2022 │ │ ├── round0 │ │ ├── 3DPrinting.cpp │ │ ├── PunchedCards.cpp │ │ └── d1000000.cpp │ │ ├── round1A │ │ ├── DoubleOrOneThing.cpp │ │ ├── EqualSum.cpp │ │ └── local_testing_tool.py │ │ ├── round1B │ │ ├── ASeDatAb.cpp │ │ ├── ASeDatAb.py │ │ ├── ControlledInflation.cpp │ │ └── Pancake.cpp │ │ └── round2 │ │ ├── PixelatedCircle.cpp │ │ ├── SavingTheJelly.cpp │ │ └── SpiralingIntoControl.cpp ├── Leetcode │ └── 1397_FindAllGoodStrings.cpp ├── LightOJ │ └── 1146_ClosestDistance.cpp ├── LiveArchive │ ├── 2387_GeneAssembly.cpp │ ├── 2389_PalindromNumbers.cpp │ ├── 3142_Bullseye.cpp │ ├── 3143_AnExcel-lentProblem.cpp │ ├── 3144_LennysLuckyLottoLists.cpp │ ├── 3145_FlippingPancake.cpp │ ├── 3146_CardTrick.cpp │ ├── 3147_ModelRocketHeight.cpp │ ├── 3148_RegionFilling.cpp │ ├── 3150_MrYoungsPicturePermutations.cpp │ ├── 3153_LongNightOfMuseums.cpp │ ├── 3197_StackingCylinders.cpp │ ├── 3476_LightUp.cpp │ ├── 3493_01000001.cpp │ ├── 3652_LazyJumpingFrog.cpp │ ├── 3653_Jukebox.cpp │ ├── 3657_PowerGeneration.cpp │ ├── 3658_ReportRecovery.cpp │ ├── 3659_TurkishRoulette.cpp │ ├── 3686_EncryptingPasswords.cpp │ ├── 3687_FootballFoundation.cpp │ ├── 3691_TheExplorer.cpp │ ├── 4008_LastDigit.cpp │ ├── 4010_GeophysicsProspection.cpp │ ├── 4010_GeophysicsProspection.py │ ├── 4011_DominoArt.cpp │ ├── 4099_Sub-dictionary.cpp │ ├── 4212_Candy.cpp │ ├── 4214_Electricity.cpp │ ├── 4215_Feynman.cpp │ ├── 4216_PolePosition.cpp │ ├── 4271_Necklace.cpp │ ├── 4382_YAPTCHA.cpp │ ├── 4476_ElectricBill.cpp │ ├── 4809_ElectricNeeds │ │ ├── canonical.cpp │ │ ├── gen.py │ │ └── sol.cpp │ ├── 4938_ManualCipher.cpp │ ├── 5319_SumItUp.cpp │ ├── 5792_DiccionárioPortuñol.cpp │ ├── 5794_FileRetrieval.cpp │ ├── 5822_DistributingBallotBoxes.cpp │ ├── 5822_DistributingBallotBoxes_v2.cpp │ ├── 6133_CellphoneTyping.cpp │ ├── 6135_EnvironmentProtection.cpp │ ├── 6137_GameOfTiles.cpp │ ├── 6525_AttackingRooks.cpp │ ├── 6528_DisjointWaterSupply.cpp │ ├── 6579_CleaningTheHallway.cpp │ ├── 6821_AutomatedCheckingMachine.cpp │ ├── 6822_BlackAndWhiteStones.cpp │ ├── 6823_CountingSubsthreengs.cpp │ ├── 6824_DividingTheNames.cpp │ ├── 6825_EvenDistribution.cpp │ ├── 6826_FenceTheVegetables.cpp │ ├── 6831_KnightsOfTheRoundTable.cpp │ ├── 7203_AtMostTwice.cpp │ ├── 7204_BloodGroups.cpp │ ├── 7205_CakeCut_v1.cpp │ ├── 7205_CakeCut_v2.cpp │ ├── 7206_DasInDaedalus.cpp │ ├── 7207_ExposingCorruption.cpp │ ├── 7210_HeightMap.cpp │ ├── 7211_IdentifyingTea.cpp │ ├── 7212_JustABitSorted.cpp │ ├── 7213_KeepItEnergized.cpp │ ├── 7213_KeepItEnergized_v2.cpp │ ├── 7324_ASCII_Addition.cpp │ ├── 7413_SqueezeTheCylinders.cpp │ ├── 7887_BackToTheFuture.cpp │ ├── 8189_BuggyICPC.cpp │ ├── 8190_CompleteNaebbirac'sSequence.cpp │ ├── 8192_Enigma.cpp │ ├── 8194_GatesOfUncertainty.cpp │ ├── 8195_HardChoice.cpp │ ├── 8198_KeepItCovered.cpp │ ├── 8200_Marblecoin.cpp │ └── random_input.cpp ├── Matcomgrader │ ├── BuildThePerfectHouse.cpp │ ├── CutInequalityDown.cpp │ ├── CutInequalityDown_v2.cpp │ ├── DazzlingStars.cpp │ ├── DazzlingStars_v2.cpp │ ├── DazzlingStars_v3.cpp │ ├── EggfruitCake.cpp │ ├── FabricatingSculptures.cpp │ ├── GluingPictures.cpp │ ├── HoldOrContinue.cpp │ ├── ImproveSPAM.cpp │ ├── JumpingGrasshoper.cpp │ ├── LeverageMDT.cpp │ └── MountainRanges.cpp ├── POJ │ ├── 2839_ConvexHullAndTriangle │ │ ├── canonical.py │ │ ├── gen.py │ │ ├── solution.cpp │ │ └── truefraction.py │ └── 2863_MissionImpossible.cpp ├── Red de Programación Competitiva │ ├── 2016-Competencia-11 │ │ ├── B_HotPotato.cpp │ │ ├── E_HexadecimalStatistic.cpp │ │ ├── G_Go__.cpp │ │ ├── H_huaauhahhuahau.cpp │ │ ├── J_OlympicGames.cpp │ │ └── L_Tiles.cpp │ ├── 2016-Competencia-12 │ │ ├── B_BuscandoElCamino.cpp │ │ ├── C_Correlatividades.cpp │ │ ├── D_DibujandoTriangulos.cpp │ │ ├── G.java │ │ ├── G_GestionEficiente.cpp │ │ ├── I_InvasionDeInsectos.cpp │ │ ├── J_JuntandoLineas.cpp │ │ ├── K_Koalas.cpp │ │ └── L_LeonardoDePisa.cpp │ ├── 2016-Competencia-14 │ │ ├── A_Dwarves.cpp │ │ ├── E_ModelRailroad.cpp │ │ ├── F_OneWayRoads.cpp │ │ ├── I_CommonKnowledge.cpp │ │ ├── J_SellingCPUs.cpp │ │ └── K_Routing.cpp │ ├── 2016-Competencia-7 │ │ ├── B_FalconDive.cpp │ │ └── I_HackingTheScreen.cpp │ ├── 2016-Competencia-8 │ │ ├── A_FreightTrain.cpp │ │ ├── E_TheKingsWalk.cpp │ │ ├── F_MapColouring.cpp │ │ ├── I_SixDegrees.cpp │ │ └── N_MedalRanking.cpp │ ├── 2016-Competencia-9 │ │ ├── B_TautobotsAndContradictions.cpp │ │ ├── F_FactorialProducts.cpp │ │ ├── G_PacManForYourNewPhone.cpp │ │ ├── I_WalkingInTheSun.cpp │ │ └── J_ShoppingSpree.cpp │ └── TCP-2017 │ │ └── D_CocinaDeProgramadores.cpp ├── SPOJ │ ├── ALLIZWEL.cpp │ ├── BBIN2_BúsquedaBinaria2.cpp │ ├── BBIN_BusquedaBinaria.cpp │ ├── BTCK_A-problem-of-Backtracking_v1.cpp │ ├── BTCK_A-problem-of-Backtracking_v2.cpp │ ├── BURGLARY_BoatBurglary.cpp │ ├── CAPCITY_CapitalCity.cpp │ ├── CERC07C_CellPhone.cpp │ ├── CERC07K_KeyTask.cpp │ ├── CLEANRBT_CleaningRobot.cpp │ ├── COCONUTS_Coconuts_sol1.cpp │ ├── COCONUTS_Coconuts_sol2.cpp │ ├── CROSSPDCT_Left-right-or-center.cpp │ ├── CTOI09_1_IOI2009Mecho.cpp │ ├── CVJETICI_Cvjetici.cpp │ ├── CVXPOLY_ConvexPolygons.cpp │ ├── DINOSM_DinosaurMenace.cpp │ ├── DQUERY_D-query.cpp │ ├── DQUERY_D-query_v2.cpp │ ├── EC_DIVS_Divisors.cpp │ ├── FACT1.cpp │ ├── FENTREE_FenwickTrees.cpp │ ├── FINDPRM_FindingPrimes.cpp │ ├── GOODA_GoodTravels.cpp │ ├── GS_GoingToSchool.cpp │ ├── HISTOGRA_LargestRectangleInAHistogram.cpp │ ├── HISTOGRA_LargestRectangleInAHistogram.py │ ├── INOROUT_Inside_or_outside.cpp │ ├── KGSS_MaximumSum.cpp │ ├── KOPC12A_BuildingConstruction.cpp │ ├── LCA_LowestCommonAncestor.cpp │ ├── MATSUM_MatrixSummation.cpp │ ├── MCARDS_CardSorting.cpp │ ├── MDOLLS_NestedDolls.cpp │ ├── MIXTURES_Mixtures.cpp │ ├── MTOTALF_TotalFlow.cpp │ ├── MULTII_YetAnotherMultipleProblem.cpp │ ├── PLD_Palindromes.cpp │ ├── PRIME1_PrimeGenerator.cpp │ ├── PRINT_PrimeIntervals.cpp │ ├── SALMAN_SalaryManagement.cpp │ ├── SAMER08A_AlmostShortestPath.cpp │ ├── SAMER08H_HiggsBoson.cpp │ ├── SCUBADIV_ScubaDiver.cpp │ ├── SEGMENTS.cpp │ ├── TAP2016I_InsectInvasion.cpp │ ├── TAXI_taxi.cpp │ ├── UPDATEIT_UpdateTheArray!.cpp │ ├── VBOSS_WhoIsTheBoss.cpp │ ├── WEIRDFN_WeirdFunction.cpp │ ├── WEIRDFN_WeirdFunction.py │ └── gen.py ├── TCP2024 │ └── H │ │ ├── gen.py │ │ ├── sanity_check_graph.cpp │ │ ├── sol_slow.cpp │ │ ├── visualize_input_output.py │ │ └── visualize_input_output_interactive.py ├── URI │ ├── AmbiguousCodes.cpp │ ├── ArrangingHeaps.cpp │ ├── ArrangingTiles.cpp │ ├── AssigningTeams.cpp │ ├── AttackingRooks.cpp │ ├── BackToTheFuture_v1.cpp │ ├── BackToTheFuture_v2.cpp │ ├── BallStacking.cpp │ ├── Balloon.cpp │ ├── CheapTrips.cpp │ ├── CountingOnes.cpp │ ├── CountingSelfRotatingSubsets_v1.cpp │ ├── CountingSelfRotatingSubsets_v2.cpp │ ├── DNASubsequences.cpp │ ├── DatingOn-Line.cpp │ ├── DauntingDevice.cpp │ ├── ElectricalPollution.cpp │ ├── FantasticBeasts.cpp │ ├── FarmRobot.cpp │ ├── Fundraising.cpp │ ├── GameOfMatchings.cpp │ ├── GatheringRed-BlackFruits.cpp │ ├── HexStatistics.cpp │ ├── HideAndSeek.cpp │ ├── HighMountains.cpp │ ├── HighMountains_v2.cpp │ ├── HotelRewards.cpp │ ├── HyperactiveGirl.cpp │ ├── ImperialRoads.cpp │ ├── InternetTrouble.cpp │ ├── InternetTrouble.py │ ├── IntervalProduct.cpp │ ├── JoinTwoKingdoms.cpp │ ├── JourneyThroughTheKingdom.cpp │ ├── JumpingFrog.cpp │ ├── JupiterAttacks.cpp │ ├── JustInTime.cpp │ ├── KillTheWerewolf.cpp │ ├── Linearville.cpp │ ├── LookingForTheRiskFactor.cpp │ ├── OlympicGames.cpp │ └── SecretChamberAtMountRushmore.cpp ├── UVA │ ├── 10276_HanoiTowerTroublesAgain!.cpp │ ├── 105_TheSkylineProblem.cpp │ ├── 11413_FillTheContainers.cpp │ ├── 12086_Potentiometers.cpp │ ├── 12145_Bases.cpp │ ├── 12193_Hooligan.cpp │ ├── 12244_GrowingStrings.cpp │ ├── 12358_Candy'sCandy.cpp │ ├── 12362_GardenFence │ │ ├── canonical.cpp │ │ ├── gen.py │ │ └── sol.cpp │ ├── 1238_FreeParentheses.cpp │ ├── 12524_ArrangingHeaps.cpp │ ├── 12533_JoiningCouples.cpp │ ├── 12672_Eleven.cpp │ ├── 12674_GoUpTheUltras.cpp │ ├── 13009_FenceTheVegetablesFail.cpp │ ├── 13010_GalacticTaxes.cpp │ ├── 13117_ACIS_A_Contagious_vIruS.cpp │ ├── 1575_Factors.cpp │ ├── 331_MappingTheSwaps.cpp │ ├── 441_Lotto.cpp │ ├── 558_Wormholes.cpp │ ├── 750_8-Queens-Chess-Problem.cpp │ └── 7788_Tight-FitSoduku.cpp ├── ZOJ │ └── 2675_LittleMammoth.cpp ├── acm.timus.ru │ ├── 1017_Staircases.cpp │ ├── 1037_MemoryManagement.cpp │ ├── 1198_Jobbery.cpp │ ├── 1361_SpaceologyVsChronistics.cpp │ ├── 1423_StringTale.cpp │ ├── 1752_Tree2.cpp │ └── tmp.cpp ├── aizu │ └── 2491_Sanpo.cpp ├── fackebook_hacker_cup │ ├── 2021 │ │ ├── QualificationRound │ │ │ ├── A1.cpp │ │ │ ├── A2.cpp │ │ │ ├── B.cpp │ │ │ └── C1.cpp │ │ └── Round1 │ │ │ ├── A1.cpp │ │ │ ├── A2.cpp │ │ │ └── B.cpp │ └── 2022 │ │ ├── QualificationRound │ │ ├── A.cpp │ │ └── B2.cpp │ │ └── Round1 │ │ ├── A1.cpp │ │ ├── A2.cpp │ │ ├── B1.cpp │ │ └── B2.cpp ├── hackerrank │ ├── ColoringGrid.cpp │ ├── GuardiansOfTheLunatic.cpp │ ├── RoadsInHackerLand.cpp │ └── TheCoinChangeProblem.cpp ├── icpcastana_challenge │ └── main.cpp ├── kattis │ ├── AbstractArt.cpp │ ├── AbstractArt_v2.cpp │ ├── AddingWords.cpp │ ├── AddingWords.py │ ├── AllAboutThatBase.cpp │ ├── AllPairsShortestPath.cpp │ ├── AssociationForCoolMachineries(part1).cpp │ ├── AssociationForCoolMachineries(part1)_v2.cpp │ ├── BobbysBet.cpp │ ├── BuildDependencies.cpp │ ├── Cetvrta.cpp │ ├── Collapse.cpp │ ├── CompoundWords.cpp │ ├── CountingStars.cpp │ ├── CountingTriangles.cpp │ ├── CrazyDriver.cpp │ ├── DigitalDisplay.cpp │ ├── EavesdropperEvasion.cpp │ ├── FairWarning.class │ ├── FairWarning.java │ ├── ForestForTheTrees.cpp │ ├── FreightTrain.cpp │ ├── Funhouse.cpp │ ├── GameRank.cpp │ ├── H-Index.cpp │ ├── H-Index.py │ ├── Heliocentric.cpp │ ├── HiddenPassword.cpp │ ├── InOrOut.cpp │ ├── KeepingTheDogsApart.cpp │ ├── KeepingTheDogsApart_v2.cpp │ ├── LimitedCorrespondence.cpp │ ├── MagicalMysterKnightsTour.cpp │ ├── MapColouring.cpp │ ├── MarblesOnATree.cpp │ ├── MatrixInverse.cpp │ ├── MoneyForNothing │ │ ├── MoneyForNothing.cpp │ │ ├── MoneyForNothing__slow.cpp │ │ └── generator.py │ ├── NeedForSpeed.cpp │ ├── NeighborhoodWatch.cpp │ ├── Pivot.cpp │ ├── PizzaHawaii.cpp │ ├── PizzaHawaii.py │ ├── PlantingTrees.cpp │ ├── PolynomialMultiplication1.cpp │ ├── Preludes.cpp │ ├── PrimeReduction.cpp │ ├── PrimeSpiral.cpp │ ├── QuickBrownFox.cpp │ ├── Reset.cpp │ ├── ReversedBinaryNumbers.cpp │ ├── SecretMessage.cpp │ ├── SierpinskiCircumference.cpp │ ├── SimonSays.cpp │ ├── Statistics.cpp │ ├── SumOfTheOthers.cpp │ ├── TakeTwoStones.cpp │ ├── TightFitSudoku.cpp │ ├── TornToPieces.cpp │ ├── TouchscreenKeyboard.cpp │ ├── TreasureSpotting.cpp │ ├── UnDetected.cpp │ ├── XYZZY.cpp │ ├── Zagrade.cpp │ ├── Zagrade.py │ ├── crisscrosscables.cpp │ ├── flightplan.cpp │ ├── generalchineseremainder.cpp │ ├── incaseofinvasion.cpp │ ├── pizzaproblems.cpp │ ├── rsamistake.py │ └── wipeyourwhiteboards.cpp ├── main.edu.pl │ └── Journey.cpp ├── others │ ├── AirRaid.cpp │ ├── AlexIsRight.cpp │ ├── AnotherCrisis.cpp │ ├── ArmyBuddies.cpp │ ├── BigNumber.cpp │ ├── BinPacking.cpp │ ├── Bingo.cpp │ ├── Brothers.cpp │ ├── BubbleMaps.cpp │ ├── Candy.cpp │ ├── CardTrick.cpp │ ├── ChatRooms.cpp │ ├── Cipher.cpp │ ├── Composition.cpp │ ├── Diamonds.cpp │ ├── DifferentDigits.cpp │ ├── DigitsCount.cpp │ ├── DinnerHall.cpp │ ├── DrawingQuadrilaterals.cpp │ ├── ElectricBill.cpp │ ├── EnigmaticTravel.cpp │ ├── FindTheMultiple.cpp │ ├── FixThePond.cpp │ ├── FlowersFlourishFromFrance.cpp │ ├── GalaxyCollision.cpp │ ├── GatheringRoquefort.cpp │ ├── Grapevine.cpp │ ├── GrowingStrings.cpp │ ├── HedgeMazes.cpp │ ├── HelpCupid.cpp │ ├── Hooligan.cpp │ ├── HoursAndMinutes.cpp │ ├── HyperactiveGirl.cpp │ ├── ICPCStrikesAgain.cpp │ ├── ImmortalPorpoises.cpp │ ├── InBraille.cpp │ ├── Inca-Mancora.cpp │ ├── IntrepidClimber.cpp │ ├── IsoscelesTriangles.cpp │ ├── JingleComposing.cpp │ ├── Jollo.cpp │ ├── KidsWishes.cpp │ ├── KingsPoker.cpp │ ├── KlingonLevels.cpp │ ├── LandDivisionTax.cpp │ ├── NKMARS.cpp │ ├── RemedialForecast.cpp │ ├── Rummaging.cpp │ ├── Skyscrapers.cpp │ ├── TheMysteriousXNetwork.cpp │ ├── Tyndex.Brome.cpp │ ├── UpdateTheArray.cpp │ ├── ValuesWhoseSumIs0.cpp │ ├── ZingZungOysterFarm.cpp │ └── test.cpp ├── test.py ├── test_written_cases.py └── time_command.py ├── Strings ├── AhoCorasick.cpp ├── KMP.cpp ├── RollingHashing.cpp ├── Trie.cpp ├── shortest_repeating_cycle.cpp └── suffix_array.cpp ├── c++_cheat_sheet.cpp ├── c++_include.cpp ├── c++_template.cpp ├── latex.pdf ├── latex.tex ├── random_tests.cpp └── script.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | *.o 3 | *.in 4 | *.exe 5 | *.class 6 | *.txt 7 | *.zip 8 | 9 | ## Core latex/pdflatex auxiliary files: 10 | *.aux 11 | *.lof 12 | *.log 13 | *.lot 14 | *.fls 15 | *.out 16 | *.toc 17 | *.fmt 18 | *.fot 19 | *.cb 20 | *.cb2 21 | 22 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 23 | *.bbl 24 | *.bcf 25 | *.blg 26 | *-blx.aux 27 | *-blx.bib 28 | *.brf 29 | *.run.xml 30 | 31 | ## Build tool auxiliary files: 32 | *.fdb_latexmk 33 | *.synctex 34 | *.synctex(busy) 35 | *.synctex.gz 36 | *.synctex.gz(busy) 37 | *.pdfsync 38 | 39 | .vscode/ 40 | _ignoreme/ 41 | _debug*.cpp 42 | *.png 43 | __pycache__ 44 | failed_input -------------------------------------------------------------------------------- /Data_Structures/C++ STL/bitset.cpp: -------------------------------------------------------------------------------- 1 | bitset<4> foo; // 0000 2 | foo.size(); // 4 3 | foo.set(); // 1111 4 | foo.set(1,0); // 1011 5 | foo.test(1); // false 6 | foo.set(1); // 1111 7 | foo.test(1); // true -------------------------------------------------------------------------------- /Data_Structures/C++ STL/deque.cpp: -------------------------------------------------------------------------------- 1 | // references: 2 | // http://www.cplusplus.com/reference/deque/deque/ 3 | // https://www.geeksforgeeks.org/deque-cpp-stl/ 4 | // SUMMARY: deque can do the same things as vector 5 | // + push_front() + emplace_front() 6 | // - contiguous memory allocation is not guaranteed 7 | // (elements may be stored in fragmented chunks of memory) 8 | deque dq = { 1, 2, 3 }; 9 | dq.push_back(8); // { 1, 2, 3, 8 } 10 | dq.push_front(100); // { 100, 1, 2, 3, 8 } 11 | dq.pop_back(); // { 100, 1, 2, 3 } 12 | dq.pop_front(); // { 1, 2, 3} -------------------------------------------------------------------------------- /Data_Structures/C++ STL/pairs & tuples.cpp: -------------------------------------------------------------------------------- 1 | // references: 2 | // https://www.geeksforgeeks.org/returning-multiple-values-from-a-function-using-tuple-and-pair-in-c/ 3 | // http://www.cplusplus.com/reference/utility/pair/ 4 | // http://www.cplusplus.com/reference/tuple/ 5 | 6 | //====== 7 | // PAIR 8 | //====== 9 | // Example: pair of ints 10 | typedef pair ii; // use ii as abbreviation 11 | // initialization 12 | ii p(5,5); // option 1 13 | ii p = make_pair(5,5) // option 2 14 | ii p = {5, 5}; // option 3 15 | // getting values 16 | int x = p.first, y = p.second; 17 | // modifying values 18 | p.first++, p.second--; // p = {6, 4} 19 | 20 | //======= 21 | // TUPLE 22 | //======= 23 | // Example: tuples of 3 ints 24 | typedef tuple iii; // use iii as abbreviation 25 | // initialization 26 | iii t(5,5,5); // option 1 27 | iii t = make_tuple(5,5,5); // option 2 28 | iii t = {5, 5, 5}; // option 3 29 | // getting values 30 | int x,y,z; 31 | x = get<0>(t), y = get<1>(t), z = get<2>(t); // option 1 32 | tie(x,y,z) = t; // option 2 33 | // modifying values 34 | get<0>(t)++, get<1>(t)--, get<2>(t)+=2; // t = {6, 4, 7} -------------------------------------------------------------------------------- /Data_Structures/C++ STL/queue & stack.cpp: -------------------------------------------------------------------------------- 1 | // references: 2 | // http://www.cplusplus.com/reference/queue/queue/ 3 | // https://www.geeksforgeeks.org/queue-cpp-stl/ 4 | // http://www.cplusplus.com/reference/stack/stack/ 5 | // https://www.geeksforgeeks.org/stack-in-cpp-stl/ 6 | 7 | //===== QUEUE ===== 8 | queue q; 9 | // adding to queue 10 | rep(i,1,5) q.push(i); // q = {1, 2, 3, 4, 5} 11 | // OR 12 | rep(i,1,5) q.emplace(i); // q = {1, 2, 3, 4, 5} 13 | // removing from queue 14 | while (!q.empty()) { 15 | cout << q.front() << ' '; 16 | q.pop(); 17 | } // output: 1 2 3 4 5 18 | 19 | //===== STACK ===== 20 | stack s; 21 | // adding to stack 22 | rep(i,1,5) s.push(i); // s = {1, 2, 3, 4, 5} 23 | // OR 24 | rep(i,1,5) s.emplace(i); // s = {1, 2, 3, 4, 5} 25 | // removing from stack 26 | while (!s.empty()) { 27 | cout << s.top() << ' '; 28 | s.pop(); 29 | } // output: 5 4 3 2 1 -------------------------------------------------------------------------------- /Data_Structures/fenwicktree.cpp: -------------------------------------------------------------------------------- 1 | #include "../c++_template.cpp" 2 | 3 | struct FT { // fenwick tree 4 | vector t; 5 | FT(int n) { t.assign(n+1, 0); } 6 | int query(int i) { // sum in range 1 .. i 7 | int ans = 0; 8 | for (; i; i -= (i & -i)) ans += t[i]; 9 | return ans; 10 | } 11 | int query(int i, int j) { return query(j) - query(i-1); } // sum in range [i .. j] 12 | void update(int i, int v) { // increment i'th value by v (and propagate) 13 | for (; i < t.size(); i += i & (-i)) t[i] += v; 14 | } 15 | void update(int i, int j, int v) { update(i, v); update(j + 1, -v); } 16 | }; -------------------------------------------------------------------------------- /Data_Structures/fenwicktree2D.cpp: -------------------------------------------------------------------------------- 1 | #include "../c++_template.cpp" 2 | 3 | template struct FT2D { 4 | vector> ft; 5 | int R, C; 6 | FT2D(int _R, int _C) : R(_R+1), C(_C+1) { 7 | ft.assign(R, vector(C, 0)); 8 | } 9 | void add(int r, int c, T val) { 10 | for (int i = r; i < R; i += (i&-i)) 11 | for (int j = c; j < C; j += (j&-j)) 12 | ft[i][j] += val; 13 | } 14 | // sum[(1, 1), (r, c)] 15 | T sum(int r, int c) { 16 | T res = 0; 17 | for (int i = r; i; i -= (i&-i)) 18 | for (int j = c; j; j -= (j&-j)) 19 | res += ft[i][j]; 20 | return res; 21 | } 22 | // sum[(r1, c1), (r2, c2)] 23 | T sum(int r1, int c1, int r2, int c2) { 24 | return sum(r2, c2) - sum(r1-1, c2) - sum(r2, c1-1) + sum(r1-1, c1-1); 25 | } 26 | // get value at cell (r,c) 27 | T get(int r, int c) { 28 | return sum(r, c, r, c); 29 | } 30 | // set value to cell (r,c) 31 | void set(int r, int c, int value) { 32 | add(r, c, -get(r, c) + value); 33 | } 34 | }; -------------------------------------------------------------------------------- /Divide&Conquer/mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mergesort_(vector& a, int i, int j, vector& b) { 5 | if (i == j) return; // caso base 6 | // caso general 7 | int m = (i+j)/2; 8 | mergesort_(a, i, m, b); 9 | mergesort_(a, m+1, j, b); 10 | // merge eficiente 11 | int r = i, s = m+1; 12 | int k = i; 13 | while (r <= m and s <= j) b[k++] = a[r] <= a[s] ? a[r++] : a[s++]; 14 | while (r <= m) b[k++] = a[r++]; 15 | while (s <= j) b[k++] = a[s++]; 16 | for (int x = i; x <= j; ++x) a[x] = b[x]; 17 | } 18 | 19 | void mergesort(vector& a) { 20 | vector b(a.size()); 21 | mergesort_(a, 0, a.size()-1, b); 22 | } -------------------------------------------------------------------------------- /Dynamic_Programming/LIS.cpp: -------------------------------------------------------------------------------- 1 | // ===================================== 2 | // LIS (Longest Increasing Subsequence) 3 | // ===================================== 4 | // references: 5 | // https://stackoverflow.com/questions/2631726/how-to-determine-the-longest-increasing-subsequence-using-dynamic-programming 6 | const int MAXLEN = 1000000; 7 | // return the length of the longest increasing (non-decreasing) 8 | // subsequence in values 9 | int LIS(vector& values) { 10 | static int q[MAXLEN+1]; 11 | int len = 0; 12 | q[0] = -INT_MAX; // make sure it's strictly smallest 13 | for (int val : values) { 14 | if (q[len] < val) { // use <= if non-decreasing 15 | q[++len] = val; 16 | } else { 17 | int l=1, r=len; 18 | while (l>1; 20 | if (q[m] >= val) { // use > if non-decreasing 21 | r = m; 22 | } else { 23 | l = m+1; 24 | } 25 | } 26 | q[l] = val; 27 | } 28 | } 29 | return len; 30 | } -------------------------------------------------------------------------------- /Dynamic_Programming/fibonnacci.cpp: -------------------------------------------------------------------------------- 1 | int fib(int i) { 2 | if (i < 2) return i; 3 | return fib(i-1) + fib(i-2); 4 | } 5 | 6 | int memo[1000000]; 7 | memset(memo, -1, sizeof memo); 8 | int fib_dp_topdown(int i) { // memoization 9 | int& ans = memo[i]; 10 | if (ans != -1) return ans; 11 | if (i < 2) return ans = i; 12 | return ans = fib_dp_topdown(i-1) + fib_dp_topdown(i-2); 13 | } 14 | 15 | const int MAXN = 1000000; 16 | int memo[MAXN + 1]; 17 | void init_memo() { 18 | memo[0] = 0; 19 | memo[1] = 1; 20 | for (int k = 2; k <= MAXN; ++k) memo[k] = memo[k-1] + memo[k-2]; 21 | } 22 | int fib_dp_bottomup(int i) { return memo[i]; } 23 | 24 | int fib_dp_bottomup_o1_memory(int i) { 25 | if (i < 2) return i; 26 | int a = 0; 27 | int b = 1; 28 | for (int k = 2; k <= i; ++k) { a = a + b; swap(a, b); } 29 | return b; 30 | } -------------------------------------------------------------------------------- /Geometry/GreensTheorem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | 5 | struct Point { double x, y; }; 6 | 7 | // Computes the line integral of the vector field <0,x> over the arc of the circle with radius 'r' 8 | // and x-coordinate 'x' from angle 'a' to angle 'b'. The 'y' goes away in the integral so it 9 | // it doesn't matter. 10 | // This can be done using a parameterization of the arc in polar coordinates: 11 | // x(t) = x + r * cos(t) 12 | // y(t) = y + r * sin(t) 13 | // a <= t <= b 14 | // The final integral can be seen here: 15 | // https://www.wolframalpha.com/input/?i=integral((x+%2B+r*cos(t))+*+derivative(y+%2B+r*sin(t))+*+dt,+t%3Da..b) 16 | double arc_integral(double x, double r, double a, double b) { 17 | return x * r * (sin(b) - sin(a)) + r * r * 0.5 * (0.5 * (sin(2*b) - sin(2*a)) + b - a); 18 | } 19 | 20 | // Computes the line integral of the vector field <0, x> over the directed segment a -> b 21 | // This can be done using the parameterization: 22 | // x(t) = a.x + (b.x - a.x) * t 23 | // y(t) = a.y + (b.y - a.y) * t 24 | // 0 <= t <= 1 25 | double segment_integral(Point& a, Point& b) { 26 | return 0.5 * (a.x + b.x) * (b.y - a.y); 27 | } -------------------------------------------------------------------------------- /Geometry/SmallestEnclosingCircle.cpp: -------------------------------------------------------------------------------- 1 | #define rep(i,a,b) for(int i=a; i < b; ++i) 2 | // find the smallest circle enclosing a list of points using Welzl’s randomized algorithm 3 | // O(N) expected time 4 | // references: https://www.cse.iitk.ac.in/users/ssahai/talks/sec.pdf 5 | // https://www.nayuki.io/res/smallest-enclosing-circle/computational-geometry-lecture-6.pdf 6 | void smallest_enclosing_circle(vector& pts, Point& center, double& r) { 7 | random_shuffle(pts.begin(), pts.end()); 8 | center = pts[0]; r = 0; 9 | int N = pts.size(); 10 | rep(i,1,N) { 11 | if ((pts[i] - center).norm() > r + EPS) { 12 | center = pts[i]; 13 | r = 0; 14 | rep(j,0,i) { 15 | if ((pts[j] - center).norm() > r + EPS) { 16 | center = (pts[i] + pts[j]) * 0.5; 17 | r = (pts[i] - center).norm(); 18 | rep(k,0,j) { 19 | if ((pts[k] - center).norm() > r + EPS) { 20 | center = circumcenter(pts[i], pts[j], pts[k]); 21 | r = (pts[k] - center).norm(); 22 | } 23 | } 24 | } 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Geometry/Trigonometry.cpp: -------------------------------------------------------------------------------- 1 | /* ================= */ 2 | /* Angle of a vector */ 3 | /* ================= */ 4 | const double PI = acos(-1); 5 | const double _2PI = 2 * PI; 6 | 7 | double correct_angle(double angle) { // to ensure 0 <= angle <= 2PI 8 | while (angle < 0) angle += _2PI; 9 | while (angle > _2PI) angle -= _2PI; 10 | return angle; 11 | } 12 | double angle(double x, double y) { 13 | // atan2 by itself returns an angle in range [-PI, PI] 14 | // no need to "correct it" if that range is ok for you 15 | return correct_angle(atan2(y, x)); 16 | } 17 | 18 | /* ============== */ 19 | /* Cosine Theorem */ 20 | /* ============== */ 21 | // Given triangle with sides a, b and c, returns the angle opposed to side a. 22 | // a^2 = b^2 + c^2 - 2*b*c*cos(alpha) 23 | // => alpha = acos((b^2 + c^2 - a^2) /(2*b*c)) 24 | double cos_theorem_angle(double a, double b, double c) { 25 | return acos((b*b + c*c - a*a)/(2*b*c)); 26 | } -------------------------------------------------------------------------------- /Graphs/BellmanFord.cpp: -------------------------------------------------------------------------------- 1 | #include "../c++_template.cpp" 2 | // complexity: O(|V||E|) 3 | vector bellman_ford(vector>& g, int s, bool& neg_cycle) { 4 | int n = g.size(); 5 | vector d(n, INT_MAX); // store min distance from s to all nodes 6 | d[s] = 0; 7 | rep(_,1,n) rep(u,0,n) if (d[u] < INT_MAX) for (auto& e : g[u]) { 8 | d[e.ff] = min(d[e.ff], d[u] + e.ss); 9 | } 10 | // optional: detect negative cycle 11 | rep(u,0,n) if (d[u] < INT_MAX) for (auto& e : g[u]) { 12 | if (d[e.ff] > d[u] + e.ss) { 13 | neg_cycle = true; return d; 14 | } 15 | } 16 | return d; 17 | } -------------------------------------------------------------------------------- /Graphs/Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include "../c++_template.cpp" 2 | // complexity: (|E| + |V|) * log |V| 3 | int N; // number of nodes 4 | vector> g; // graph 5 | vector D, P; // D: min distance from source to each node 6 | // P: parent of each node in shortest path from source 7 | void dijkstra(int s) { 8 | P.assign(N, -1); 9 | D.assign(N, INT_MAX); 10 | D[s] = 0; 11 | priority_queue, greater> q; // minheap 12 | q.emplace(0, s); 13 | while (q.size()) { 14 | int d, u; tie(d, u) = q.top(); q.pop(); // u = node, d = mindist from s to u 15 | if (D[u] < d) continue; // skip outdated improvements 16 | for (auto& e : g[u]) { 17 | if (D[e.ff] > d + e.ss) { 18 | D[e.ff] = d + e.ss; 19 | P[e.ff] = u; 20 | q.emplace(D[e.ff], e.ff); 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Graphs/FloydWarshall.cpp: -------------------------------------------------------------------------------- 1 | #include "../c++_template.cpp" 2 | // complexity: O(|V|^3) 3 | #define rep(i,a,b) for(int i=a; i>& g, bool& neg_cycle) { 5 | int n = g.size(); 6 | rep(k,0,n) rep(i,0,n) rep(j,0,n) { 7 | if (g[i][k] < INT_MAX and g[k][j] < INT_MAX) { 8 | g[i][j] = min(g[i][j], g[i][k] + g[k][j]); 9 | } 10 | } 11 | // optional: detect negative cycle 12 | rep(k,0,n) if (g[k][k] < 0) { 13 | rep(i,0,n) rep(j,0,n) if (g[i][k] < INT_MAX and g[k][j] < INT_MAX) { 14 | g[i][j] = -INT_MAX; 15 | neg_cycle = true; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Graphs/SCC.cpp: -------------------------------------------------------------------------------- 1 | #include "../c++_include.cpp" 2 | // Tarjan's SCC algorithm 3 | vector s, D, L; 4 | vector instack; 5 | vector>* g; 6 | int n, ID; 7 | void dfs(int u) { 8 | D[u] = L[u] = ID++; 9 | instack[u] = true; 10 | s.push_back(u); 11 | for (int v : (*g)[u]) { 12 | if (D[v] == -1) { 13 | dfs(v); 14 | L[u] = min(L[v], L[u]); 15 | } else if (instack[v]) { 16 | L[u] = min(L[v], L[u]); 17 | } 18 | } 19 | if (L[u] == D[u]) { // u is root of a SCC 20 | cout << "SCC found!\n"; 21 | while (1) { // remove SCC from top of the stack 22 | int x = s.back(); s.pop_back(); 23 | instack[x] = false; 24 | if (x == u) break; 25 | } 26 | } 27 | } 28 | void find_sccs(vector>& _g) { 29 | n = _g.size(); ID = 0; 30 | s.reserve(n); D.assign(n, -1); L.resize(n); 31 | instack.assign(n, 0); 32 | g = &_g; 33 | rep(u,0,n) if (D[u] == -1) dfs(u); 34 | } 35 | int main() { 36 | int n, m; cin >> n >> m; 37 | vector> g(n); 38 | while(m--) { 39 | int u, v; cin >> u >> v; u--, v--; 40 | g[u].push_back(v); 41 | } 42 | find_sccs(g); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Graphs/diameter_of_tree.cpp: -------------------------------------------------------------------------------- 1 | // ========================== 2 | // Find Tree's Diameter Ends 3 | // ========================== 4 | const int MAXN = 10000; 5 | 6 | int farthest_from(vector& g, int s) { // find farthest node from 's' with BFS 7 | static int dist[MAXN]; 8 | memset(dist, -1, sizeof(int) * g.size()); 9 | int farthest = s; 10 | queue q; 11 | q.push(s); 12 | dist[s] = 0; 13 | while (!q.empty()) { 14 | int u = q.front(); q.pop(); 15 | for (int v : g[u]) { 16 | if (dist[v] == -1) { 17 | dist[v] = dist[u] + 1; 18 | q.push(v); 19 | if (dist[v] > dist[farthest]) farthest = v; 20 | } 21 | } 22 | } 23 | return farthest; 24 | } 25 | 26 | void find_diameter(vector& g, int& e1, int& e2) { 27 | e1 = farthest_from(g, 0); 28 | e2 = farthest_from(g, e1); 29 | } 30 | -------------------------------------------------------------------------------- /Mathematics/binary_modular_exponentiation.cpp: -------------------------------------------------------------------------------- 1 | // compute a^b (mod m) 2 | int binary_exp(int a, int b, int m) { 3 | a %= m; 4 | int res = 1; 5 | while (b > 0) { 6 | if (b&1) res = (res * a) % m; 7 | a = (a * a) % m; 8 | b >>= 1; 9 | } 10 | return res; 11 | } -------------------------------------------------------------------------------- /Mathematics/integer_root_square.cpp: -------------------------------------------------------------------------------- 1 | // using sqrt() 2 | bool perfect_square(ll x, ll& root) { 3 | if (x < 0) return false; 4 | root = (ll)sqrt(x); 5 | return (root * root == x || ++root * root == x); 6 | } 7 | 8 | // Newton's method 9 | ll isqrt(ll x) { 10 | ll y0 = x; 11 | while (true) { 12 | ll y1 = (y0 + x / y0) / 2; 13 | if (y1 == y0) break; 14 | y0 = y1; 15 | } 16 | return y0; 17 | } 18 | bool isPerfectSquare(ll x, ll& root) { 19 | root = isqrt(x); 20 | return root * root == x; 21 | } -------------------------------------------------------------------------------- /Mathematics/modular_multinomial_coefficient.cpp: -------------------------------------------------------------------------------- 1 | typedef long long int ll; 2 | const ll MOD = 1000000007ll; // a prime number 3 | const int MAXN = 1000; 4 | 5 | /* ===================== */ 6 | /* MODULAR MULTINOMIAL */ 7 | /* ===================== */ 8 | 9 | ll memo[MAXN+1][MAXN+1]; 10 | ll choose(int n, int k) { 11 | ll& ans = memo[n][k]; 12 | if (ans != -1) return ans; 13 | if (k == 0) return ans = 1; 14 | if (n == k) return ans = 1; 15 | if (n < k) return ans = 0; 16 | return ans = (choose(n-1,k) + choose(n-1,k-1)) % MOD; 17 | } 18 | 19 | // reference: https://math.stackexchange.com/a/204209/503889 20 | ll multinomial(vector ks) { 21 | int n = 0; 22 | ll ans = 1; 23 | for (int k : ks) { 24 | n += k; 25 | ans = (ans * choose(n,k)) % MOD; 26 | } 27 | return ans; 28 | } -------------------------------------------------------------------------------- /Mathematics/sieve_of_eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #define rep(i,a,b) for(int i=a;i get_primes_up_to(int n) { 7 | vector is_prime(n + 1, true); 8 | int limit = floor(sqrt(n)); 9 | rep (i,2,limit+1) 10 | if (is_prime[i]) 11 | for (int j = i*i; j <= n; j += i) 12 | is_prime[j] = false; 13 | vector primes; 14 | rep(i,2,n) if (is_prime[i]) primes.push_back(i); 15 | return primes; 16 | } -------------------------------------------------------------------------------- /Solved problems/AtCoder/abc128_d_equeue.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, greedy 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(0); 9 | int N, K; cin >> N >> K; 10 | vector values(N); 11 | rep(i,0,N) cin >> values[i]; 12 | int ans = 0; 13 | rep(l,0,min(N,K)+1) { 14 | rep(r,0,min(N,K)-l+1) { 15 | int sum = 0; 16 | priority_queue q; 17 | rep(i,0,l) { 18 | sum += values[i]; 19 | if (values[i] < 0) q.push(-values[i]); 20 | } 21 | rep(i,0,r) { 22 | sum += values[N-i-1]; 23 | if (values[N-i-1] < 0) q.push(-values[N-i-1]); 24 | } 25 | int k = K-l-r; 26 | while (q.size() and k--) { 27 | sum += q.top(); q.pop(); 28 | } 29 | ans = max(ans, sum); 30 | } 31 | } 32 | cout << ans << '\n'; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solved problems/AtCoder/arc139_a_TrailingZeroes.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy, implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | #define ff first 11 | #define ss second 12 | #define pb push_back 13 | #define eb emplace_back 14 | // typedefs 15 | typedef vector vi; 16 | typedef pair ii; 17 | typedef unsigned long long int ull; 18 | typedef long long int ll; 19 | // ------------------------------- 20 | int main() { 21 | ios::sync_with_stdio(false); cin.tie(0); 22 | int n; cin >> n; 23 | vector T(n); 24 | rep(i,0,n) cin >> T[i]; 25 | ll x = 1LL << T[0]; 26 | rep(i,1,n) { 27 | ll y = x >> T[i]; 28 | do { ++y; } while (!(y & 1)); 29 | x = y << T[i]; 30 | } 31 | cout << x << '\n'; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codechef/AMCS03_RaceTime!.cpp: -------------------------------------------------------------------------------- 1 | // tags: ternary search 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | vector S, D; 8 | int N, K; 9 | double f(double t) { 10 | double maxx = S[0] * t + D[0]; 11 | double minx = maxx; 12 | rep(i,1,N) { 13 | double x = S[i] * t + D[i]; 14 | maxx = max(maxx, x); 15 | minx = min(minx, x); 16 | } 17 | return maxx - minx; 18 | } 19 | int main() { 20 | ios::sync_with_stdio(false); 21 | cin.tie(0); 22 | cin >> N >> K; 23 | S.resize(N); 24 | D.resize(N); 25 | rep(i,0,N) cin >> S[i] >> D[i]; 26 | double l = 0.0, r = K; 27 | rep(_,0,100) { 28 | double d = (r-l)/3.0; 29 | double m1 = l+d; 30 | double m2 = r-d; 31 | if (f(m1) < f(m2)) r = m2; 32 | else l = m1; 33 | } 34 | cout << setprecision(6) << fixed << f((l+r)*0.5) << '\n'; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/Codechef/ICM2003_IsThisJEE.cpp: -------------------------------------------------------------------------------- 1 | // tags: ternary search 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | const double PI = acos(-1); 8 | double b, c; 9 | double f(double x) { 10 | return (x * x + b * x + c) / sin(x); 11 | } 12 | int main() { 13 | ios::sync_with_stdio(false); 14 | cin.tie(0); 15 | cout << fixed << setprecision(10); 16 | int t; cin >> t; 17 | while (t--) { 18 | cin >> b >> c; 19 | double l = 0; 20 | double r = PI * 0.5; 21 | rep(_,0,100) { 22 | double d = (r-l)/3.0; 23 | double m1 = l + d; 24 | double m2 = r - d; 25 | if (f(m1) <= f(m2)) r = m2; 26 | else l = m1; 27 | } 28 | cout << f(0.5 * (l+r)) << '\n'; 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solved problems/Codechef/SPREAD_Gravel.cpp: -------------------------------------------------------------------------------- 1 | // tags: fenwick tree, difference array (conceptually) 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | // ------------------------------- 7 | int N, M; ll C; 8 | struct BIT { 9 | vector bit; 10 | BIT() { bit.assign(N+1, 0); } 11 | void add(int i, ll d) { 12 | for (; i < bit.size(); i += (i & -i)) bit[i] += d; 13 | } 14 | ll psq(int i) { 15 | ll sum = 0; 16 | for (; i; i -= (i & -i)) sum += bit[i]; 17 | return sum; 18 | } 19 | }; 20 | int main() { 21 | ios::sync_with_stdio(false); cin.tie(0); 22 | cin >> N >> M >> C; 23 | BIT bit; 24 | bit.add(1, C); 25 | while (M--) { 26 | string op; cin >> op; 27 | if (op == "S") { 28 | int l, r; ll k; 29 | cin >> l >> r >> k; 30 | bit.add(l, k); bit.add(r+1, -k); 31 | } else { 32 | int p; cin >> p; 33 | cout << bit.psq(p) << '\n'; 34 | } 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1015C_SongsCompression.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, greedy 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | #define ff first 11 | #define ss second 12 | // typedefs 13 | typedef pair ii; 14 | typedef unsigned long long int ull; 15 | typedef long long int ll; 16 | // ------------------------------- 17 | int main() { 18 | ios::sync_with_stdio(false); cin.tie(0); 19 | int n, m; cin >> n >> m; 20 | vector a(n), b(n); 21 | ll sum = 0; 22 | rep(i,0,n) { cin >> a[i] >> b[i]; b[i] -= a[i]; sum += a[i]; } 23 | vector idxs(n); rep(i,0,n) idxs[i] = i; 24 | sort(idxs.begin(), idxs.end(), [&b](int i, int j) { return b[i] < b[j]; }); 25 | int count = 0; 26 | for (int i : idxs) { 27 | if (sum <= m) break; 28 | sum += b[i], count++; 29 | } 30 | if (sum <= m) cout << count << '\n'; 31 | else cout << "-1\n"; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1043A_Elections.cpp: -------------------------------------------------------------------------------- 1 | // implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | #define ff first 11 | #define ss second 12 | // typedefs 13 | typedef pair ii; 14 | typedef unsigned long long int ull; 15 | typedef long long int ll; 16 | // ------------------------------- 17 | int main() { 18 | ios::sync_with_stdio(false); cin.tie(0); 19 | int n; cin >> n; 20 | vector a(n); 21 | int s = 0; 22 | for (int& x : a) { cin >> x; s += x; } 23 | int k = *max_element(a.begin(), a.end()); 24 | while (k*n - s <= s) ++k; 25 | cout << k << '\n'; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1163B2_CatParty(HardEdition).cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, std::set 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | int N; 8 | int freq[100001]; 9 | int main() { 10 | ios::sync_with_stdio(false); 11 | cin.tie(0); cout.tie(0); 12 | set> s; 13 | cin >> N; 14 | int max_x = -1; 15 | rep(x,1,N) { 16 | int u; cin >> u; 17 | auto it = s.find({freq[u], u}); 18 | if (it != s.end()) s.erase(it); 19 | freq[u]++; 20 | s.emplace(freq[u], u); 21 | if ((s.size() == 1) or 22 | ((s.rbegin()->first == s.begin()->first + 1) and 23 | (s.rbegin()->first == next(s.rbegin())->first + 1)) or 24 | ((s.begin()->first == 1) and 25 | (next(s.begin())->first == s.rbegin()->first)) 26 | ) max_x = x; 27 | } 28 | cout << max_x << '\n'; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1180C_ValeriyAndDeque.cpp: -------------------------------------------------------------------------------- 1 | // tags: simulation, implementation, adhoc, modular arithmetics 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | int N, Q; 9 | int main() { 10 | ios::sync_with_stdio(false); cin.tie(0); 11 | cin >> N >> Q; 12 | int maxv = INT_MIN; 13 | deque d(N); 14 | rep(i,0,N+1) { 15 | cin >> d[i]; 16 | maxv = max(maxv, d[i]); 17 | } 18 | vector> pairs; 19 | pairs.reserve(N); 20 | while (d.front() != maxv) { 21 | int a = d.front(); d.pop_front(); 22 | int b = d.front(); d.pop_front(); 23 | pairs.emplace_back(a,b); 24 | if (a < b) swap(a,b); 25 | d.push_front(a); 26 | d.push_back(b); 27 | } 28 | while (Q--) { 29 | ll m; cin >> m; --m; 30 | if (m < pairs.size()) { 31 | auto& p = pairs[m]; 32 | cout << p.first << ' ' << p.second << '\n'; 33 | } else { 34 | int i = (m - pairs.size()) % (N-1) + 1; 35 | cout << maxv << ' ' << d[i] << '\n'; 36 | } 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1209F_KoalaAndNotebook/canonical.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | 6 | const int N = 1000005, P = 1000000007; 7 | int r, n, m, now, num, ans[N], h[N], e[N], w[N], pre[N]; 8 | bool vis[N]; 9 | vector q[N]; 10 | void add(int x, int y, int z){ e[++num]=y, w[num]=z, pre[num]=h[x], h[x]=num;} 11 | void Add(int x, int y, int z){ 12 | int last=y; 13 | while(z>9) add(++now, last, z%10), last=now, z/=10; 14 | add(x, last, z); 15 | } 16 | int main() { 17 | scanf("%d%d", &n, &m), now=n; 18 | for(int i=1; i<=m; ++i){ 19 | int x, y; 20 | scanf("%d%d", &x, &y); 21 | Add(x, y, i), Add(y, x, i); 22 | } 23 | q[r=1]={1}; 24 | for(int l=1; l<=r; ++l){ 25 | for(int i=0; i<10; ++i){ 26 | vector a; 27 | for(int u:q[l]) for(int v=h[u]; v; v=pre[v]) 28 | if(w[v]==i && !vis[e[v]]) 29 | vis[e[v]]=1, a.push_back(e[v]), 30 | ans[e[v]]=(ans[u]*10ll+w[v])%P; 31 | if(!a.empty()) q[++r]=a; 32 | } 33 | } 34 | for(int i=2; i<=n; ++i) printf("%d\n", ans[i]); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1209F_KoalaAndNotebook/gen.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 20 4 | M = 30 5 | 6 | nodes = list(range(1,N+1)) 7 | random.shuffle(nodes) 8 | edges = [] 9 | edge_set = set() 10 | for i in range(1, N): 11 | u = nodes[random.randint(0, i-1)] 12 | v = nodes[i] 13 | if random.random() <= 0.5: 14 | u,v = v,u 15 | edges.append((u,v)) 16 | edge_set.add((u,v)) 17 | edge_set.add((v,u)) 18 | 19 | while len(edges) < M: 20 | u, v = random.sample(nodes, 2) 21 | e1 = (u,v) 22 | e2 = (v,u) 23 | if e1 in edge_set: continue 24 | edges.append(e1) 25 | edge_set.add(e1) 26 | edge_set.add(e2) 27 | 28 | random.shuffle(edges) 29 | print(N, M) 30 | for e in edges: print(e[0], e[1]) -------------------------------------------------------------------------------- /Solved problems/Codeforces/1243A_MaximumSquare.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int main() { 15 | ios::sync_with_stdio(false); cin.tie(0); 16 | int k; cin >> k; 17 | vector a; 18 | a.reserve(1000); 19 | while (k--) { 20 | int n; cin >> n; 21 | a.resize(n); 22 | for (int& x : a) cin >> x; 23 | sort(a.begin(), a.end(), greater()); 24 | int i = 0; 25 | while (i+1 < n and a[i+1] >= i+2) ++i; 26 | cout << i+1 << '\n'; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1243C_TilePainting.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, number theory, CRT 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); cout.tie(0); 17 | ll n; cin >> n; 18 | int count = 0; 19 | ll p = 1; 20 | for (ll x = 2, m = n; x * x <= m; ++x) { 21 | if (n % x == 0) { 22 | count++; 23 | p = x; 24 | do { n /= x; } while (n % x == 0); 25 | } 26 | } 27 | if (n > 1) { 28 | p = n; 29 | count++; 30 | } 31 | if (count > 1) { 32 | cout << "1\n"; 33 | } else { 34 | cout << p << '\n'; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1270B_InterestingSubarrays.cpp: -------------------------------------------------------------------------------- 1 | // tags: logic, adhoc 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); cout.tie(0); 17 | int t; cin >> t; 18 | while (t--) { 19 | int n; cin >> n; 20 | vector a(n); 21 | rep(i,0,n) cin >> a[i]; 22 | bool yes = false; 23 | int j; 24 | rep(i,0,n-1) { 25 | if (abs(a[i] - a[i+1]) >= 2) { 26 | yes = true; 27 | j = i; 28 | break; 29 | } 30 | } 31 | if (yes) { 32 | cout << "YES\n"; 33 | cout << j+1 << ' ' << j+2 << '\n'; 34 | } 35 | else cout << "NO\n"; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1270C_MakeGood.cpp: -------------------------------------------------------------------------------- 1 | // tags: xor, bitwise, adhoc 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); cout.tie(0); 17 | int t; cin >> t; 18 | while (t--) { 19 | int n; cin >> n; 20 | ll s, x, a; 21 | s = 0, x = 0; 22 | while (n--) { 23 | cin >> a; 24 | s += a; 25 | x ^= a; 26 | } 27 | ll A = (1LL << 50) + (x&1LL); 28 | s += A; 29 | x ^= A; 30 | ll B = ((x << 1) - s) >> 1; 31 | cout << "3\n"; 32 | cout << A << ' ' << B << ' ' << B << '\n'; 33 | assert (s + B + B == 2LL * (x ^ B ^ B)); 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1270D_StrangeDevice.cpp: -------------------------------------------------------------------------------- 1 | // tags: interactive, adhoc 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); cout.tie(0); 17 | int n, k; cin >> n >> k; 18 | map f; 19 | rep(i,0,k+1) { 20 | cout << '?'; 21 | rep(j,0,k+1) { 22 | if (i == j) continue; 23 | cout << ' ' << j+1; 24 | } 25 | cout << '\n'; 26 | cout.flush(); 27 | int pos, a; 28 | cin >> pos >> a; 29 | f[a]++; 30 | } 31 | cout << "! " << f.rbegin()->second << '\n'; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1282A_TemporarilyUnavailable.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | typedef vector vi; 14 | typedef pair ii; 15 | // ------------------------------- 16 | int main() { 17 | ios::sync_with_stdio(false); 18 | cin.tie(0); cout.tie(0); 19 | int t; cin >> t; 20 | while (t--) { 21 | int a, b, c, r; 22 | cin >> a >> b >> c >> r; 23 | if (a > b) swap(a, b); 24 | int x = max(a, c - r); 25 | int y = min(b, c + r); 26 | cout << b - a - max(y-x, 0) << '\n'; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1284A_NewYearAndNaming.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); cout.tie(0); 17 | int n, m; 18 | cin >> n >> m; 19 | vector s, t; 20 | string tmp; 21 | rep(i,0,n) { 22 | cin >> tmp; 23 | s.push_back(tmp); 24 | } 25 | rep(i,0,m) { 26 | cin >> tmp; 27 | t.push_back(tmp); 28 | } 29 | int q; cin >> q; 30 | while (q--) { 31 | int y; cin >> y; --y; 32 | cout << s[y % s.size()] << t[y % t.size()] << '\n'; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1284C_NewYearAndPermutation.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, modular arithmetics, combinatorics, counting 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned long long int ull; 12 | typedef long long int ll; 13 | // ------------------------------- 14 | int n; ll m; 15 | ll add(ll x, ll y) { return (x+y) % m; } 16 | ll mul(ll x, ll y) { return (x*y) % m; } 17 | vector memo; 18 | ll fact(int k) { 19 | ll& ans = memo[k]; 20 | if (ans != -1) return ans; 21 | if (k == 0) return ans = 1LL; 22 | return ans = mul(k, fact(k-1)); 23 | } 24 | int main() { 25 | ios::sync_with_stdio(false); 26 | cin.tie(0); cout.tie(0); 27 | cin >> n >> m; 28 | ll ans = 0; 29 | memo.assign(n+1, -1); 30 | rep(k,1,n+1) { 31 | ans = add(ans, mul(mul(mul(fact(k), fact(n-k)), n-k+1), n-k+1) ); 32 | } 33 | cout << ans << '\n'; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1287B_Hyperset.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define uset unordered_set 7 | // ------------------------------- 8 | int main() { 9 | ios::sync_with_stdio(false); 10 | cin.tie(0); cout.tie(0); 11 | int n, k; 12 | cin >> n >> k; 13 | vector cards(n); 14 | uset cards_set; 15 | for (string& s : cards) { 16 | cin >> s; cards_set.insert(s); 17 | } 18 | int ways = 0; 19 | rep(i,0,n) rep(j,i+1,n) { 20 | string q(k, 0); 21 | rep(s,0,k) { 22 | if (cards[i][s] == cards[j][s]) { 23 | q[s] = cards[i][s]; 24 | } else { 25 | for (char c : "SET") { 26 | if (cards[i][s] != c and cards[j][s] != c) { 27 | q[s] = c; 28 | break; 29 | } 30 | } 31 | } 32 | } 33 | ways += cards_set.count(q); 34 | } 35 | cout << ways/3 << '\n'; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1287B_Hyperset.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n, k = map(int, input().split()) 3 | cards = [] 4 | for i in range(n): 5 | cards.append(input().strip()) 6 | cards_set = set(cards) 7 | ways = 0 8 | for i in range(n): 9 | for j in range(i+1, n): 10 | s = '' 11 | for z in range(k): 12 | if cards[i][z] == cards[j][z]: 13 | s += cards[i][z] 14 | else: 15 | for c in "SET": 16 | if c != cards[i][z] and c != cards[j][z]: 17 | s += c 18 | break 19 | if s in cards_set: 20 | ways += 1 21 | ways //= 3 22 | print(ways) 23 | 24 | main() -------------------------------------------------------------------------------- /Solved problems/Codeforces/1365B_TroubleSort.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | #define ff first 11 | #define ss second 12 | // typedefs 13 | typedef unsigned long long int ull; 14 | typedef long long int ll; 15 | // ------------------------------- 16 | int main() { 17 | ios::sync_with_stdio(false); 18 | cin.tie(0); 19 | int t; cin >> t; 20 | nextcase: 21 | while (t--) { 22 | int n; cin >> n; 23 | vector a(n), b(n); 24 | for (int& x: a) cin >> x; 25 | int c[2] = {0}; 26 | for (int& x: b) { cin >> x; c[x]++; } 27 | if (c[0] and c[1]) cout << "Yes\n"; 28 | else { 29 | rep(i,1,n) if (a[i-1] > a[i]) { 30 | cout << "No\n"; goto nextcase; 31 | } 32 | cout << "Yes\n"; 33 | } 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1365C_RotationMatching.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, modular arithmetic, math 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(0); 9 | int n; cin >> n; 10 | map am, bm; 11 | rep(i,0,n) { 12 | int x; cin >> x; 13 | am.emplace(x, i); 14 | } 15 | rep(i,0,n) { 16 | int x; cin >> x; 17 | bm.emplace(x, i); 18 | } 19 | int ans = 0; 20 | map counts; 21 | rep(i,1,n+1) { 22 | int x = (n + am[i] - bm[i]) % n; 23 | counts[x]++; 24 | ans = max(ans, counts[x]); 25 | } 26 | cout << ans << '\n'; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1365E_MaximumSubsequenceValue.cpp: -------------------------------------------------------------------------------- 1 | // tags: brute force, bitwise, implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | #define ff first 11 | #define ss second 12 | // typedefs 13 | typedef unsigned long long int ull; 14 | typedef long long int ll; 15 | // ------------------------------- 16 | int n, m, k; 17 | vector a; 18 | vector counts; 19 | ll ans; 20 | int main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(0); 23 | cin >> n; 24 | a.resize(n); 25 | rep(i,0,n) cin >> a[i]; 26 | ll ans = 0; 27 | rep(i,0,n) { 28 | ans = max(ans, a[i]); 29 | rep(j,i+1,n) { 30 | ans = max(ans, a[i] | a[j]); 31 | rep(k,j,n) { 32 | ans = max(ans, a[i] | a[j] | a[k]); 33 | } 34 | } 35 | } 36 | cout << ans << '\n'; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/1523D_Love-Hate.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | n, m, p = map(int, input().split()) 4 | c = [input() for _ in range(n)] 5 | max_size = 0 6 | ans = ['0'] * m 7 | for _ in range(10): 8 | x = randint(0, n-1) 9 | idxs = [] 10 | for i in range(m): 11 | if c[x][i] == '1': 12 | idxs.append(i) 13 | p = len(idxs) 14 | f = [0] * (1 << p) 15 | for i in range(n): 16 | mask = 0 17 | for j in range(p): 18 | if c[i][idxs[j]] == '1': 19 | mask |= (1 << j) 20 | f[mask] += 1 21 | for i in range(p): 22 | for s in range(1 << p): 23 | if (s & (1 << i)) == 0: 24 | f[s] += f[s | (1 << i)] 25 | for s in range(1, 1 << p): 26 | if f[s] * 2 < n: 27 | continue 28 | n_ones = sum(1 for i in range(p) if (s & (1 << i)) > 0) 29 | if n_ones <= max_size: 30 | continue 31 | max_size = n_ones 32 | for i in range(m): ans[i] = '0' 33 | for i in range(p): 34 | if s & (1 << i): ans[idxs[i]] = '1' 35 | 36 | print (''.join(ans)) -------------------------------------------------------------------------------- /Solved problems/Codeforces/166E_Tetrahedron.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP bottom-up, math, counting 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i < b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | #define ff first 11 | #define ss second 12 | // typedefs 13 | typedef pair ii; 14 | typedef unsigned long long int ull; 15 | typedef long long int ll; 16 | // ------------------------------- 17 | const ll MOD = 1000000007; 18 | ll add(ll x, ll y) { return (x + y) % MOD; } 19 | ll mul(ll x, ll y) { return (x * y) % MOD; } 20 | ll dp(int n) { 21 | ll ways0 = 1, ways1 = 0; 22 | rep(k,1,n+1) { 23 | ll ways0_ = mul(ways1, 3); 24 | ll ways1_ = add(ways0, mul(ways1, 2)); 25 | ways0 = ways0_; 26 | ways1 = ways1_; 27 | } 28 | return ways0; 29 | } 30 | int main() { 31 | ios::sync_with_stdio(false); cin.tie(0); 32 | int n; cin >> n; 33 | cout << dp(n) << '\n'; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/182E_WoodenFence.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | typedef long long int ll; 6 | 7 | const ll MOD = 1000000007; 8 | int N, L; 9 | int width[100], height[100]; 10 | ll memo[100][2][3001]; 11 | ll dp(int i, int o, int len) { 12 | ll& ans = memo[i][o][len]; 13 | if (ans != -1) return ans; 14 | int w = width[i], h = height[i]; 15 | if (o == 1) swap(w, h); 16 | if (w > len) return ans = 0; 17 | if (len == w) return ans = 1; 18 | ans = 0; 19 | rep(j,0,N) { 20 | if (i == j) continue; 21 | if (width[j] == h) ans = (ans + dp(j,0,len-w)) % MOD; 22 | if (width[j] != height[j] and height[j] == h) ans = (ans + dp(j,1,len-w)) % MOD; 23 | } 24 | return ans; 25 | } 26 | 27 | int main() { 28 | ios::sync_with_stdio(false); 29 | cin.tie(0); cout.tie(0); 30 | cin >> N >> L; 31 | rep(i,0,N) cin >> width[i] >> height[i]; 32 | memset(memo, -1, sizeof memo); 33 | ll ways = 0; 34 | rep(i,0,N) { 35 | ways = (ways + dp(i,0,L)) % MOD; 36 | if (width[i] != height[i]) { 37 | ways = (ways + dp(i,1,L)) % MOD; 38 | } 39 | } 40 | cout << ways << '\n'; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/230B_T-primes.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, prime numbers, sieve of eratosthenes 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | #define uset unordered_set 6 | typedef long long int ll; 7 | // ------------------------------- 8 | 9 | uset get_primes_squared(int n) { 10 | vector is_prime(n + 1, true); 11 | int limit = (int) floor(sqrt(n)); 12 | rep(i,2,limit) { 13 | if (is_prime[i]) { 14 | for (int j=i*i; j <= n; j+=i) { 15 | is_prime[j] = false; 16 | } 17 | } 18 | } 19 | uset primes_squared; 20 | rep(i,2,n) if (is_prime[i]) primes_squared.insert((ll)i * (ll)i); 21 | return primes_squared; 22 | } 23 | 24 | int main() { 25 | uset primes_squared = get_primes_squared(1000000); 26 | int n; 27 | scanf("%d", &n); 28 | while(n--) { 29 | ll x; scanf("%I64d", &x); 30 | if (primes_squared.find(x) == primes_squared.end()) { 31 | puts("NO"); 32 | } else { 33 | puts("YES"); 34 | } 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/234G_Practice.cpp: -------------------------------------------------------------------------------- 1 | // tags: divide and conquer 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | vector> teams; 8 | void search(int l, int r, int i) { 9 | int m = (l+r) >> 1; 10 | if (teams.size() == i) teams.resize(i+1); 11 | rep(k,l,m) teams[i].push_back(k); 12 | if (m-l > 1) search(l,m,i+1); 13 | if (r-m > 1) search(m,r,i+1); 14 | } 15 | int main() { 16 | freopen("input.txt","r",stdin); 17 | freopen("output.txt","w",stdout); 18 | ios::sync_with_stdio(false); 19 | cin.tie(0); 20 | int n; cin >> n; 21 | search(0,n,0); 22 | cout << teams.size() << '\n'; 23 | for (auto& team : teams) { 24 | cout << team.size(); 25 | for (int x : team) cout << ' ' << x+1; 26 | cout << '\n'; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/235A_LCM_Challenge.cpp: -------------------------------------------------------------------------------- 1 | // tags: lcm, least common multiple, gcd, brute force, 2 | // number theory, math 3 | #pragma GCC optimize("Ofast") 4 | #include 5 | using namespace std; 6 | #define rep(i,a,b) for(int i=a; i<=b; i++) 7 | typedef long long int ll; 8 | 9 | ll lcm(ll a, ll b) { 10 | return (a / __gcd(a,b)) * b; 11 | } 12 | ll lcm(ll a, ll b, ll c) { 13 | ll x = a; 14 | if (b > 0) x = lcm(x, b); 15 | if (c > 0) x = lcm(x, c); 16 | return x; 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(false); 21 | cin.tie(0); cout.tie(0); 22 | ll n; cin >> n; 23 | ll ans; 24 | if (n&1) { 25 | ans = lcm(n, n-1, n-2); 26 | } else { 27 | ans = lcm(n-1, n-2, n-3); 28 | rep(i,2,30) { 29 | ans = max(ans, lcm(n, n-1, n-i)); 30 | } 31 | rep(i,3,30) { 32 | ans = max(ans, lcm(n, n-2, n-i)); 33 | } 34 | } 35 | cout << ans << '\n'; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/24E_BerlandCollider.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | // -------------------------------------- 6 | const int MAXN = 500000; 7 | int N; 8 | double pos[MAXN], vel[MAXN]; 9 | 10 | bool collision(double t) { 11 | double maxr = -1e18; 12 | rep(i,0,N) { 13 | if (vel[i] > 0) maxr = max(maxr, pos[i] + vel[i] * t); 14 | else if (maxr > pos[i] + vel[i] * t) return true; 15 | } 16 | return false; 17 | } 18 | 19 | int main() { 20 | scanf("%d", &N); // printf/scanf because cin/cout gets TLE 21 | double r_minpos = 2e9, l_maxpos = -2e9; 22 | rep(i,0,N) { 23 | scanf("%lf %lf", &pos[i], &vel[i]); 24 | if (vel[i] > 0) r_minpos = min(r_minpos, pos[i]); 25 | else l_maxpos = max(l_maxpos, pos[i]); 26 | } 27 | if (r_minpos > l_maxpos) puts("-1"); 28 | else { 29 | double tl = 0, tr = 1e9, tm; 30 | rep(_,0,70) { 31 | tm = 0.5*(tl+tr); 32 | if (collision(tm)) tr = tm; 33 | else tl = tm; 34 | } 35 | printf("%.20lf\n", 0.5*(tl+tr)); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/269B_GreenhouseEffect.cpp: -------------------------------------------------------------------------------- 1 | // tags: dp, LIS, longest increasing subsequence 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | 7 | const int MAXLEN = 5000; 8 | 9 | int LIS(vector& values) { 10 | static int q[MAXLEN+1]; 11 | int len = 0; 12 | q[0] = -INT_MAX; 13 | for (int val : values) { 14 | if (q[len] <= val) { 15 | q[++len] = val; 16 | } else { 17 | int l=1, r=len; 18 | while (l>1; 20 | if (q[m] > val) { 21 | r = m; 22 | } else { 23 | l = m+1; 24 | } 25 | } 26 | q[l] = val; 27 | } 28 | } 29 | return len; 30 | } 31 | 32 | int main() { 33 | ios::sync_with_stdio(false); 34 | cin.tie(0); cout.tie(0); 35 | int n, m; cin >> n >> m; 36 | vector species(n); 37 | rep(i,0,n-1) { double _; cin >> species[i] >> _; } 38 | cout << n - LIS(species) << '\n'; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/26A_AlmostPrime.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | // typedefs 12 | typedef pair ii; 13 | typedef unsigned long long int ull; 14 | typedef long long int ll; 15 | // ------------------------------- 16 | int main() { 17 | ios::sync_with_stdio(false); 18 | cin.tie(0); 19 | int n; cin >> n; 20 | vector count(n + 1, 0); 21 | rep(i,2,n+1) 22 | if (count[i] == 0) 23 | for (int j = i; j <= n; j += i) 24 | count[j]++; 25 | int ans = 0; 26 | rep(i,2,n+1) if (count[i] == 2) ans++; 27 | cout << ans << '\n'; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/276D_LittleGirlAndMaximumXOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 4 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 5 | #define umap unordered_map 6 | #define uset unordered_set 7 | typedef vector vi; 8 | typedef vector vvi; 9 | typedef long long int ll; 10 | typedef pair ii; 11 | typedef tuple iii; 12 | typedef pair pdi; 13 | typedef pair lli; 14 | // ------------------------------- 15 | 16 | const ll ONE = 1; 17 | 18 | int main() { 19 | ll left, right ; scanf("%I64d %I64d", &left, &right); 20 | int n = sizeof(right) * 8 - __builtin_clzll(right); 21 | int i = n-1; 22 | while (i >= 0 and ((left >> i) & 1) == ((right >> i) & 1) ) i--; 23 | printf("%I64d\n", (ONE << (i+1))-1 ); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/289B_PoloThePenguinAndMatrix.cpp: -------------------------------------------------------------------------------- 1 | // tags: sorting, median, minimum absolute deviation 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | // typedefs 10 | typedef vector vi; 11 | typedef vector vvi; 12 | typedef long long int ll; 13 | typedef pair ii; 14 | typedef tuple iii; 15 | typedef pair pdi; 16 | typedef pair lli; 17 | 18 | int main() { 19 | int n, m, d, nm; 20 | scanf("%d%d%d", &n, &m, &d); 21 | nm = n * m; 22 | int a[nm]; 23 | rep(i,0,nm-1) scanf("%d", &a[i]); 24 | sort(a, a+nm); 25 | bool ok = true; 26 | rep(i,1,nm-1) { 27 | if ((a[i] - a[i-1]) % d > 0) { 28 | ok = false; 29 | break; 30 | } 31 | } 32 | if (!ok) { 33 | puts("-1"); 34 | } else { 35 | int median = a[nm/2]; 36 | int count = 0; 37 | rep(i, 0, nm-1) { 38 | count += abs(a[i] - median) / d; 39 | } 40 | printf("%d\n", count); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/445A_DZY_LovesChessboard.py: -------------------------------------------------------------------------------- 1 | from sys import stdin 2 | from collections import deque 3 | 4 | dirs = ((-1, 0), (1, 0), (0, -1), (0, 1)) 5 | 6 | def fill(r, c): 7 | board[r][c] = 'B' 8 | q = deque() 9 | q.append((r, c)) 10 | while q: 11 | r, c = q.popleft() 12 | for u in dirs: 13 | rr, cc = r + u[0], c + u[1] 14 | if 0 <= rr < n and 0 <= cc < m and board[rr][cc] == '.': 15 | board[rr][cc] = 'W' if board[r][c] == 'B' else 'B' 16 | q.append((rr, cc)) 17 | 18 | def main(): 19 | global n, m, board 20 | 21 | line = stdin.readline() 22 | n, m = map(int, line.strip().split(' ')) 23 | 24 | board = [[None for _ in range(m)] for _ in range(n)] 25 | for r in range(n): 26 | line = stdin.readline() 27 | for c in range(m): 28 | board[r][c] = line[c] 29 | 30 | for r in range(n): 31 | for c in range(m): 32 | if board[r][c] == '.': 33 | fill(r, c) 34 | print(board[r][c], end='') 35 | print('') 36 | 37 | main() -------------------------------------------------------------------------------- /Solved problems/Codeforces/448C_PaintingFence_v2.cpp: -------------------------------------------------------------------------------- 1 | // tags: divide and conquer, implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | ll a[5000]; 9 | ll paint(int l, int r, int h) { 10 | ll hmin = *min_element(a + l, a + r); 11 | ll ans = hmin - h; 12 | int i = l; 13 | while (i < r) { 14 | while (i < r and a[i] == hmin) ++i; 15 | int j = i; 16 | while (j < r and a[j] > hmin) ++j; 17 | if (i < j) ans += paint(i, j, hmin); 18 | i = j; 19 | } 20 | return min(ans, (ll)(r - l)); 21 | } 22 | int main() { 23 | ios::sync_with_stdio(false); cin.tie(0); 24 | int n; cin >> n; 25 | rep(i,0,n) cin >> a[i]; 26 | cout << paint(0, n, 0) << '\n'; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/453A_LittlePonyAndExpectedMaximum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int m, n; 7 | scanf("%d%d",&m,&n); 8 | double ans = 0; 9 | for (int z = 1; z <= m; ++z) { 10 | double a = z / (double)m; 11 | double b = (z-1) / (double)m; 12 | ans += z * (pow(a,n) - pow(b,n)); 13 | } 14 | printf("%lf\n", ans); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/459B_PashmakAndFlowers.cpp: -------------------------------------------------------------------------------- 1 | // tags: combinatorics, math 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | typedef long long int ll; 6 | int n; 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | cin.tie(0); cout.tie(0); 10 | vector vals; 11 | cin >> n; 12 | ll x_min = LLONG_MAX; 13 | ll x_max = 0; 14 | while (n--) { 15 | ll x; cin >> x; vals.push_back(x); 16 | x_min = min(x_min, x); 17 | x_max = max(x_max, x); 18 | } 19 | ll min_count = 0; 20 | ll max_count = 0; 21 | for (ll x : vals) { 22 | if (x == x_max) max_count++; 23 | if (x == x_min) min_count++; 24 | } 25 | ll ways; 26 | if (x_max == x_min) { 27 | ways = max_count * (max_count-1) / 2; 28 | } else { 29 | ways = min_count * max_count; 30 | } 31 | cout << (x_max - x_min) << ' ' << ways << '\n'; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/466C_NumberOfWays.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP, counting 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | 9 | const int MAXN = 500000; 10 | ll a[MAXN]; 11 | ll acc[MAXN]; 12 | ll counts[MAXN]; 13 | 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); cout.tie(0); 17 | int n; cin >> n; 18 | ll sum = 0; 19 | rep(i,0,n-1) { 20 | cin >> a[i]; 21 | sum += a[i]; 22 | acc[i] = sum; 23 | } 24 | if (sum % 3 != 0 or n < 3) { 25 | cout << "0\n"; 26 | } else { 27 | ll ans = 0; 28 | ll one_third = sum/3; 29 | ll two_thirds = sum*2/3; 30 | ll c = 0; 31 | rep(i,0,n-1) { 32 | if (acc[i] == two_thirds) c++; 33 | counts[i] = c; 34 | } 35 | rep(i,0,n-3) { 36 | if (acc[i] == one_third) { 37 | ans += (counts[n-2] - counts[i]); 38 | } 39 | } 40 | cout << ans << '\n'; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/467C_GeorgeAndJob.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP, top-down 2 | #include 3 | using namespace std; 4 | 5 | #define MAXN 5000 6 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 7 | 8 | typedef long long int ll; 9 | 10 | int N, M, K; 11 | ll acc[MAXN]; 12 | 13 | ll inline rsq(int i, int j) { 14 | return acc[j] - (i>0? acc[i-1] : 0); 15 | } 16 | 17 | ll memo[MAXN+1][MAXN]; 18 | 19 | ll dp(int k, int i) { 20 | if (k == 0) return 0; 21 | if (i + k * M > N) return 0; 22 | if (memo[k][i] != -1) return memo[k][i]; 23 | return memo[k][i] = max( 24 | rsq(i, i+M-1) + dp(k-1, i+M), 25 | dp(k, i+1) 26 | ); 27 | } 28 | 29 | int main() { 30 | scanf("%d%d%d", &N, &M, &K); 31 | ll prev = 0; 32 | rep(i,0,N-1) { 33 | scanf("%I64d", &acc[i]); 34 | acc[i] += prev; 35 | prev = acc[i]; 36 | } 37 | memset(memo, -1, sizeof memo); 38 | printf("%I64d\n", dp(K,0)); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/474B_Worms.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | a = list(map(int, input().split())) 4 | m = int(input()) 5 | q = list(map(int, input().split())) 6 | acc_a = [0] * n 7 | acc = 0 8 | for i, x in enumerate(a): 9 | acc = acc_a[i] = x + acc 10 | for x in q: 11 | low, high = 0, n-1 12 | while low < high: 13 | mid = (low+high)>>1 14 | if x <= acc_a[mid]: 15 | high = mid 16 | else: 17 | low = mid + 1 18 | print(low+1) 19 | 20 | main() -------------------------------------------------------------------------------- /Solved problems/Codeforces/492B_VanyaAndLanterns.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | // ------------------------------- 6 | double as[1000]; 7 | int main() { 8 | int n; double len; 9 | scanf("%d %lf", &n, &len); 10 | rep(i,0,n-1) scanf("%lf", as+i); 11 | sort(as, as + n); 12 | double dl=0, dr=1e9; 13 | rep(_,1,60) { 14 | double dm = (dl + dr) * 0.5; 15 | bool cover = !(as[0] - dm > 0 or as[n-1] + dm < len); 16 | if (cover) rep(i,0,n-2) { 17 | if (as[i] + dm < as[i+1] - dm) { 18 | cover = false; 19 | break; 20 | } 21 | } 22 | if (cover) dr = dm; 23 | else dl = dm; 24 | } 25 | printf("%.15lf\n", (dl+dr)*0.5); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/49B_Sum.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, bases, implementation 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | 6 | int max_digit(string& s) { 7 | int d = 0; 8 | for (char c : s) d = max(d, c-'0'); 9 | return d; 10 | } 11 | 12 | int parse_num(string& s, int base) { 13 | int x = 0; 14 | for (char c : s) x = x * base + (c - '0'); 15 | return x; 16 | } 17 | 18 | int num_digits(int x, int base) { 19 | int count = 0; 20 | while (x > 0) { 21 | count++; 22 | x /= base; 23 | } 24 | return count; 25 | } 26 | 27 | int main() { 28 | ios::sync_with_stdio(false); 29 | cin.tie(0); cout.tie(0); 30 | string a, b; 31 | cin >> a >> b; 32 | int base = max(max_digit(a), max_digit(b))+1; 33 | int sum = parse_num(a, base) + parse_num(b, base); 34 | cout << num_digits(sum, base) << '\n'; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/514A_ChewbaccaAndNumber.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #include // import everything in one shot 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | 8 | int main() { 9 | string num; cin >> num; // read input 10 | int n = num.size(); 11 | rep(i,0,n-1) { // update each character inplace when it's correct to do so 12 | int x = num[i] - '0'; 13 | if (i == 0) { 14 | if (5 <= x and x <= 8) { 15 | num[i] = (char)('0' + 9 - x); 16 | } 17 | } else if (5 <= x) { 18 | num[i] = (char)('0' + 9 - x); 19 | } 20 | } 21 | cout << num << endl; // print output 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/543A_WritingCode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithms used: 3 | * DP (bottom-up) + memory optimization 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | #define FOR(i,a,b) for(int i=a;i<=b;++i) 9 | typedef long long int ll; 10 | 11 | int main() { 12 | int n,m,b,mod; 13 | scanf("%d %d %d %d",&n,&m,&b,&mod); 14 | int a; 15 | ll ways[m+1][b+1]; 16 | memset(ways, 0, sizeof ways); 17 | // last programmer (base case) 18 | scanf("%d",&a); 19 | FOR(l,0,m) FOR(e,0,b) { 20 | if (l*a <= e) ways[l][e] = 1 % mod; 21 | } 22 | // other programmers 23 | FOR(p,1,n-1) { 24 | scanf("%d",&a); 25 | FOR(l,1,m) FOR(e,a,b) { 26 | if ((ways[l][e] += ways[l-1][e - a]) >= mod) 27 | ways[l][e] -= mod; 28 | } 29 | } 30 | printf("%I64d\n", ways[m][b]); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/579E_WeaknessAndPoorness_v2.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search, DP (maximum subarray sum - Kadane's algorithm) 2 | // Kadane's algorithm: https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane's_algorithm 3 | #include // import everything in one shot 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | //--------------- 7 | const int MAXN = 200000; 8 | double a[MAXN]; 9 | int N; 10 | bool increasing(double x, double& maxsum) { 11 | double last_maxp, last_maxn, maxp, maxn; 12 | last_maxp = last_maxn = maxp = maxn = 1e-18; 13 | rep(i,0,N) { 14 | last_maxp = max(last_maxp + a[i] - x, a[i] - x); 15 | maxp = max(maxp, last_maxp); 16 | last_maxn = max(last_maxn + x - a[i], x - a[i]); 17 | maxn = max(maxn, last_maxn); 18 | } 19 | maxsum = max(maxn, maxp); 20 | return maxn > maxp; 21 | } 22 | int main() { 23 | cin >> N; 24 | rep(i,0,N) cin >> a[i]; 25 | double l = -1e4, r = 1e4; 26 | double ans; 27 | rep(_,0,100) { 28 | double m = (l+r)*0.5; 29 | if (increasing(m, ans)) r = m; 30 | else l = m; 31 | } 32 | cout << setprecision(20) << fixed << ans << '\n'; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/580A_KefaAndFirstSteps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 100000 5 | int nums[MAXN]; 6 | int n; 7 | 8 | int main() { 9 | scanf("%d", &n); 10 | int prev = -1; 11 | int maxlen = 0; 12 | int currlen = 0; 13 | while(n--) { 14 | int curr; scanf("%d", &curr); 15 | if (curr >= prev) { 16 | currlen++; 17 | } else { 18 | currlen = 1; 19 | } 20 | if (maxlen < currlen) maxlen = currlen; 21 | prev = curr; 22 | } 23 | printf("%d\n", maxlen); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/580C_KefaAndPark.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i=a; i<=b; i++) 4 | #define MAXN 100000 5 | 6 | int n,m; 7 | int has_cat[MAXN]; 8 | vector> g; 9 | 10 | int leaves_count = 0; 11 | 12 | void dfs(int u, int p, int cats) { 13 | cats = has_cat[u] ? cats + 1 : 0; 14 | if (cats > m) return; 15 | if ((g[u].size() == 1 and u != 0) or g[u].size() == 0) { 16 | leaves_count++; 17 | } else { 18 | for (int v : g[u]) { 19 | if (v == p) continue; 20 | dfs(v, u, cats); 21 | } 22 | } 23 | } 24 | 25 | int main() { 26 | scanf("%d%d", &n, &m); 27 | g.assign(n, vector()); 28 | rep(i,0,n-1) scanf("%d", &has_cat[i]); 29 | rep(i,2,n) { 30 | int x, y; scanf("%d%d", &x, &y); --x, --y; 31 | g[x].push_back(y); 32 | g[y].push_back(x); 33 | } 34 | leaves_count = 0; 35 | dfs(0,-1,0); 36 | printf("%d\n",leaves_count); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/5B_CenterAlignment.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | lines = sys.stdin.readlines() 5 | lines = [line.strip() for line in lines] 6 | maxlen = max(len(line) for line in lines) 7 | print('*' * (maxlen + 2)) 8 | left = True 9 | for line in lines: 10 | space = maxlen - len(line) 11 | if space % 2 == 0: 12 | lpad = rpad = space // 2 13 | elif left: 14 | lpad = space // 2 15 | rpad = space - lpad 16 | left = False 17 | else: 18 | rpad = space // 2 19 | lpad = space - rpad 20 | left = True 21 | print('*' + ' ' * lpad + line + ' ' * rpad + '*') 22 | print('*' * (maxlen + 2)) 23 | 24 | main() -------------------------------------------------------------------------------- /Solved problems/Codeforces/600B_QueriesAboutLessOrEqualNumbers.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n, m = map(int, input().split()) 3 | a = list(map(int, input().split())) 4 | b = [(i,x) for i,x in enumerate(map(int, input().split()))] 5 | a.sort() 6 | b.sort(key=lambda p:p[1]) 7 | j = -1 8 | output = [None] * m 9 | for i,x in b: 10 | while (j+1 < n and a[j+1] <= x): 11 | j += 1 12 | output[i] = j+1 13 | for i in range(m): 14 | if i > 0: 15 | print(' ', end='') 16 | print(output[i], end='') 17 | print('') 18 | 19 | main() -------------------------------------------------------------------------------- /Solved problems/Codeforces/645C_EnduringExodus.cpp: -------------------------------------------------------------------------------- 1 | // tags: two pointers 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | int N,K; 6 | vector empty_rooms; 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | cin.tie(0); 10 | cin >> N >> K; 11 | string line; cin >> line; 12 | int i = 0; 13 | empty_rooms.reserve(N); 14 | for (char c : line) { 15 | if (c == '0') empty_rooms.push_back(i); 16 | i++; 17 | } 18 | int M = empty_rooms.size(); 19 | int j = 0; 20 | int ans = INT_MAX; 21 | int curr, next; 22 | rep(i,0,M-K-1) { 23 | while (true) { 24 | curr = max(empty_rooms[j] - empty_rooms[i], empty_rooms[i+K] - empty_rooms[j]); 25 | next = max(empty_rooms[j+1] - empty_rooms[i], empty_rooms[i+K] - empty_rooms[j+1]); 26 | if (next < curr) ++j; 27 | else break; 28 | } 29 | ans = min(ans, curr); 30 | } 31 | cout << ans << '\n'; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/652E_PursuitForArtifacts.cpp: -------------------------------------------------------------------------------- 1 | // tags: adhoc, DFS, graphs 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | int N, M; 8 | vector>> g; 9 | vector parent; 10 | vector tag; 11 | void dfs(int u, int p, bool a) { 12 | parent[u] = p; 13 | tag[u] = a; 14 | for (auto& x : g[u]) { 15 | int v = x.first; 16 | if (v == p) continue; 17 | if (tag[v]) continue; 18 | if (parent[v] == -1) { 19 | dfs(v, u, a or x.second); 20 | } else if (a or x.second) { 21 | dfs(v, parent[v], a or x.second); 22 | } 23 | } 24 | } 25 | int main() { 26 | ios::sync_with_stdio(false); 27 | cin.tie(0); cout.tie(0); 28 | cin >> N >> M; 29 | g.resize(N); 30 | while (M--) { 31 | int x, y, z; 32 | cin >> x >> y >> z; 33 | --x, --y; 34 | g[x].emplace_back(y, z); 35 | g[y].emplace_back(x, z); 36 | } 37 | int a, b; cin >> a >> b; --a, --b; 38 | tag.assign(N, false); 39 | parent.assign(N, -1); 40 | dfs(a, -1, false); 41 | cout << (tag[b] ? "YES" : "NO") << '\n'; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/659C_TanyaAndToys.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy, implementation 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 6 | #define umap unordered_map 7 | #define uset unordered_set 8 | typedef vector vi; 9 | typedef vector vvi; 10 | typedef long long int ll; 11 | typedef pair ii; 12 | typedef tuple iii; 13 | typedef pair pdi; 14 | typedef pair lli; 15 | // ------------------------------- 16 | 17 | int n, m; 18 | 19 | int main() { 20 | scanf("%d%d", &n, &m); 21 | uset old_types; 22 | rep(i,0,n-1) { 23 | int x; scanf("%d", &x); 24 | old_types.insert(x); 25 | } 26 | 27 | vector new_types; 28 | for (int k =1;;++k) { 29 | if (k > m) break; 30 | if (old_types.count(k)) continue; 31 | new_types.push_back(k); 32 | m -= k; 33 | } 34 | 35 | printf("%d\n", new_types.size()); 36 | bool first = true; 37 | for (int x : new_types) { 38 | if (first) { 39 | printf("%d", x); 40 | first = false; 41 | } else { 42 | printf(" %d", x); 43 | } 44 | } 45 | puts(""); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/675A_InfiniteSequence.cpp: -------------------------------------------------------------------------------- 1 | // tags: math 2 | #include // import everything in one shot 3 | using namespace std; 4 | typedef long long int ll; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(0); cout.tie(0); 9 | ll a,b,c; cin >> a >> b >> c; 10 | bool possible; 11 | if (c == 0) { 12 | possible = a == b; 13 | } else if ((b-a) % c == 0) { 14 | possible = (b - a) / c >= 0; 15 | } else { 16 | possible = false; 17 | } 18 | if (possible) cout << "YES\n"; 19 | else cout << "NO\n"; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/682C_AlyonaAndTheTree.cpp: -------------------------------------------------------------------------------- 1 | // tags: graphs, trees, DFS 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define ff first 7 | #define ss second 8 | typedef long long int ll; 9 | // ------------------------------- 10 | vector>> g; 11 | vector a; 12 | int n; 13 | int dfs(int u, int p, ll x) { 14 | if (a[u] < x) return 0; 15 | int ans = 1; 16 | for (auto& e : g[u]) if (e.ff != p) ans += dfs(e.ff, u, max(e.ss, e.ss + x)); 17 | return ans; 18 | } 19 | int main() { 20 | ios::sync_with_stdio(false); 21 | cin.tie(0); 22 | cin >> n; 23 | g.resize(n); 24 | a.resize(n); 25 | rep(i,0,n) cin >> a[i]; 26 | rep(i,1,n) { 27 | int p; ll c; 28 | cin >> p >> c; --p; 29 | g[i].emplace_back(p, c); 30 | g[p].emplace_back(i, c); 31 | } 32 | cout << n - dfs(0, -1, 0) << '\n'; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/687B_RemaindersGame.cpp: -------------------------------------------------------------------------------- 1 | // tags: gcd, lcm, math 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | typedef long long int ll; 7 | ll lcm(ll a, ll b) { return (a/__gcd(a,b))*b; } 8 | int main() { 9 | ios::sync_with_stdio(false); 10 | cin.tie(0); cout.tie(0); 11 | ll n, k; cin >> n >> k; 12 | ll x = 1; 13 | rep(i,0,n-1) { 14 | ll c; cin >> c; 15 | x = lcm(x, __gcd(c, k)); 16 | } 17 | if (x == k) cout << "Yes\n"; 18 | else cout << "No\n"; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/709A_Juicer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a;i<=b;++i) 5 | 6 | int N,B,D; 7 | 8 | int main() { 9 | scanf("%d%d%d",&N,&B,&D); 10 | int total = 0; 11 | int count = 0; 12 | rep(i,1,N) { 13 | int a; scanf("%d", &a); 14 | if (a <= B) { 15 | total += a; 16 | if (total > D) { 17 | total = 0; 18 | count++; 19 | } 20 | } 21 | } 22 | printf("%d\n", count); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/709B_Checkpoints.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy 2 | #include 3 | using namespace std; 4 | 5 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 6 | 7 | int n, a; 8 | 9 | int main() { 10 | scanf("%d%d",&n,&a); 11 | vector dist(n); 12 | rep(i,0,n-1) { 13 | scanf("%d", &dist[i]); 14 | } 15 | if (n == 1) { 16 | puts("0"); 17 | return 0; 18 | } 19 | sort(dist.begin(), dist.end()); 20 | int d = min( 21 | dist[n-2] - dist[0] + min(abs(a - dist[0]), abs(dist[n-2] - a)), 22 | dist[n-1] - dist[1] + min(abs(a - dist[1]), abs(dist[n-1] - a)) 23 | ); 24 | printf("%d\n", d); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/709C_LetterCyclicShift.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy 2 | #include 3 | using namespace std; 4 | int main() { 5 | string s; cin >> s; 6 | int i = 0; 7 | while (i < s.size() and s[i] == 'a') ++i; 8 | if (i == s.size()) s.back() = 'z'; 9 | else while (i < s.size() and s[i] != 'a') s[i++]--; 10 | cout << s << '\n'; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/712B_MemoryAndTrident.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define umap unordered_map 6 | // ------------------------------- 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | cin.tie(0); cout.tie(0); 10 | string s; cin >> s; 11 | umap count; 12 | for (char c : s) count[c]++; 13 | int d1 = abs(count['L'] - count['R']); 14 | int d2 = abs(count['U'] - count['D']); 15 | if ( ((d1&1) + (d2&1)) & 1 ) { 16 | cout << "-1\n"; 17 | } else { 18 | cout << ( (d1/2) + (d2/2) + ((d1&1) + (d2&1))/2 ) << '\n'; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/740A_AlyonaAndCopybooks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 4 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 5 | #define umap unordered_map 6 | #define uset unordered_set 7 | typedef vector vi; 8 | typedef vector vii; 9 | typedef long long int ll; 10 | typedef pair pii; 11 | 12 | int n; 13 | ll a,b,c; 14 | ll memo[1000]; 15 | ll mincost(int k) { 16 | if (k == 0) return 0; 17 | if (memo[k] != -1) return memo[k]; 18 | ll ans = LLONG_MAX; 19 | if (k >= 3) ans = min(ans, c + mincost(k-3)); 20 | if (k >= 2) ans = min(ans, b + mincost(k-2)); 21 | if (k >= 1) ans = min(ans, a + mincost(k-1)); 22 | return memo[k] = ans; 23 | } 24 | 25 | int first_4_mult(int n) { 26 | if (n % 4 == 0) return n; 27 | return (n / 4 + 1) * 4; 28 | }; 29 | 30 | int main() { 31 | scanf("%d%I64d%I64d%I64d",&n,&a,&b,&c); 32 | memset(memo, -1, sizeof memo); 33 | ll ans = LLONG_MAX; 34 | for (int k = first_4_mult(n) - n;;k+=4) { 35 | ll tmp = mincost(k); 36 | if (tmp >= ans) break; 37 | ans = tmp; 38 | } 39 | printf("%I64d\n", ans); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/740B_AlyonaAndFlowers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 4 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 5 | #define umap unordered_map 6 | #define uset unordered_set 7 | typedef vector vi; 8 | typedef vector vvi; 9 | typedef long long int ll; 10 | typedef pair pii; 11 | 12 | int main() { 13 | int n,m; 14 | scanf("%d%d",&n,&m); 15 | int vals[n]; 16 | int acc[n]; 17 | int tmp = 0; 18 | rep(i,0,n-1) { 19 | scanf("%d", &vals[i]); 20 | tmp = acc[i] = vals[i] + tmp; 21 | } 22 | int ans = 0; 23 | rep(i,1,m) { 24 | int l,r; scanf("%d%d",&l,&r); --l,--r; 25 | int sum = acc[r] - (l>0? acc[l-1] : 0); 26 | if (sum > 0) ans += sum; 27 | } 28 | printf("%d", ans); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/766B_MahamoudAndTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i=a; i<=b; i++) 4 | 5 | int main() { 6 | int n; 7 | scanf("%d", &n); 8 | int segs[n]; 9 | rep(i,0,n-1) scanf("%d", &segs[i]); 10 | sort(segs, segs+n); 11 | bool possible = false; 12 | rep(i,0,n-3) { 13 | if (segs[i] + segs[i+1] > segs[i+2]) { 14 | possible = true; break; 15 | } 16 | } 17 | if (possible) puts("YES"); 18 | else puts("NO"); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/780B_TheMeetingPlaceCannotBeChanged.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | // ------------------------------- 6 | double xs[60000]; 7 | double ms[60000]; 8 | int main() { 9 | double xl = 1e9, xr = 1; 10 | int n; scanf("%d", &n); 11 | rep(i,0,n-1) { 12 | scanf("%lf", xs+i); 13 | xl = min(xl, xs[i]); 14 | xr = max(xr, xs[i]); 15 | } 16 | rep(i,0,n-1) { 17 | double v; scanf("%lf", &v); 18 | ms[i] = 1 / v; 19 | } 20 | double tmax; 21 | rep(_,1,60) { 22 | double xm = (xl+xr) * 0.5; 23 | int i_max; 24 | tmax = 0; 25 | rep(i,0,n-1) { 26 | double t = fabs(xs[i] - xm) * ms[i]; 27 | if (tmax < t) { 28 | tmax = t; 29 | i_max = i; 30 | } 31 | } 32 | if (xs[i_max] > xm) { 33 | xl = xm; 34 | } else { 35 | xr = xm; 36 | } 37 | } 38 | printf("%.15lf\n", tmax); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/839C_Journey.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 4 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 5 | #define umap unordered_map 6 | #define uset unordered_set 7 | typedef vector vi; 8 | typedef vector vvi; 9 | typedef long long int ll; 10 | typedef pair ii; 11 | typedef tuple iii; 12 | typedef pair pdi; 13 | typedef pair lli; 14 | // ------------------------------- 15 | 16 | int main() { 17 | int n; scanf("%d", &n); 18 | vvi g.assign(n, vi()); 19 | rep(2, i, n) { 20 | int u, v; scanf("%d%d", &u, &v): --u, --v; 21 | g[u].push_back(v); 22 | g[v].push_back(u); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/863E_TurnOffTheTV.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | tuples = [] 4 | seen_pairs = set() 5 | for i in range(n): 6 | l, r = map(int, input().split()) 7 | tuples.append((2 * l - 1, 2 * r + 1, i)) 8 | pair = (tuples[-1][0], tuples[-1][1]) 9 | if pair in seen_pairs: 10 | print(i + 1) 11 | return 12 | seen_pairs.add(pair) 13 | tuples.sort(key=lambda x: (x[0], -x[1])) 14 | l = -2 15 | r = -2 16 | prev_i = -1 17 | for t in tuples: 18 | assert l <= r 19 | if t[0] <= l and r <= t[1]: 20 | print(prev_i + 1) 21 | return 22 | if t[1] <= r: 23 | print(t[2] + 1) 24 | return 25 | l = max(t[0], r) 26 | r = t[1] 27 | prev_i = t[2] 28 | print(-1) 29 | main() -------------------------------------------------------------------------------- /Solved problems/Codeforces/999E_ReachabilityFromTheCapital.cpp: -------------------------------------------------------------------------------- 1 | // tags: dfs, directed graph, DAG, biconnected components 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | #define uset unordered_set 7 | // ------------------------------- 8 | const int MAXN = 5005; 9 | int N,M,S; 10 | vector> g; 11 | int ids[MAXN] = {0}; 12 | void dfs(int u, int id) { 13 | if (ids[u] == id) return; 14 | ids[u] = id; 15 | for (int v : g[u]) dfs(v, id); 16 | } 17 | int main() { 18 | ios::sync_with_stdio(false); 19 | cin.tie(0); cout.tie(0); 20 | cin >> N >> M >> S; 21 | g.resize(N+1); 22 | while (M--) { 23 | int u, v; cin >> u >> v; 24 | g[u].push_back(v); 25 | } 26 | int ID = 1; 27 | rep(u,1,N) if (ids[u] == 0 and u != S) dfs(u, ID++); 28 | dfs(S, ID); 29 | uset unique; 30 | rep(u,1,N) unique.insert(ids[u]); 31 | cout << unique.size() - 1 << '\n'; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/gym_100829A_ChasingTheCheetahs.cpp: -------------------------------------------------------------------------------- 1 | // TODO: finish this 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | const int MAXN = 100000; 8 | int N; 9 | double S[MAXN], V[MAXN]; 10 | double f(double t) { 11 | double maxx = max(t - S[0], 0.) * V[0]; 12 | double minx = maxx; 13 | rep(i,1,N) { 14 | double x = max(t - S[i], 0.) * V[i]; 15 | maxx = max(maxx, x); 16 | minx = min(minx, x); 17 | } 18 | return maxx - minx; 19 | } 20 | int main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(0); 23 | cout << setprecision(5) << fixed; 24 | while (cin >> N and N) { 25 | rep(i,0,N) cin >> S[i] >> V[i]; 26 | double l = *max_element(S, S+N); 27 | double r = 1e5; 28 | double fl = f(l); 29 | while (f(r) < fl) r *= 2.0; 30 | rep(_,0,40) { 31 | double d = (r-l)/3.0; 32 | double m1 = l+d; 33 | double m2 = r-d; 34 | if (f(m1) < f(m2)) r = m2; 35 | else l = m1; 36 | } 37 | cout << f(0.5*(l+r)) << '\n'; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/gym_101873G_WaterTesting.cpp: -------------------------------------------------------------------------------- 1 | // tags: Pick's Theorem, Polygon Area, geometry 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | const int MAXN = 100000; 9 | int N; 10 | ll X[MAXN], Y[MAXN]; 11 | int main() { 12 | ios::sync_with_stdio(false); 13 | cin.tie(0); cout.tie(0); 14 | cin >> N; 15 | rep(i,0,N-1) cin >> X[i] >> Y[i]; 16 | ll area = 0; 17 | ll p_border = 0; 18 | for (int i = N-1, j = 0; j < N; i=j++) { 19 | area += (X[i] + X[j]) * (Y[j] - Y[i]); 20 | p_border += __gcd(abs(X[j] - X[i]), abs(Y[j] - Y[i])); 21 | } 22 | area = abs(area) >> 1; 23 | ll p_in = area - p_border/2 + 1; 24 | cout << p_in << '\n'; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/gym_101875D_Checkerbaord.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | ll N, rs, cs, rt, ct; 22 | cin >> N >> rs >> cs >> rt >> ct; 23 | ll mhd = abs(rs - rt) + abs(cs - ct); 24 | ll ans = 0; 25 | if (N >= mhd) { 26 | if (mhd > 0) ans++; 27 | ans += (N - mhd) / 2; 28 | } 29 | cout << ans << '\n'; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/gym_101875G_TrafficManagement.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int N; 20 | const int MAXN = 1e5 + 5; 21 | ii SV[MAXN]; 22 | 23 | int main() { 24 | ios::sync_with_stdio(false); cin.tie(0); 25 | cin >> N; 26 | rep(i,0,N) cin >> SV[i].ff >> SV[i].ss; 27 | sort(SV, SV + N); 28 | int j = N-1; 29 | double ans = 0; 30 | invrep(i,N-2,0) { 31 | if (SV[j].ss >= SV[i].ss) { 32 | j = i; 33 | } else { 34 | ans = max(ans, (double)(SV[j].ff - SV[i].ff) / (SV[i].ss - SV[j].ss)); 35 | } 36 | } 37 | cout << fixed << setprecision(10) << ans << '\n'; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Solved problems/Codeforces/gym_103640E_ExpeditionPlans/generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | n = random.randint(2, 5) 3 | S = [random.randint(0, 1e9) for _ in range(n-1)] 4 | D = [random.randint(0, 1e9) for _ in range(n)] 5 | F = [random.randint(0, 1e9) for _ in range(n+1)] 6 | print(n) 7 | print(' '.join(map(str, S))) 8 | print(' '.join(map(str, D))) 9 | print(' '.join(map(str, F))) -------------------------------------------------------------------------------- /Solved problems/Codeforces/outputs_comparators.py: -------------------------------------------------------------------------------- 1 | with open('./output_mio.out') as f: 2 | my_ouput = list(map(float, f.readlines())) 3 | with open('./output_chino.out') as f: 4 | his_ouput = list(map(float, f.readlines())) 5 | assert(len(my_ouput) == len(his_ouput)) 6 | for i, (x1, x2) in enumerate(zip(my_ouput, his_ouput)): 7 | if abs(x1 - x2) > 1e-6: 8 | print('test %d: yo=%.8lf chino=%.8lf' % (i+1, x1, x2)) -------------------------------------------------------------------------------- /Solved problems/Codeforces/random_input_generator.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | for _ in range(100000): 4 | cx = randint(-1000, 1000) 5 | cy = randint(-1000, 1000) 6 | r = randint(1, 1000) 7 | x1 = randint(-1000, 1000) 8 | while True: 9 | x2 = randint(-1000, 1000) 10 | if x1 != x2: 11 | break 12 | y1 = randint(-1000, 1000) 13 | while True: 14 | y2 = randint(-1000, 1000) 15 | if y1 != y2: 16 | break 17 | print('%d %d %d\n%d %d %d %d' % (cx,cy,r,x1,y1,x2,y2)) -------------------------------------------------------------------------------- /Solved problems/Codejam/2017/round_0/A_OversizedPancakeFlipper.cpp: -------------------------------------------------------------------------------- 1 | #include // add almost everything in one shot 2 | using namespace std; 3 | // defines 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 6 | #define umap unordered_map 7 | #define uset unordered_set 8 | // typedefs 9 | typedef vector vi; 10 | typedef vector vvi; 11 | typedef long long int ll; 12 | typedef pair ii; 13 | typedef tuple iii; 14 | 15 | int main() { 16 | int T; 17 | char buff[1001]; 18 | scanf("%d", &T); 19 | rep(t,1,T) { 20 | int k; 21 | scanf("%s %d", buff, &k); 22 | string s(buff); 23 | // printf("s = %s, k = %d\n", s.c_str(), k); 24 | int count = 0; 25 | invrep(i, (int)s.size()-1, k-1) { 26 | if (s[i] == '-') { 27 | rep(j,i-k+1,i) s[j] = ((s[j] == '-') ? '+' : '-'); 28 | count++; 29 | } 30 | } 31 | rep(i,0,k-1) { 32 | if (s[i] == '-') { count = -1; break; } 33 | } 34 | if (count >= 0) printf("Case #%d: %d\n", t, count); 35 | else printf("Case #%d: IMPOSSIBLE\n", t); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2017/round_1B/A.cpp: -------------------------------------------------------------------------------- 1 | /* ========= */ 2 | // Template // 3 | /* ========= */ 4 | #include // add almost everything in one shot 5 | using namespace std; 6 | // defines 7 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 8 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 9 | #define umap unordered_map 10 | #define uset unordered_set 11 | // typedefs 12 | typedef vector vi; 13 | typedef vector vvi; 14 | typedef long long int ll; 15 | typedef pair ii; 16 | typedef tuple iii; 17 | 18 | int main() { 19 | int T; scanf("%d", &T); 20 | rep(_case,1,T) { 21 | double D; int N; 22 | scanf("%lf%d", &D, &N); 23 | double max_t = -1.0; 24 | while(N--) { 25 | double K,S; 26 | scanf("%lf%lf", &K, &S); 27 | max_t = max(max_t, (D - K) / S); 28 | } 29 | printf("Case #%d: %lf\n", _case, D / max_t); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2021/round0/ReverseSort.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | // typedefs 12 | typedef pair ii; 13 | typedef unsigned long long int ull; 14 | typedef long long int ll; 15 | // ------------------------------- 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(0); 18 | int T; cin >> T; 19 | rep(_case, 1, T+1) { 20 | int N; cin >> N; 21 | vector L(N); 22 | for (int& x : L) cin >> x; 23 | int cost = 0; 24 | rep(i,0,N-1) { 25 | auto it = min_element(L.begin() + i, L.end()); 26 | cost += it - (L.begin() + i) + 1; 27 | reverse(L.begin() + i, it + 1); 28 | } 29 | cout << "Case #" << _case << ": " << cost << '\n'; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2022/round0/PunchedCards.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | int t; cin >> t; 22 | rep(k,1,t+1) { 23 | cout << "Case #" << k <<":\n"; 24 | int r,c; cin >> r >> c; 25 | cout << "..+"; 26 | rep(j,1,c) cout << "-+"; 27 | cout << '\n'; 28 | rep(i,0,r) { 29 | cout << (i == 0 ? '.' : '|'); 30 | rep(j,0,c) cout << ".|"; 31 | cout << '\n'; 32 | cout << '+'; 33 | rep(j,0,c) cout << "-+"; 34 | cout << '\n'; 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2022/round0/d1000000.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | int t; cin >> t; 22 | rep(k,1,t+1) { 23 | int ans = 0; 24 | int n; cin >> n; 25 | vector c(n); 26 | rep(i,0,n) cin >> c[i]; 27 | sort(c.begin(), c.end()); 28 | int j = 1; 29 | rep(i,0,n) if (j <= c[i]) ans++, j++; 30 | cout << "Case #" << k << ": " << ans << '\n'; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2022/round1A/DoubleOrOneThing.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | int t; cin >> t; 22 | rep(k,1,t+1) { 23 | string s; cin >> s; 24 | string o = ""; 25 | int n = s.size(); 26 | int i = 0; 27 | while (i < n) { 28 | int j = i; 29 | while (j+1 < n and s[j+1] == s[i]) ++j; 30 | if (j+1 < n and s[i] < s[j+1]) { 31 | rep(k,i,j+1) { o += s[i]; o += s[i]; } 32 | } else { 33 | rep(k,i,j+1) { o += s[i]; } 34 | } 35 | i = j + 1; 36 | } 37 | cout << "Case #" << k << ": " << o << '\n'; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2022/round1A/EqualSum.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | int t; cin >> t; 22 | rep(k,1,t+1) { 23 | string s; cin >> s; 24 | string o = ""; 25 | int n = s.size(); 26 | int i = 0; 27 | while (i < n) { 28 | int j = i; 29 | while (j+1 < n and s[j+1] == s[i]) ++j; 30 | if (j+1 < n and s[i] < s[j+1]) { 31 | rep(k,i,j+1) { o += s[i]; o += s[i]; } 32 | } else { 33 | rep(k,i,j+1) { o += s[i]; } 34 | } 35 | i = j + 1; 36 | } 37 | cout << "Case #" << k << ": " << o << '\n'; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2022/round1B/ASeDatAb.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | // ios::sync_with_stdio(false); cin.tie(0); 21 | int T; cin >> T; 22 | rep(k,1,T+1) { 23 | string query = "00000000"; 24 | cout << query << '\n'; 25 | int feedback; 26 | cin >> feedback; 27 | while (feedback != -1 and feedback != 0) { 28 | int n_ones = feedback; 29 | query = "00000000"; 30 | rep(i,0,n_ones) query[i] = '1'; 31 | cout << query << '\n'; 32 | cin >> feedback; 33 | } 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/Codejam/2022/round1B/ASeDatAb.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | T = int(input()) 3 | for _ in range(1, T+1): 4 | i = 0 5 | query = list("00000000") 6 | print(''.join(query)) 7 | feedback = int(input()) 8 | while feedback != -1 and feedback != 0: 9 | n_ones = feedback 10 | query = list("00000000") 11 | for i in range(n_ones): 12 | query[i] = '1' 13 | print(''.join(query)) 14 | feedback = int(input()) 15 | 16 | main() -------------------------------------------------------------------------------- /Solved problems/LiveArchive/2389_PalindromNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | typedef unsigned long long int ull; 7 | 8 | int main() { 9 | ull num, n; 10 | vector remainders; 11 | vector bases; 12 | while(true) { 13 | scanf("%llu", &num); 14 | if (num == 0) break; 15 | for (int b = 2; b <= 16; ++b) { 16 | n = num; 17 | do { 18 | remainders.push_back(n % b); 19 | } while ((n /= b) > 0); 20 | bool isPalindrom = true; 21 | for (int i = 0, j = remainders.size() - 1; i < j; ++i, --j) { 22 | if (remainders[i] != remainders[j]) { 23 | isPalindrom = false; 24 | break; 25 | } 26 | } 27 | if (isPalindrom) bases.push_back(b); 28 | remainders.clear(); 29 | } 30 | if (bases.size() > 0) { 31 | printf("Number %llu is palindrom in basis", num); 32 | for (int i = 0; i < bases.size(); ++i) printf(" %d", bases[i]); 33 | puts(""); 34 | } else { 35 | printf("Number %llu is not palindrom\n", num); 36 | } 37 | bases.clear(); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/3142_Bullseye.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | 6 | double x[2][3]; 7 | double y[2][3]; 8 | int scores[2]; 9 | 10 | void computeScore(int p) { 11 | scores[p] = 0; 12 | rep(i,0,2) { 13 | double d = x[p][i] * x[p][i] + y[p][i] * y[p][i]; 14 | // printf("scores[%d] = %d, d = %lf\n", p, scores[p], d); 15 | if (d <= 9.0) scores[p] += 100; 16 | else if (d <= 36.0) scores[p] += 80; 17 | else if (d <= 81.0) scores[p] += 60; 18 | else if (d <= 144.0) scores[p] += 40; 19 | else if (d <= 225.0) scores[p] += 20; 20 | } 21 | } 22 | 23 | int main () { 24 | while (true) { 25 | rep(i,0,2) scanf("%lf%lf",&x[0][i], &y[0][i]); 26 | rep(i,0,2) scanf("%lf%lf",&x[1][i], &y[1][i]); 27 | if (x[0][0] == -100) break; 28 | computeScore(0); 29 | computeScore(1); 30 | if (scores[0] == scores[1]) { 31 | printf("SCORE: %d to %d, TIE.\n", scores[0], scores[1]); 32 | } else { 33 | printf("SCORE: %d to %d, PLAYER %d WINS.\n", 34 | scores[0], scores[1], (scores[0] > scores[1] ? 1 : 2)); 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/3143_AnExcel-lentProblem.cpp: -------------------------------------------------------------------------------- 1 | // tags: finding patterns in number sequences 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | typedef long long int ll; 10 | 11 | void toBase(int b, int x, vector& digits) { 12 | do { 13 | digits.push_back(x % b); 14 | } while (x /= b); 15 | } 16 | 17 | void getDigits(int x, vector& digits) { 18 | int count = 1; 19 | ll aux = 26; 20 | while (x >= aux) { 21 | x -= aux; 22 | aux *= 26; 23 | count++; 24 | } 25 | toBase(26, x, digits); 26 | while (digits.size() < count) digits.push_back(0); 27 | } 28 | 29 | int main() { 30 | string line; 31 | while (true) { 32 | getline(cin, line); 33 | std::size_t pos = line.find('C'); 34 | int row = atoi(line.substr(1, pos-1).c_str()); 35 | int col = atoi(line.substr(pos+1).c_str()); 36 | if (row == 0 && col == 0) break; 37 | vector digits; 38 | getDigits(col-1, digits); 39 | for (int i = (int)digits.size()-1; i >= 0; --i) 40 | printf("%c", (char)(digits[i] + 'A')); 41 | printf("%d\n", row); 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/3144_LennysLuckyLottoLists.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 6 | typedef long long int ll; 7 | 8 | int n; 9 | ll m; 10 | ll memo[10][2001]; 11 | 12 | ll dp(int i, ll l) { 13 | if (l << (n-i-1) > m) return 0; 14 | if (i == n-1) return m - l + 1; 15 | if (memo[i][l] != -1) return memo[i][l]; 16 | return memo[i][l] = dp(i+1, l*2) + dp(i,l+1); 17 | } 18 | 19 | int main() { 20 | int tt; 21 | scanf("%d", &tt); 22 | rep(t,1,tt) { 23 | scanf("%d%lld",&n,&m); 24 | memset(memo, -1, sizeof memo); 25 | printf("Data set %d: %d %lld %lld\n",t,n,m,dp(0, 1)); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/3145_FlippingPancake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define MAXN 30 6 | #define rep(i,a,b) for(int i=a;i<=b;++i) 7 | #define invrep(i,b,a) for(int i=b;i>=a;--i) 8 | int sizes[MAXN]; 9 | 10 | int max_i(int n) { 11 | int i = n-1; 12 | int m = sizes[i]; 13 | invrep(j, n-2, 0) { 14 | if (m < sizes[j]) { 15 | m = sizes[j]; 16 | i = j; 17 | } 18 | } 19 | return i; 20 | } 21 | 22 | void invert(int i, int j) { 23 | while (i < j) { 24 | int aux = sizes[i]; 25 | sizes[i] = sizes[j]; 26 | sizes[j] = aux; 27 | i++; j--; 28 | } 29 | } 30 | 31 | int main() { 32 | int n; 33 | while(true) { 34 | scanf("%d", &n); 35 | if (n == 0) break; 36 | rep (i,0,n-1) scanf("%d", &sizes[i]); 37 | vector flips; 38 | while (n) { 39 | int mi = max_i(n); 40 | if (mi < n-1) { 41 | if (mi > 0) { 42 | invert(0, mi); 43 | flips.push_back(mi + 1); 44 | } 45 | invert(0, n-1); 46 | flips.push_back(n); 47 | } 48 | n--; 49 | } 50 | printf("%d", (int)flips.size()); 51 | for (int f : flips) printf(" %d", f); 52 | puts(""); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/3147_ModelRocketHeight.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, trigonometry 2 | #include // add almost everything in one shot 3 | using namespace std; 4 | typedef long long int ll; 5 | const double PI = acos(-1); 6 | 7 | int main() { 8 | double D, H; 9 | double a, b, c; 10 | scanf("%lf%lf", &D, &H); 11 | while (true) { 12 | scanf("%lf%lf%lf",&a,&b,&c); 13 | if (a <= 0 || b <= 0 || c <= 0) break; 14 | a *= PI / 180; 15 | b *= PI / 180; 16 | c *= PI / 180; 17 | double auxa = 1 / (tan(a) * tan(a)); 18 | double auxb = 1 / (tan(b) * tan(b)); 19 | double auxc = 1 / (tan(c) * tan(c)); 20 | double aux = round(sqrt(2 * D * D / (auxa + auxc - 2 * auxb)) + H); 21 | ll ans = (ll) aux; 22 | printf("%lld\n", ans); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/4215_Feynman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int memo[101]; 6 | 7 | void fill_memo() { 8 | memset(memo,0,sizeof(memo)); 9 | for(int n = 1; n <= 100; ++n) { 10 | int sum = 0; 11 | for(int i = 0; i < n; ++i) { 12 | sum += (n-i)*(n-i); 13 | } 14 | memo[n] = sum; 15 | } 16 | } 17 | 18 | int main() { 19 | 20 | fill_memo(); 21 | 22 | while (true) { 23 | int n; 24 | scanf("%d", &n); 25 | if (n == 0) 26 | break; 27 | 28 | printf("%d\n", memo[n]); 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Solved problems/LiveArchive/4216_PolePosition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i,first,n) for(int i=first; i < n; ++i) 6 | 7 | int standings[1005]; 8 | int main() { 9 | int n; 10 | while (true) { 11 | scanf("%d", &n); 12 | if (n == 0) 13 | break; 14 | 15 | bool valid = true; 16 | memset(standings, 0, sizeof(standings)); 17 | 18 | FOR(i,0,n) { 19 | int carnumber; 20 | int shift; 21 | scanf("%d %d", &carnumber, &shift); 22 | 23 | if (!valid) 24 | continue; 25 | int j = i + shift; 26 | if (j < 0 || j >= n || standings[j] != 0) { 27 | valid = false; 28 | continue; 29 | } 30 | standings[j] = carnumber; 31 | } 32 | 33 | if (valid) { 34 | printf("%d", standings[0]); 35 | FOR(i,1,n) printf(" %d", standings[i]); 36 | puts(""); 37 | } else { 38 | printf("-1\n"); 39 | } 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Solved problems/LiveArchive/4382_YAPTCHA.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, primes, sieve of eratosthenes 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int MAXN = 1000000; 8 | const int MAXNK = 3 * MAXN + 7; 9 | 10 | vector isprime(MAXNK + 1, true); 11 | vector prime_counts(MAXN + 1, 0); 12 | 13 | void set_primes() { 14 | int limit = (int) floor(sqrt(MAXNK)); 15 | for (int i = 2; i <= limit; ++i) 16 | if (isprime[i]) 17 | for (int j = i * i; j <= MAXNK; j += i) 18 | isprime[j] = false; 19 | } 20 | void compute_prime_counts() { 21 | for (int n = 1; n <= MAXN; ++n) { 22 | prime_counts[n] = prime_counts[n - 1]; 23 | if (isprime[3 * n + 7]) 24 | prime_counts[n]++; 25 | } 26 | } 27 | 28 | int main() { 29 | set_primes(); 30 | compute_prime_counts(); 31 | int t, n; 32 | scanf("%d", &t); 33 | while (t-- > 0) { 34 | scanf("%d", &n); 35 | printf("%d\n", prime_counts[n]); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Solved problems/LiveArchive/4809_ElectricNeeds/gen.py: -------------------------------------------------------------------------------- 1 | import random 2 | N = 6 3 | M = 6 4 | P = 2 5 | Q = N*M - P 6 | sample = random.sample(range(N*M), P) 7 | print(N, M, P) 8 | for i in range(P): 9 | r = sample[i] // M 10 | c = sample[i] % M 11 | print(r+1, c+1) 12 | print(Q) 13 | print(' '.join(map(str, range(1, Q+1)))) 14 | print('0 0 0') -------------------------------------------------------------------------------- /Solved problems/LiveArchive/4938_ManualCipher.cpp: -------------------------------------------------------------------------------- 1 | // tags: string manipulation 2 | #include 3 | using namespace std; 4 | 5 | string lowkeys = "abcdefghijklmnopqrstuvwxyz "; 6 | string upkeys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "; 7 | 8 | int main() { 9 | string line; 10 | // first line 11 | getline(cin, line); 12 | std:size_t pos = line.find(':'); 13 | vector offsets, digits; 14 | for (int i = 0; i < pos; ++i) 15 | offsets.push_back(line[i]-'A'); 16 | for (int i = pos+1; i < line.size(); ++i) 17 | digits.push_back(line[i] - '0'); 18 | 19 | // next lines 20 | while(getline(cin, line)) { 21 | int level = 0; 22 | int count = 0; 23 | for (char c : line) { 24 | int i = (c == ' ') ? 26 : (c - 'A'); 25 | i = (27 + (i - offsets[level])) % 27; 26 | printf("%c", lowkeys[i]); 27 | 28 | if (++count == digits[level]) { 29 | count = 0; 30 | if (++level == digits.size()) 31 | level = 0; 32 | } 33 | } 34 | puts(""); 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/5822_DistributingBallotBoxes.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy, implementation, std::priority_queue 2 | #pragma GCC optimize("Ofast") 3 | #include // import everything in one shot 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | const int MAXN = 500000; 8 | int boxes[MAXN]; 9 | int pop[MAXN]; 10 | int score[MAXN]; 11 | struct cmp { 12 | bool operator()(int i, int j) { return score[i] < score[j]; } 13 | }; 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); 17 | while (true) { 18 | int N, B; cin >> N >> B; 19 | if (N==-1) break; 20 | priority_queue, cmp> pq; 21 | rep(i,0,N-1) { 22 | cin >> pop[i]; 23 | score[i] = pop[i]; 24 | boxes[i] = 1; 25 | pq.push(i); 26 | } 27 | B -= N; 28 | while (B--) { 29 | int i = pq.top(); pq.pop(); 30 | boxes[i]++; 31 | score[i] = (pop[i] / boxes[i]) + (pop[i] % boxes[i] > 0); 32 | pq.push(i); 33 | } 34 | cout << score[pq.top()] << '\n'; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/5822_DistributingBallotBoxes_v2.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | // ------------------------------- 6 | int pop[500000]; 7 | int main() { 8 | ios::sync_with_stdio(false); cin.tie(0); 9 | while (true) { 10 | int N, B; cin >> N >> B; 11 | if (N==-1) break; 12 | rep(i,0,N) cin >> pop[i]; 13 | int l = 1, r = *max_element(pop, pop+N); 14 | while (l < r) { 15 | int m = (l+r) >> 1; // (l+r) / 2; 16 | int c = B; 17 | rep(i,0,N) { 18 | c -= ((pop[i] / m) + (pop[i] % m > 0)); 19 | if (c < 0) break; 20 | } 21 | if (c >= 0) r = m; 22 | else l = m + 1; 23 | } 24 | cout << l << '\n'; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/6528_DisjointWaterSupply.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP, DAG 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i=a; i<=b; i++) 5 | typedef vector vi; 6 | 7 | int C,P; 8 | vector g; 9 | int memo[1000][1000]; 10 | int dp(int i, int j) { 11 | if (i > j) swap(i,j); 12 | if (memo[i][j] != -1) 13 | return memo[i][j]; 14 | int ans; 15 | if (i == 0 or j == 0) 16 | ans = true; 17 | else if (i == j) 18 | ans = false; 19 | else { 20 | ans = false; 21 | for (int v : g[j]) 22 | if (dp(i, v)) { 23 | ans = true; break; 24 | } 25 | } 26 | return memo[i][j] = ans; 27 | } 28 | 29 | int main() { 30 | while(scanf("%d%d",&C,&P)==2) { 31 | g.assign(C,vi()); 32 | rep(i,1,P) { 33 | int u,v; scanf("%d%d",&u,&v); --u,--v; 34 | g[v].push_back(u); 35 | } 36 | int count = 0; 37 | memset(memo, -1, sizeof memo); 38 | rep(i,0,C-2) rep(j,i+1,C-1) 39 | if (dp(i,j)) count++; 40 | printf("%d\n", count); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/6821_AutomatedCheckingMachine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i=a; i<=b; i++) 4 | int main() { 5 | int x[5], y[5]; 6 | while (scanf("%d",&x[0]) == 1) { 7 | rep(i,1,4) scanf("%d",&x[i]); 8 | rep(i,0,4) scanf("%d",&y[i]); 9 | bool comp = true; 10 | rep(i,0,4) if (x[i] == y[i]) { comp = false; break; } 11 | if (comp) puts("Y"); 12 | else puts("N"); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/6822_BlackAndWhiteStones.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy, two pointers 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | typedef long long int ll; 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(0); 9 | ll a, b; 10 | while (cin >> a >> b) { 11 | b = a - b; 12 | string s; cin >> s; 13 | int count = 0, n = s.size(); 14 | for (char c : s) if (c == 'B') count++; 15 | int i = count - 1, j = count; 16 | ll ans = 0; 17 | while (true) { 18 | while (i >= 0 and s[i] == 'B') --i; 19 | while (j < n and s[j] == 'W') ++j; 20 | if (i < 0 or j >= n) break; 21 | ans += min((j-i) * b, a); 22 | --i, ++j; 23 | } 24 | cout << ans << '\n'; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/7206_DasInDaedalus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i=a; i<=b; i++) 4 | #define invrep(i,a,b) for(int i=a; i>=b; i--) 5 | int n,m; 6 | int b, d_prev, d_after; 7 | int C[5] = {1,10,100,1000,10000}; 8 | int main() { 9 | while (scanf("%d%d",&n,&m) == 2) { 10 | d_prev = d_after = 0; 11 | rep(i,1,m) { 12 | int tot = 0, tmp, dmove; 13 | scanf("%d %d", &b, &dmove); 14 | rep(j,2,n) { scanf("%d", &tmp); tot += tmp; } 15 | if (dmove + tot <= b) 16 | d_prev += dmove; 17 | invrep(j,4,0) { 18 | if (C[j] + tot <= b) { 19 | d_after += C[j]; 20 | break; 21 | } 22 | } 23 | } 24 | printf("%d\n",d_after-d_prev); 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/7211_IdentifyingTea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int t,a,b,c,d,e; 5 | while(scanf("%d%d%d%d%d%d",&t,&a,&b,&c,&d,&e) == 6) { 6 | printf("%d\n",(a==t)+(b==t)+(c==t)+(d==t)+(e==t)); 7 | } 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/7413_SqueezeTheCylinders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #define FOR(i,a,b) for(int i = a; i <= b; ++i) 7 | 8 | struct Circle { 9 | double offset; 10 | int radius; 11 | Circle (double o, int r) : offset(o), radius(r) {} 12 | }; 13 | 14 | int main() { 15 | int N; 16 | double left, right; 17 | int r; 18 | while (scanf("%d", &N) == 1) { 19 | left = 0; 20 | right = 0; 21 | vector circles; 22 | FOR(i,0,N-1) 23 | { 24 | scanf("%d", &r); 25 | double offset = 0; 26 | FOR(j,0,i-1) { 27 | Circle c = circles[j]; 28 | double d = sqrt((c.radius + r) * (c.radius + r) - (c.radius - r) * (c.radius - r)); 29 | offset = max(offset, c.offset + d); 30 | } 31 | left = min(left, offset - r); 32 | right = max(right, offset + r); 33 | circles.push_back(Circle(offset, r)); 34 | } 35 | double width = right - left; 36 | printf("%.8lf\n", width); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Solved problems/LiveArchive/8195_HardChoice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int ca, ba, pa, cr, br, pr; 5 | while (cin >> ca >> ba >> pa >> cr >> br >> pr) { 6 | cout << max(cr - ca, 0) + max(br - ba, 0) + max(pr - pa, 0) << endl; 7 | } 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Solved problems/LiveArchive/random_input.cpp: -------------------------------------------------------------------------------- 1 | #include /* printf, scanf, puts, NULL */ 2 | #include /* srand, rand */ 3 | #include /* time */ 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int times = 1000; 9 | srand (time(NULL)); 10 | std::random_device rd; //Will be used to obtain a seed for the random number engine 11 | std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() 12 | std::uniform_real_distribution<> dis(0.0, 1.0); 13 | while (times--) { 14 | int R = rand() % 10 + 1; 15 | int C = rand() % 10 + 1; 16 | printf("%d %d\n", R, C); 17 | for (int r = 0; r < R; ++r) { 18 | for (int c = 0; c < C; ++c) { 19 | if (dis(gen) <= 0.7) { 20 | putchar('#'); 21 | } else { 22 | putchar('.'); 23 | } 24 | } 25 | puts(""); 26 | } 27 | puts(""); 28 | } 29 | puts("0 0"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solved problems/Matcomgrader/EggfruitCake.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, two pointers 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | int main() { 9 | ios::sync_with_stdio(false); 10 | cin.tie(0); cout.tie(0); 11 | string B; int S; 12 | cin >> B >> S; 13 | int N = B.size(); 14 | ll count = 0; 15 | int j = 0; 16 | rep(i,0,N) { 17 | if (j < i) j = i; 18 | while (B[j%N] != 'E' and j - i + 1 < S) ++j; 19 | if (B[j%N] == 'E') count += S - (j-i); 20 | } 21 | cout << count << '\n'; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solved problems/Matcomgrader/ImproveSPAM.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP top-down, dfs 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | const ll MOD = 1000000007; 9 | int N, L; 10 | vector> g; 11 | vector memo; 12 | ll dp(int u) { 13 | ll& ans = memo[u]; 14 | if (ans != -1) return ans; 15 | if (u >= L) return ans = 1; 16 | ans = 0; 17 | for (int v : g[u]) ans = (ans + dp(v)) % MOD; 18 | return ans; 19 | } 20 | 21 | vector vis; 22 | void dfs(int u) { 23 | vis[u] = true; 24 | for (int v : g[u]) { 25 | if (vis[v]) continue; 26 | dfs(v); 27 | } 28 | } 29 | 30 | int main() { 31 | ios::sync_with_stdio(false); cin.tie(0); 32 | cin >> N >> L; 33 | g.resize(N); 34 | rep(i,0,L) { 35 | int k; cin >> k; 36 | while (k--) { 37 | int j; cin >> j; --j; 38 | g[i].push_back(j); 39 | } 40 | } 41 | memo.assign(N, -1); 42 | ll bef = dp(0); 43 | vis.assign(N, 0); 44 | dfs(0); 45 | int aft = 0; 46 | rep(i,L,N) if (vis[i]) aft++; 47 | cout << bef << ' ' << aft << '\n'; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Solved problems/Matcomgrader/MountainRanges.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | int N, X; 8 | int main() { 9 | ios::sync_with_stdio(false); 10 | cin.tie(0); cout.tie(0); 11 | cin >> N >> X; 12 | vector A(N); 13 | rep(i,0,N) cin >> A[i]; 14 | int ans = 1; 15 | int i = 0; 16 | while (i < N) { 17 | int len = 1; 18 | while (i+1 < N and A[i+1] - A[i] <= X) ++i, ++len; 19 | ans = max(ans, len); 20 | ++i; 21 | } 22 | cout << ans << '\n'; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Solved problems/POJ/2839_ConvexHullAndTriangle/gen.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | L1 = 1000000 5 | L2 = 3000000 6 | 7 | n = 60 8 | print(n) 9 | for _ in range(n): 10 | print(" ".join(map(str, (random.randint(-L1, L1) for _ in range(2))))) 11 | m = 1000 12 | print(m) 13 | for _ in range(m): 14 | print(" ".join(map(str, (random.randint(-L2, L2) for _ in range(6))))) -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-11/H_huaauhahhuahau.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | set vowels_set = {'a','e','i','o','u'}; 5 | 6 | int main() { 7 | string line; 8 | while(getline(cin, line)) { 9 | vector vowels; 10 | for(char c : line) { 11 | if (vowels_set.find(c) != vowels_set.end()) { 12 | vowels.push_back(c); 13 | } 14 | } 15 | int i = 0, j = (int)vowels.size()-1; 16 | bool valid = true; 17 | while (i < j) { 18 | if (vowels[i] != vowels[j]) { 19 | valid = false; 20 | break; 21 | } 22 | i++, j--; 23 | } 24 | if (valid) 25 | puts("S"); 26 | else 27 | puts("N"); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-11/L_Tiles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for (int i=a; i<=b; ++i) 5 | 6 | int H, L; 7 | int board[200][200]; 8 | 9 | int shifts[4][2] = { 10 | {0, 1}, 11 | {0, -1}, 12 | {1, 0}, 13 | {-1, 0} 14 | }; 15 | 16 | int dfs(int r, int c, int color) { 17 | if (r < 0 || r >= H || c < 0 || c >= L) 18 | return 0; 19 | if (board[r][c] != color) 20 | return 0; 21 | 22 | board[r][c] = -1; 23 | int tot = 1; 24 | rep(i,0,3) { 25 | int rr = r + shifts[i][0]; 26 | int cc = c + shifts[i][1]; 27 | tot += dfs(rr, cc, color); 28 | } 29 | return tot; 30 | } 31 | 32 | int main() { 33 | while (scanf("%d%d", &H, &L) == 2) { 34 | 35 | rep(r,0,H-1) rep(c,0,L-1) scanf("%d", &board[r][c]); 36 | 37 | int marea = 999999; 38 | rep(r,0,H-1) { 39 | rep(c,0,L-1) { 40 | if (board[r][c] != -1) { 41 | marea = min(dfs(r,c,board[r][c]), marea); 42 | } 43 | } 44 | } 45 | 46 | printf("%d\n", marea); 47 | } 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-12/J_JuntandoLineas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | #define MAXN 5000 6 | 7 | int N,L,C; 8 | int lengths[MAXN]; 9 | 10 | int main() { 11 | scanf("%d%d%d",&N,&L,&C); 12 | rep(i,0,N-1) scanf("%d",&lengths[i]); 13 | sort(lengths, lengths + N); 14 | int ncols = (N / L) + ((N % L == 0) ? 0 : 1); 15 | int totwidth = 0; 16 | rep (c, 0, ncols-1) { 17 | totwidth += lengths[N-1-L*c]; 18 | } 19 | if (totwidth <= C) puts("S"); 20 | else puts("N"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-12/L_LeonardoDePisa.cpp: -------------------------------------------------------------------------------- 1 | // tags: ad-hoc 2 | #include 3 | using namespace std; 4 | 5 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 6 | 7 | int N,K; 8 | 9 | int main() { 10 | scanf("%d%d",&N,&K); 11 | int prev_l, prev_l2, curr_l; 12 | prev_l = -1; 13 | prev_l2 = -1; 14 | bool valid = true; 15 | 16 | rep(i,1,K) { 17 | scanf("%d", &curr_l); 18 | 19 | if (prev_l != -1) { 20 | int d = abs(curr_l - prev_l); 21 | if ((d != 1 && d != 2) || prev_l2 == curr_l) { 22 | valid = false; 23 | break; 24 | } 25 | } 26 | 27 | prev_l2 = prev_l; 28 | prev_l = curr_l; 29 | } 30 | 31 | if (valid) puts("S"); 32 | else puts("N"); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-14/I_CommonKnowledge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | typedef long long int ll; 6 | 7 | int main() { 8 | int n; 9 | ll eight = 8; 10 | ll ans = 1; 11 | scanf("%d", &n); 12 | while(n--) { 13 | ans *= eight; 14 | } 15 | printf("%lld\n", ans); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-14/J_SellingCPUs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | typedef long long int ll; 6 | 7 | 8 | int c, m; 9 | ll memo[101][101]; 10 | int p[100][101]; 11 | 12 | ll dp(int x, int i) { 13 | if (x == 0 || i == m) 14 | return 0; 15 | if (memo[x][i] != -1) 16 | return memo[x][i]; 17 | 18 | ll ans = 0; 19 | rep(k,0,x) { 20 | ans = max(ans, p[i][k] + dp(x-k,i+1)); 21 | } 22 | return memo[x][i] = ans; 23 | } 24 | 25 | int main() { 26 | scanf("%d%d",&c,&m); 27 | rep(i,0,m-1) { 28 | p[i][0] = 0; 29 | rep(j,1,c) { 30 | scanf("%d", &p[i][j]); 31 | } 32 | } 33 | memset(memo, -1, sizeof memo); 34 | printf("%lld\n", dp(c,0)); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-8/A_FreightTrain.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | 5 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 6 | #define MAXW 10000 7 | 8 | int N, W, L; 9 | int pos[MAXW]; 10 | 11 | bool possible(int K) { 12 | int l = 0, r = K-1; 13 | int i = 0; 14 | int count = 0; 15 | while (l <= r) { 16 | ++count; 17 | if (i == W) 18 | break; 19 | if (pos[i] > r) { 20 | l = pos[i]; 21 | r = min(l + K-1, N-1); 22 | } else { 23 | while(++i < W && pos[i] <= r) {} 24 | l = r+1; 25 | r = min(l + K-1, N-1); 26 | } 27 | } 28 | return count <= L; 29 | } 30 | 31 | int main() { 32 | int T; 33 | scanf("%d", &T); 34 | while (T--) { 35 | scanf("%d%d%d", &N, &W, &L); 36 | rep (i, 0, W-1) { 37 | scanf("%d", &pos[i]); 38 | pos[i]--; 39 | } 40 | // 41 | int kmin = 1, kmax = N; 42 | while (kmin < kmax) { 43 | int kmid = (kmin + kmax) / 2; 44 | if (possible(kmid)) 45 | kmax = kmid; 46 | else 47 | kmin = kmid + 1; 48 | } 49 | printf("%d\n", kmin); 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-8/E_TheKingsWalk.cpp: -------------------------------------------------------------------------------- 1 | // TODO: it's wrong, yields TLE 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 8 | 9 | typedef long long int ll; 10 | 11 | const ll M = 5318008; 12 | ll dp[5001]; 13 | ll aux[5001]; 14 | 15 | inline void swap(int x, int y) { 16 | int tmp = x; x = y; y = tmp; 17 | } 18 | 19 | int main() { 20 | int T; 21 | scanf("%d", &T); 22 | while (T--) { 23 | int n, x1, x2, y1, y2; 24 | scanf("%d", &n); 25 | scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 26 | x1--,y1--,x2--,y2--; 27 | if (abs(x2-x1) < abs(y2-y1)) 28 | swap(x1, y1), swap(x2, y2); 29 | if (x1 > x2) 30 | swap(x1, x2), swap(y1, y2); 31 | memset(dp, 0, sizeof(dp[0]) * n); 32 | dp[y1] = 1; 33 | rep (i, x1, x2-1) { 34 | memset(aux, 0, sizeof(aux[0]) * n); 35 | rep (j, 0, n-1) { 36 | if (j > 0) aux[j-1] = (aux[j-1] + dp[j]) % M; 37 | aux[j] = (aux[j] + dp[j]) % M; 38 | if (j < n-1) aux[j+1] = (aux[j+1] + dp[j]) % M; 39 | } 40 | memcpy(dp, aux, sizeof(dp[0]) * n); 41 | } 42 | printf("%lld\n", dp[y2] % M); 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-8/N_MedalRanking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | scanf("%d", &n); 7 | while(n--) { 8 | int g1, s1, b1, g2, s2, b2; 9 | scanf("%d%d%d%d%d%d",&g1,&s1,&b1,&g2,&s2,&b2); 10 | bool color = (g1>g2 || 11 | (g1 == g2 && (s1 > s2 || (s1 == s2 && b1 > b2)))); 12 | bool count = g1 + s1 + b1 > g2 + s2 + b2; 13 | printf("%d %d %d %d %d %d\n",g1,s1,b1,g2,s2,b2); 14 | if (color && count) 15 | puts("both"); 16 | else if(color) 17 | puts("color"); 18 | else if(count) 19 | puts("count"); 20 | else 21 | puts("none"); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-9/F_FactorialProducts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | #define invrep(i,a,b) for(int i=a; i>=b; --i) 6 | 7 | const double pr = 4.342727686266486E-5; 8 | double logfac[2502]; 9 | 10 | int main() { 11 | 12 | logfac[0] = 0; 13 | rep(i,1,2501) { 14 | logfac[i] = logfac[i-1] + log(i) / log(10); 15 | } 16 | 17 | int T; scanf("%d", &T); 18 | 19 | rep(test,1,T) { 20 | 21 | int A,B,C; scanf("%d%d%d",&A,&B,&C); 22 | double totA = 0, totB = 0, totC = 0; 23 | rep(i,0,A-1) { 24 | int k; scanf("%d", &k); 25 | totA += logfac[k]; 26 | } 27 | rep(i,0,B-1) { 28 | int k; scanf("%d", &k); 29 | totB += logfac[k]; 30 | } 31 | rep(i,0,C-1) { 32 | int k; scanf("%d", &k); 33 | totC += logfac[k]; 34 | } 35 | 36 | printf("Case #%d: ", test); 37 | if (totA - pr >= totB && totA - pr >= totC) { 38 | puts("A"); 39 | } else if (totB - pr >= totA && totB - pr >= totC) { 40 | puts("B"); 41 | } else if (totC - pr >= totA && totC - pr >= totB) { 42 | puts("C"); 43 | } else { 44 | puts("TIE"); 45 | } 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Solved problems/Red de Programación Competitiva/2016-Competencia-9/J_ShoppingSpree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | #define MAXS 500 6 | 7 | int n, s; 8 | int value[MAXS + 1]; 9 | int memo[MAXS+1][MAXS+1]; 10 | 11 | int dp(int i, int k) { 12 | if (i > s) return 0; 13 | if (memo[i][k] != -1) 14 | return memo[i][k]; 15 | int ans; 16 | if (i == 1 || i/2 > k) { 17 | ans = max(value[i] + dp(i+1, k+1), dp(i+1, k)); 18 | } else { 19 | ans = dp(i+1, k); 20 | } 21 | return memo[i][k] = ans; 22 | } 23 | 24 | int main() { 25 | scanf("%d", &n); 26 | rep(spree, 1, n) { 27 | scanf("%d", &s); 28 | rep(i,1,s) { 29 | scanf("%d", &value[i]); 30 | } 31 | memset(memo, -1, sizeof memo); 32 | printf("Spree #%d: %d\n", spree, dp(1, 0)); 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/BBIN2_BúsquedaBinaria2.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | // ------------------------------- 6 | int nums[100000]; 7 | int main() { 8 | int N, Q; cin >> N >> Q; 9 | rep(i,0,N) cin >> nums[i]; 10 | while(Q--) { 11 | int x; cin >> x; 12 | int l=0, r=N; 13 | while (l < r) { 14 | int m = (l+r)>>1; 15 | if (nums[m] > x) r = m; 16 | else l = m+1; 17 | } 18 | l--; 19 | if (l < 0 or nums[l] != x) cout << "-1\n"; 20 | else cout << l << '\n'; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/BBIN_BusquedaBinaria.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | // ------------------------------- 6 | int nums[100000]; 7 | int main() { 8 | int N, Q; cin >> N >> Q; 9 | rep(i,0,N) cin >> nums[i]; 10 | while(Q--) { 11 | int x; cin >> x; 12 | int l=0, r=N; 13 | while (l < r) { 14 | int m = (l+r)>>1; 15 | if (nums[m] >= x) r = m; 16 | else l = m+1; 17 | } 18 | if (l == N or nums[l] != x) cout << "-1\n"; 19 | else cout << l << '\n'; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/BTCK_A-problem-of-Backtracking_v1.cpp: -------------------------------------------------------------------------------- 1 | // tags: brute force, implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | ll X[10], D[10]; 9 | int main() { 10 | ios::sync_with_stdio(false); 11 | cin.tie(0); 12 | int T; cin >> T; 13 | loop: 14 | while (T--) { 15 | rep(i,0,10) D[i] = i; 16 | rep(i,0,10) cin >> X[i]; 17 | ll K; cin >> K; 18 | do { 19 | ll sum = 0; 20 | rep(i,0,10) sum += D[i] * X[i]; 21 | if (sum <= K) { 22 | rep(i,0,10) { 23 | if (i) cout << ' '; 24 | cout << D[i]; 25 | } 26 | cout << '\n'; 27 | goto loop; 28 | } 29 | } while (next_permutation(D, D+10)); 30 | cout << "-1\n"; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/CROSSPDCT_Left-right-or-center.cpp: -------------------------------------------------------------------------------- 1 | // tags: geometry, cross product 2 | #include // import everything in one shot 3 | using namespace std; 4 | typedef long long int ll; 5 | 6 | // ------------------------------- 7 | // references: 8 | // https://github.com/PabloMessina/Competitive-Programming-Material/blob/master/Geometry/Geometry2DUtils.cpp 9 | // https://www.geeksforgeeks.org/orientation-3-ordered-points/ 10 | struct Point { ll x, y; }; 11 | ll cross(Point& a, Point& b, Point& c) { 12 | ll dx0 = b.x - a.x, dy0 = b.y - a.y; 13 | ll dx1 = c.x - a.x, dy1 = c.y - a.y; 14 | return dx0 * dy1 - dy0 * dx1; 15 | } 16 | int main() { 17 | Point a, b; int n; 18 | cin >> a.x >> a.y >> b.x >> b.y >> n; 19 | while (n--) { 20 | Point p; cin >> p.x >> p.y; 21 | ll tmp = cross(a, b, p); 22 | // ternary operator: https://www.cprogramming.com/reference/operators/ternary-operator.html 23 | char ans = tmp > 0 ? 'I' : tmp < 0 ? 'D' : 'C'; 24 | cout << ans << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/FACT1.cpp: -------------------------------------------------------------------------------- 1 | // TODO: finish this 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | // defines 6 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 7 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 8 | #define umap unordered_map 9 | #define uset unordered_set 10 | // typedefs 11 | typedef unsigned int uint; 12 | typedef unsigned long long int ull; 13 | typedef long long int ll; 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef tuple iii; 17 | // ------------------------------- 18 | typedef long long int128_t; 19 | typedef unsigned long long uint128_t; 20 | 21 | uint128_t in() { 22 | string s; cin >> s; 23 | uint128_t x = 0; 24 | for (char c : s) x = x * 10 + (c-'0'); 25 | return x; 26 | } 27 | 28 | out(uint128_t x) { 29 | static int digits[128]; 30 | int i = 0; 31 | while(x) { digits[i++] = x%10; x/=10; } 32 | invrep(j,i-1,0) cout << digits[j]; 33 | } 34 | 35 | int main() { 36 | // OPTIONAL: for faster input/output (only if using cin/cout) 37 | ios::sync_with_stdio(false); 38 | cin.tie(0); 39 | uint128_t x = in(); 40 | cout << "rofl "; out(x); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/FINDPRM_FindingPrimes.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, primes, sieve of eratosthenes, DP 2 | #pragma GCC optimize("Ofast") // OPTIONAL: for faster execution 3 | #include // import everything in one shot 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | const int MAXN = 10000000; 8 | vector isprime(MAXN+1, true); 9 | int prime_count[MAXN+1] = {0}; 10 | 11 | int main() { 12 | ios::sync_with_stdio(false); 13 | cin.tie(0); 14 | int limit = (int) floor(sqrt(MAXN)); 15 | rep(x,2,limit) { 16 | if (isprime[x]) { 17 | for (int y = x*x; y <= MAXN; y+=x) { 18 | isprime[y] = false; 19 | } 20 | } 21 | } 22 | rep(x,2,MAXN) prime_count[x] = prime_count[x-1] + isprime[x]; 23 | int t; cin >> t; 24 | while (t--) { 25 | int n; cin >> n; 26 | cout << prime_count[n] - prime_count[n>>1] << '\n'; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/HISTOGRA_LargestRectangleInAHistogram.py: -------------------------------------------------------------------------------- 1 | MAXN = 100000 2 | L = [None] * MAXN 3 | R = [None] * MAXN 4 | 5 | if __name__ == '__main__': 6 | while True: 7 | N, *H = map(int, input().split()) 8 | if N == 0: break 9 | 10 | # Llenamos L 11 | s = [] 12 | s.append((-1, -1)) 13 | for i in range(0, N): 14 | while s[-1][1] >= H[i]: 15 | s.pop() 16 | L[i] = s[-1][0] 17 | s.append((i, H[i])) 18 | 19 | # Llenamos R 20 | s = [] 21 | s.append((N, -1)) 22 | for i in range(N-1, -1, -1): 23 | while s[-1][1] >= H[i]: 24 | s.pop() 25 | R[i] = s[-1][0] 26 | s.append((i, H[i])) 27 | 28 | # Encontramos el área más grande 29 | ans = max((R[i] - L[i] - 1) * H[i] for i in range(N)) 30 | print(ans) -------------------------------------------------------------------------------- /Solved problems/SPOJ/KOPC12A_BuildingConstruction.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search, ternary search 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | const int MAXN = 10000; 9 | ll H[MAXN], C[MAXN], n; 10 | 11 | ll cost(int h) { 12 | ll c = 0; 13 | rep(i,0,n) c += C[i] * abs(H[i] - h); 14 | return c; 15 | } 16 | 17 | int main() { 18 | ios::sync_with_stdio(false); 19 | cin.tie(0); 20 | int T; cin >> T; 21 | while (T--) { 22 | cin >> n; 23 | rep(i,0,n) cin >> H[i]; 24 | rep(i,0,n) cin >> C[i]; 25 | int max_h = *max_element(H, H+n); 26 | int l = 0, r = max_h; 27 | while (l < r) { 28 | int m = (l+r) >> 1; 29 | if (cost(m+1) >= cost(m)) r = m; 30 | else l = m+1; 31 | } 32 | cout << cost(l) << '\n'; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/PLD_Palindromes.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define FOR(i,a,b) for(int i=a;i<=b;++i) 8 | 9 | typedef unsigned long long int ull; 10 | #define MAXN 30000 11 | const ull a = 22695477; 12 | 13 | int n; 14 | int k; 15 | string s; 16 | 17 | ull ak; 18 | ull rhf[MAXN]; 19 | ull rhb[MAXN]; 20 | 21 | int main() { 22 | scanf("%d\n",&k); 23 | getline(cin, s); 24 | n = s.size(); 25 | 26 | // a raised to k power 27 | ak = 1; 28 | FOR (i, 1, k) ak *= a; 29 | 30 | // forward 31 | rhf[0] = 0; 32 | FOR (i, 0, k-1) 33 | rhf[0] = (rhf[0] * a + (s[i] - 'a')); 34 | for (int i = 1, lim = n - k; i <= lim; ++i) 35 | rhf[i] = rhf[i-1] * a + (s[i + k - 1] - 'a') - (s[i-1] - 'a') * ak; 36 | 37 | // backward 38 | rhb[n-1] = 0; 39 | for (int i = n-1; i >= n-k; --i) 40 | rhb[n-1] = (rhb[n-1] * a + (s[i] - 'a')); 41 | for (int i = n-2; i >= k-1; --i) 42 | rhb[i] = rhb[i+1] * a + (s[i - k + 1] - 'a') - (s[i + 1] - 'a') * ak; 43 | 44 | // count palindromes 45 | int count = 0; 46 | for (int i = 0, lim = n-k; i <= lim; ++i) 47 | if (rhf[i] == rhb[i+k-1]) count++; 48 | 49 | // answer 50 | printf("%d\n", count); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Solved problems/SPOJ/WEIRDFN_WeirdFunction.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | MOD = 1000000007 3 | def mul(a, b): 4 | return (a*b)%MOD 5 | def add(a, b): 6 | return (a+b)%MOD 7 | 8 | def main(): 9 | T = int(input()) 10 | for _ in range(T): 11 | a, b, c, n = map(int, input().split()) 12 | lower = [] 13 | upper = [] 14 | sum = 1 15 | heapq.heappush(lower, -1) 16 | for i in range(2, n+1): 17 | median = -lower[0] 18 | x = add(add(mul(a,median),mul(b,i)),c) 19 | sum += x 20 | if x > median: 21 | heapq.heappush(upper, x) 22 | if (len(upper) > len(lower)): 23 | heapq.heappush(lower, -heapq.heappop(upper)) 24 | else: 25 | heapq.heappush(lower, -x) 26 | if (len(lower) > len(upper) + 1): 27 | heapq.heappush(upper, -heapq.heappop(lower)) 28 | print(sum) 29 | 30 | main() -------------------------------------------------------------------------------- /Solved problems/SPOJ/gen.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from random import randint 3 | 4 | n_tests, N, R, K = map(int, sys.argv[1:]) 5 | print('5 2 3') 6 | print('2 4') 7 | print('1 2 1') 8 | print('1 1 1') 9 | print('1234 4 4') 10 | print('23 25 1000 67') 11 | print('20 4 26 222') 12 | print('18 28 1232 222') 13 | print('2 4 6 222') 14 | print('2 2 2 2') 15 | print('2 2 1') 16 | print('1 2') 17 | print('1') 18 | print('1') 19 | for _ in range(n_tests): 20 | print(N, R, K) 21 | print(*[randint(1,N) for _ in range(R)]) 22 | for r in range(R): 23 | print(*[randint(1,N-1) for _ in range(K)]) -------------------------------------------------------------------------------- /Solved problems/URI/AssigningTeams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i=a;i<=b;++i) 4 | 5 | int main() { 6 | int nums[4]; 7 | rep(i,0,3) scanf("%d", &nums[i]); 8 | sort(nums, nums+4); 9 | int diff = abs(nums[3]+nums[0] - nums[1]-nums[2]); 10 | printf("%d\n", diff); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Solved problems/URI/CheapTrips.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | int N; 8 | int D[10000], C[10000]; 9 | double memo[10001]; 10 | double discounts[6] = {1.0, 0.5, 0.25, 0.25, 0.25, 0.25}; 11 | double dp(int i) { 12 | double& ans = memo[i]; 13 | if (ans > -0.5) return ans; 14 | if (i == N) return ans = 0; 15 | ans = 1e9; 16 | int max_j = min(i + 6, N); 17 | int acc_time = 0; 18 | double acc_cost = 0; 19 | rep(j,i,max_j) { 20 | if (acc_time >= 120) break; 21 | acc_cost += discounts[j-i] * C[j]; 22 | ans = min(ans, acc_cost + dp(j+1)); 23 | acc_time += D[j]; 24 | } 25 | return ans; 26 | } 27 | int main() { 28 | ios::sync_with_stdio(false); 29 | cin.tie(0); cout.tie(0); 30 | cin >> N; 31 | rep(i,0,N) cin >> D[i] >> C[i]; 32 | rep(i,0,N+1) memo[i] = -1.0; 33 | cout << setprecision(2) << fixed; 34 | cout << dp(0) << '\n'; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/URI/CountingOnes.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, combinatorics, bitwise operations 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | ll count_ones(ll x) { 7 | if (x == 0) return 0; 8 | int len = sizeof(x) * 8 - __builtin_clzll(x); 9 | ll xx = x; 10 | ll count = 0; 11 | for (ll i=len-1, bit = 1LL << (len-1); i>=0; i--, bit>>=1) { 12 | if (x & bit) { 13 | if (i > 0) { 14 | count += i << (i-1); 15 | } 16 | xx -= bit; 17 | count += xx + 1; 18 | } 19 | } 20 | return count; 21 | } 22 | 23 | int main() { 24 | ll a, b; 25 | while (scanf("%lld %lld", &a, &b) == 2) { 26 | printf("%lld\n", count_ones(b) - count_ones(a-1)); 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/URI/FarmRobot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 4 | 5 | int main() { 6 | int n,c,s; 7 | scanf("%d%d%d", &n,&c,&s); 8 | vector counts(n, 0); 9 | counts[0] = 1; 10 | int i = 0, d; 11 | while(c--) { 12 | scanf("%d", &d); 13 | i = (i + d + n) % n; 14 | counts[i]++; 15 | } 16 | printf("%d\n", counts[s-1]); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Solved problems/URI/HotelRewards.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i=a;i<=b;++i) 5 | #define invrep(i,a,b) for(int i=a;i>=b;--i) 6 | int main() { 7 | int n, k; 8 | scanf("%d%d", &n, &k); 9 | int prices[n]; 10 | rep(i,0,n-1) scanf("%d", &prices[i]); 11 | int cost = 0; 12 | priority_queue pq; 13 | invrep(i,n-1,0) { 14 | pq.push(prices[i]); 15 | cost += prices[i]; 16 | if ((i+1) % (k+1) == 0) 17 | cost -= pq.top(), pq.pop(); 18 | } 19 | printf("%d\n", cost); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Solved problems/URI/JumpingFrog.cpp: -------------------------------------------------------------------------------- 1 | // tags: modular arithmetics, gcd, math, memoization 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | // ------------------------------- 6 | 7 | string input; 8 | int N; 9 | int memo[100000]; 10 | int valid(int g) { 11 | int& ans = memo[g]; 12 | if (ans != -1) return ans; 13 | int k = N / g; 14 | rep(i,0,g-1) { 15 | bool works = true; 16 | rep(x,1,k) { 17 | if (input[(i + x * g) % N] != 'R') { 18 | works = false; break; 19 | } 20 | } 21 | if (works) return ans = 1; 22 | } 23 | return ans = 0; 24 | } 25 | 26 | int main() { 27 | cin >> input; 28 | N = input.size(); 29 | memset(memo, -1, sizeof(int) * N); 30 | int count = 0; 31 | rep(k,1,N-1) { 32 | int g = __gcd(k,N); 33 | if (valid(g)) count++; 34 | } 35 | cout << count << '\n'; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Solved problems/URI/JustInTime.cpp: -------------------------------------------------------------------------------- 1 | // tags: primality check, sieve of eratosthenes 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | 6 | vector get_primes(int n) { 7 | vector is_prime(n, true); 8 | int limit = (int)floor(sqrt(n)); 9 | rep(i,2,limit) 10 | if (is_prime[i]) 11 | for (int j = i*i; j <= n; j += i) 12 | is_prime[j] = false; 13 | vector primes; 14 | rep(i,2,n) if (is_prime[i]) primes.push_back(i); 15 | return primes; 16 | } 17 | 18 | int main() { 19 | int n; scanf("%d", &n); 20 | vector primes = get_primes((int)floor(sqrt(n))); 21 | while (true) { 22 | bool is_prime = true; 23 | for (int p : primes) if (n % p == 0) {is_prime = false; break;} 24 | if (is_prime) break; 25 | n--; 26 | } 27 | printf("%d\n", n); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solved problems/UVA/10276_HanoiTowerTroublesAgain!.cpp: -------------------------------------------------------------------------------- 1 | // tags: backtracking, pruning, greedy 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | #define uset unordered_set 6 | // ------------------------------- 7 | 8 | stack stacks[50]; 9 | uset squares; 10 | int N; 11 | int xmax; 12 | 13 | void search(int i, int x) { 14 | bool worked = false; 15 | rep(j,0,i-1) { 16 | stack& s = stacks[j]; 17 | if (squares.count(s.top() + x)) { 18 | worked = true; 19 | s.push(x); 20 | xmax = max(xmax, x); 21 | search(i, x+1); 22 | s.pop(); 23 | } 24 | } 25 | if (i < N and !worked) { 26 | stack& s = stacks[i]; 27 | s.push(x); 28 | xmax = max(xmax, x); 29 | search(i+1, x+1); 30 | s.pop(); 31 | } 32 | } 33 | 34 | int main() { 35 | for (int x=1; x < INT_MAX/x; ++x) 36 | squares.insert(x*x); 37 | int T; cin >> T; 38 | while (T--) { 39 | cin >> N; 40 | xmax = 0; 41 | search(0,1); 42 | cout << xmax << '\n'; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Solved problems/UVA/11413_FillTheContainers.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <=b; ++i) 5 | // -------------------------------------- 6 | int cap[1000]; 7 | int N, M; 8 | 9 | bool possible(int maxcap) { 10 | int x = M-1; 11 | int r = maxcap; 12 | rep(i,0,N-1) { 13 | r -= cap[i]; 14 | if (r < 0) { 15 | r = maxcap - cap[i]; 16 | assert (r >= 0); 17 | if (--x < 0) return false; 18 | } 19 | } 20 | return true; 21 | } 22 | 23 | int main() { 24 | while (cin >> N >> M) { 25 | rep(i,0,N-1) cin >> cap[i]; 26 | int l = *max_element(cap, cap+N), r = accumulate(cap, cap+N, 0); 27 | while (l < r) { 28 | int m = (l+r) >> 1; 29 | if (possible(m)) r = m; 30 | else l = m+1; 31 | } 32 | cout << l << '\n'; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/UVA/12362_GardenFence/gen.py: -------------------------------------------------------------------------------- 1 | import random 2 | P = 3 3 | L = 3 4 | R = 4 5 | V = 10 6 | used = set() 7 | print(P, L) 8 | while len(used) < P + L: 9 | x = random.randint(-R, R) 10 | y = random.randint(-R, R) 11 | p = (x,y) 12 | if p in used: continue 13 | used.add(p) 14 | print(x, y, random.randint(1, V)) 15 | print(0, 0) -------------------------------------------------------------------------------- /Solved problems/UVA/331_MappingTheSwaps.cpp: -------------------------------------------------------------------------------- 1 | // tags: backtracking, implementation 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | // ------------------------------- 6 | 7 | int N; 8 | int vals[5]; 9 | 10 | int solve(int depth) { 11 | int count = 0; 12 | rep(i, 0, N-2) { 13 | if (vals[i] > vals[i+1]) { 14 | swap(vals[i], vals[i+1]); 15 | count += solve(depth+1); 16 | swap(vals[i], vals[i+1]); 17 | } 18 | } 19 | if (depth == 0) return count; 20 | else return max(count, 1); 21 | } 22 | 23 | int main() { 24 | int _case = 1; 25 | while (cin >> N and N) { 26 | rep(i,0,N-1) cin >> vals[i]; 27 | cout << "There are " << solve(0) << 28 | " swap maps for input data set " << _case << ".\n"; 29 | _case++; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solved problems/acm.timus.ru/1017_Staircases.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | typedef long long int ll; 6 | 7 | int N; 8 | // ways(n, h) = number of ways we can 9 | // build a starcaise with 'n' blocks 10 | // such that the first step uses 'h' vertical blocks 11 | ll memo[501][501]; 12 | ll ways(int n, int h) { 13 | if (n < h) return 0; // base case 1: impossible 14 | if (n == h) return 1; // base case 2: only 1 way 15 | ll& ans = memo[n][h]; 16 | if (ans != -1) return ans; // already solved 17 | // general case 18 | ll tmp = 0; 19 | rep(x,h+1,n-h+1) tmp += ways(n-h,x); 20 | return ans = tmp; 21 | } 22 | 23 | int main() { 24 | cin >> N; 25 | memset(memo, -1, sizeof memo); 26 | ll ans = 0; 27 | rep(h,1,N) ans += ways(N,h); 28 | cout << ans << '\n'; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solved problems/acm.timus.ru/1423_StringTale.cpp: -------------------------------------------------------------------------------- 1 | // tags: strings, KMP 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | void init_lps(string& pattern, int lps[]) { 8 | int n = pattern.size(); lps[0] = 0; 9 | rep(j,1,n) { 10 | int i = lps[j-1]; 11 | while (pattern[i] != pattern[j] and i > 0) i = lps[i-1]; 12 | lps[j] = pattern[i] == pattern[j] ? i+1 : 0; 13 | } 14 | } 15 | int find(string& pattern, string& target) { 16 | int n = pattern.size(), m = target.size(); 17 | int lps[n]; 18 | init_lps(pattern, lps); 19 | int i = 0; 20 | rep(j,0,m) { 21 | while (pattern[i] != target[j] and i > 0) i = lps[i-1]; 22 | if (pattern[i] == target[j]) if (++i == n) return (n - (j - n + 1)) % n; 23 | } 24 | return -1; 25 | } 26 | int main() { 27 | ios::sync_with_stdio(false); cin.tie(0); 28 | int n; cin >> n; string a, b; 29 | a.reserve(2*n); cin >> a >> b; a += a; 30 | cout << find(b, a) << '\n'; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solved problems/fackebook_hacker_cup/2021/Round1/A1.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | int T; cin >> T; 22 | rep(t,1,T+1) { 23 | int n; string s; cin >> n >> s; 24 | int op1 = 0, op2 = 0; 25 | for (char c : s) { 26 | if (op1 % 2 == 0 and c == 'O') op1++; 27 | if (op1 % 2 == 1 and c == 'X') op1++; 28 | if (op2 % 2 == 0 and c == 'X') op2++; 29 | if (op2 % 2 == 1 and c == 'O') op2++; 30 | } 31 | cout << "Case #" << t << ": " << min(op1, op2) << '\n'; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solved problems/hackerrank/TheCoinChangeProblem.cpp: -------------------------------------------------------------------------------- 1 | // tags: DP 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | typedef long long int ll; 6 | int n, m; 7 | int coins[50]; 8 | ll memo[251][50]; 9 | // ways(k, i) = number of ways we can 10 | // generate the amount of money 'k' 11 | // using coin types i, i+1, ..., m-1 12 | ll ways(int k, int i) { 13 | if (k < 0) return 0; // base case: impossible 14 | if (k == 0) return 1; // base case: no money -> 1 way (use 0 coins) 15 | if (i == m) return 0; // base case: invalid coin type -> impossible 16 | ll& ans = memo[k][i]; 17 | if (ans != -1) return ans; // base case: already solved 18 | // general case 19 | // option 1: don't use i-th coin type 20 | // option 2: use i-th coin type 21 | return ans = ways(k, i+1) + ways(k - coins[i], i); 22 | // ** se puede demostrar correctitud por inducción 23 | } 24 | int main() { 25 | cin >> n >> m; 26 | rep(i,0,m) cin >> coins[i]; 27 | memset(memo, -1, sizeof memo); 28 | cout << ways(n,0) << '\n'; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solved problems/kattis/BobbysBet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | 6 | typedef long long ll; 7 | ll fact[11]; 8 | 9 | double comb(int n, int k) { 10 | return fact[n] / (double)(fact[k] * fact[n-k]); 11 | } 12 | 13 | int main() { 14 | int N, R, S, X, Y, W; 15 | 16 | fact[0] = 1; 17 | rep(i, 1, 10) 18 | fact[i] = i * fact[i-1]; 19 | 20 | scanf("%d", &N); 21 | 22 | while(N--) { 23 | scanf("%d%d%d%d%d", &R, &S, &X, &Y, &W); 24 | double prob = 0; 25 | double fav = (S-R+1) / (double) S; 26 | rep(k, X, Y) { 27 | prob += comb(Y, k) * pow(fav, k) * pow(1.0 - fav, Y - k); 28 | } 29 | if (prob * W > 1.0) 30 | puts("yes"); 31 | else 32 | puts("no"); 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/kattis/Cetvrta.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | 6 | int main() { 7 | int minx = 1000, miny = 1000, maxx = 1, maxy = 1; 8 | set> coords; 9 | rep(i,1,3) { 10 | int x,y; scanf("%d%d", &x, &y); 11 | minx = min(minx, x); 12 | miny = min(miny, y); 13 | maxx = max(maxx, x); 14 | maxy = max(maxy, y); 15 | coords.emplace(x,y); 16 | } 17 | if (!coords.count({minx, miny})) { 18 | printf("%d %d", minx, miny); 19 | return 0; 20 | } 21 | if (!coords.count({minx, maxy})) { 22 | printf("%d %d", minx, maxy); 23 | return 0; 24 | } 25 | if (!coords.count({maxx, miny})) { 26 | printf("%d %d", maxx, miny); 27 | return 0; 28 | } 29 | printf("%d %d", maxx, maxy); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Solved problems/kattis/Collapse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i =a; i<=b; ++i) 5 | #define MAXN 100000 6 | 7 | typedef pair pii; 8 | typedef vector vii; 9 | 10 | int N; 11 | vector g; 12 | int thresh[MAXN]; 13 | int input[MAXN]; 14 | bool dead[MAXN]; 15 | 16 | int survivors() { 17 | queue q; 18 | q.push(0); 19 | dead[0] = true; 20 | int dcount = 0; 21 | while (!q.empty()) { 22 | int u = q.front(); q.pop(); 23 | dcount++; 24 | for (pii& p : g[u]) { 25 | int v = p.first; 26 | int c = p.second; 27 | input[v] -= c; 28 | if (!dead[v] && input[v] < thresh[v]) { 29 | dead[v] = true; 30 | q.push(v); 31 | } 32 | } 33 | } 34 | return N - dcount; 35 | } 36 | 37 | int main() { 38 | scanf("%d", &N); 39 | 40 | g.assign(N,vii()); 41 | memset(dead, 0, sizeof(dead[0]) * N); 42 | memset(input, 0, sizeof(input[0]) * N); 43 | 44 | rep(i,0,N-1) { 45 | int k; 46 | scanf("%d%d",&thresh[i],&k); 47 | rep(j,1,k) { 48 | int s,v; 49 | scanf("%d%d",&s,&v); --s; 50 | g[s].push_back({i, v}); 51 | input[i] += v; 52 | } 53 | } 54 | 55 | printf("%d", survivors()); 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Solved problems/kattis/CompoundWords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | 6 | void split(string& s, vector& tokens) { 7 | stringstream ss(s); 8 | string tok; 9 | while(getline(ss, tok, ' ')) 10 | tokens.push_back(tok); 11 | } 12 | 13 | int main() { 14 | string line; 15 | vector tokens; 16 | set uniques; 17 | 18 | while (getline(cin, line)) 19 | split(line, tokens); 20 | 21 | int n = tokens.size(); 22 | rep(i,0,n-2) { 23 | rep(j,i+1,n-1) { 24 | 25 | uniques.insert(tokens[i] + tokens[j]); 26 | uniques.insert(tokens[j] + tokens[i]); 27 | } 28 | } 29 | 30 | for (string str : uniques) { 31 | puts(str.c_str()); 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/kattis/CountingStars.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #include 3 | using namespace std; 4 | 5 | typedef pair pii; 6 | 7 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 8 | 9 | char board[100][101]; 10 | int m,n; 11 | 12 | int dirs[4][2] = { 13 | {0, 1}, 14 | {0, -1}, 15 | {1, 0}, 16 | {-1, 0} 17 | }; 18 | 19 | void bfs(int r, int c) { 20 | board[r][c] = '#'; 21 | queue q; 22 | q.push(pii(r,c)); 23 | while (!q.empty()) { 24 | pii u = q.front(); q.pop(); 25 | rep(i,0,3) { 26 | int rr = u.first + dirs[i][0]; 27 | int cc = u.second + dirs[i][1]; 28 | if (0 <= rr and rr < m && 0 <= cc and cc < n and board[rr][cc] == '-') { 29 | board[rr][cc] = '#'; 30 | q.push(pii(rr,cc)); 31 | } 32 | } 33 | } 34 | } 35 | 36 | int main() { 37 | 38 | int _case = 1; 39 | while(scanf("%d%d", &m, &n) == 2) { 40 | 41 | rep(r,0,m-1) 42 | scanf("%s\n", board[r]); 43 | 44 | int count = 0; 45 | 46 | rep(r,0,m-1) { 47 | rep(c,0,n-1) { 48 | if (board[r][c] == '-') { 49 | bfs(r,c); 50 | count++; 51 | } 52 | } 53 | } 54 | 55 | printf("Case %d: %d\n", _case, count); 56 | _case++; 57 | 58 | } 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Solved problems/kattis/CrazyDriver.cpp: -------------------------------------------------------------------------------- 1 | // tag: greedy 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | typedef vector vi; 6 | typedef long long int ll; 7 | int n; 8 | vi costs; 9 | vi times; 10 | int main() { 11 | scanf("%d",&n); 12 | costs.resize(n); 13 | times.resize(n); 14 | rep(i,0,n-2) scanf("%d", &costs[i]); 15 | rep(i,0,n-1) scanf("%d", ×[i]); 16 | 17 | int curr_t = 0; 18 | int curr_i = 0; 19 | int reach_i = 1; 20 | ll tot = 0; 21 | 22 | while(true) { 23 | int next_t = curr_t + (reach_i - curr_i); 24 | if (times[reach_i] <= next_t) { 25 | if (reach_i == n-1) { 26 | rep(i, curr_i, reach_i-1) { 27 | tot += costs[i]; 28 | } 29 | break; 30 | } else { 31 | reach_i++; 32 | if (costs[reach_i-1] < costs[curr_i]) { 33 | rep(i, curr_i, reach_i-2) 34 | tot += costs[i]; 35 | curr_t += reach_i-1 - curr_i; 36 | curr_i = reach_i-1; 37 | } 38 | } 39 | } else { 40 | int steps = ((times[reach_i] - next_t + 1) / 2) * 2; 41 | curr_t += steps; 42 | tot += (ll)costs[curr_i] * (ll)steps; 43 | } 44 | } 45 | printf("%lld", tot); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Solved problems/kattis/FairWarning.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PabloMessina/Competitive-Programming-Material/28fd0457f5197b54a9805bdc66ff3ce822eed595/Solved problems/kattis/FairWarning.class -------------------------------------------------------------------------------- /Solved problems/kattis/FreightTrain.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i=a; i r) { 18 | l = pos[i]; 19 | } else { 20 | while(++i < W && pos[i] <= r) {} 21 | l = r+1; 22 | } 23 | r = min(l + K-1, N-1); 24 | } 25 | return count <= L; 26 | } 27 | 28 | int main() { 29 | int T; cin >> T; 30 | while (T--) { 31 | cin >> N >> W >> L; 32 | rep(i, 0, W) { cin >> pos[i]; pos[i]--; } 33 | int l = 1, r = N; 34 | while (l < r) { 35 | int m = (l + r) >> 1; 36 | if (possible(m)) { 37 | r = m; 38 | } else { 39 | l = m + 1; 40 | } 41 | } 42 | cout << l << '\n'; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Solved problems/kattis/H-Index.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, sorting, greedy 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | // ------------------------------- 6 | int main() { 7 | // ios::sync_with_stdio(false); cin.tie(0); 8 | int n; cin >> n; 9 | vector h(n); 10 | rep(i,0,n) cin >> h[i]; 11 | sort(h.begin(), h.end(), greater()); 12 | int ans = 0; 13 | rep(i,0,n) { 14 | if (i+1 > h[i]) break; 15 | ans = i+1; 16 | } 17 | cout << ans << '\n'; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Solved problems/kattis/H-Index.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | h = [int(input()) for _ in range(n)] 4 | h.sort(reverse=True) 5 | ans = 0 6 | for i in range(n): 7 | if i+1 > h[i]: 8 | break 9 | ans = i+1 10 | print(ans) 11 | main() -------------------------------------------------------------------------------- /Solved problems/kattis/Heliocentric.cpp: -------------------------------------------------------------------------------- 1 | // tags: math, implementation 2 | #include 3 | using namespace std; 4 | 5 | typedef pair pii; 6 | 7 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 8 | 9 | 10 | int main() { 11 | int e, m, x; 12 | int _case = 1; 13 | while(scanf("%d%d",&e,&m) == 2) { 14 | e = (365 - e) % 365; 15 | m = (687 - m) % 687; 16 | for (x = 0; x < 365*687; ++x) { 17 | int re = x % 365; 18 | int rm = x % 687; 19 | if (re == e && rm == m) break; 20 | } 21 | printf("Case %d: %d\n", _case, x); 22 | _case++; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Solved problems/kattis/HiddenPassword.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for (int i=a; i<=b; ++i) 5 | #define invrep(i,a,b) for (int i=a; i>=b; --i) 6 | 7 | typedef long long int ll; 8 | 9 | char buff[50]; 10 | int counts[26]; 11 | int main() { 12 | 13 | memset(counts, 0, sizeof counts); 14 | 15 | scanf("%s", buff); 16 | string password(buff); 17 | scanf("%s", buff); 18 | string message(buff); 19 | 20 | for (char c : password) 21 | counts[c-'A']++; 22 | 23 | int index = 0; 24 | bool success = true; 25 | for(char c : message) { 26 | if (counts[c-'A']) { 27 | if (password[index] != c) { 28 | success = false; 29 | break; 30 | } 31 | index++; 32 | counts[c-'A']--; 33 | } 34 | } 35 | success &= (index == password.size()); 36 | 37 | if (success) 38 | puts("PASS"); 39 | else 40 | puts("FAIL"); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solved problems/kattis/InOrOut.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair pii; 5 | 6 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 7 | 8 | int main() { 9 | double x, y; int r; 10 | int _case = 1; 11 | 12 | while (scanf("%lf%lf%d",&x,&y,&r) == 3) { 13 | 14 | bool in = true; 15 | double cx = 0, cy = 0; 16 | double xx, yy; 17 | 18 | rep(i, 0, r) { 19 | 20 | if (cx*cx + cy*cy > 4.0) { 21 | in = false; 22 | break; 23 | } 24 | 25 | xx = cx; yy = cy; 26 | cx = xx*xx - yy*yy + x; 27 | cy = 2*xx*yy + y; 28 | } 29 | 30 | if (in) printf("Case %d: IN\n", _case); 31 | else printf("Case %d: OUT\n", _case); 32 | _case++; 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solved problems/kattis/MarblesOnATree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 5 | #define MAXN 10000 6 | 7 | typedef vector vi; 8 | 9 | int N; 10 | vector g; 11 | int marb[MAXN]; 12 | 13 | int tot; 14 | int dfs(int u, int p) { 15 | int c = marb[u]; 16 | for (int v : g[u]) 17 | if (v != p) 18 | c += dfs(v, u); 19 | tot += abs(c - 1); 20 | return c - 1; 21 | } 22 | 23 | int main() { 24 | while (true) { 25 | scanf("%d", &N); 26 | if (N == 0) 27 | break; 28 | 29 | g.assign(N, vi()); 30 | 31 | rep(i,1,N) { 32 | int u, m, d; 33 | scanf("%d%d%d", &u, &m, &d); --u; 34 | marb[u] = m; 35 | while(d--) { 36 | int v; scanf("%d", &v); --v; 37 | g[u].push_back(v); 38 | g[v].push_back(u); 39 | } 40 | } 41 | 42 | tot = 0; 43 | dfs(0, -1); 44 | printf("%d\n", tot); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Solved problems/kattis/MatrixInverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair pii; 5 | 6 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 7 | 8 | int main() { 9 | int a,b,c,d; 10 | int aa,bb,cc,dd, den; 11 | int _case = 1; 12 | while (scanf("%d%d%d%d",&a,&b,&c,&d) == 4) { 13 | den = a*d - c*b; 14 | aa = d / den; 15 | bb = -b / den; 16 | cc = -c / den; 17 | dd = a / den; 18 | printf("Case %d:\n%d %d\n%d %d\n",_case,aa,bb,cc,dd); 19 | _case++; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Solved problems/kattis/MoneyForNothing/MoneyForNothing__slow.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | 20 | vector> A, B; 21 | 22 | int main() { 23 | ios::sync_with_stdio(false); cin.tie(0); 24 | int m, n; cin >> m >> n; 25 | 26 | A.resize(m); B.resize(n); 27 | for (auto& p : A) cin >> p.ff >> p.ss; 28 | for (auto& p : B) cin >> p.ff >> p.ss; 29 | 30 | ll ans = 0; 31 | for (auto& x : A) for (auto& y : B) ans = max(ans, max(y.ff - x.ff, 0ll) * max(y.ss - x.ss, 0ll)); 32 | cout << ans << '\n'; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/kattis/MoneyForNothing/generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | m = 20 3 | n = 20 4 | minx = 1 5 | maxx = 10 6 | miny = 1 7 | maxy = 10 8 | print(m, n) 9 | for _ in range(m+n): 10 | x = random.randint(minx, maxx) 11 | y = random.randint(miny, maxy) 12 | print(x, y) 13 | 14 | 15 | -------------------------------------------------------------------------------- /Solved problems/kattis/NeedForSpeed.cpp: -------------------------------------------------------------------------------- 1 | // tags: binary search 2 | #include // import everything in one shot 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 5 | int N; double T; 6 | double dist[1000], speed[1000]; 7 | double total_time(double c) { 8 | double t = 0; 9 | rep(i,0,N-1) t += dist[i] / (c + speed[i]); 10 | return t; 11 | } 12 | int main() { 13 | // read input 14 | cin >> N >> T; 15 | rep(i,0,N-1) cin >> dist[i] >> speed[i]; 16 | // find best answer in range [l,r] using binary search 17 | double l = -*min_element(speed, speed+N); 18 | double r = 1001000.0; 19 | rep(_,1,100) { 20 | double m = (l+r)*0.5; 21 | if (total_time(m) > T) l = m; 22 | else r = m; 23 | } 24 | cout << setprecision(20) << fixed << (l+r)*0.5 << '\n'; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Solved problems/kattis/NeighborhoodWatch.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, two pointers 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | typedef long long int ll; 7 | // ------------------------------- 8 | int N, K; 9 | int main() { 10 | ios::sync_with_stdio(false); 11 | cin.tie(0); cout.tie(0); 12 | cin >> N >> K; 13 | vector watch(N, false); 14 | rep(i,0,K) { 15 | int j; cin >> j; --j; 16 | watch[j] = true; 17 | } 18 | ll count = 0; 19 | int j = 0; 20 | rep(i,0,N) { 21 | while (j < i or (j < N and not watch[j])) ++j; 22 | count += N - j; 23 | } 24 | cout << count << '\n'; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Solved problems/kattis/Pivot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define rep(i,a,b) for(int i=a; i<=b; ++i) 6 | #define invrep(i,a,b) for(int i=a; i>=b; --i) 7 | #define MAXN 100000 8 | 9 | int nums[MAXN]; 10 | bool candidate[MAXN]; 11 | 12 | int main() { 13 | int n; scanf("%d", &n); 14 | int minn = INT_MAX; 15 | int maxn = INT_MIN; 16 | int count = 0; 17 | 18 | rep(i,0,n-1) { 19 | scanf("%d", &nums[i]); 20 | if (nums[i] >= maxn) { 21 | candidate[i] = true; 22 | } 23 | if (nums[i] > maxn) 24 | maxn = nums[i]; 25 | } 26 | invrep(i, n-1, 0) { 27 | if ((i == n-1 || nums[i] < minn)) { 28 | minn = nums[i]; 29 | if (candidate[i]) count++; 30 | } 31 | } 32 | printf("%d", count); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Solved problems/kattis/PizzaHawaii.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation, bitmask, bitwise, std::map 2 | #include 3 | using namespace std; 4 | #define rep(i,a,b) for(int i = a; i < b; ++i) 5 | typedef long long ll; 6 | // ------------------------------- 7 | int main() { 8 | ios::sync_with_stdio(false); cin.tie(0); 9 | int t; cin >> t; 10 | while (t--) { 11 | int n; cin >> n; 12 | map A, B; 13 | string name; 14 | rep(i,0,n) { 15 | cin >> name; 16 | int k; 17 | // idioma A 18 | cin >> k; 19 | rep(j,0,k) { 20 | cin >> name; 21 | A[name] |= 1LL << i; 22 | } 23 | // idioma B 24 | cin >> k; 25 | rep(j,0,k) { 26 | cin >> name; 27 | B[name] |= 1LL << i; 28 | } 29 | } 30 | for (auto& a : A) { // C++11 31 | for (auto& b : B){ 32 | if (a.second == b.second) { 33 | cout << "(" << a.first << ", " << b.first << ")\n"; 34 | } 35 | } 36 | } 37 | if (t) cout << '\n'; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Solved problems/kattis/PizzaHawaii.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | t = int(input()) 3 | for _ in range(t): 4 | if _: print() 5 | n = int(input()) 6 | A = dict() 7 | B = dict() 8 | for i in range(n): 9 | name = input() # nombre de la pizza 10 | tokens = input().strip().split() # ingredientes restaurant 1 11 | for token in tokens[1:]: 12 | A[token] = A.get(token, 0) | (1 << i) 13 | tokens = input().strip().split() # ingredientes restaurant 2 14 | for token in tokens[1:]: 15 | B[token] = B.get(token, 0) | (1 << i) 16 | A = list(A.items()) 17 | A.sort() 18 | B = list(B.items()) 19 | B.sort() 20 | for a in A: 21 | for b in B: 22 | if a[1] == b[1]: 23 | print('(%s, %s)' % (a[0], b[0])) 24 | 25 | main() -------------------------------------------------------------------------------- /Solved problems/kattis/PlantingTrees.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy 2 | #include // import everything in one shot 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | 8 | int main() { 9 | int n; cin >> n; 10 | vector ts(n); 11 | rep(i,0,n-1) cin >> ts[i]; 12 | sort(ts.begin(), ts.end()); 13 | int ans = 0; 14 | rep(i,0,n-1) ans = max(ans, ts[n-i-1] + i + 2); 15 | cout << ans << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Solved problems/kattis/PolynomialMultiplication1.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #include // import everything in one shot 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i <= b; ++i) 6 | // ------------------------------- 7 | int main() { 8 | int T; cin >> T; 9 | while (T--) { 10 | // read first polynomial 11 | int dg1; cin >> dg1; 12 | vector poly1(dg1 + 1); 13 | rep(i,0,dg1) cin >> poly1[i]; 14 | // read second polynomial 15 | int dg2; cin >> dg2; 16 | vector poly2(dg2 + 1); 17 | rep(i,0,dg2) cin >> poly2[i]; 18 | // compute polynomial multiplication 19 | int dg3 = dg1 + dg2; 20 | vector poly3(dg3 + 1, 0); 21 | rep(i,0,dg1) rep(j,0,dg2) { 22 | poly3[i + j] += poly1[i] * poly2[j]; 23 | } 24 | // print output 25 | cout << dg3 << '\n'; 26 | rep(i,0,dg3) { 27 | if (i) cout << " "; 28 | cout << poly3[i]; 29 | } 30 | cout << '\n'; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solved problems/kattis/Preludes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | map dict = { 5 | {"A","A"}, 6 | {"A#","Bb"}, {"Bb","A#"}, 7 | {"B","B"}, 8 | {"C","C"}, 9 | {"C#","Db"}, {"Db","C#"}, 10 | {"D","D"}, 11 | {"D#","Eb"}, {"Eb","D#"}, 12 | {"E","E"}, 13 | {"F","F"}, 14 | {"F#","Gb"}, {"Gb","F#"}, 15 | {"G","G"}, 16 | {"G#","Ab"}, {"Ab","G#"} 17 | }; 18 | 19 | int main() { 20 | string line, note, ton, eq; 21 | int c = 1; 22 | while (getline(cin, line)) { 23 | stringstream ss(line); 24 | getline(ss, note, ' '); 25 | getline(ss, ton, ' '); 26 | eq = dict[note]; 27 | printf("Case %d: ", c++); 28 | if (eq == note) { 29 | puts("UNIQUE"); 30 | } else { 31 | printf("%s %s\n", eq.c_str(), ton.c_str()); 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solved problems/kattis/QuickBrownFox.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a;i<=b;++i) 5 | 6 | bool found[26]; 7 | int main() { 8 | int n; 9 | scanf("%d\n", &n); 10 | string line; 11 | while(n--) { 12 | getline(cin, line); 13 | memset(found, 0, sizeof found); 14 | for (char c : line) { 15 | if ('a' <= c && c <= 'z') 16 | found[c-'a'] = true; 17 | else if ('A' <= c && c <= 'Z') 18 | found[c-'A'] = true; 19 | } 20 | vector missing; 21 | rep(i,0,25) { 22 | if (!found[i]) missing.push_back((char)('a' + i)); 23 | } 24 | if (missing.size() == 0) { 25 | puts("pangram"); 26 | } else { 27 | printf("missing "); 28 | for (char c : missing) 29 | printf("%c", c); 30 | puts(""); 31 | } 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solved problems/kattis/ReversedBinaryNumbers.cpp: -------------------------------------------------------------------------------- 1 | // tags: bitwise operators, bit manipulation, implementation 2 | #include 3 | using namespace std; 4 | int reverse_bits(int x) { 5 | int v = 0; 6 | while (x) v <<= 1, v |= x&1, x >>= 1; 7 | return v; 8 | } 9 | int main() { 10 | int n; cin >> n; cout << reverse_bits(n); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Solved problems/kattis/SecretMessage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i=a; i <= b; ++i) 5 | #define invrep(i,a,b) for(int i=a; i>= b; --i) 6 | 7 | char matrix[101][101]; 8 | int min_side(int n) { 9 | int i = 1, j = n; 10 | while (i < j) { 11 | int m = (i + j) / 2; 12 | if (m*m >= n) j = m; 13 | else i = m+1; 14 | } 15 | return i; 16 | } 17 | 18 | int main() { 19 | int n; 20 | scanf("%d\n", &n); 21 | string line; 22 | while(n--) { 23 | getline(cin, line); 24 | int k = min_side(line.size()); 25 | int i = 0; 26 | invrep (c, k-1, 0) { 27 | rep(r, 0, k-1) { 28 | if (i < line.size()) 29 | matrix[r][c] = line[i++]; 30 | else 31 | matrix[r][c] = '*'; 32 | } 33 | } 34 | rep(r, 0, k-1) { 35 | rep(c, 0, k-1) { 36 | if (matrix[r][c] != '*') 37 | printf("%c", matrix[r][c]); 38 | } 39 | } 40 | puts(""); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solved problems/kattis/SierpinskiCircumference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double a = log(3.0)/log(10.0); 5 | double b = log(1.5)/log(10.0); 6 | int main() { 7 | int n; 8 | int c = 1; 9 | while (scanf("%d", &n) == 1) { 10 | int x = (int)ceil(a + n * b); 11 | printf("Case %d: %d\n",c++,x); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Solved problems/kattis/SimonSays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | scanf("%d\n", &n); 7 | string line; 8 | string prefix = "Simon says"; 9 | while(n--) { 10 | getline(cin, line); 11 | if (line.compare(0, prefix.size(), prefix) == 0) { 12 | printf("%s\n", line.c_str() + prefix.size() + 1); 13 | } 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Solved problems/kattis/Statistics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string line, tok; 6 | int _case = 1; 7 | int n; 8 | while (scanf("%d", &n) == 1) { 9 | int _min = -1, _max = -1; 10 | while (n--) { 11 | int x; scanf("%d", &x); 12 | _min = _min == -1 ? x : min(_min, x); 13 | _max = _max == -1 ? x : max(_max, x); 14 | } 15 | printf("Case %d: %d %d %d\n",_case++,_min,_max,_max-_min); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Solved problems/kattis/SumOfTheOthers.cpp: -------------------------------------------------------------------------------- 1 | // tags: implementation 2 | #include // import everything in one shot 3 | using namespace std; 4 | 5 | int main() { 6 | string line; 7 | while (getline(cin, line)) { // for each line (until EOF) 8 | // http://www.cplusplus.com/reference/string/string/getline/ 9 | int tmp; 10 | int sum = 0; 11 | vector nums; 12 | // we use a stringstream to read and collect intergers from the current line 13 | // http://www.cplusplus.com/reference/sstream/stringstream/ 14 | stringstream ss(line); 15 | while (ss >> tmp) { 16 | nums.push_back(tmp); 17 | sum += tmp; // we also compute the total sum along the way 18 | } 19 | // finally, we find the first number which is equal to the sum of the other numbers 20 | for (int num : nums) { 21 | if (num == sum - num) { // found -> print number and break loop 22 | cout << num << endl; 23 | break; 24 | } 25 | } 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solved problems/kattis/TakeTwoStones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | if (n % 2 == 1) 7 | puts("Alice"); 8 | else 9 | puts("Bob"); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Solved problems/kattis/Zagrade.py: -------------------------------------------------------------------------------- 1 | # parte 1: acumulamos pares de paréntesis 2 | line = input().strip() 3 | stack = [] 4 | brackets = [] 5 | for i in range(len(line)): 6 | if line[i] == '(': 7 | stack.append(i) 8 | elif line[i] == ')': 9 | brackets.append((stack.pop(), i)) 10 | n = len(brackets) 11 | # parte 2: generamos todas las expresiones e imprimimos 12 | expressions = set() 13 | for mask in range(1, 1 << n): # x << n == x * (2 ^ n) 14 | skip = [False] * len(line) 15 | for j in range(n): 16 | if (1 << j) & mask > 0: 17 | skip[brackets[j][0]] = True 18 | skip[brackets[j][1]] = True 19 | exp = '' 20 | for j, c in enumerate(line): 21 | if skip[j]: continue 22 | exp += c 23 | expressions.add(exp) 24 | expressions = sorted(list(expressions)) 25 | for exp in expressions: print(exp) 26 | 27 | -------------------------------------------------------------------------------- /Solved problems/kattis/crisscrosscables.cpp: -------------------------------------------------------------------------------- 1 | // tags: greedy, std::priority_queue, implementation 2 | #pragma GCC optimize("Ofast") 3 | #include 4 | using namespace std; 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | // ------------------------------- 7 | const int MAXN = 500000; 8 | int N,M; 9 | int X[MAXN], L[MAXN]; 10 | struct Interval { 11 | int i, j, len; 12 | Interval(int i, int j) : i(i), j(j) { 13 | len = X[j] - X[i]; 14 | } 15 | bool operator<(const Interval& rhs) const { 16 | return len > rhs.len; 17 | } 18 | }; 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | cin >> N >> M; 22 | rep(i,0,N) cin >> X[i]; 23 | rep(i,0,M) cin >> L[i]; 24 | sort(L, L+M); 25 | priority_queue q; 26 | int i = 0; 27 | rep(j,0,N-1) q.emplace(j,j+1); 28 | while (!q.empty() and i < M) { 29 | auto& x = q.top(); 30 | if (x.len > L[i]) break; 31 | if (x.j + 1 < N) q.emplace(x.i, x.j+1); 32 | q.pop(); 33 | ++i; 34 | } 35 | if (i == M) cout << "yes\n"; 36 | else cout << "no\n"; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Solved problems/kattis/rsamistake.py: -------------------------------------------------------------------------------- 1 | # TODO: finish this 2 | import math 3 | 4 | def sieve(n): 5 | isprime = [True] * n 6 | limit = math.floor(math.sqrt(n)) 7 | for i in range(2, limit+1): 8 | if isprime[i]: 9 | for j in range(i*i, n+1, i): 10 | isprime[j] = False 11 | primes = [] 12 | for i in range(2, n): 13 | if isprime[i]: 14 | primes.append(i) 15 | return primes 16 | 17 | def get_prime_factorization(x, primes): 18 | fact = dict() 19 | for p in primes: 20 | if p * p > x: break 21 | while (x % p == 0): 22 | fact[p] = fact.get(p, 0) + 1 23 | x /= p 24 | if x > 1: 25 | fact[x] = fact.get(x, 0) + 1 26 | 27 | def main(): 28 | a, b = map(int, input().split()) 29 | primes = sieve() 30 | fact_a = get_prime_factorization(a) 31 | fact_b = get_prime_factorization(b) 32 | 33 | pass 34 | 35 | main() -------------------------------------------------------------------------------- /Solved problems/others/BigNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define MAX_SIZE 10000000 5 | 6 | double accum_logs[MAX_SIZE + 1]; 7 | 8 | int main() { 9 | 10 | accum_logs[0] = 0; 11 | for(int i = 1; i <= MAX_SIZE; ++i) 12 | accum_logs[i] = accum_logs[i-1] + log10(i); 13 | 14 | int n; 15 | scanf("%d",&n); 16 | while(n--) { 17 | int x; 18 | scanf("%d",&x); 19 | int n_digits = (int)floor(accum_logs[x])+1; 20 | printf("%d\n",n_digits); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Solved problems/others/BinPacking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int lengths[100000]; 8 | 9 | int cases; 10 | scanf("%d", &cases); 11 | 12 | bool first_time = true; 13 | while (cases--) { 14 | int n, l; 15 | scanf("%d %d", &n, &l); 16 | for (int i = 0; i < n; ++i) 17 | scanf("%d", &lengths[i]); 18 | sort(lengths, lengths + n); 19 | int count = 0; 20 | int first = 0, last = n - 1; 21 | 22 | while (true) { 23 | if (first == last) { 24 | count++; 25 | break; 26 | } else if (first > last) { 27 | break; 28 | } 29 | if (lengths[last] + lengths[first] <= l) { 30 | last--; 31 | first++; 32 | } else 33 | last--; 34 | count++; 35 | } 36 | if (first_time) 37 | first_time = false; 38 | else 39 | printf("\n"); 40 | printf("%d\n", count); 41 | } 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Solved problems/others/Bingo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool canGenerate[91]; 9 | 10 | int main() { 11 | 12 | setvbuf(stdout, NULL, _IONBF, 0); 13 | 14 | loop: while (true) { 15 | 16 | int n, b; 17 | scanf("%d %d", &n, &b); 18 | if (n == 0 && b == 0) 19 | break; 20 | 21 | vector balls; 22 | for (int i = 0; i < b; ++i) { 23 | int ball; 24 | scanf("%d", &ball); 25 | balls.push_back(ball); 26 | } 27 | 28 | memset(canGenerate, 0, sizeof canGenerate); 29 | 30 | for (int i = 0; i < b - 1; ++i) 31 | for (int j = i + 1; j < b; ++j) 32 | canGenerate[abs(balls[i] - balls[j])] = true; 33 | 34 | for (int i = 1; i <= n; ++i) 35 | if (!canGenerate[i]) { 36 | printf("N\n"); 37 | goto loop; 38 | } 39 | 40 | printf("Y\n"); 41 | 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Solved problems/others/Candy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int m,n; 7 | int row[100000+5]; 8 | int col[100000+5]; 9 | int dp[100000+5]; 10 | 11 | int main() { 12 | while(true) { 13 | scanf("%d %d",&m,&n); 14 | if(m == 0 && n == 0) 15 | break; 16 | 17 | for(int i = 0; i < m; ++i) { 18 | for(int j = 0; j < n; ++j) { 19 | scanf("%d",&row[j]); 20 | } 21 | dp[n] = 0; 22 | dp[n-1] = row[n-1]; 23 | for(int j = n-2; j>= 0; --j) { 24 | dp[j] = std::max(dp[j+1],row[j]+dp[j+2]); 25 | } 26 | col[i] = dp[0]; 27 | } 28 | dp[m] = 0; 29 | dp[m-1] = col[m-1]; 30 | for(int i = m-2; i >= 0; --i) { 31 | dp[i] = std::max(dp[i+1],col[i]+dp[i+2]); 32 | } 33 | printf("%d\n",dp[0]); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Solved problems/others/CardTrick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define LSOne(S) (S & (-S)) 7 | 8 | int ft[20000+5]; 9 | int ans[20000+5]; 10 | 11 | int rsq(int b) { 12 | int sum = 0; for (; b; b -= LSOne(b)) sum += ft[b]; 13 | return sum; 14 | } 15 | 16 | void adjust(int k, int v, int size) { 17 | for (; k < size; k += LSOne(k)) ft[k] += v; 18 | } 19 | 20 | int binSearch(int l, int r, int k) { 21 | while(l>1; 23 | if(rsq(imid) < k) { 24 | l = imid + 1; 25 | } else { 26 | r = imid; 27 | } 28 | } 29 | return l; 30 | } 31 | 32 | int main() { 33 | int t,n,size; 34 | scanf("%d",&t); 35 | while(t-->0) { 36 | scanf("%d",&n); 37 | size = n+1; 38 | memset(ft,0,sizeof(int)*size); 39 | for(int k = 1; k <= n; ++k) { 40 | adjust(k,1,size); 41 | } 42 | int freeSpaces = n; 43 | int index = 0; 44 | for(int i = 1; i<= n; ++i) { 45 | int k = (rsq(index)+i)%freeSpaces + 1; 46 | index = binSearch(k,n,k); 47 | adjust(index,-1,size); 48 | ans[index]=i; 49 | freeSpaces--; 50 | } 51 | for(int i=1;i<=n; ++i) { 52 | if(i==1) 53 | printf("%d",ans[i]); 54 | else 55 | printf(" %d",ans[i]); 56 | } 57 | printf("\n"); 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Solved problems/others/Composition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef unsigned long long int ull; 5 | bool invalid[31]; 6 | ull counts[31]; 7 | int main() { 8 | int P; 9 | scanf("%d", &P); 10 | while (P-- > 0) { 11 | int d, n, m, k; 12 | scanf("%d %d %d %d", &d, &n, &m, &k); 13 | memset(invalid, 0, sizeof invalid); 14 | for (int i = m; i <= 30; i += k) 15 | invalid[i] = true; 16 | counts[1] = invalid[1] ? 0 : 1; 17 | for (int i = 2; i <= n; ++i) { 18 | counts[i] = 0; 19 | for (int j = 1; j < i; ++j) { 20 | if (invalid[i-j]) 21 | continue; 22 | counts[i] += counts[j]; 23 | } 24 | if (!invalid[i]) 25 | counts[i]++; 26 | } 27 | printf("%d %llu\n", d, counts[n]); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Solved problems/others/DifferentDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int count[5001]; 6 | char buff[10]; 7 | int dcounts[10]; 8 | 9 | int main() { 10 | 11 | count[0] = 0; 12 | count[1] = 1; 13 | for(int i = 2; i <= 5000; ++i) { 14 | count[i] = count[i-1]; 15 | sprintf(buff,"%d",i); 16 | int length = strlen(buff); 17 | memset(dcounts,0,sizeof dcounts); 18 | 19 | bool repeats = false; 20 | for(int j = 0; j < length; ++j) { 21 | int d = buff[j]-'0'; 22 | if(++dcounts[d] > 1) { 23 | repeats = true; 24 | break; 25 | } 26 | } 27 | 28 | if(!repeats) 29 | count[i]++; 30 | } 31 | 32 | int n,m; 33 | while(scanf("%d %d",&n,&m) == 2) { 34 | int ans = count[m] - count[n-1]; 35 | printf("%d\n",ans); 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Solved problems/others/EnigmaticTravel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double F1(double L) { 6 | double L_1toL = pow(L - 1, L); 7 | return (L_1toL * (L * L - 2 * L - 1) + 1) / ((L_1toL - 1) * (L - 2)); 8 | } 9 | 10 | double F2(int L) { 11 | long long numerator = 0; 12 | long long denominator = 0; 13 | 14 | long long coef = 1; 15 | for (int k = 1; k < L; ++k) { 16 | coef *= L - k; 17 | numerator += k * coef; 18 | denominator += coef; 19 | } 20 | 21 | return numerator / (double) denominator; 22 | 23 | } 24 | 25 | double F3(int L) { 26 | long long numerator = 0; 27 | long long denominator = 0; 28 | 29 | long long coef = 1; 30 | for (int k = 2; k < L; ++k) { 31 | coef *= L - k; 32 | numerator += (k + 1) * coef; 33 | denominator += coef; 34 | } 35 | return numerator / (double) denominator; 36 | } 37 | 38 | double f1[15]; 39 | double f2[15]; 40 | double f3[15]; 41 | 42 | int main() { 43 | int L; 44 | 45 | for (L = 3; L <= 15; ++L) { 46 | f1[L] = F1((double) L); 47 | f2[L] = F2(L); 48 | f3[L] = F3(L); 49 | } 50 | 51 | while (true) { 52 | scanf("%d", &L); 53 | if (L == 0) 54 | break; 55 | printf("%.4f %.4f %.4f\n", f1[L], f2[L], f3[L]); 56 | } 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Solved problems/others/FindTheMultiple.cpp: -------------------------------------------------------------------------------- 1 | // tags: dp, math 2 | #include 3 | using namespace std; 4 | 5 | int choice[101][200]; 6 | int dp[101][200]; 7 | int MOD; 8 | char answer[101]; 9 | 10 | int findNumber(int i, int remainder) { 11 | if (remainder == 0) 12 | return i; 13 | 14 | if (i == 100) 15 | return 999999; 16 | 17 | if (dp[i][remainder] != -1) 18 | return dp[i][remainder]; 19 | 20 | int a = findNumber(i + 1, (remainder * 10) % MOD); 21 | int b = findNumber(i + 1, (remainder * 10 + 1) % MOD); 22 | if (a < b) 23 | choice[i][remainder] = 0; 24 | else 25 | choice[i][remainder] = 1; 26 | 27 | return dp[i][remainder] = (a < b) ? a : b; 28 | 29 | } 30 | 31 | int main() { 32 | while (true) { 33 | scanf("%d", &MOD); 34 | if (MOD == 0) 35 | break; 36 | 37 | for (int i = 1; i <= 100; ++i) 38 | for (int j = 1; j < MOD; ++j) 39 | dp[i][j] = -1; 40 | 41 | int length = findNumber(1, 1 % MOD); 42 | 43 | answer[0] = '1'; 44 | 45 | int rem = 1 % MOD; 46 | for (int i = 1; i < length; ++i) { 47 | answer[i] = '0' + choice[i][rem]; 48 | rem = (rem * 10 + choice[i][rem]) % MOD; 49 | } 50 | answer[length] = '\0'; 51 | printf("%s\n", answer); 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Solved problems/others/FlowersFlourishFromFrance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | char buffer[1500]; 9 | 10 | bool areEqual(char c1, char c2) { 11 | int val1 = ('a' <= c1 && c1 <= 'z') ? c1 - 'a' : c1 - 'A'; 12 | int val2 = ('a' <= c2 && c2 <= 'z') ? c2 - 'a' : c2 - 'A'; 13 | return val1 == val2; 14 | } 15 | 16 | int main() { 17 | 18 | setvbuf(stdout, NULL, _IONBF, 0); 19 | 20 | while (true) { 21 | 22 | char cmp; 23 | 24 | //scanf(" %[^\n]",lineBuffer); 25 | 26 | bool first = true; 27 | bool yes = true; 28 | while (true) { 29 | 30 | scanf("%s", buffer); 31 | char c = getchar(); 32 | if(first) { 33 | first = false; 34 | cmp = buffer[0]; 35 | if(cmp == '*') 36 | return 0; 37 | } else { 38 | if(!areEqual(cmp,buffer[0])) { 39 | yes = false; 40 | } 41 | } 42 | if(c == '\n') { 43 | if(yes) 44 | printf("Y\n"); 45 | else 46 | printf("N\n"); 47 | break; 48 | } 49 | 50 | } 51 | 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Solved problems/others/HelpCupid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N; 6 | int T[1005]; 7 | 8 | int main() { 9 | 10 | while (scanf("%d", &N) == 1) { 11 | for (int i = 0; i < N; ++i) 12 | scanf("%d", &T[i]); 13 | sort(T, T + N); 14 | 15 | int sum1 = 0; 16 | for (int i = 0; i + 1 < N; i += 2) { 17 | int dt = T[i + 1] - T[i]; 18 | sum1 += min(dt, 24 - dt); 19 | } 20 | 21 | int dt2 = (T[N - 1] - T[0]); 22 | int sum2 = min(dt2, 24 - dt2); 23 | for (int i = 1; i + 1 < N; i += 2) { 24 | int dt = T[i + 1] - T[i]; 25 | sum2 += min(dt, 24 - dt); 26 | } 27 | 28 | printf("%d\n", min(sum1, sum2)); 29 | 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Solved problems/others/HoursAndMinutes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool exists[361]; 7 | 8 | int main() { 9 | 10 | //setvbuf(stdout, NULL, _IONBF, 0); 11 | 12 | memset(exists, false, sizeof exists); 13 | 14 | for (int c = 0; c < 720; ++c) { 15 | int m = c % 60; 16 | int h = (c / 60) % 12; 17 | int a_m = m * 6; 18 | int a_h = h * 30; 19 | int d = abs(a_m - a_h); 20 | exists[min(360 - d, d)] = true; 21 | } 22 | 23 | int a; 24 | while (scanf("%d", &a) == 1) { 25 | if (exists[a]) 26 | printf("Y\n"); 27 | else 28 | printf("N\n"); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solved problems/others/ImmortalPorpoises.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef unsigned long long int ull; 5 | const ull MOD = 1000000000; 6 | 7 | vector mult(const vector& A, const vector& B) { 8 | vector res { (((A[0] * B[0]) % MOD) + ((A[1] * B[2]) % MOD)) % MOD, 9 | (((A[0] * B[1]) % MOD) + ((A[1] * B[3]) % MOD)) % MOD, (((A[2] 10 | * B[0]) % MOD) + ((A[3] * B[2]) % MOD)) % MOD, (((A[2] 11 | * B[1]) % MOD) + ((A[3] * B[3]) % MOD)) % MOD }; 12 | return res; 13 | } 14 | 15 | vector raise(const vector& matrix, ull exp) { 16 | if (exp == 1) 17 | return matrix; 18 | ull m = exp / 2; 19 | vector A = raise(matrix, m); 20 | if (exp % 2 == 0) 21 | return mult(A, A); 22 | else 23 | return mult(mult(A, A), matrix); 24 | } 25 | 26 | int main() { 27 | int P; 28 | int k; 29 | ull y; 30 | scanf("%d", &P); 31 | vector fib_matrix { 1, 1, 1, 0 }; 32 | while (P-- > 0) { 33 | scanf("%d %llu", &k, &y); 34 | vector ansm = raise(fib_matrix, y); 35 | ull ans = ansm[1]; 36 | printf("%d %llu\n", k, ans); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Solved problems/others/JingleComposing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | setvbuf(stdout, NULL, _IONBF, 0); 8 | 9 | char c; 10 | 11 | int count = 0; 12 | int charsum = 0; 13 | 14 | while (true) { 15 | 16 | c = getchar(); 17 | if (c == '*') 18 | return 0; 19 | else { 20 | 21 | switch (c) { 22 | 23 | case 'W': 24 | charsum += 64; 25 | break; 26 | case 'H': 27 | charsum += 32; 28 | break; 29 | case 'Q': 30 | charsum += 16; 31 | break; 32 | case 'E': 33 | charsum += 8; 34 | break; 35 | case 'S': 36 | charsum += 4; 37 | break; 38 | case 'T': 39 | charsum += 2; 40 | break; 41 | case 'X': 42 | charsum += 1; 43 | break; 44 | case '/': 45 | if (charsum == 64) 46 | count++; 47 | charsum = 0; 48 | break; 49 | case '\n': 50 | printf("%d\n", count); 51 | count = 0; 52 | c = getchar(); 53 | if (c == '*') 54 | return 0; 55 | break; 56 | } 57 | 58 | } 59 | 60 | } 61 | 62 | return 0; 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Solved problems/others/KlingonLevels.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | 10 | setvbuf(stdout, NULL, _IONBF, 0); 11 | 12 | while (true) { 13 | 14 | int n; 15 | scanf("%d", &n); 16 | if (n == 0) 17 | break; 18 | 19 | vector > scores(n); 20 | for (int i = 0; i < n; ++i) { 21 | int k; 22 | scanf("%d", &k); 23 | for (int j = 0; j < k; ++j) { 24 | int score; 25 | scanf("%d", &score); 26 | scores[i].push_back(score); 27 | } 28 | sort(scores[i].begin(), scores[i].end()); 29 | } 30 | int minAccDiff = 9999999; 31 | 32 | for (int T = 0; T <= 1000; ++T) { 33 | int accDiff = 0; 34 | for (int i = 0; i < n; ++i) { 35 | vector::iterator low = lower_bound(scores[i].begin(), 36 | scores[i].end(), T); 37 | int above = (scores[i].end() - low); 38 | int below = (low - scores[i].begin()); 39 | accDiff += abs(above - below); 40 | } 41 | if (minAccDiff > accDiff) 42 | minAccDiff = accDiff; 43 | } 44 | 45 | printf("%d\n", minAccDiff); 46 | 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Solved problems/others/UpdateTheArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int vals[10001]; 8 | int t; 9 | scanf("%d", &t); 10 | while (t-- > 0) { 11 | int n, u; 12 | scanf("%d %d",&n,&u); 13 | memset(vals,0,(n+1)*sizeof(int)); 14 | while(u-->0) { 15 | int l, r, val; 16 | scanf("%d %d %d",&l, &r, &val); 17 | vals[l] += val; 18 | vals[r+1] -= val; 19 | } 20 | int div = 0; 21 | for(int i = 0; i < n; ++i) { 22 | div+=vals[i]; 23 | vals[i] = div; 24 | } 25 | int q; 26 | scanf("%d",&q); 27 | for(int i = 0; i < q; ++i) { 28 | int index; 29 | scanf("%d",&index); 30 | printf("%d\n",vals[index]); 31 | } 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solved problems/others/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int reverse_bits(int x) { 5 | int v = 0; 6 | while (x) v <<= 1, v |= x&1, x >>= 1; 7 | return v; 8 | } 9 | 10 | string bitstring(int x) { 11 | int len = sizeof(x) * 8 - __builtin_clz(x); 12 | if (len == 0) return "0"; 13 | 14 | char buff[len+1]; buff[len] = '\0'; 15 | for (int i = len-1; i >= 0; --i, x >>= 1) 16 | buff[i] = (char)('0' + (x&1)); 17 | return string(buff); 18 | } 19 | 20 | int main() { 21 | while (true) { 22 | int n; scanf("%d", &n); 23 | string sbef = bitstring(n); 24 | string saft = bitstring(reverse_bits(n)); 25 | printf("bef = %s, aft = %s\n", sbef.c_str(), saft.c_str()); 26 | } 27 | } -------------------------------------------------------------------------------- /Solved problems/time_command.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import subprocess 3 | import time 4 | 5 | def main(): 6 | # Set up argument parser 7 | parser = argparse.ArgumentParser(description="Time a shell command.") 8 | parser.add_argument("--command", type=str, required=True, help="The command to execute.") 9 | args = parser.parse_args() 10 | 11 | # Capture the start time 12 | start_time = time.time() 13 | 14 | # Run the command 15 | try: 16 | result = subprocess.run(args.command, shell=True, check=True) 17 | except subprocess.CalledProcessError as e: 18 | print(f"Command failed with exit code {e.returncode}") 19 | return 20 | 21 | # Capture the end time 22 | end_time = time.time() 23 | 24 | # Calculate and print the execution time 25 | elapsed_time = end_time - start_time 26 | print(f"Execution Time: {elapsed_time:.2f} seconds") 27 | 28 | if __name__ == "__main__": 29 | main() 30 | -------------------------------------------------------------------------------- /Strings/AhoCorasick.cpp: -------------------------------------------------------------------------------- 1 | struct AC { 2 | int c = 0, ec = 0, M, A; 3 | vector N, G; vi L, E; // L = suffix link, G = reverse suffix link 4 | AC (int M, int A) : M(M), A(A), N(M, vi(A, 0)), E(M, 0), L(M, 0) {} 5 | int add(const string& s) { 6 | int p = 0; 7 | for (char l : s) { 8 | int t = l - 'a'; 9 | if (!N[p][t]) N[p][t] = ++c; 10 | p = N[p][t]; 11 | } E[p]++; return p; 12 | } 13 | void init() { 14 | G.assign(M, {}); 15 | queue q; q.push(0); L[0] = -1; 16 | while (!q.empty()) { 17 | int p = q.front(); q.pop(); 18 | rep(c, 0, A) { 19 | int u = N[p][c]; if (!u) continue; 20 | L[u] = L[p] == -1 ? 0 : N[L[p]][c], q.push(u); 21 | G[L[u]].pb(u); 22 | } 23 | if (p) rep(c, 0, A) if (!N[p][c]) N[p][c] = N[L[p]][c]; 24 | } 25 | } 26 | }; -------------------------------------------------------------------------------- /Strings/shortest_repeating_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int shortest_repeating_cycle(string& seq) { 5 | // KMP : lps step 6 | int n = seq.size(); 7 | int lps[n]; 8 | lps[0] = 0; 9 | int i = 0, j = 1; 10 | while (j < n) { 11 | while (i > 0 and seq[i] != seq[j]) 12 | i = lps[i-1]; 13 | if (seq[i] == seq[j]) 14 | lps[j] = ++i; 15 | else 16 | lps[j] = 0; 17 | j++; 18 | } 19 | int len = n - lps[n-1]; 20 | return (n % len) ? n : len; 21 | } 22 | 23 | // test 24 | int main() { 25 | string line; cin >> line; 26 | int cycle = shortest_repeating_cycle(line); 27 | cout << line.substr(0, cycle) << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /c++_include.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; -------------------------------------------------------------------------------- /c++_template.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #include 3 | using namespace std; 4 | // defines 5 | #define rep(i,a,b) for(int i = a; i < b; ++i) 6 | #define invrep(i,b,a) for(int i = b; i >= a; --i) 7 | #define umap unordered_map 8 | #define uset unordered_set 9 | #define ff first 10 | #define ss second 11 | #define pb push_back 12 | #define eb emplace_back 13 | // typedefs 14 | typedef vector vi; 15 | typedef pair ii; 16 | typedef unsigned long long int ull; 17 | typedef long long int ll; 18 | // ------------------------------- 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(0); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /latex.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PabloMessina/Competitive-Programming-Material/28fd0457f5197b54a9805bdc66ff3ce822eed595/latex.pdf -------------------------------------------------------------------------------- /random_tests.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAXN 9999 4 | #define umap unordered_map 5 | #define rep(i,a,b) for(int i=a;i<=b;++i) 6 | 7 | vector get_primes(int n) { 8 | vector is_prime(n + 1, true); 9 | int limit = (int) floor(sqrt(n)); 10 | rep (i,2,limit) 11 | if (is_prime[i]) 12 | for (int j = i * i; j <= n; j += i) 13 | is_prime[j] = false; 14 | vector primes; 15 | rep(i,2,n) if (is_prime[i]) primes.push_back(i); 16 | return primes; 17 | } 18 | 19 | void factorial_prime_factorization(int x, umap& counts) { 20 | static vector primes = get_primes(MAXN); 21 | 22 | for (int p : primes) { 23 | if (p > x) break; 24 | int count = 0; 25 | int n = x; 26 | while ((n /= p) > 0) 27 | count += n; 28 | if (count) counts[p] = count; 29 | } 30 | } 31 | 32 | int main() { 33 | int x; 34 | while(scanf("%d",&x) == 1) { 35 | umap counts; 36 | factorial_prime_factorization(x, counts); 37 | for(auto& kv : counts) { 38 | printf(" counts[%d] = %d\n", kv.first, kv.second); 39 | } 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | def pattern(inputv): 2 | if not inputv: 3 | return inputv 4 | 5 | nxt = [0]*len(inputv) 6 | for i in range(1, len(nxt)): 7 | k = nxt[i - 1] 8 | while True: 9 | if inputv[i] == inputv[k]: 10 | nxt[i] = k + 1 11 | break 12 | elif k == 0: 13 | nxt[i] = 0 14 | break 15 | else: 16 | k = nxt[k - 1] 17 | 18 | smallPieceLen = len(inputv) - nxt[-1] 19 | if len(inputv) % smallPieceLen != 0: 20 | return inputv 21 | 22 | for i,k in enumerate(nxt): 23 | print("{}, {}".format(i,k)) 24 | 25 | return inputv[0:smallPieceLen] 26 | 27 | print(pattern("abckabcabckabcabckabcabckabcabckabc")) 28 | --------------------------------------------------------------------------------