├── .gitignore ├── Material ├── DP │ ├── DivideAndConquer.cpp │ ├── KnuthOptimization.cpp │ └── LIS.cpp ├── Geometry │ ├── 2D.cpp │ └── 3D.cpp ├── Graphs │ ├── BellmanFord.cpp │ ├── Centroid.cpp │ ├── Dijkstra.cpp │ ├── Dinic.cpp │ ├── FloydWarshall.cpp │ ├── HeavyLightDecomposition.cpp │ ├── HopcroftKarp.cpp │ ├── Hungarian.cpp │ ├── LCA.cpp │ ├── LinkCutTree.cpp │ ├── MST.cpp │ ├── MinCostMaxFlow.cpp │ ├── SCC.cpp │ ├── Tarjan.cpp │ ├── Toposort.cpp │ ├── TreeDecomposition2.cpp │ └── TreeWidthShortestPath.cpp ├── Math │ ├── CRT.cpp │ ├── Euclid.cpp │ ├── FFT.cpp │ ├── Modular.cpp │ ├── ModularBinomial.cpp │ ├── Primality.cpp │ ├── PrimeFactorization.cpp │ └── Sieve.cpp ├── README.md ├── Strings │ ├── AhoCorasick.cpp │ ├── Hash.cpp │ ├── KMP.cpp │ ├── Manacher.cpp │ ├── SuffixArray.cpp │ ├── SuffixAutomaton.cpp │ └── Trie.cpp ├── Structures │ ├── DSU.cpp │ ├── DynamicConnectivity.cpp │ ├── FenwickTree.cpp │ ├── FenwickTree2D.cpp │ ├── LineContainer.cpp │ ├── MoQueries.cpp │ ├── PersistentSegmentTree.cpp │ ├── PersistentSegmentTreeLazy.cpp │ ├── PolicyBased.cpp │ ├── SegmentTree.cpp │ ├── SegmentTreeBeats.cpp │ ├── SegmentTreeLazy.cpp │ ├── SparseTable.cpp │ ├── Treap.cpp │ └── WaveletTree.cpp ├── Template.cpp └── notebook.pdf ├── Problems ├── AtCoder │ ├── 123Triangle.cpp │ ├── BuyAnInteger.cpp │ ├── CantWaitForHoliday.cpp │ ├── FortuneCookies.cpp │ ├── MAX_min.cpp │ ├── ManySlimes.cpp │ ├── Roadwork.cpp │ └── RotN.cpp ├── BaekJoon │ └── AutomaticAccountant.cpp ├── CodeChef │ ├── Gravel.cpp │ ├── IsThisJEE.cpp │ ├── MaximizeLetterBeauty.cpp │ ├── RaceTime!.cpp │ └── TowerCounting.cpp ├── Codeforces │ ├── 01Game.cpp │ ├── 13thLaborOfHeracles.cpp │ ├── 1TreesAndQueries.cpp │ ├── 3-coloring.cpp │ ├── ABBB.cpp │ ├── ABBalance.cpp │ ├── ABBalance.py │ ├── ACookieForYou.cpp │ ├── AGAGAXOOORRR.cpp │ ├── AGoodString.cpp │ ├── AND0SumBig.cpp │ ├── ANDSequences.cpp │ ├── AcurateLee.cpp │ ├── AddOne.cpp │ ├── Aerodynamic.cpp │ ├── Ahahahahahahahaha.cpp │ ├── AirConditioner.cpp │ ├── AlarmClock.cpp │ ├── AlmostPrime.cpp │ ├── AlmostSorted.cpp │ ├── AlternatingSubsequence.cpp │ ├── AlternativeReality.cpp │ ├── AnadiAndDomino.cpp │ ├── AncientTowers.cpp │ ├── AngryStudents.cpp │ ├── AnuHasAFunction.cpp │ ├── ApolloVersusPan.cpp │ ├── ApplejackAndStorages.cpp │ ├── ArcheryTournament.cpp │ ├── ArmyCreation.cpp │ ├── AromasSearch.py │ ├── ArrayAndPeaks.cpp │ ├── ArrayCancelation.cpp │ ├── ArraySharpening.cpp │ ├── ArrayWalk.cpp │ ├── ArraysSum.cpp │ ├── Arrow.cpp │ ├── AssigningToClasses.cpp │ ├── Asterism.cpp │ ├── AsyaAndKittens.cpp │ ├── AvoidRainbowQueries.cpp │ ├── AvoidTrygub.cpp │ ├── AvoidingZero.cpp │ ├── AyoubsFunction.cpp │ ├── BabyEhabPartitionsAgain.cpp │ ├── BadTriangle.cpp │ ├── BadUglyNumbers.cpp │ ├── BalanceTheBits.cpp │ ├── BalancedBitstring.cpp │ ├── Ball.cpp │ ├── BallsOfSteel.cpp │ ├── BanditInACity.cpp │ ├── Bargain.cpp │ ├── BasicDiplomacy.cpp │ ├── BattalionStrength.cpp │ ├── BearAndPrimeNumbers.cpp │ ├── BeautifulNumbers.cpp │ ├── BeautifulNumbers_.cpp │ ├── BeautifulRegionalContest.cpp │ ├── BeautifulString.cpp │ ├── BecauseArt!.cpp │ ├── BeltedRooms.cpp │ ├── BerlandCollider.cpp │ ├── BerlandPoker.cpp │ ├── BerlandRegional.cpp │ ├── BestACMrSolvesTheHardestProblem.cpp │ ├── BigVova.cpp │ ├── Billiard.cpp │ ├── BinaryLiterature.cpp │ ├── BinaryPeriod.cpp │ ├── BinaryRemovals.cpp │ ├── BinarySearch.cpp │ ├── BinaryStringMinimizing.cpp │ ├── BinaryStringReconstruction.cpp │ ├── BinaryTable.cpp │ ├── BoboniuAndBitOperations.cpp │ ├── BoboniuChatsWithDu.cpp │ ├── BoboniuLikesToColorBalls.cpp │ ├── BoboniuPlaysChess.cpp │ ├── Bombs.cpp │ ├── BooksExchangeEasy.cpp │ ├── BouncingBall.cpp │ ├── BouncingBoomerangs.cpp │ ├── BovineDilemma.cpp │ ├── Box.cpp │ ├── BoxIsPull.cpp │ ├── Box_ICPC.cpp │ ├── BuildString.cpp │ ├── C+=.cpp │ ├── CandiesDivision.cpp │ ├── CaninePoetry.cpp │ ├── CaptainFlintAndCrewRecruitment.cpp │ ├── CaptainFlintAndLongVoyage.cpp │ ├── CaptainFlintAndTreasure.cpp │ ├── CardConstructions.cpp │ ├── CardGame.cpp │ ├── CarrotsForRabbits.cpp │ ├── CatParty.cpp │ ├── CatchingCheaters.cpp │ ├── CelexUpdate.cpp │ ├── CenterAlignement.cpp │ ├── CenterAlignment.py │ ├── Centroids.cpp │ ├── ChangeFree.cpp │ ├── ChangingAString.cpp │ ├── ChangingVolume.cpp │ ├── CharacterSwap.cpp │ ├── CharacterSwapEasy.cpp │ ├── ChasingTheCheetahs.cpp │ ├── Checkposts.cpp │ ├── ChefMonocarp.cpp │ ├── ChessCheater.cpp │ ├── ChocolateBunny.cpp │ ├── CircleColoring.cpp │ ├── CircleGame.cpp │ ├── CircleOfMonsters.cpp │ ├── Classical?.cpp │ ├── ClosingTheGap.cpp │ ├── ClosingTheGap.py │ ├── CodeforcesSubsequences.cpp │ ├── Colorblindness.cpp │ ├── Colorblindness.py │ ├── ColoredRectangles.cpp │ ├── ColourTheFlag.cpp │ ├── CompetitiveProgrammer.cpp │ ├── CompressionAndExpansion.cpp │ ├── ComputerCache.cpp │ ├── ConneRAndTheARCMarklandN.cpp │ ├── Connections.cpp │ ├── ContestForRobots.cpp │ ├── CooperativeEscape.cpp │ ├── CoprimeQueries.cpp │ ├── CopyCopyCopyCopyCopy.cpp │ ├── CopyingHomework.cpp │ ├── CormenTheBestFriendOfAMan.cpp │ ├── CountPaths.cpp │ ├── CountPathsQueries.cpp │ ├── CountTheBlocks.cpp │ ├── CountTriangles.cpp │ ├── CountingOrders.cpp │ ├── CowAndFields.cpp │ ├── CowAndFriend.cpp │ ├── CowAndHaybales.cpp │ ├── CowAndMessage.cpp │ ├── CubesSorting.cpp │ ├── Cut.cpp │ ├── CycleInMaze.cpp │ ├── CyclicPermutations.cpp │ ├── CyclistsVsClouds.cpp │ ├── DZYLovesModification.cpp │ ├── DailyTurnovers.cpp │ ├── DauntingDevice.cpp │ ├── DeadPixel.cpp │ ├── Deadline.cpp │ ├── DeathStars.cpp │ ├── Decryption.cpp │ ├── DeleteASegment.cpp │ ├── DeletingNumbers.cpp │ ├── DesignTutorialInverseTheProblem.cpp │ ├── DevuAndHisBrother.cpp │ ├── DiceTower.cpp │ ├── DigitGame.cpp │ ├── DiscreteAcceleration.cpp │ ├── DiscreteCentrifugalJumps.cpp │ ├── DisjointTriangles.cpp │ ├── DistanceAndAxis.cpp │ ├── DiverseMatrix.cpp │ ├── DivideAndMultiply.cpp │ ├── DivideAndMultiply.py │ ├── DivideAndSummarize.cpp │ ├── Division.cpp │ ├── DonutShops.cpp │ ├── DrawingQuadrilaterals.cpp │ ├── DreamoonAndRankingCollection.cpp │ ├── DreamoonLikesColoring.cpp │ ├── DreamoonLikesPermutations.cpp │ ├── EdgeWeightAssignment.cpp │ ├── EdgesInMST.cpp │ ├── EhAbAnDgCd.cpp │ ├── EhabAndPath-ethicMEXs.cpp │ ├── EhabAndPrefixMEXs.cpp │ ├── EhabTheXorcist.cpp │ ├── EhabsLastCorolary.cpp │ ├── ElementExtermination.cpp │ ├── EngineerArtem.cpp │ ├── EngineerAssignment.cpp │ ├── Enigma.cpp │ ├── ErasingZeroes.cpp │ ├── ErrorCorrection.cpp │ ├── EuclidsNightmare.cpp │ ├── EugeneAndArray.cpp │ ├── EvenButNotEven.cpp │ ├── EvenPath.cpp │ ├── EvenPicture.cpp │ ├── EveryoneIsAWinner.cpp │ ├── ExcellentViews.cpp │ ├── Exercising.cpp │ ├── ExpeditionPlans.cpp │ ├── ExplorerSpace.cpp │ ├── ExtremeSubstraction.cpp │ ├── FashonabLee.cpp │ ├── FastFoodRestaurant.cpp │ ├── Fence.cpp │ ├── Fibonacci.cpp │ ├── FieldsDivision.cpp │ ├── FillingDiamonds.cpp │ ├── FillingShapes.cpp │ ├── Filomino2.cpp │ ├── FindANumber.cpp │ ├── FindTheSpruce.cpp │ ├── FixYou.cpp │ ├── FlightCollision.cpp │ ├── FourSegments.cpp │ ├── FourSegments.py │ ├── FrequencyProblem.cpp │ ├── FretsOnFire.cpp │ ├── FridgeLockers.cpp │ ├── FriendsAndGifts.cpp │ ├── GCDAndMST.cpp │ ├── GCDCompression.cpp │ ├── GCDLength.cpp │ ├── GameOfLife.cpp │ ├── GameWithArray.cpp │ ├── GameWithChips.cpp │ ├── Garland.cpp │ ├── GatesOfUncertainty.cpp │ ├── GatheringRoquefort.cpp │ ├── GeneTree.cpp │ ├── GeneratorTree.cpp │ ├── GoodString.cpp │ ├── GoodSubarrays.cpp │ ├── GradeAllocation.cpp │ ├── GreedyShopping.cpp │ ├── GreenhouseEffect.cpp │ ├── GregAndArray.cpp │ ├── GregAndGraph.cpp │ ├── Grid-00100.cpp │ ├── GuessThePath.cpp │ ├── Hamilton-TheMusical.cpp │ ├── Hexagons.cpp │ ├── HighLoad.cpp │ ├── HilbertsHotel.cpp │ ├── HillsAndValleys.cpp │ ├── HistogramUgliness.cpp │ ├── Holes.cpp │ ├── Homecoming.cpp │ ├── HuntingVampires.cpp │ ├── Hurgando.cpp │ ├── Hyperset.cpp │ ├── Hyperset.py │ ├── IchihimeAndTriangle.cpp │ ├── IdentifyTheOperations.cpp │ ├── Image.cpp │ ├── Image.py │ ├── ImpenetrableWall.cpp │ ├── Inca-Mancora.cpp │ ├── InfinitePaths.cpp │ ├── Interactivity.cpp │ ├── InterestingSubarray.cpp │ ├── InvestedMoney.cpp │ ├── IrreducibleAnagrams.cpp │ ├── IslandTour.cpp │ ├── JobAllocator.cpp │ ├── JoeIsOnTV.cpp │ ├── JohnnyAndAncientComputer.cpp │ ├── JohnnyAndAnotherRatingDrop.cpp │ ├── JohnnyAndContribution.cpp │ ├── JohnnyAndGrandmaster.cpp │ ├── JohnnyAndHisHobbies.cpp │ ├── JoiningPairs.cpp │ ├── JourneyPlanning.cpp │ ├── JugglingLetters.cpp │ ├── K-AmazingNumbers.cpp │ ├── K-CompleteWord.cpp │ ├── KForThePriceOfOne.cpp │ ├── KForThePriceOfOneHard.cpp │ ├── KIARAIsARecursiveAcronym.cpp │ ├── KanaAndDragonQuestGame.cpp │ ├── Karaoke.cpp │ ├── Keyboardd.cpp │ ├── Keylogger.cpp │ ├── KilaniAndTheGame.cpp │ ├── Killjoy.cpp │ ├── KindAnton.cpp │ ├── Knapsack.cpp │ ├── KnightPaths.cpp │ ├── KuroniAndSimpleStrings.cpp │ ├── KuroniAndTheGifts.cpp │ ├── LCMProblem.cpp │ ├── LaminarFamily.cpp │ ├── LargeTriangle.cpp │ ├── LastMinuteEnhancements.cpp │ ├── Lavaspar.cpp │ ├── Leaders.cpp │ ├── LeagueOfLeesins.cpp │ ├── LeavingYharnam.cpp │ ├── LetsBurnAndRobManhootan.cpp │ ├── LevelStatistics.cpp │ ├── Line.cpp │ ├── LinkCutCentroids.cpp │ ├── LinovaAndKingdom.cpp │ ├── LittleAlawnsPuzzle.cpp │ ├── LittleArtem.cpp │ ├── LolasSchedule.cpp │ ├── LongJumps.cpp │ ├── LongestPalindrome.cpp │ ├── LookingForOrder.cpp │ ├── LookingForOwls.cpp │ ├── LordOfTheValues.cpp │ ├── LostArray.cpp │ ├── LostTree.cpp │ ├── LuckyArray.cpp │ ├── LuckyCommonSubsequence.cpp │ ├── LuckyNumberRepresentation.cpp │ ├── MADMAX.cpp │ ├── MachiningDiscRotors.cpp │ ├── MafiaWar.cpp │ ├── MagicalCalendar.cpp │ ├── MagicalSticks.cpp │ ├── MakeGood.cpp │ ├── MakeItIncreasing.cpp │ ├── MakingSequencesIsFun.cpp │ ├── MakingSequencesIsFun.py │ ├── MariaBraksTheSelfIsolation.cpp │ ├── MarkAndHisUnfinishedEssay.cpp │ ├── MarkAndHisUnfinishedEssay.py │ ├── MarketingScheme.cpp │ ├── MatchAndCatch.cpp │ ├── MathProblem.cpp │ ├── MatrixGame.cpp │ ├── MaximumDistributedTree.cpp │ ├── MaximumGCD.cpp │ ├── MaximumProduct.cpp │ ├── MaximumSquare.cpp │ ├── MaximumSquare.py │ ├── MaximumSubsequenceValue.cpp │ ├── MaximumSumOfProducts.cpp │ ├── MaximumSumOnEvenPositions.cpp │ ├── Maximums.cpp │ ├── MayIAddALetter.cpp │ ├── MereArray.cpp │ ├── MessengerSimulator.cpp │ ├── MiddleClass.cpp │ ├── MinPath.cpp │ ├── MindControl.cpp │ ├── MinimalHeightTree.cpp │ ├── MinimalSegmentCover.cpp │ ├── MinimaxProblem.cpp │ ├── MinimizeThePermutation.cpp │ ├── MinimumCostMaximumFlow.cpp │ ├── MinimumSpanningTreeForEachEdge.cpp │ ├── Minlexes.cpp │ ├── MinutesBeforeTheNewYear.cpp │ ├── MirrorGrid.cpp │ ├── MirrorGrid.py │ ├── MixingWater.cpp │ ├── MonopoleMagnets.cpp │ ├── MorningJogging.cpp │ ├── MostOrderedWay.cpp │ ├── MostSociallyDistancedSubsequence.cpp │ ├── MotaracksBirthday.cpp │ ├── MountainView.cpp │ ├── MultipleTestCases.cpp │ ├── MultiplesOfLenghts.cpp │ ├── NEKOsMazeGame.cpp │ ├── NastiaAndNearlyGoodNumbers.cpp │ ├── NastyaAndDoor.cpp │ ├── NastyaAndRice.cpp │ ├── NastyaAndScoreboard.cpp │ ├── NastyaAndStrangeGenerator.cpp │ ├── NationalProject.cpp │ ├── NavigationSystem.cpp │ ├── NeighborGrid.cpp │ ├── NewTheatreSquare.cpp │ ├── NewYearAndAscentSequence.cpp │ ├── NewYearAndCastleConstruction.cpp │ ├── NewYearAndConference.cpp │ ├── NewYearAndNaming.cpp │ ├── NewYearAndPermutation.cpp │ ├── NewYearGarland.cpp │ ├── NewYearPermutations.cpp │ ├── NewsDistribution.cpp │ ├── NiceMatrix.cpp │ ├── Non-IntegerDonuts.cpp │ ├── Non-SubstringSubsequence.cpp │ ├── Non-Zero.cpp │ ├── NotSoSimplePolygonEmbedding.cpp │ ├── NumberGame.cpp │ ├── NumberMultiplication.cpp │ ├── NumberOfWays.cpp │ ├── OmkarAndBedWars.cpp │ ├── OmkarAndDuck.cpp │ ├── OmkarAndInfinityClock.cpp │ ├── OmkarAndPassword.cpp │ ├── OmkarAndWaterslide.cpp │ ├── OracAndFactors.cpp │ ├── OracAndLCM.cpp │ ├── OracAndMedians.cpp │ ├── OracAndModels.cpp │ ├── PINCodes.cpp │ ├── PalindromicPaths.cpp │ ├── Palindromifier.cpp │ ├── ParkLightning.cpp │ ├── PartyCompany.cpp │ ├── PartyInAmerica.cpp │ ├── PassportControlGates.cpp │ ├── PaymentWithoutChange.cpp │ ├── PeaCity.cpp │ ├── PearTreaP.cpp │ ├── PerfectFlush.cpp │ ├── PerfectKeyboard.cpp │ ├── PerfectlyImperfectArray.cpp │ ├── PerformEasily.cpp │ ├── PermutationForgery.cpp │ ├── PermutationPartitions.cpp │ ├── PetyaAndExam.cpp │ ├── PhoenixAndBalance.cpp │ ├── PhoenixAndBeauty.cpp │ ├── PhoenixAndComputers.cpp │ ├── PhoenixAndDistribution.cpp │ ├── PhoenixAndGold.cpp │ ├── PhoenixAndPuzzle.cpp │ ├── PhoenixAndScience.cpp │ ├── PhoenixAndSocks.cpp │ ├── PhoenixAndTowers.cpp │ ├── PhotoProcessing.cpp │ ├── PinkiePieEatsPattyCakes.cpp │ ├── PlanB.cpp │ ├── PlatformsJumping.cpp │ ├── Playlist.cpp │ ├── PlusesAndMinuses.cpp │ ├── PokemonArmy.cpp │ ├── PolicePatrol.cpp │ ├── PoliceStation.cpp │ ├── PollandBallAndHypothesis.cpp │ ├── PowerSequence.cpp │ ├── PoweredAddition.cpp │ ├── PowerfullArray.cpp │ ├── Prefix-SuffixPalindrome.cpp │ ├── PrefixesAndSuffixes.cpp │ ├── PrimeSquare.cpp │ ├── PrimitivePrimes.cpp │ ├── PrincessesAndPrinces.cpp │ ├── PursuitForArtifacts.cpp │ ├── PuttingBricksOnTheWall.cpp │ ├── PuzzlePieces.cpp │ ├── PythonClases.cpp │ ├── QueriesForNumberOfPalindromes.cpp │ ├── RPGProtagonist.cpp │ ├── RadioTower.cpp │ ├── RainbowDash,FuttershyAndChessColoring.cpp │ ├── RandomEvents.cpp │ ├── RandomMood.cpp │ ├── RatingCompression.cpp │ ├── RationalLee.cpp │ ├── RecurrenceWithSquare.cpp │ ├── RedAndBlueBeans.cpp │ ├── RegularBracketSequences.cpp │ ├── RegularBracketSequences.py │ ├── RemoveAdjacent.cpp │ ├── RemoveOneElement.cpp │ ├── RemoveSmallest.cpp │ ├── RemoveSmallest.py │ ├── RemovingLeaves.cpp │ ├── Reorder.cpp │ ├── ReplaceByMEX.cpp │ ├── RescueNibel.cpp │ ├── RestoringPermutation.cpp │ ├── ReturningHome.cpp │ ├── ReverseBinaryStrings.cpp │ ├── ReviewSite.cpp │ ├── RoadToZero.cpp │ ├── RoadsInBerland.cpp │ ├── RobotProgram.cpp │ ├── RockAndLever.cpp │ ├── RotationMatching.cpp │ ├── RunawayToAShadow.cpp │ ├── SBCsHangar.cpp │ ├── SagesBirthday.cpp │ ├── SantaClausAndCandies.cpp │ ├── SantasBot.cpp │ ├── SashaAndHisTrip.cpp │ ├── Searchlights.cpp │ ├── SecretPasswords.cpp │ ├── SegmentIntersections.cpp │ ├── SequenceWithDigits.cpp │ ├── ShandomRuffle.cpp │ ├── ShortestAndLongestLIS.cpp │ ├── ShovelsAndSwords.cpp │ ├── Shuffle.cpp │ ├── Shurikens.cpp │ ├── SideTransmutations.cpp │ ├── SignFlipping.cpp │ ├── Skyscrapers.cpp │ ├── SneetchesAndSpeeches1.cpp │ ├── SneetchesAndSpeeches3.cpp │ ├── SnowWalkingRobot.cpp │ ├── Snowball.cpp │ ├── SoftDrinking.cpp │ ├── SoftDrinking.py │ ├── SolveTheMaze.cpp │ ├── SongsCompression.cpp │ ├── SortedAdjacentDifferences.cpp │ ├── SpaceshipSolitaire.cpp │ ├── SpecificTastesOfAndre.cpp │ ├── SquareSubsets.cpp │ ├── StackOfPresents.cpp │ ├── Stairs.cpp │ ├── StatisticsOfRecompressingVideos.cpp │ ├── StonedGame.cpp │ ├── StonesOnTheTable.cpp │ ├── StonesOnTheTable.py │ ├── StrangeBirthdayParty.cpp │ ├── StrangeDefinition.cpp │ ├── StrangeHousing.cpp │ ├── StrikeZone.cpp │ ├── StringEquality.cpp │ ├── StringGeneration.cpp │ ├── StringModification.cpp │ ├── StringMood.cpp │ ├── StringMoodUpdates.cpp │ ├── StringProblem.cpp │ ├── StringSimilarity.cpp │ ├── StronglyConnectedCity.cpp │ ├── Suborray.cpp │ ├── SubsequencesOfLenghtTwo.cpp │ ├── SubsetMex.cpp │ ├── SubstractOrDivide.cpp │ ├── Substring.cpp │ ├── SubstringRemovalGame.cpp │ ├── Sum.cpp │ ├── Sum.py │ ├── SumOf2050.cpp │ ├── SumOfLog.cpp │ ├── SumOfOddIntegers.cpp │ ├── SumOfPaths.cpp │ ├── SummerEarnings.cpp │ ├── SwapsAgain.cpp │ ├── SwapsInPemutation.cpp │ ├── SweetProblem.cpp │ ├── TROYQuery.cpp │ ├── TediousLee.cpp │ ├── TemporarilyUnavailable.cpp │ ├── TemporarilyUnavailable.py │ ├── TernarySequence.cpp │ ├── TernaryString.cpp │ ├── Test.cpp │ ├── TheBestVactaion.cpp │ ├── TheCakeIsALie.cpp │ ├── TheFairNutAndRectangles.cpp │ ├── TheLabyrinth.cpp │ ├── TheMeetingPlaceCannotBeChanged.cpp │ ├── TheMeetingPlaceCannotBeChanged.py │ ├── TheQueue.cpp │ ├── TheSportsFestival.cpp │ ├── TheyAreEverywhere.cpp │ ├── TheyAreEverywhere.py │ ├── ThreeBags.cpp │ ├── ThreeFriends.cpp │ ├── ThreeSequences.cpp │ ├── ThreeStrings.cpp │ ├── TilePainting.cpp │ ├── TimeToRun.cpp │ ├── TitForTat.cpp │ ├── TooManyHiphens.cpp │ ├── TooManySegmentsEasy.cpp │ ├── ToyBlocks.cpp │ ├── TravelingSalesmanProblem.cpp │ ├── Treasure.cpp │ ├── TreeTag.cpp │ ├── TrickyFunction.cpp │ ├── TroubleSort.cpp │ ├── TurnOffTheTV.cpp │ ├── TwinBuildings.cpp │ ├── TwoArrays.cpp │ ├── TwoArraysE.cpp │ ├── TwoBrackets.cpp │ ├── TwoDifferent.cpp │ ├── TwoDistinctPoints.cpp │ ├── TwoDistinctPoints.py │ ├── TwoDivisors.cpp │ ├── TwoRabbits.cpp │ ├── TwoSquares.cpp │ ├── TwoTeams.cpp │ ├── UncleBogdanAndCountryOfHappiness.cpp │ ├── UncleBogdanAndProjections.cpp │ ├── UniqueOccurrences.cpp │ ├── ValeriiAgainstEveryone.cpp │ ├── ValeriyAndDeque.cpp │ ├── VanyaAndLanterns.cpp │ ├── VanyaAndLanterns.py │ ├── VerseForSanta.cpp │ ├── VikaAndSegments.cpp │ ├── Vitamins.cpp │ ├── VladikAndEntertainingFlags.cpp │ ├── Volcanoes.cpp │ ├── Volleyball.cpp │ ├── WalkOnMatrix.cpp │ ├── WaterBalance.cpp │ ├── WaterTesting.cpp │ ├── WeaknessAndPoorness.cpp │ ├── WetSharkAndBlocks.cpp │ ├── WizardOfOrz.cpp │ ├── WorriedSchool.cpp │ ├── XOR-gun.cpp │ ├── XORwice.cpp │ ├── XXXXX.cpp │ ├── XeniaAndBitOperations.cpp │ ├── XeniaAndColorfulGems.cpp │ ├── XorTree.cpp │ ├── YetAnotherBrokenKeyboard.cpp │ ├── YetAnotherCardDeck.cpp │ ├── YetAnotherCountingProblem.cpp │ ├── YetAnotherDividingIntoTeams.cpp │ ├── YetAnotherMemeProblem.cpp │ ├── YetAnotherYetAnotherTask.cpp │ ├── YoungExplorers.cpp │ ├── YounglingTournament.cpp │ ├── ZigZags.cpp │ └── e-Government.cpp ├── Facebook │ └── 2020 │ │ ├── Alchemy.cpp │ │ ├── Perimetric1.cpp │ │ ├── Perimetric2.cpp │ │ ├── Perimetric3.cpp │ │ └── TravelRestrictions.cpp ├── Google │ ├── CodeJam │ │ ├── AppendSort.cpp │ │ ├── HiddenPancackes.cpp │ │ ├── Matrygons.cpp │ │ ├── MinimumSort.cpp │ │ ├── MoonsAndUmbrellas.cpp │ │ ├── NestingDepth.cpp │ │ ├── ParentingPartneringReturns.cpp │ │ ├── PascalWalk.cpp │ │ ├── PatternMatching.cpp │ │ ├── PrimeTime.cpp │ │ ├── Retiling.cpp │ │ ├── Revesort.cpp │ │ ├── RevesortEngineering.cpp │ │ └── Vestigium.cpp │ └── Kickstart │ │ ├── ATMQueue.cpp │ │ ├── Kick_Start.cpp │ │ ├── MaximumCoins.cpp │ │ └── MetalHarvest.cpp ├── HackerRank │ ├── GuardiansOfTheLunatics.cpp │ ├── RoadsInHackerLand.cpp │ ├── StringSimilarity.cpp │ └── TheCoinChangeProblem.cpp ├── ICPC │ ├── AnotherCrisis.cpp │ ├── ArmyBuddies.cpp │ ├── AtMostTwice.cpp │ ├── AutomatedCheckingMachine.cpp │ ├── BallroomLights.cpp │ ├── Bingo.cpp │ ├── BlackAndWhiteStones.cpp │ ├── BoraBora.cpp │ ├── Brothers.cpp │ ├── BubbleMaps.cpp │ ├── BuggyICPC.cpp │ ├── CarPlatesCompetition.cpp │ ├── CocircularPoints.cpp │ ├── CodeLock.cpp │ ├── CompleteNaebbiracsSequence.cpp │ ├── DAsInDaedalus.cpp │ ├── DinnerHall.cpp │ ├── DropTheTriples.cpp │ ├── ElectricBill.cpp │ ├── Electriciy.py │ ├── Eleven.cpp │ ├── EnviromentProtection.cpp │ ├── EvenDistribution.cpp │ ├── FileRecover.cpp │ ├── FileRetrieval.cpp │ ├── FlowersFlourishFromFrance.py │ ├── GameOfMatchings.cpp │ ├── GeophysicsProspection.cpp │ ├── HardChoice.cpp │ ├── HeIsOffside.cpp │ ├── HelpCupid.cpp │ ├── HiddenPassword.cpp │ ├── Hooligan.cpp │ ├── HoursAndMinutes.cpp │ ├── ICPCScoreboard.py │ ├── IdentifyingTea.cpp │ ├── InBraille.py │ ├── IntervalProduct.cpp │ ├── IsoscelesTriangles.cpp │ ├── JingleComposing.cpp │ ├── JoinTwoKingdoms.cpp │ ├── Jollo.cpp │ ├── JumpingFrog.cpp │ ├── KeepItCovered.cpp │ ├── KingsPoker.cpp │ ├── KlingonLevels.cpp │ ├── LastDigit.cpp │ ├── Marblecoin.cpp │ ├── Necklace.cpp │ ├── OddOrEven.cpp │ ├── OpticalFiber.cpp │ ├── P-Networks.cpp │ ├── PascalLibrary.cpp │ ├── PolePosition.cpp │ ├── RegionFilling.cpp │ ├── ReportRecovery.cpp │ ├── StackingCylinders.cpp │ ├── Tornado.cpp │ ├── TravelingShoemakerProblem.cpp │ ├── Triangles.cpp │ ├── WeekendLottery.cpp │ └── WhereAreMyGenes.cpp ├── Kattis │ ├── AbstractArt.cpp │ ├── AddingWords.cpp │ ├── AddingWords.py │ ├── AllPairsShortestPath.cpp │ ├── Antiarithmetic.cpp │ ├── AssociationForConvexMainOffice.cpp │ ├── AssociationOfCatsAndMagicalLights.cpp │ ├── BigTruck.cpp │ ├── BiggestSlice.cpp │ ├── BirthdayParadox.cpp │ ├── Brackets.cpp │ ├── BuildDependencies.cpp │ ├── Cars.cpp │ ├── ChineeseRemainderTheorem.cpp │ ├── CountingStars.cpp │ ├── CountingStars.py │ ├── CountingTriangles.cpp │ ├── CrazyDriver.cpp │ ├── DistributingBallotBoxes.cpp │ ├── DistributingBallotBoxes.py │ ├── EvenUpSolitaire.cpp │ ├── FlightPlanEvaluation.cpp │ ├── ForestForTheTrees.cpp │ ├── FreightTrain.cpp │ ├── FreightTrain.py │ ├── GreedyPolygons.cpp │ ├── H-Index.cpp │ ├── H-Index.py │ ├── InCaseOfInvasion.cpp │ ├── IslandBuses.cpp │ ├── IsomorphicInversion.cpp │ ├── KeepingTheDogsApart.cpp │ ├── LimitedCorrespondence.cpp │ ├── LineSegmentIntersection.cpp │ ├── MagicalCows.cpp │ ├── MapColouring.cpp │ ├── MoneyForNothing.cpp │ ├── NeedForSpeed.cpp │ ├── NeedForSpeed.py │ ├── PizzaHawaii.cpp │ ├── PizzaHawaii.py │ ├── PolynomialMultiplication.cpp │ ├── PolynomialMultiplication.py │ ├── PrimeReduction.cpp │ ├── QuickEstimates.cpp │ ├── ReversedBinaryNumbers.cpp │ ├── ReversedBinaryNumbers.py │ ├── RocketPoweredHovercraft.cpp │ ├── RoundTrips.cpp │ ├── SecretChamberAtMountRushmore.cpp │ ├── StopCounting.cpp │ ├── SumOfTheOthers.cpp │ ├── SumOfTheOthers.py │ ├── SumOfTheOthers2.py │ ├── SurelyYouCongest.cpp │ ├── Tight-FitSudoku.cpp │ ├── TomoSynthesis.cpp │ ├── TouchscreenKeyboard.cpp │ ├── TouchscreenKeyboard.py │ ├── Tractor.cpp │ ├── TrapezoidWalkway.cpp │ ├── TreasureSpotting.cpp │ ├── Typo.cpp │ ├── UnDetected.cpp │ ├── Units.cpp │ ├── WipeYourWhiteboards.cpp │ ├── WitchDance.cpp │ ├── Zagrade.cpp │ └── Zagrade.py ├── LeetCode │ └── InsertDeleteGetRandom.cpp ├── Matcomgrader │ ├── BuildThePerfectHouse.cpp │ ├── CutInequalityDown.cpp │ ├── CutInequalityDown2.cpp │ ├── DazzlingStars.cpp │ ├── EggfruitCake.cpp │ ├── FabricatingSculptures.cpp │ ├── GluingPictures.cpp │ ├── GluingPicturesv2.cpp │ ├── JumpingGrasshopper.cpp │ ├── LeverageMDT.cpp │ └── MountainRanges.cpp ├── Otros │ ├── CatapultThatBall.cpp │ ├── ChefAndDivisorTree.cpp │ ├── ClosestDistance.cpp │ ├── DominoArt.cpp │ ├── ForestForTheTrees.cpp │ ├── Fundraising.cpp │ ├── JeopardisedJourney.cpp │ ├── LowEffortLeague.cpp │ ├── LowestCommonAncestor.cpp │ ├── LuckyDraw.cpp │ ├── MatrixSummation.cpp │ ├── MemoryAndTrident.cpp │ ├── MinimumSpanningTree.cpp │ ├── NCR.cpp │ ├── NeighborhoodWatch.cpp │ ├── NewYearTree.cpp │ ├── RangeMinimumQuery.cpp │ ├── Reklame.cpp │ ├── Staircases.cpp │ ├── StringSimilarity.cpp │ ├── SubMatrixSum.cpp │ ├── SumItUp.cpp │ ├── Union-Find.cpp │ ├── UpdateTheArray.cpp │ ├── WeirdPoints.cpp │ └── YetAnotherMultipleProblem.cpp ├── README.md ├── SPOJ │ ├── ALLIZZWELL.cpp │ ├── AProblemOfBacktracking.cpp │ ├── AdaAndFence.cpp │ ├── BMW.cpp │ ├── BoatBurglary.cpp │ ├── BoyScouts.cpp │ ├── BusquedaBinaria.cpp │ ├── BusquedaBinaria.py │ ├── BusquedaBinaria2.cpp │ ├── BusquedaBinaria2.py │ ├── CapitalCity.cpp │ ├── CellPhone.cpp │ ├── CleaningRobot.cpp │ ├── ConvexPolygons.cpp │ ├── CountOnATree.cpp │ ├── CountOnATreeII.cpp │ ├── D-Query.cpp │ ├── DYNACON1.cpp │ ├── DYNALCA.cpp │ ├── DinosaurMenace.cpp │ ├── Divisor.cpp │ ├── Divisors.cpp │ ├── DrawingTriangles.cpp │ ├── EfficientManaging.cpp │ ├── GMPlants.cpp │ ├── GoingToSchool.cpp │ ├── GoodTravels.cpp │ ├── ILKQUERY.cpp │ ├── InsideOrOutside.cpp │ ├── JustAddIt.cpp │ ├── K-thNumber.cpp │ ├── K12-BoredOfSuffixesAndPrefixes.cpp │ ├── K12-BuildingConstruction.cpp │ ├── LandAcquisition.cpp │ ├── LarguestRectangleInAHistogram.cpp │ ├── LeftRightOrCenter.cpp │ ├── LowestCommonAncestor.cpp │ ├── MST.cpp │ ├── MatrixSumation.cpp │ ├── Meteors.cpp │ ├── MinimumSpanningTree.cpp │ ├── Mixtures.cpp │ ├── NestedDolls.cpp │ ├── PrimeGenerator.cpp │ ├── PrimeIntervals.cpp │ ├── PrintingSomePrimes.cpp │ ├── QTREE.cpp │ ├── QTREE2.cpp │ ├── QTREE3.cpp │ ├── RangeMinimumQuery.cpp │ ├── SalaryManagement.cpp │ ├── ScubaDiver.cpp │ ├── Segments.cpp │ ├── SubmergingIslands.cpp │ ├── ToTheMoon.cpp │ ├── UpdateTheArray.cpp │ ├── WeirdFunction.cpp │ ├── WeirdPoints.cpp │ ├── WhoIsTheBoss.cpp │ ├── YetAnotherMultipleProblem.cpp │ └── YetAnotherSubsetSumProblem.cpp ├── Timus │ ├── Jobbery.cpp │ ├── MemoryManagement.cpp │ ├── SpaceologyVsChronistics.cpp │ ├── StringTale.cpp │ ├── Tree2.cpp │ └── Wall.cpp ├── URI │ ├── ArrangingHeaps.cpp │ ├── ArrangingTiles.cpp │ ├── AssigningTeams.cpp │ ├── BackToTheFuture.cpp │ ├── Balloon.cpp │ ├── BloggerLanguage.cpp │ ├── BuildingAField.cpp │ ├── CellphoneTyping.cpp │ ├── ComputerDJ.cpp │ ├── CountingOnes.cpp │ ├── CountingSelf-RotatingSubsets.cpp │ ├── DNASubsequences.cpp │ ├── DatabaseOfClients.cpp │ ├── DatingOn-Line.cpp │ ├── DifferentDigits.cpp │ ├── DigitsCount.cpp │ ├── DisjointWaterSupply.cpp │ ├── ElectricNeeds.cpp │ ├── Emoticons.cpp │ ├── EscapePolygon.cpp │ ├── FantasticBeasts.cpp │ ├── FarmRobot.cpp │ ├── FenceTheVegetables.cpp │ ├── FenceTheVegetablesFail.cpp │ ├── Feynman.cpp │ ├── FindingSeats.cpp │ ├── FixTheMaze.cpp │ ├── Football.cpp │ ├── GalacticTaxes.cpp │ ├── GalaxyCollision.cpp │ ├── GalouIsBack.cpp │ ├── GardenFence.cpp │ ├── GoUpTheUltras.cpp │ ├── HedgeMazes.cpp │ ├── HeightMap.cpp │ ├── HideAndSeek.cpp │ ├── HotelRewards.cpp │ ├── InkColors.cpp │ ├── InternetTrouble.cpp │ ├── InvertingHuffman.cpp │ ├── JupyterAttacks.cpp │ ├── JustInTime.cpp │ ├── KeepItEnergized.cpp │ ├── KillTheWerewolf.cpp │ ├── LazyJumpingFrog.cpp │ ├── LookingForTheRiskFactor.cpp │ ├── MountMarathon.cpp │ ├── OnionLayers.cpp │ └── SymmetricalPizza.cpp └── UVA │ ├── 8QueensChessProblem.cpp │ ├── AContagiousVirus.cpp │ ├── AhoyPirates.cpp │ ├── BuyOneGetTheRestFree.cpp │ ├── Buzzwords.cpp │ ├── CakeCut.cpp │ ├── CuttingSticks.cpp │ ├── DiccionarioPortunol.cpp │ ├── FreeParentheses.cpp │ ├── FrequentValues.cpp │ ├── GlassBeads.cpp │ ├── OptimalBinarySearchTree.cpp │ ├── PackingPolygons.cpp │ ├── Railway.cpp │ ├── SignOfMatrix.cpp │ ├── SoyaMilk.cpp │ ├── SunnyMountains.cpp │ ├── TheSkylineProblem.cpp │ ├── TheSultansProblem.cpp │ ├── TransportationSystem.cpp │ ├── TrashRemoval.cpp │ └── Whormholes.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | ## Ignora inputs, outputs y binarios 3 | 4 | A 5 | B 6 | *.txt 7 | in 8 | int 9 | out 10 | out1 11 | out2 12 | *.out 13 | .* 14 | test.* 15 | total.* 16 | generate.py 17 | submit.py 18 | Extras 19 | Contest 20 | ProjectEuler 21 | Facebook 22 | Google 23 | *.sh 24 | cf 25 | TW* 26 | gen.py 27 | -------------------------------------------------------------------------------- /Material/DP/DivideAndConquer.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | ll cost(int i, int j) { return ; } 4 | 5 | vl last, now; 6 | 7 | void compute(int l, int r, int optl, int optr) 8 | { 9 | if (l > r) return; 10 | 11 | int mid = (l + r) / 2; 12 | pair best = {cost(0, mid), -1}; 13 | 14 | repx(k, max(1, optl), min(mid, optr) + 1) 15 | best = min(best, {last[k - 1] + cost(k, mid), k}); 16 | 17 | now[mid] = best.ff; 18 | 19 | compute(l, mid - 1, optl, best.ss); 20 | compute(mid + 1, r, best.ss, optr); 21 | } 22 | -------------------------------------------------------------------------------- /Material/DP/KnuthOptimization.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int N; vi A; 4 | vvi DP, OPT; 5 | 6 | int main() 7 | { 8 | DP.assign(N + 1, vi(N + 1)); 9 | OPT.assign(N + 1, vi(N + 1)); 10 | 11 | rep(i, N) DP[i][i + 1] = A[i + 1] - A[i], OPT[i][i + 1] = i; 12 | 13 | repx(d, 2, N + 1) rep(l, N + 1 - d) 14 | { 15 | int r = l + d, l_ = OPT[l][r - 1], r_ = OPT[l + 1][r]; 16 | DP[l][r] = 1e9; 17 | repx(i, l_, r_ + 1) 18 | { 19 | int aux = DP[l][i] + DP[i][r] + A[r] - A[l]; 20 | if (aux < DP[l][r]) DP[l][r] = aux, OPT[l][r] = i; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Material/DP/LIS.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int LIS(vi &v) 4 | { 5 | vi L; int S = 0; 6 | for(int x : v) 7 | { 8 | int i = upper_bound(all(L), x) - L.begin(); 9 | if(i == S) L.pb(x), S++; 10 | else L[i] = x; 11 | } 12 | return S; 13 | } 14 | -------------------------------------------------------------------------------- /Material/Graphs/BellmanFord.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | const int INF = 1e9; 4 | 5 | struct Edge 6 | { 7 | int v, w; 8 | Edge(int v, int w) : v(v), w(w) {} 9 | }; 10 | 11 | int n; vi D; 12 | vector> G; 13 | 14 | bool bellmanFord(int s) 15 | { 16 | D.assign(n, INF); D[s] = 0; 17 | rep(i, n - 1) rep(j, n) for (Edge e : G[j]) 18 | D[e.v] = min(D[e.v], D[j] + e.w); 19 | 20 | bool neg = false; 21 | rep(i, n) for (Edge e : G[i]) if (D[e.v] > D[i] + e.w) neg = true; 22 | 23 | return neg; 24 | } 25 | -------------------------------------------------------------------------------- /Material/Graphs/Centroid.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int N, u, v; 4 | vvi G; 5 | vi R, S, P; 6 | 7 | int size(int u, int p) 8 | { 9 | S[u] = 1; 10 | for (int v : G[u]) if (v != p && !R[v]) S[u] += size(v, u); 11 | return S[u]; 12 | } 13 | 14 | int get_centroid(int u, int s, int p, int d) 15 | { 16 | for (int v : G[u]) if (v != p && !R[v] && S[v] * 2 > s) return get_centroid(v, s, u, d + 1); 17 | return u; 18 | } 19 | 20 | void build(int u, int p) 21 | { 22 | int c = get_centroid(u, size(u, -1), -1, 1); 23 | P[c] = p, R[c] = 1; 24 | for (int v : G[c]) if (!R[v]) build(v, c); 25 | } 26 | -------------------------------------------------------------------------------- /Material/Graphs/Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | const int INF = 1e9; 4 | 5 | struct Edge 6 | { 7 | int v, w; // CHECK FOR OVERFLOW 8 | Edge(int v, int w) : v(v), w(w) {} 9 | bool operator<(const Edge &e) const { return w > e.w; } 10 | }; 11 | 12 | int n; 13 | vector> G; 14 | 15 | int dijkstra(int s, int t) 16 | { 17 | vi C(n, INF); C[s] = 0; // CHECK FOR OVERFLOW 18 | priority_queue q; q.emplace(s, 0); 19 | while (!q.empty()) 20 | { 21 | int u = q.top().v, w = q.top().w; q.pop(); // CHECK FOR OVERFLOW 22 | if (C[u] < w) continue; 23 | for (auto e : G[u]) if (C[e.v] > e.w + w) 24 | C[e.v] = e.w + w, q.emplace(e.v, C[e.v]); 25 | } 26 | return C[t]; 27 | } 28 | -------------------------------------------------------------------------------- /Material/Graphs/FloydWarshall.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | const int INF = 1e9; 4 | 5 | int n; 6 | vvi D; 7 | 8 | //D is an adjacency matrix at the beginning 9 | void floydWarshall () 10 | { 11 | rep(k, n) rep(i, n) rep(j, n) if (D[i][k] < INF && D[k][j] < INF) 12 | D[i][j] = min(D[i][j], D[i][k] + D[k][j]); 13 | 14 | // opcional, si hay ciclos negativos 15 | rep(k, n) rep(i, n) rep(j, n) 16 | if (D[i][k] < INF && D[k][j] < INF && D[k][k] < 0) D[i][j] = -INF; 17 | } 18 | -------------------------------------------------------------------------------- /Material/Graphs/SCC.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int N, id; 4 | vvi G; // Directed Graph 5 | vi D, L, I; stack S; 6 | 7 | void dfs(int u) 8 | { 9 | D[u] = L[u] = id++, I[u] = 1; S.push(u); 10 | for (int v : G[u]) 11 | { 12 | if (D[v] == -1) { dfs(v); L[u] = min(L[v], L[u]); } 13 | else if (I[v]) L[u] = min(L[v], L[u]); 14 | } 15 | if (L[u] == D[u]) while (1) // SCC FOUND 16 | { 17 | int x = S.top(); S.pop(); I[x] = 0; 18 | if (x == u) break; 19 | } 20 | } 21 | 22 | void find_sccs() 23 | { 24 | D.assign(N, -1); L.resize(N); I.assign(N, 0); 25 | id = 0; rep(u, N) if (D[u] == -1) dfs(u); 26 | } 27 | -------------------------------------------------------------------------------- /Material/Graphs/Toposort.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int N; vi V, S; 4 | vvi G; 5 | 6 | void dfs(int u) 7 | { 8 | V[u] = 1; 9 | for (int v : G[u]) if (!V[v]) dfs(v); 10 | S.pb(u); 11 | } 12 | 13 | void topo_sort() 14 | { 15 | V.assign(N, 0); S.clear(); 16 | rep(i, N) if (!V[i]) dfs(i); 17 | } 18 | -------------------------------------------------------------------------------- /Material/Math/CRT.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | #include "Euclid.cpp" 3 | 4 | pll CRT(pll a, pll b) 5 | { 6 | if (a.ss < b.ss) swap(a, b); 7 | ll x, y; tie(x, y) = euclid(a.ss, b.ss); 8 | ll g = a.ss * x + b.ss * y, l = a.ss / g * b.ss; 9 | if ((b.ff - a.ff) % g) return {-1, -1}; // no solution 10 | x = (b.ff - a.ff) % b.ss * x % b.ss / g * a.ss + a.ff; 11 | return {x + (x < 0) * l, l}; 12 | } 13 | 14 | pll CRT(vector &v) 15 | { 16 | pll ans = v[0]; 17 | rep(i, sz(v)) if (i) ans = CRT(ans, v[i]); 18 | return ans; 19 | } 20 | -------------------------------------------------------------------------------- /Material/Math/Modular.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | const ll MOD = 1e9 + 7; 4 | 5 | inline ll add(ll x, ll y, ll M = MOD) { return (x + y) % M; } 6 | inline ll mul(ll x, ll y, ll M = MOD) { return (x * y) % M; } 7 | 8 | ll binPow(ll a, ll b, const ll M) 9 | { 10 | if (b == 0) return 1; 11 | ll r = binPow(a, b / 2, M); r = mul(r, r, M); 12 | return (b & 1) ? mul(r, a, M) : r; 13 | } 14 | 15 | // If multiplication goes over ll 16 | 17 | ul modMul(ul a, ul b, const ul M) 18 | { 19 | ll r = a * b - M * (ul)((db)a * b / M); 20 | return r + ((r < 0) - (r >= (ll)M)) * M; 21 | } 22 | 23 | ul modPow(ul a, ul b, const ul M) 24 | { 25 | if (b == 0) return 1; 26 | ul r = modPow(a, b / 2, M); r = modMul(r, r, M); 27 | return b & 1 ? modMul(r, a, M) : r; 28 | } 29 | -------------------------------------------------------------------------------- /Material/Math/ModularBinomial.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | #include "Euclid.cpp" 3 | #include "Modular.cpp" 4 | 5 | const ll MOD = 1e9 + 7; 6 | const int MAXN = 1000; 7 | 8 | // DP bottom-up 9 | ll C[MAXN + 1][MAXN + 1]; 10 | void init_C() 11 | { 12 | rep(m, MAXN + 1) if (m) 13 | { 14 | C[m][0] = C[m][m] = 1; 15 | rep(k, m) if (k) C[m][k] = add(C[m - 1][k], C[m - 1][k - 1]); 16 | } 17 | } 18 | 19 | // By definition and euclid 20 | ll F[MAXN]; 21 | ll choose(int n, int k) 22 | { 23 | return mul(F[n], minv(mul(F[k], F[n - k]), MOD)); 24 | } 25 | 26 | // Multinomial Coefficient 27 | ll multinomial(vector K) 28 | { 29 | ll n = 0, ans = 1; 30 | for (int k : K) n += k, ans = mul(ans, choose(n, k)); 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /Material/Math/Primality.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | #include "Modular.cpp" 3 | 4 | bool prime(ul n) 5 | { 6 | if (n < 2 || n % 6 % 4 != 1) return n - 2 < 2; 7 | ul A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; 8 | ul s = __builtin_ctzll(n - 1), d = n >> s; 9 | for (int a : A) 10 | { 11 | ul p = modPow(a, d, n), i = s; 12 | while (p != 1 && p != n - 1 && a % n && i--) p = modMul(p, p, n); 13 | if (p != n - 1 && i != s) return 0; 14 | } 15 | return 1; 16 | } 17 | -------------------------------------------------------------------------------- /Material/Math/Sieve.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | template struct Sieve 4 | { 5 | bitset I; vi P; 6 | Sieve() 7 | { 8 | I.set(); 9 | for (int i = 2; i * i < SZ; i++) if (I[i]) 10 | for (int j = i * i; j < SZ; j += i) I[j] = 0; 11 | rep(i, SZ) if (i > 1 && I[i]) P.pb(i); 12 | } 13 | }; 14 | 15 | Sieve<320000> S; 16 | -------------------------------------------------------------------------------- /Material/Strings/KMP.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int match(string &p, string &t) 4 | { 5 | int n = sz(p), m = sz(t), L[n]; L[0] = 0; 6 | rep(j, n - 1) 7 | { 8 | int i = L[j]; while (p[i] != p[j + 1] && i) i = L[i - 1]; 9 | L[j + 1] = (p[i] == p[j + 1] ? i + 1 : 0); 10 | } 11 | int ans = 0, i = 0; rep(j, m) 12 | { 13 | while (p[i] != t[j] && i) i = L[i - 1]; 14 | if (p[i] == t[j] && ++i == n) i = L[n - 1], ans++; 15 | } 16 | return ans; 17 | } 18 | -------------------------------------------------------------------------------- /Material/Strings/Manacher.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int n; 4 | string s; 5 | 6 | int main() 7 | { 8 | vi d1(n); // odd sized palindromes 9 | for (int i = 0, l = 0, r = -1; i < n; i++) { 10 | int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); 11 | while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) k++; 12 | d1[i] = k--; 13 | if (i + k > r) l = i - k, r = i + k; 14 | } 15 | vi d2(n); // even sized palindromes (center to the right) 16 | for (int i = 0, l = 0, r = -1; i < n; i++) { 17 | int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1); 18 | while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) k++; 19 | d2[i] = k--; 20 | if (i + k > r) l = i - k - 1, r = i + k; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Material/Structures/FenwickTree.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | // 1 - indexed / inclusive - inclusive 4 | 5 | // Queries puntuales y Updates por rango 6 | // xor 7 | // Queries por rango y Updates puntuales 8 | struct FT 9 | { 10 | vi t; 11 | FT(int N) { t.resize(N + 1, 0); } 12 | int query(int i) 13 | { 14 | int ans = 0; 15 | for (; i; i -= i & (-i)) ans += t[i]; 16 | return ans; 17 | } 18 | int query(int i, int j) { return query(j) - query(i - 1); } 19 | void update(int i, int v) 20 | { 21 | int s = query(i, i); // Sets 22 | for (; i < sz(t); i += i & (-i)) t[i] += v - s; 23 | } 24 | void update(int i, int j, int v) { update(i, v); update(j + 1, -v); } 25 | }; 26 | -------------------------------------------------------------------------------- /Material/Structures/MoQueries.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | 3 | int S; // Size of buckets (generally sqrt(N)) 4 | 5 | struct Query 6 | { 7 | int l, r, id; 8 | Query(int l, int r, int id) : l(l), r(r), id(id) {} 9 | bool operator<(Query &q) 10 | { 11 | return l / S < q.l / S or (l / S == q.l / S and r < q.r); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Material/Structures/PolicyBased.cpp: -------------------------------------------------------------------------------- 1 | #include "../Template.cpp" 2 | #include 3 | #include 4 | 5 | using namespace __gnu_pbds; 6 | 7 | template > 8 | using super_set = tree; 10 | 11 | // order_of_key(T x) 12 | // -> returns the number of elements strictly smaller than x 13 | 14 | // find_by_order(size_t i) 15 | // -> returns iterator to i-th largest element (counting from 0) 16 | -------------------------------------------------------------------------------- /Material/notebook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BenjaminRubio/CompetitiveProgramming/866c9ead9f7682707003f0047d17ed5f3e834758/Material/notebook.pdf -------------------------------------------------------------------------------- /Problems/AtCoder/123Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define rep_(i, n) for (int i = (int)n - 1; i >= 0; i--) 6 | 7 | int N; char aux; bool flag; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | flag = 0; 15 | A.resize(N); 16 | rep(i, N) 17 | { 18 | cin >> aux; 19 | A[i] = aux - '1'; 20 | if (A[i] == 1) flag = 1; 21 | } 22 | 23 | rep(i, N) if (A[i] == 2) A[i] = !flag; 24 | 25 | rep_(i, 32) if ((N - 1) & (1 << i)) 26 | { 27 | rep(j, N - (1 << i)) A[j] = A[j] ^ A[j + (1 << i)]; 28 | N -= (1 << i); 29 | } 30 | 31 | if (flag) cout << A[0] << '\n'; 32 | else cout << A[0] * 2 << '\n'; 33 | } -------------------------------------------------------------------------------- /Problems/AtCoder/BuyAnInteger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | ll A, B, X; 7 | 8 | ll digits(ll x) 9 | { 10 | ll ans = 0; 11 | while (x > 0) 12 | { 13 | x /= 10; 14 | ans++; 15 | } 16 | return ans; 17 | } 18 | 19 | int main() 20 | { 21 | cin >> A >> B >> X; 22 | 23 | ll i = 0; 24 | ll j = 1e9 + 1; 25 | 26 | while (i < j) 27 | { 28 | ll m = (i + j) >> 1; 29 | if (A * m + B * digits(m) > X) 30 | j = m; 31 | else 32 | i = m + 1; 33 | } 34 | 35 | cout << i - 1 << '\n'; 36 | } -------------------------------------------------------------------------------- /Problems/AtCoder/CantWaitForHoliday.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string day; 5 | map mapping = {{"MON", 6}, {"TUE", 5}, {"WED", 4}, {"THU", 3}, {"FRI", 2}, {"SAT", 1}, {"SUN", 7}}; 6 | 7 | int main() 8 | { 9 | cin >> day; 10 | cout << mapping[day] << '\n'; 11 | } -------------------------------------------------------------------------------- /Problems/AtCoder/FortuneCookies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int A[4]; 7 | 8 | int main() 9 | { 10 | rep(i, 4) cin >> A[i]; 11 | 12 | if ((A[0] == A[1] + A[2] + A[3]) || (A[1] == A[0] + A[2] + A[3]) || 13 | (A[2] == A[0] + A[1] + A[3]) || (A[3] == A[0] + A[1] + A[2])) cout << "Yes\n"; 14 | else if ((A[0] + A[1] == A[2] + A[3]) || (A[0] + A[2] == A[1] + A[3]) || 15 | (A[1] + A[2] == A[0] + A[3])) cout << "Yes\n"; 16 | else cout << "No\n"; 17 | } -------------------------------------------------------------------------------- /Problems/AtCoder/MAX_min.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> N; 12 | 13 | A.resize(N); 14 | rep(i, N) cin >> A[i]; 15 | 16 | while (1) 17 | { 18 | int m = INT_MAX, M = INT_MIN; 19 | rep(i, N) m = min(A[i], m), M = max(A[i], M); 20 | 21 | if (m == M) { cout << m << '\n'; return 0; } 22 | 23 | rep(i, N) A[i] = (A[i] % m ? A[i] % m : m); 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/AtCoder/RotN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | string s; 6 | 7 | int main() 8 | { 9 | cin >> n >> s; 10 | for (char c : s) 11 | { 12 | cout << char(((c - 'A') + n) % 26 + 'A'); 13 | } 14 | cout << '\n'; 15 | } -------------------------------------------------------------------------------- /Problems/CodeChef/IsThisJEE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | const double PI = acos(-1.0L); 7 | 8 | int T; 9 | double b, c; 10 | 11 | double f(double x) 12 | { 13 | return (x * x + b * x + c) / sin(x); 14 | } 15 | 16 | int main() 17 | { 18 | cin >> T; 19 | while (T--) 20 | { 21 | cin >> b >> c; 22 | 23 | double l = 0, r = .5 * PI; 24 | rep(_, 100) 25 | { 26 | double t = (r - l) / 3, m1 = l + t, m2 = r - t; 27 | 28 | double f1 = f(m1), f2 = f(m2); 29 | if (f1 <= f2) r = m2; 30 | if (f1 >= f2) l = m1; 31 | } 32 | 33 | cout.precision(10); 34 | cout << f((l + r) * .5) << '\n'; 35 | } 36 | } -------------------------------------------------------------------------------- /Problems/Codeforces/01Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string s; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> s; 15 | int o = 0, c = 0; 16 | for (char ch : s) 17 | { 18 | if (ch == '1') o++; 19 | else c++; 20 | } 21 | 22 | if (min(o, c) % 2) cout << "DA\n"; 23 | else cout << "NET\n"; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ABBB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string S; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> S; 15 | 16 | int c = 0, b = 0, ans = 0; 17 | for (char ch : S) 18 | { 19 | if (ch == 'A') c++; 20 | if (ch == 'B') 21 | { 22 | if (c) ans += 2, c--; 23 | else b++; 24 | } 25 | } 26 | ans += 2 * (b / 2); 27 | 28 | cout << (int)S.size() - ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ABBalance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T; 5 | string S; 6 | 7 | int main() 8 | { 9 | cin >> T; 10 | while (T--) 11 | { 12 | cin >> S; 13 | 14 | int N = S.size(); 15 | for (int i = 0; i < N - 1; i++) cout << S[i]; 16 | cout << S[0] << '\n'; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Problems/Codeforces/ABBalance.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | 5 | S = input() 6 | N = len(S) 7 | 8 | print(S[:N - 1] + S[0]) 9 | -------------------------------------------------------------------------------- /Problems/Codeforces/ACookieForYou.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll T, v, c, n, m; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> v >> c >> n >> m; 15 | 16 | if (v > c) swap(v, c); 17 | 18 | if (v < m) { cout << "No\n"; continue; } 19 | v -= m; 20 | 21 | if (v + c >= n) cout << "Yes\n"; 22 | else cout << "No\n"; 23 | } 24 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AND0SumBig.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | const ll MOD = 1e9 + 7; 8 | 9 | inline ll add(ll a, ll b) { return (a + b) % MOD; } 10 | inline ll mul(ll a, ll b) { return (a * b) % MOD; } 11 | 12 | ll T, N, K; 13 | 14 | int main() 15 | { 16 | cin >> T; 17 | while (T--) 18 | { 19 | cin >> N >> K; 20 | 21 | ll ans = 1; 22 | rep(i, K) ans = mul(ans, N); 23 | 24 | cout << ans << '\n'; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AcurateLee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define repx(i, a, b) for (int i = (int)a; i < (int)b; i++) 6 | 7 | int T, N; 8 | string s; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N >> s; 16 | 17 | int c = 0; 18 | while (c < s.size() && s[c] == '0') c++; 19 | int o = 0; 20 | 21 | bool p = 0; 22 | repx(i, c, N) if (s[i] == '0') p = 1; 23 | 24 | while (N - o - 1 >= 0 && s[N - o - 1] == '1') o++; 25 | 26 | rep(i, c + p) cout << '0'; 27 | rep(i, o) cout << '1'; 28 | cout << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AlarmClock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, a, b, c, d; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> a >> b >> c >> d; 15 | 16 | if (b >= a) { cout << b << '\n'; continue; } 17 | 18 | if (d >= c) { cout << -1 << '\n'; continue; } 19 | 20 | ll d_ = (a - b) / (c - d); 21 | ll ans = b + (d_ + ll(bool((a - b) % (c - d)))) * c; 22 | cout << ans << '\n'; 23 | } 24 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AlternatingSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T, N; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | A.resize(N); rep(i, N) cin >> A[i]; 18 | 19 | ll ans = 0; 20 | 21 | int i = 0, j = 0; 22 | while (i < N) 23 | { 24 | while (j < N && (A[j] > 0) == (A[i] > 0)) j++; 25 | 26 | ll aux = INT_MIN; 27 | for (int k = i; k < j; k++) aux = max(aux, (ll)A[k]); 28 | 29 | ans += aux; 30 | i = j; 31 | } 32 | 33 | cout << ans << '\n'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Problems/Codeforces/AngryStudents.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, k; 5 | char type; 6 | 7 | int main() 8 | { 9 | cin >> T; 10 | while (T--) 11 | { 12 | cin >> k; 13 | int ans = 0; 14 | int aux = -1; 15 | while (k--) 16 | { 17 | cin >> type; 18 | if (type == 'A') 19 | { 20 | ans = max(ans, aux); 21 | aux = 0; 22 | } 23 | else if (type == 'P' && aux != -1) 24 | aux++; 25 | } 26 | ans = max(ans, aux); 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AnuHasAFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n; 7 | vector arr; 8 | vector b(31, 0); 9 | int b_ = 0; 10 | 11 | int main() 12 | { 13 | cin >> n; 14 | 15 | arr.resize(n); 16 | rep(i, n) cin >> arr[i]; 17 | 18 | rep(i, n) rep(j, 31) if ((1 << j) & arr[i]) 19 | { 20 | b[j]++; 21 | if (b[j] == 2) b_ |= (1 << j); 22 | } 23 | 24 | int M = -1, aux; 25 | rep(i, n) 26 | { 27 | int c = (arr[i] | b_) - b_; 28 | 29 | if (c > M) 30 | { 31 | M = c; 32 | aux = i; 33 | } 34 | } 35 | 36 | cout << arr[aux] << ' '; 37 | rep(i, n) if (i != aux) cout << arr[i] << ' '; 38 | cout << '\n'; 39 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ArrayAndPeaks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, K; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> K; 14 | if (K > ((N / 2) - !(N & 1))) { cout << -1 << '\n'; continue; } 15 | 16 | int i = 1, j = N; 17 | while (K--) 18 | { 19 | cout << i << ' ' << j << ' '; 20 | i++, j--; 21 | } 22 | 23 | for (int x = i; x <= j; x++) cout << x << ' '; 24 | cout << '\n'; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ArrayCancelation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T, N; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | A.resize(N); 18 | rep(i, N) cin >> A[i]; 19 | 20 | ll ans = LLONG_MAX, s = 0; 21 | rep(i, N) s += A[i], ans = min(ans, s); 22 | 23 | cout << -ans << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ArraysSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, K, a; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> K; 14 | 15 | set s; 16 | rep(i, N) { cin >> a; s.insert(a); } 17 | 18 | if (K == 1) { (s.size() == 1 ? cout << 1 << '\n' : cout << -1 << '\n'); continue; } 19 | 20 | int S = s.size(); 21 | 22 | if (S <= K) { cout << 1 << '\n'; continue; } 23 | S -= K; 24 | 25 | cout << 2 + ((S - 1) / (K - 1)) << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AssigningToClasses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | vector arr, arr1, arr2; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> n; 15 | 16 | arr.resize(2 * n); 17 | rep(i, 2 * n) cin >> arr[i]; 18 | 19 | sort(arr.begin(), arr.end()); 20 | 21 | int ans = abs(arr[n - 1] - arr[n]); 22 | 23 | cout << ans << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AvoidTrygub.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | string a; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N >> a; 15 | 16 | sort(a.begin(), a.end()); 17 | 18 | cout << a << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/AyoubsFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, n, m; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> n >> m; 15 | 16 | ll b = min(m + 1, n - m); 17 | 18 | if (b == 0) 19 | { 20 | cout << (n * (n + 1)) / 2 << '\n'; 21 | continue; 22 | } 23 | 24 | ll d = (n - m) / b; 25 | ll r = (n - m) % b; 26 | 27 | // cerr << b - r << ' ' << r << '\n'; 28 | 29 | ll ans = (n * (n + 1)) / 2; 30 | ans -= (b - r) * (d * (d + 1)) / 2; 31 | ans -= r * ((d + 1) * (d + 2)) / 2; 32 | 33 | cout << ans << '\n'; 34 | } 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BadTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | #define repx(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) 7 | 8 | ll T, N; 9 | vector A; 10 | 11 | int main() 12 | { 13 | cin >> T; 14 | while (T--) 15 | { 16 | cin >> N; 17 | 18 | A.resize(N); 19 | rep(i, N) cin >> A[i]; 20 | 21 | repx(i, 2, N) if (A[0] + A[1] <= A[i]) 22 | { cout << 1 << ' ' << 2 << ' ' << i + 1 << '\n'; goto next; } 23 | 24 | cout << -1 << '\n'; 25 | 26 | next: 27 | continue; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BadUglyNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(_, T) 12 | { 13 | cin >> n; 14 | if (n == 1) { cout << -1 << '\n'; continue; } 15 | else 16 | { 17 | cout << 2; 18 | rep(i, n - 1) cout << 3; 19 | cout << '\n'; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BeautifulNumbers_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef vector::iterator iter; 6 | 7 | int T, n, x; 8 | vector p; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> n; 16 | p.resize(n); 17 | rep(i, n) 18 | { 19 | cin >> x; 20 | p[x - 1] = i; 21 | } 22 | 23 | int m = 1e9; 24 | int M = -1; 25 | rep(i, n) 26 | { 27 | m = min(m, p[i]); 28 | M = max(M, p[i]); 29 | 30 | if (M - m == i) 31 | cout << 1; 32 | else 33 | cout << 0; 34 | } 35 | cout << '\n'; 36 | } 37 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BeltedRooms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; char ch; 7 | vector B; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | bool l = 0, r = 0; 17 | B.assign(N, 0); 18 | rep(i, N) 19 | { 20 | cin >> ch; 21 | if (ch == '<') B[i] = -1, l = 1; 22 | if (ch == '>') B[i] = 1, r = 1; 23 | } 24 | 25 | int ans = 0; 26 | rep(i, N) ans += (!B[i] || !B[(i - 1 + N) % N] || (B[i] == B[(i - 1 + N) % N] && !(l && r))); 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BerlandPoker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, m, k; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n >> m >> k; 14 | 15 | int M = min(m, n / k); 16 | int r = (m - M) / (k - 1) + bool((m - M) % (k - 1)); 17 | cout << M - r << '\n'; 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BoboniuAndBitOperations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, M; 7 | vector a, b; 8 | 9 | int DP[200][512]; 10 | 11 | int dp(int i, int o) 12 | { 13 | if (i == N) return 0; 14 | if (DP[i][o] != -1) return DP[i][o]; 15 | 16 | int ans = 1e9; 17 | rep(j, M) ans = min(ans, o | (a[i] & b[j]) | dp(i + 1, o | (a[i] & b[j]))); 18 | return DP[i][o] = ans; 19 | } 20 | 21 | int main() 22 | { 23 | cin >> N >> M; 24 | 25 | a.resize(N); 26 | rep(i, N) cin >> a[i]; 27 | 28 | b.resize(M); 29 | rep(i, M) cin >> b[i]; 30 | 31 | memset(DP, -1, sizeof DP); 32 | 33 | cout << dp(0, 0) << '\n'; 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BoboniuLikesToColorBalls.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0ll; i < (ll)n; i++) 6 | 7 | ll T, r, g, b, w; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> r >> g >> b >> w; 15 | 16 | if ((r + g + b + w) % 2ll == 1ll) 17 | { 18 | ll s = (r % 2ll) + (g % 2ll) + (b % 2ll) + (w % 2ll); 19 | if (s == 1ll || (min({r, g, b}) > 0 && s == 3ll)) { cout << "Yes\n"; continue; } 20 | } 21 | else 22 | { 23 | ll s = (r % 2ll) + (g % 2ll) + (b % 2ll) + (w % 2ll); 24 | if (s == 0ll || (min({r, g, b}) > 0 && s == 4ll)) { cout << "Yes\n"; continue; } 25 | } 26 | cout << "No" << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BovineDilemma.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, X[50]; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | rep(i, N) cin >> X[i]; 15 | 16 | set s; 17 | rep(i, N) rep(j, N) if (i != j) s.insert(abs(X[i] - X[j])); 18 | 19 | cout << s.size() << '\n'; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/BoxIsPull.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, a, b, c, d; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> a >> b >> c >> d; 14 | 15 | if (a == c || b == d) cout << abs(a - c) + abs(b - d) << '\n'; 16 | else cout << abs(a - c) + abs(b - d) + 2 << '\n'; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/C+=.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T, a, b, n; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> a >> b >> n; 15 | 16 | if (a > b) swap(a, b); 17 | 18 | int x = max(a, b); 19 | 20 | int cnt = 0; 21 | while (n >= x) 22 | { 23 | if (cnt & 1) b += a; 24 | else a += b; 25 | x = max(a, b); 26 | cnt++; 27 | } 28 | 29 | cout << cnt << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CandiesDivision.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, n, k; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | while (T--) 10 | { 11 | cin >> n >> k; 12 | 13 | if (n % k > k / 2) 14 | cout << n / k * k + k / 2 << '\n'; 15 | else 16 | cout << n << '\n'; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CaninePoetry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | string S; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> S; N = S.size(); 15 | 16 | int ll = -1, l = -1, b = 0, ans = 0; 17 | rep(i, N) 18 | { 19 | if (S[i] == ll || (!b && S[i] == l)) 20 | { 21 | ans++, b = 1; 22 | ll = l, l = -1; 23 | continue; 24 | } 25 | if (b) b = 0; 26 | ll = l, l = S[i]; 27 | } 28 | 29 | cout << ans << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CaptainFlintAndCrewRecruitment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | int aux = N - 2 * (3 + 5 + 7); 15 | if (aux > 0 && aux != 6 && aux != 10 && aux != 14) 16 | { 17 | cout << "YES\n"; 18 | cout << "6 10 14" << ' ' << aux << '\n'; 19 | } 20 | else if (aux > 1) 21 | { 22 | cout << "YES\n"; 23 | cout << "6 10 15" << ' ' << aux - 1 << '\n'; 24 | } 25 | else cout << "NO\n"; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CaptainFlintAndLongVoyage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | int x = (N - 1) / 4 + 1; 15 | rep(i, N - x) cout << 9; 16 | rep(i, x) cout << 8; 17 | cout << '\n'; 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CardConstructions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, n; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n; 15 | 16 | ll ans = 0; 17 | while (n > 1) 18 | { 19 | ll h = 1; 20 | while ((3ll * (h * (h + 1ll)) / 2ll - h) <= n) h++; 21 | h--; 22 | n -= (3ll * (h * (h + 1ll)) / 2ll - h); 23 | ans++; 24 | } 25 | cout << ans << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CardGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, k1, k2, x; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n >> k1 >> k2; 14 | 15 | bool first = false; 16 | rep(i, k1) 17 | { 18 | cin >> x; 19 | if (x == n) 20 | first = true; 21 | } 22 | 23 | rep(i, k2) cin >> x; 24 | 25 | if (first) 26 | cout << "YES\n"; 27 | else 28 | cout << "NO\n"; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CelexUpdate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | long long T, x, y, x_, y_; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> x >> y >> x_ >> y_; 14 | 15 | cout << (x_ - x) * (y_ - y) + 1ll << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CenterAlignment.py: -------------------------------------------------------------------------------- 1 | 2 | m = [] 3 | max_size = 0 4 | 5 | while True: 6 | 7 | try: 8 | line = input() 9 | except: 10 | break 11 | 12 | line = line.strip() 13 | max_size = max(max_size, len(line)) 14 | m.append(line) 15 | 16 | size = max_size 17 | 18 | print("*" * (size + 2)) 19 | 20 | i = 0 21 | for line in m: 22 | s = "*" 23 | 24 | spaces = size - len(line) 25 | 26 | if spaces % 2 == 1 and i % 2 == 1: 27 | spacel = spaces // 2 + 1 28 | else: 29 | spacel = spaces // 2 30 | 31 | s += (" " * spacel) 32 | s += line 33 | s += (" " * (spaces - spacel)) 34 | s += "*" 35 | print(s) 36 | 37 | if spaces % 2 == 1: 38 | i += 1 39 | 40 | print("*" * (size + 2)) 41 | -------------------------------------------------------------------------------- /Problems/Codeforces/CircleGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll T, D, K; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> D >> K; 15 | 16 | ll r = 0; 17 | while (2ll * (K * r) * (K * r) <= D * D) r++; 18 | r--; 19 | if ((K * r) * (K * r) + (K * (r + 1ll)) * (K * (r + 1ll)) <= D * D) 20 | cout << "Ashish\n"; 21 | else cout << "Utkarsh\n"; 22 | } 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ClosingTheGap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, N, A; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | while (T--) 10 | { 11 | cin >> N; 12 | 13 | int S = 0; 14 | for (int i = 0; i < N; i++) 15 | { 16 | cin >> A; 17 | S += A; 18 | } 19 | 20 | cout << bool(S % N) << '\n'; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Problems/Codeforces/ClosingTheGap.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | 5 | N = int(input()) 6 | A = list(map(int, input().split())) 7 | 8 | S = sum(A) 9 | 10 | print(int(bool(S % N))) 11 | -------------------------------------------------------------------------------- /Problems/Codeforces/CodeforcesSubsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll K; 8 | string base = "codeforces"; 9 | 10 | int main() 11 | { 12 | cin >> K; 13 | 14 | ll C = 1, R = 0, curr = 1; 15 | while (C < K) 16 | { 17 | C += C / curr; 18 | R++; 19 | if (R % 10 == 0) curr++; 20 | } 21 | 22 | rep(i, 10) 23 | { 24 | rep(_, curr + bool(R % 10)) cout << base[i]; 25 | if (bool(R % 10)) R--; 26 | } 27 | cout << '\n'; 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Colorblindness.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, N; 5 | string A, B; 6 | 7 | int main() 8 | { 9 | cin >> T; 10 | while (T--) 11 | { 12 | cin >> N >> A >> B; 13 | bool ans = true; 14 | for (int i = 0; i < N; i++) 15 | { 16 | if ((A[i] == 'R' || B[i] == 'R') && A[i] != B[i]) ans = false; 17 | } 18 | if (ans) cout << "YES\n"; 19 | else cout << "NO\n"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Problems/Codeforces/Colorblindness.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | 5 | N = int(input()) 6 | A = input() 7 | B = input() 8 | 9 | ans = True 10 | for i in range(N): 11 | if (A[i] == 'R' or B[i] == 'R') and A[i] != B[i]: 12 | ans = False 13 | 14 | if ans: 15 | print("YES") 16 | else: 17 | print("NO") 18 | -------------------------------------------------------------------------------- /Problems/Codeforces/CompetitiveProgrammer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | string num; 6 | int sum; 7 | int par; 8 | int zeros; 9 | 10 | int main() 11 | { 12 | cin >> n; 13 | 14 | while (n--) 15 | { 16 | cin >> num; 17 | 18 | sum = 0; 19 | zeros = 0; 20 | par = 0; 21 | for (char c : num) 22 | { 23 | sum += c - '0'; 24 | if (c == '0') 25 | zeros++; 26 | else if ((c - '0') % 2 == 0) 27 | par++; 28 | } 29 | if (zeros > 1) 30 | par++; 31 | 32 | if (zeros && par && sum % 3 == 0) 33 | cout << "red\n"; 34 | else 35 | cout << "cyan\n"; 36 | } 37 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ContestForRobots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n; 7 | vector r1, r2; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | r1.resize(n); 14 | rep(i, n) cin >> r1[i]; 15 | r2.resize(n); 16 | rep(i, n) cin >> r2[i]; 17 | 18 | int aux1 = 0, aux2 = 0; 19 | rep(i, n) 20 | { 21 | if (r1[i] and !r2[i]) aux1++; 22 | if (!r1[i] and r2[i]) aux2++; 23 | } aux2++; 24 | 25 | if (!aux1) 26 | { 27 | cout << -1 << '\n'; 28 | return 0; 29 | } 30 | 31 | int d = aux2 / aux1; 32 | if (d * aux1 != aux2) cout << d + 1 << '\n'; 33 | else cout << d << '\n'; 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CopyCopyCopyCopyCopy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, x; 7 | set s; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(_, T) 13 | { 14 | cin >> n; 15 | 16 | rep(i, n) 17 | { 18 | cin >> x; 19 | s.insert(x); 20 | } 21 | 22 | cout << s.size() << '\n'; 23 | s.clear(); 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CountTheBlocks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | const ll MOD = 998244353; 8 | 9 | int N; 10 | ll p[200000 + 1]; 11 | 12 | int main() 13 | { 14 | p[0] = 1; 15 | rep(i, 200000) p[i + 1] = (p[i] * 10) % MOD; 16 | 17 | cin >> N; 18 | 19 | ll sum1 = 0, sum2 = 0; 20 | vector ans; 21 | rep(i, N) 22 | { 23 | ll tot = ((i + 1) * p[i + 1]) % MOD; 24 | ans.push_back((tot - sum2 + MOD) % MOD); 25 | sum1 = (sum1 + (tot - sum2 + MOD) % MOD) % MOD; 26 | sum2 = (sum1 + tot) % MOD; 27 | } 28 | rep(i, N) cout << ans[N - i - 1] << ' '; 29 | cout << '\n'; 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CountingOrders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, p, v, k; 7 | vector> g; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | g.resize(n); 14 | rep(i, n - 1) 15 | { 16 | cin >> p; 17 | g[p - 1].push_back(i); 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CowAndFriend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | int T; 8 | ll n, x, M, a; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | rep(_, T) 14 | { 15 | cin >> n >> x; 16 | 17 | M = -1; 18 | ll ans = LLONG_MAX; 19 | rep(i, n) 20 | { 21 | cin >> a; 22 | ll aux = x / a; 23 | if (aux) ans = min(ans, aux + (ll)bool(x % a)); 24 | else ans = min(2LL, ans); 25 | } 26 | 27 | cout << ans << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CowAndHaybales.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, d; 7 | vector a; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(_, T) 13 | { 14 | cin >> n >> d; 15 | 16 | a.resize(n); 17 | rep(i, n) cin >> a[i]; 18 | 19 | int ans = a[0], aux = 0; 20 | rep(i, n) 21 | { 22 | if (!i) continue; 23 | int c = min(a[i], (d - aux) / i); 24 | aux += c * i; 25 | ans += c; 26 | } 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CowAndMessage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | string s; 8 | vector c(26, 0); 9 | vector> aux; 10 | 11 | int main() 12 | { 13 | cin >> s; 14 | int n = s.size(); 15 | 16 | aux.assign(n, {}); 17 | rep(i, n) 18 | { 19 | c[s[i] - 'a']++; 20 | aux[i] = c; 21 | } 22 | 23 | ll ans = 0; 24 | rep(i, 26) ans = max(ans, c[i]); 25 | 26 | rep(i, 26) rep(j, 26) 27 | { 28 | ll partial = 0; 29 | rep(k, n) if (s[k] == char(j + 'a')) partial += (aux[k][i] - (i == j)); 30 | ans = max(ans, partial); 31 | } 32 | 33 | cout << ans << '\n'; 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CubesSorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, a, p; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | p = INT_MAX; 16 | bool ans = 0; 17 | rep(i, N) 18 | { 19 | cin >> a; 20 | if (a >= p) ans = 1; 21 | p = a; 22 | } 23 | 24 | if (ans) cout << "YES\n"; 25 | else cout << "NO\n"; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/CyclicPermutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | const ll MOD = 1e9 + 7; 8 | 9 | ll N; 10 | 11 | int main() 12 | { 13 | cin >> N; 14 | 15 | ll ans = 1ll; 16 | rep(i, N) ans = (ans * (i + 1ll)) % MOD; 17 | 18 | ll res = 1; 19 | rep(i, N - 1) res = (res * 2ll) % MOD; 20 | 21 | cout << ((ans - res) % MOD + MOD) % MOD << '\n'; 22 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DeadPixel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, a, b, x, y; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(_, T) 12 | { 13 | cin >> a >> b >> x >> y; 14 | 15 | cout << max({x * b, y * a, (a - x - 1) * b, (b - y - 1) * a}) << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Deadline.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, d; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | 12 | while (T--) 13 | { 14 | cin >> n >> d; 15 | 16 | bool done = false; 17 | 18 | rep(i, int(sqrt(d))) 19 | { 20 | int ans = i + (int)ceil((double)d / (double)(i + 1)); 21 | if (ans <= n) 22 | { 23 | done = true; 24 | break; 25 | } 26 | } 27 | 28 | if (done) 29 | cout << "YES\n"; 30 | else 31 | cout << "NO\n"; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DevuAndHisBrother.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int N, M; 8 | vector A, B; 9 | 10 | ll calc(ll m) 11 | { 12 | ll ans = 0; 13 | rep(i, N) ans += max(0ll, m - A[i]); 14 | rep(i, M) ans += max(0ll, B[i] - m); 15 | return ans; 16 | } 17 | 18 | int main() 19 | { 20 | cin >> N >> M; 21 | 22 | A.resize(N); rep(i, N) cin >> A[i]; 23 | B.resize(M); rep(i, M) cin >> B[i]; 24 | 25 | ll l = 0, r = 1e9; 26 | while (l < r) 27 | { 28 | ll m = (l + r) / 2; 29 | 30 | if (calc(m) <= calc(m + 1)) r = m; 31 | else l = m + 1; 32 | } 33 | 34 | cout << calc(l) << '\n'; 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DiceTower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T; 5 | long long x; 6 | 7 | int main() 8 | { 9 | cin >> T; 10 | while (T--) 11 | { 12 | cin >> x; 13 | if (x % 14LL < 7LL && x % 14LL > 0 && x > 14LL) 14 | cout << "YES\n"; 15 | else 16 | cout << "NO\n"; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DistanceAndAxis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, K; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> K; 14 | if (N <= K) cout << K - N << '\n'; 15 | else if ((N & 1) + (K & 1) == 1) cout << 1 << '\n'; 16 | else cout << 0 << '\n'; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DiverseMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int r, c; 7 | 8 | int main() 9 | { 10 | cin >> r >> c; 11 | if (r == 1 && c == 1) 12 | { 13 | cout << 0 << '\n'; 14 | return 0; 15 | } 16 | if (r <= c) 17 | { 18 | rep(i, r) 19 | { 20 | rep(j, c) cout << (i + 1) * (r + j + 1) << ' '; 21 | cout << '\n'; 22 | } 23 | } 24 | else 25 | { 26 | rep(i, r) 27 | { 28 | rep(j, c) cout << (c + i + 1) * (j + 1) << ' '; 29 | cout << '\n'; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DivideAndMultiply.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | 5 | ll T, N; 6 | vector A; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | A.resize(N); 16 | for (int i = 0; i < N; i++) cin >> A[i]; 17 | 18 | ll cnt = 0; 19 | for (int i = 0; i < N; i++) 20 | { 21 | while (A[i] % 2 == 0) A[i] /= 2, cnt++; 22 | } 23 | 24 | ll M = *max_element(A.begin(), A.end()); 25 | while (cnt--) M *= 2ll; 26 | 27 | ll ans = M; 28 | for (int i = 0; i < N; i++) ans += A[i]; 29 | ans -= *max_element(A.begin(), A.end()); 30 | 31 | cout << ans << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DivideAndMultiply.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | 5 | N = int(input()) 6 | A = list(map(int, input().split())) 7 | 8 | cnt = 0 9 | for i in range(N): 10 | while A[i] % 2 == 0: 11 | A[i] //= 2 12 | cnt += 1 13 | 14 | M = max(A) 15 | for _ in range(cnt): 16 | M *= 2 17 | 18 | print(M + sum(A) - max(A)) 19 | -------------------------------------------------------------------------------- /Problems/Codeforces/DonutShops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll T, a, b, c; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> a >> b >> c; 15 | 16 | if (a < c) cout << 1 << ' '; 17 | else cout << -1 << ' '; 18 | if (a * b > c) cout << b << '\n'; 19 | else cout << -1 << '\n'; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/DreamoonAndRankingCollection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, x, a; 7 | vector p; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n >> x; 15 | 16 | p.assign(101, false); 17 | rep(i, n) 18 | { 19 | cin >> a; a--; 20 | p[a] = true; 21 | } 22 | 23 | int v = 0; 24 | rep(i, 100) 25 | { 26 | v++; 27 | if (!x and !p[i]) { v--; break; } 28 | if (!p[i]) x--; 29 | } 30 | v += x; 31 | 32 | cout << v << '\n'; 33 | } 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/EhAbAnDgCd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, x; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(_, T) 12 | { 13 | cin >> x; 14 | cout << 1 << ' ' << x - 1 << '\n'; 15 | } 16 | } -------------------------------------------------------------------------------- /Problems/Codeforces/EhabAndPrefixMEXs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define repx(i, a, b) for (int i = (int)a; i < (int)b; i++) 6 | 7 | int N; 8 | vector a, b, l; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | a.resize(N); 15 | set s; 16 | rep(i, N) { cin >> a[i]; s.insert(a[i]); } 17 | 18 | priority_queue, greater> q; 19 | rep(i, 2 * N) if (!s.count(i)) q.push(i); 20 | 21 | b.push_back(q.top()); q.pop(); 22 | rep(i, N - 1) 23 | { 24 | if (a[i] != a[i + 1]) b.push_back(a[i]); 25 | else { b.push_back(q.top()); q.pop(); } 26 | } 27 | 28 | for (int x : b) cout << x << ' '; 29 | cout << '\n'; 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ElementExtermination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | A.resize(N); 17 | rep(i, N) cin >> A[i]; 18 | 19 | if (A[0] > A[N - 1]) cout << "NO\n"; 20 | else cout << "YES\n"; 21 | } 22 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ErasingZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string s; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> s; 15 | 16 | bool ones = false; 17 | int ans = 0, aux = 0; 18 | for (char c : s) 19 | { 20 | if (c == '1' and ones) 21 | { 22 | ans += aux; 23 | aux = 0; 24 | } 25 | if (c == '1') ones = true; 26 | if (c == '0' and ones) aux++; 27 | } 28 | 29 | cout << ans << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/EugeneAndArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll n, a; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | ll ans = 0, sum = 0; 14 | set S; S.insert(0); 15 | queue Q; Q.push(0); 16 | rep(_, n) 17 | { 18 | cin >> a; 19 | sum += a; 20 | 21 | if (S.count(sum)) while (S.count(sum)) 22 | { 23 | ans += S.size() - 1; 24 | S.erase(Q.front()); Q.pop(); 25 | } 26 | S.insert(sum); Q.push(sum); 27 | } 28 | ans += ((ll) S.size() * ((ll) S.size() - 1ll)) / 2ll; 29 | 30 | cout << ans << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/EvenButNotEven.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int t, n; 7 | string num; 8 | 9 | int main() 10 | { 11 | cin >> t; 12 | while (t--) 13 | { 14 | cin >> n >> num; 15 | 16 | bool done = false; 17 | int primes = 0; 18 | int c; 19 | rep(i, n) 20 | { 21 | if (int(num[i]) % 2) 22 | { 23 | primes++; 24 | if (primes == 2) 25 | { 26 | cout << num[c] << num[i] << '\n'; 27 | done = true; 28 | break; 29 | } 30 | c = i; 31 | } 32 | } 33 | 34 | if (!done) cout << -1 << '\n'; 35 | } 36 | } -------------------------------------------------------------------------------- /Problems/Codeforces/EveryoneIsAWinner.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef vector vi; 6 | 7 | int T, n; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n; 15 | 16 | set ans = {0, 1}; 17 | rep(i, floor(sqrt(n))) 18 | { 19 | ans.insert(n / (i + 1)); 20 | ans.insert(i + 1); 21 | } 22 | 23 | cout << ans.size() << '\n'; 24 | for (int r : ans) 25 | cout << r << ' '; 26 | cout << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ExtremeSubstraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | A.resize(N); 17 | rep(i, N) cin >> A[i]; 18 | 19 | int L = 0; bool flag = 1; 20 | for (int i = N - 1; i > 0; i--) 21 | { 22 | if (A[i - 1] > A[i] - L) L = max(L, A[i - 1] - (A[i] - L)); 23 | if (A[i - 1] - L < 0) flag = 0; 24 | } 25 | 26 | if (flag) cout << "YES\n"; 27 | else cout << "NO\n"; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FashonabLee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | if (N % 4 == 0) cout << "YES\n"; 15 | else cout << "NO\n"; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FastFoodRestaurant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, a, b, c; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(_, T) 12 | { 13 | cin >> a >> b >> c; 14 | 15 | if (a < b) swap(a, b); 16 | if (a < c) swap(a, c); 17 | if (b < c) swap(b, c); 18 | 19 | int ans = 0; 20 | 21 | if (a) ans++, a--; 22 | if (b) ans++, b--; 23 | if (c) ans++, c--; 24 | 25 | if (a and b) ans++, a--, b--; 26 | if (a and c) ans++, a--, c--; 27 | if (b and c) ans++, b--, c--; 28 | 29 | if (a and b and c) ans++; 30 | 31 | cout << ans << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Fence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T; 8 | ll a, b, c; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> a >> b >> c; 16 | 17 | if (b < c) swap(b, c); 18 | if (a < b) swap(a, b); 19 | if (b < c) swap(b, c); 20 | 21 | if (a > b + c) cout << a - b + c + 1 << '\n'; 22 | else cout << 1 << '\n'; 23 | } 24 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef vector> matrix; 6 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 7 | 8 | const ll MOD = 1e9 + 7; 9 | 10 | matrix mul(matrix &A, matrix &B) 11 | { 12 | matrix ans = {{0, 0}, {0, 0}}; 13 | rep(i, 2) rep(j, 2) rep(k, 2) ans[i][k] = (ans[i][k] + A[i][j] * B[j][k]) % MOD; 14 | return ans; 15 | } 16 | 17 | ll N; 18 | 19 | int main() 20 | { 21 | cin >> N; N++; 22 | 23 | matrix A = {{0, 1}, {1, 1}}, ans = {{1, 0}, {0, 1}}; 24 | while (N) 25 | { 26 | if (N & 1ll) ans = mul(ans, A); 27 | A = mul(A, A), N >>= 1ll; 28 | } 29 | 30 | cout << ans[0][0] << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FillingDiamonds.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | ll T, n; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n; 14 | cout << n << '\n'; 15 | } 16 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FillingShapes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int N; 8 | 9 | int main() 10 | { 11 | cin >> N; 12 | if (N & 1) { cout << 0 << '\n'; return 0; } 13 | 14 | ll ans = 1; 15 | rep(_, N / 2) ans *= 2; 16 | 17 | cout << ans << '\n'; 18 | } 19 | -------------------------------------------------------------------------------- /Problems/Codeforces/Filomino2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | vector P; 8 | vector> A; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | P.resize(N); rep(i, N) cin >> P[i]; 15 | A.assign(N, vector(N, -1)); 16 | 17 | rep(i, N) 18 | { 19 | int k = P[i] - 1; 20 | int x = i, y = i; 21 | 22 | A[x][y] = P[i]; 23 | while (k--) 24 | { 25 | if (y && A[x][y - 1] == -1) A[x][--y] = P[i]; 26 | else A[++x][y] = P[i]; 27 | } 28 | } 29 | 30 | rep(i, N) 31 | { 32 | for (int j = 0; j <= i; j++) cout << A[i][j] << ' '; 33 | cout << '\n'; 34 | } 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FixYou.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, M; 7 | char d; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N >> M; 15 | 16 | int ans = 0; 17 | rep(i, N) rep(j, M) 18 | { 19 | cin >> d; 20 | if (j == M - 1 && d == 'R') ans++; 21 | if (i == N - 1 && d == 'D') ans++; 22 | } 23 | cout << ans << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FourSegments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define mp make_pair 5 | 6 | int x, y, x_, y_; 7 | 8 | int main() 9 | { 10 | int H = 0, V = 0; 11 | set> p; 12 | set, pair>> s; 13 | 14 | for (int i = 0; i < 4; i++) 15 | { 16 | cin >> x >> y >> x_ >> y_; 17 | if (x == x_) V++; 18 | if (y == y_) H++; 19 | 20 | p.emplace(x, y); p.emplace(x_, y_); 21 | s.emplace(mp(x, y), mp(x_, y_)); 22 | s.emplace(mp(x_, y_), mp(x, y)); 23 | } 24 | 25 | if (s.size() != 8 || p.size() != 4 || H != 2 || V != 2) cout << "NO\n"; 26 | else cout << "YES\n"; 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/FourSegments.py: -------------------------------------------------------------------------------- 1 | 2 | H, V = 0, 0 3 | 4 | S = [] 5 | for _ in range(4): 6 | x, y, x_, y_ = list(map(int, input().split())) 7 | S.append(((x, y), (x_, y_))) 8 | if x == x_: 9 | V += 1 10 | if y == y_: 11 | H += 1 12 | 13 | ans = True 14 | for s in S: 15 | cnt1 = 0; cnt2 = 0 16 | for s_ in S: 17 | cnt1 += (s[0] in s_) 18 | cnt2 += (s[1] in s_) 19 | if cnt1 != 2 or cnt2 != 2: 20 | ans = False 21 | 22 | print("YES" if (ans and H == 2 and V == 2) else "NO") 23 | -------------------------------------------------------------------------------- /Problems/Codeforces/GCDLength.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll T, a, b, c; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> a >> b >> c; 15 | 16 | ll gcd = 1; 17 | rep(_, c - 1) gcd *= 10ll; 18 | 19 | ll A = gcd, B = gcd; 20 | while (((int)log10(A) + 1) < a) A *= 2ll; 21 | while (((int)log10(B) + 1) < b) B *= 3ll; 22 | 23 | cout << A << ' ' << B << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/GameWithArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, S; 7 | 8 | int main() 9 | { 10 | cin >> N >> S; 11 | 12 | int a = S / N; 13 | int b = a + bool(S % N); 14 | 15 | bool pos = false; int K; 16 | rep(i, a + 1) if (i) 17 | { 18 | if (i != a && i != b && (S - i) != a && (S - 1) != b) 19 | { 20 | pos = true; 21 | K = i; 22 | break; 23 | } 24 | } 25 | 26 | if (pos) 27 | { 28 | cout << "YES\n"; 29 | rep(i, S % N) cout << b << ' '; 30 | rep(i, N - S % N) cout << a << ' '; 31 | cout << '\n'; 32 | cout << K << '\n'; 33 | } 34 | else cout << "NO\n"; 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/GameWithChips.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, m, k, x, y; 7 | 8 | int main() 9 | { 10 | cin >> n >> m >> k; 11 | 12 | rep(i, 2 * k) cin >> x, y; 13 | 14 | cout << n * m + n + m - 3 << '\n'; 15 | rep(i, n - 1) cout << 'D'; 16 | rep(i, m - 1) cout << 'L'; 17 | rep(i, n) 18 | { 19 | if (i) cout << 'U'; 20 | if (i % 2 == 0) rep(i, m - 1) cout << 'R'; 21 | if (i % 2 == 1) rep(i, m - 1) cout << 'L'; 22 | } 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/GoodSubarrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T, N; 8 | char x; 9 | vector A, S; 10 | 11 | int main() 12 | { 13 | cin >> T; 14 | while (T--) 15 | { 16 | cin >> N; 17 | 18 | A.resize(N); 19 | rep(i, N) { cin >> x; A[i] = x - '0' - 1; } 20 | 21 | map m; 22 | ll ans = 0, s = 0; 23 | rep(i, N) 24 | { 25 | s += A[i]; 26 | ans += m[s] + (s == 0); 27 | m[s]++; 28 | } 29 | 30 | cout << ans << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/GradeAllocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, m, x, a; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(_, T) 12 | { 13 | cin >> n >> m; 14 | 15 | cin >> x; 16 | 17 | int s = 0; 18 | rep(i, n - 1) 19 | { 20 | cin >> a; 21 | s += a; 22 | } 23 | 24 | cout << min(x + s, m) << '\n'; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/GreenhouseEffect.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, m; 7 | string aux; 8 | vector t; 9 | int DP[5010][5010]; 10 | 11 | int dp(int i, int l) 12 | { 13 | if (i == n) return 0; 14 | 15 | if (DP[i][l] != -1) return DP[i][l]; 16 | 17 | int ans = dp(i + 1, l) + 1; 18 | if (t[i] >= l) ans = min(ans, dp(i + 1, t[i])); 19 | 20 | return DP[i][l] = ans; 21 | } 22 | 23 | int main() 24 | { 25 | cin >> n >> m; 26 | 27 | t.resize(n); 28 | rep(i, n) cin >> t[i] >> aux; 29 | 30 | memset(DP, -1, sizeof DP); 31 | cout << dp(0, 0) << '\n'; 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/GuessThePath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, m, t, r, c; 7 | 8 | string make_path(vector &D, vector &L) 9 | { 10 | string p = ""; 11 | 12 | rep(k, L.size()) 13 | { 14 | rep(_, D[k]) p += 'D'; 15 | rep(_, L[k]) p += 'L'; 16 | } 17 | rep(_, D[D.size() - 1]) p += 'D'; 18 | 19 | return p; 20 | } 21 | 22 | int main() 23 | { 24 | cin >> n >> m; 25 | 26 | vector D = {n / 2, n - n / 2}; 27 | vector L = {m}; 28 | rep(i, 10) 29 | { 30 | string p = make_path(D, L); 31 | cout << p << endl; 32 | 33 | cin >> t; 34 | rep(i, t) 35 | { 36 | 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Problems/Codeforces/HighLoad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, k; 7 | 8 | int main() 9 | { 10 | cin >> n >> k; 11 | 12 | int d = (n - k - 1) / k; 13 | int r = (n - k - 1) % k; 14 | if (r) cout << 2 * (d + 1) + 1 << '\n'; 15 | else cout << 2 * (d + 1) << '\n'; 16 | d++; 17 | 18 | int l = 1; 19 | rep(i, k) 20 | { 21 | cout << 1 << ' ' << ++l << '\n'; 22 | rep(_, d - 1 + bool(r)) cout << l << ' ' << ++l << '\n'; 23 | if (r) r--; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/HilbertsHotel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, a; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n; 14 | 15 | set s; 16 | rep(i, n) 17 | { 18 | cin >> a; 19 | s.insert(((a + i) % n + n) % n); 20 | } 21 | 22 | if (s.size() == n) cout << "YES\n"; 23 | else cout << "NO\n"; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Homecoming.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define rep_(i, n) for (int i = (int)n - 1; i >= 0; i--) 6 | 7 | int T, a, b, p; 8 | string s; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | rep(_, T) 14 | { 15 | cin >> a >> b >> p; 16 | cin >> s; 17 | 18 | int n = s.size(); 19 | int ans = n, accum = 0; 20 | char last = '.'; 21 | rep_(i, n - 1) 22 | { 23 | if (s[i] != last) accum += (s[i] == 'A') ? a : b; 24 | if (accum <= p) ans = i + 1; 25 | last = s[i]; 26 | } 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/IchihimeAndTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, a, b, c, d; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> a >> b >> c >> d; 14 | 15 | int x = b; 16 | int y = c; 17 | int z = c; 18 | 19 | cout << x << ' ' << y << ' ' << z << '\n'; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Image.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, N; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | while (T--) 10 | { 11 | set s; char c; 12 | for (int i = 0; i < 4; i++) { cin >> c; s.insert(c); } 13 | cout << s.size() - 1 << '\n'; 14 | } 15 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Image.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | l1 = input() 5 | l2 = input() 6 | s = set([l1[0], l1[1], l2[0], l2[1]]) 7 | print(len(s) - 1) 8 | -------------------------------------------------------------------------------- /Problems/Codeforces/InfinitePaths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | 8 | int main() 9 | { 10 | 11 | } -------------------------------------------------------------------------------- /Problems/Codeforces/InterestingSubarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | vector a; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n; 15 | 16 | a.resize(n); 17 | rep(i, n) cin >> a[i]; 18 | 19 | bool done = false; 20 | rep(i, n) 21 | { 22 | if (i < n - 1 && abs(a[i] - a[i + 1]) > 1) 23 | { 24 | cout << "YES" << '\n'; 25 | cout << i + 1 << ' ' << i + 2 << '\n'; 26 | done = true; 27 | break; 28 | } 29 | } 30 | 31 | if (!done) 32 | cout << "NO\n"; 33 | } 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/JohnnyAndAnotherRatingDrop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T; 8 | ll N; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | ll ans = 0; 18 | rep(i, 63) if ((1ll << i) & N) ans += (1ll << (i + 1)) - 1ll; 19 | 20 | cout << ans << '\n'; 21 | } 22 | } -------------------------------------------------------------------------------- /Problems/Codeforces/JourneyPlanning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll n, x; 8 | vector b; 9 | map cnt; 10 | 11 | int main() 12 | { 13 | cin >> n; 14 | 15 | b.resize(n); 16 | rep(i, n) 17 | { 18 | cin >> x; x -= i; 19 | b[i] = x; 20 | if (cnt.find(x) != cnt.end()) cnt[x] += x + i; 21 | else cnt[x] = x + i; 22 | } 23 | 24 | ll ans = 0; 25 | rep(i, n) ans = max(ans, cnt[b[i]]); 26 | 27 | cout << ans << '\n'; 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/JugglingLetters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector C; 8 | string s; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | C.assign(28, 0); 18 | rep(_, N) 19 | { 20 | cin >> s; 21 | for (char c : s) C[c - 'a']++; 22 | } 23 | 24 | rep(i, 28) if (C[i] % N) { cout << "NO\n"; goto next; } 25 | cout << "YES\n"; 26 | 27 | next: 28 | continue; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/KanaAndDragonQuestGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, x, n, m; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> x >> n >> m; 14 | 15 | bool pos = false; 16 | 17 | if (x <= 10 * m) pos = true; 18 | while (n--) 19 | { 20 | x = x / 2 + 10; 21 | if (x <= 10 * m) pos = true; 22 | } 23 | 24 | if (pos) cout << "YES\n"; 25 | else cout << "NO\n"; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Killjoy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, X; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N >> X; 15 | 16 | A.resize(N); multiset s; 17 | rep(i, N) { cin >> A[i]; s.insert(A[i]); } 18 | 19 | if (s.count(X) == N && A[0] == X) { cout << 0 << '\n'; continue; } 20 | if (s.count(X) > 1) { cout << 1 << '\n'; continue; } 21 | if (s.count(X) == 0) 22 | { 23 | int s = 0; 24 | rep(i, N) s += A[i] - X; 25 | if (s == 0) cout << 1 << '\n'; 26 | else cout << 2 << '\n'; 27 | } 28 | else cout << 1 << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/KuroniAndTheGifts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | vector a, b; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(_, T) 13 | { 14 | cin >> n; 15 | 16 | a.resize(n); 17 | rep(i, n) cin >> a[i]; 18 | 19 | b.resize(n); 20 | rep(i, n) cin >> b[i]; 21 | 22 | sort(a.begin(), a.end()); 23 | sort(b.begin(), b.end()); 24 | 25 | rep(i, n) cout << a[i] << ' '; 26 | cout << '\n'; 27 | rep(i, n) cout << b[i] << ' '; 28 | cout << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/LCMProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T; 8 | ll l, r; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> l >> r; 16 | 17 | if (l * (l + 1) <= r) cout << l << ' ' << l + 1ll << '\n'; 18 | else if (2ll * l <= r) cout << l << ' ' << 2ll * l << '\n'; 19 | else cout << -1 << ' ' << -1 << '\n'; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/LastMinuteEnhancements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, X[100000]; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | rep(i, N) cin >> X[i]; 15 | 16 | set s; int l = 0; 17 | rep(i, N) 18 | { 19 | if (X[i] <= l) s.insert(X[i] + 1), l = max(l, X[i] + 1); 20 | else s.insert(X[i]), l = max(l, X[i]); 21 | } 22 | 23 | cout << s.size() << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/LevelStatistics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n; 14 | 15 | bool pos = true; 16 | int p, c, lp = 0, lc = 0; 17 | rep(i, n) 18 | { 19 | cin >> p >> c; 20 | if (p < lp or c < lc) pos = false; 21 | if (p - lp < c - lc) pos = false; 22 | if (c > p) pos = false; 23 | lp = p; lc = c; 24 | } 25 | 26 | if (pos) cout << "YES\n"; 27 | else cout << "NO\n"; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/LittleArtem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, m; 7 | vector> ans; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n >> m; 15 | 16 | ans.assign(n, vector(m)); 17 | rep(i, n) rep(j, m) ans[i][j] = (((i + j) % 2) ? 'W' : 'B'); 18 | if ((n * m) % 2 == 0) ans[0][1] = 'B'; 19 | 20 | rep(i, n) 21 | { 22 | rep(j, m) cout << ans[i][j]; 23 | cout << '\n'; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/LongJumps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T, N; 8 | vector A; 9 | 10 | int DP[200010]; 11 | 12 | int dp(int i) 13 | { 14 | if (i >= N) return 0; 15 | if (DP[i] != -1) return DP[i]; 16 | 17 | return DP[i] = A[i] + dp(i + A[i]); 18 | } 19 | 20 | int main() 21 | { 22 | cin >> T; 23 | while (T--) 24 | { 25 | cin >> N; 26 | 27 | A.resize(N); rep(i, N) cin >> A[i]; 28 | 29 | rep(i, N) DP[i] = -1; 30 | 31 | int ans = 0; 32 | rep(i, N) ans = max(ans, dp(i)); 33 | 34 | cout << ans << '\n'; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Problems/Codeforces/MafiaWar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | long long N, S; 7 | vector C; 8 | 9 | int main() 10 | { 11 | cin >> N >> S; 12 | 13 | C.resize(N - 1); 14 | rep(i, N - 1) cin >> C[i]; 15 | 16 | sort(C.begin(), C.end()); 17 | 18 | S += N - 1; 19 | 20 | rep(i, N - 1) 21 | S -= (N - 1 - i) * C[i]; 22 | 23 | if (S > 0) 24 | cout << "SI\n"; 25 | else 26 | cout << "NO\n"; 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MagicalCalendar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll T, N, R; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N >> R; 15 | 16 | if (R < N) cout << R * (R + 1ll) / 2ll << '\n'; 17 | else cout << N * (N - 1ll) / 2ll + 1ll << '\n'; 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MagicalSticks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | cout << N / 2 + (N % 2) << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MakingSequencesIsFun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll w, m, k; 8 | 9 | int main() 10 | { 11 | cin >> w >> m >> k; 12 | 13 | ll ans = 0, c = 0; int n = log10(m) + 1; 14 | while (c + (pow(10, n) - m) * n * k <= w) 15 | { 16 | c += (pow(10, n) - m) * n * k; 17 | ans += pow(10, n) - m; 18 | m = pow(10, n), n++; 19 | } 20 | ans += (w - c) / (n * k); 21 | 22 | cout << ans << '\n'; 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MakingSequencesIsFun.py: -------------------------------------------------------------------------------- 1 | 2 | w, m, k = list(map(int, input().split())) 3 | 4 | ans = 0; c = 0; n = 0 5 | while 10 ** n <= m: 6 | n += 1 7 | 8 | while c + (10 ** (n) - m) * n * k <= w: 9 | c += (10 ** (n) - m) * n * k 10 | ans += 10 ** (n) - m 11 | m = 10 ** n; n += 1 12 | ans += (w - c) // (n * k) 13 | 14 | print(ans) 15 | -------------------------------------------------------------------------------- /Problems/Codeforces/MariaBraksTheSelfIsolation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | vector a; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n; 15 | 16 | a.resize(n); 17 | rep(i, n) cin >> a[i]; 18 | 19 | sort(a.begin(), a.end()); 20 | 21 | int ans = n - 1; 22 | while (ans >= 0 && a[ans] > ans + 1) ans--; 23 | 24 | cout << ans + 2 << '\n'; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MarkAndHisUnfinishedEssay.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | 5 | N, C, Q = list(map(int, input().split())) 6 | S = input() 7 | 8 | L = [0 for _ in range(C)]; R = [0 for _ in range(C)] 9 | for i in range(C): 10 | L[i], R[i] = list(map(int, input().split())) 11 | L[i] -= 1; R[i] -= 1 12 | 13 | for _ in range(Q): 14 | K = int(input()) - 1 15 | 16 | while K >= N: 17 | p = N - 1 18 | for i in range(C): 19 | ln = R[i] - L[i] + 1 20 | p += ln 21 | if K <= p: 22 | K = L[i] - 1 + (K - (p - ln)) 23 | break 24 | 25 | print(S[K]) 26 | -------------------------------------------------------------------------------- /Problems/Codeforces/MarketingScheme.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, l, r; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> l >> r; 14 | 15 | if (r >= l * 2) cout << "NO\n"; 16 | else cout << "YES\n"; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MathProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, l, r, l_, r_, n; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(t, T) 12 | { 13 | l_= -1; 14 | r_ = INT_MAX; 15 | cin >> n; 16 | rep(i, n) 17 | { 18 | cin >> l >> r; 19 | l_ = max(l_, l); 20 | r_ = min(r_, r); 21 | } 22 | 23 | cout << max(l_ - r_, 0) << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MatrixGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, m, x; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n >> m; 14 | 15 | vector r(n, 0), c(m, 0); 16 | rep(i, n) rep(j, m) 17 | { 18 | cin >> x; 19 | if (x) r[i] = 1, c[j] = 1; 20 | } 21 | 22 | int cntr = 0, cntc = 0; 23 | rep(i, n) if (!r[i]) cntr++; 24 | rep(i, m) if (!c[i]) cntc++; 25 | 26 | x = min(cntr, cntc); 27 | 28 | if (x & 1) cout << "Ashish\n"; 29 | else cout << "Vivek\n"; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MaximumGCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | cout << N / 2 << '\n'; 15 | } 16 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MaximumSquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int k, n; 7 | vector a; 8 | 9 | int main() 10 | { 11 | cin >> k; 12 | rep(_, k) 13 | { 14 | cin >> n; 15 | a.resize(n); 16 | 17 | rep(i, n) cin >> a[i]; 18 | 19 | sort(a.begin(), a.end()); 20 | 21 | rep(i, n) if (a[i] >= n - i) 22 | { 23 | cout << n - i << '\n'; 24 | break; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MaximumSquare.py: -------------------------------------------------------------------------------- 1 | 2 | k = int(input()) 3 | 4 | for _ in range(k): 5 | 6 | n = int(input()) 7 | a = list(map(int, input().split())) 8 | 9 | a.sort() 10 | 11 | for i in range(n): 12 | if a[i] >= n - i: 13 | print(n - i) 14 | break -------------------------------------------------------------------------------- /Problems/Codeforces/MaximumSubsequenceValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int n; 8 | vector a; 9 | 10 | int main() 11 | { 12 | cin >> n; 13 | 14 | a.resize(n); 15 | rep(i, n) cin >> a[i]; 16 | 17 | ll ans = 0; 18 | rep(i, n) rep(j, n) rep(k, n) ans = max(ans, a[i] | a[j] | a[k]); 19 | 20 | cout << ans << '\n'; 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Maximums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, x; 7 | vector ans; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | int aux = 0; 14 | rep(i, n) 15 | { 16 | cin >> x; 17 | cout << aux + x << ' '; 18 | aux = max(aux, aux + x); 19 | } 20 | cout << '\n'; 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MiddleClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, x, a; 7 | priority_queue q; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n >> x; 15 | rep(i, n) { cin >> a; q.push(a); } 16 | 17 | int cnt = 0, ans = 0; 18 | double sum = 0; 19 | while (!q.empty()) 20 | { 21 | cnt++; 22 | sum += q.top(); q.pop(); 23 | if ((sum / cnt) >= x) ans++; 24 | } 25 | 26 | cout << ans << '\n'; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MindControl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define repx(i, a, b) for (int i = (int)a; i < (int)b; i++) 6 | 7 | int t, n, m, k; 8 | vector a; 9 | 10 | int main() 11 | { 12 | cin >> t; 13 | while (t--) 14 | { 15 | cin >> n >> m >> k; 16 | k = min(k, m - 1); 17 | 18 | a.resize(n); 19 | rep(i, n) cin >> a[i]; 20 | 21 | SparseTable st(a); 22 | 23 | int ans = -1; 24 | rep(i, k + 1) 25 | { 26 | int aux = INT_MAX; 27 | repx(j, i, i + m - k) 28 | aux = min(aux, max(a[j], a[j + n - m])); 29 | 30 | ans = max(ans, aux); 31 | } 32 | 33 | cout << ans << '\n'; 34 | } 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MinimalHeightTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | A.resize(N); 17 | rep(i, N) cin >> A[i]; 18 | 19 | int ans = 0, p = 1, n = 0, l = -1; 20 | rep(i, N) if (i) 21 | { 22 | if (A[i] > l) n++; 23 | else if (A[i] < l && p > 1) p--, n++; 24 | else if (A[i] < l && p == 1) p = n, n = 1, ans++; 25 | l = A[i]; 26 | } 27 | if (n) ans++; 28 | 29 | cout << ans << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MinutesBeforeTheNewYear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, h, m; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | while (T--) 10 | { 11 | cin >> h >> m; 12 | 13 | if (m) 14 | cout << (23 - h) * 60 + (60 - m) << '\n'; 15 | else 16 | cout << (24 - h) * 60 << '\n'; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/MirrorGrid.py: -------------------------------------------------------------------------------- 1 | 2 | def rotate(i, j): 3 | return j, N - i - 1 4 | 5 | T = int(input()) 6 | for _ in range(T): 7 | 8 | N = int(input()) 9 | 10 | A = [] 11 | for i in range(N): 12 | A.append(input()) 13 | 14 | ans = 0 15 | for i in range(N): 16 | for j in range(N): 17 | x = i; y = j; s = 0 18 | for _ in range(4): 19 | s += int(A[x][y]) 20 | x, y = rotate(x, y) 21 | ans += min(s, 4 - s) 22 | 23 | print(ans // 4) 24 | -------------------------------------------------------------------------------- /Problems/Codeforces/MostSociallyDistancedSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, p; 7 | 8 | int sgn(int x) { return x > 0 ? 1 : -1; } 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | int ls = 0, l; 18 | vector ans; 19 | rep(i, N) 20 | { 21 | cin >> p; 22 | if (i && sgn(p - l) != ls) ans.push_back(l); 23 | if (i == N - 1) ans.push_back(p); 24 | if (i) ls = sgn(p - l); 25 | l = p; 26 | } 27 | 28 | cout << ans.size() << '\n'; 29 | for (int x : ans) cout << x << ' '; 30 | cout << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NastyaAndRice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, a, b, c, d; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n >> a >> b >> c >> d; 14 | if (n * (a + b) >= c - d and n * (a + b) <= c + d) cout << "Yes\n"; 15 | else if (n * (a - b) <= c + d and n * (a + b) >= c - d) cout << "Yes\n"; 16 | else cout << "No\n"; 17 | } 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NastyaAndStrangeGenerator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, p; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n; 14 | int m = INT_MAX, l; 15 | bool pos = true; 16 | rep(i, n) 17 | { 18 | cin >> p; 19 | if (p > m and p != l + 1) pos = false; 20 | if (p <= l and p >= m) pos = false; 21 | l = p; m = min(m, p); 22 | } 23 | 24 | if (pos) cout << "Yes\n"; 25 | else cout << "No\n"; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NationalProject.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, n, g, b; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> n >> g >> b; 15 | 16 | ll m = n - n / 2; 17 | ll c = m / g; 18 | ll r = m % g; 19 | ll aux = c * g + (c - 1) * b + bool(r) * b + r; 20 | if (aux < n) aux = n; 21 | 22 | cout << aux << '\n'; 23 | } 24 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NewYearAndNaming.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, m, q, y; 7 | vector s1, s2; 8 | 9 | int main() 10 | { 11 | cin >> n >> m; 12 | 13 | s1.resize(n); 14 | rep(i, n) cin >> s1[i]; 15 | 16 | s2.resize(m); 17 | rep(i, m) cin >> s2[i]; 18 | 19 | cin >> q; 20 | rep(i, q) 21 | { 22 | cin >> y; 23 | y--; 24 | cout << s1[y % n] << s2[y % m] << '\n'; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NewYearAndPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | long long n, m; 7 | vector f; 8 | 9 | int main() 10 | { 11 | cin >> n >> m; 12 | f.resize(n + 1); 13 | f[0] = 1; 14 | rep(i, n) f[i + 1] = (((i + 1) * f[i]) % m + m) % m; 15 | 16 | long long ans = 0; 17 | rep(i, n) 18 | { 19 | ans += f[i + 1] * f[n - i - 1] % m * (n - i) % m * (n - i) % m; 20 | ans %= m; 21 | } 22 | cout << ans << '\n'; 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NewYearGarland.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | long long T, r, g, b; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(t, T) 12 | { 13 | cin >> r >> g >> b; 14 | long long m = (r + g + b) - (r + g + b) / 2; 15 | 16 | if (r > m || g > m || b > m) 17 | cout << "No\n"; 18 | else 19 | cout << "Yes\n"; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Non-SubstringSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, Q, l, r; 7 | string S; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N >> Q >> S; 15 | 16 | while (Q--) 17 | { 18 | cin >> l >> r; l--, r--; 19 | 20 | bool b1 = 0, b2 = 0; 21 | for (int i = 0; i < l; i++) if (S[i] == S[l]) b1 = 1; 22 | for (int i = r + 1; i < N; i++) if (S[i] == S[r]) b2 = 1; 23 | if (b1 || b2) cout << "YES\n"; 24 | else cout << "NO\n"; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Non-Zero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | vector arr; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> n; 15 | 16 | arr.resize(n); 17 | rep(i, n) cin >> arr[i]; 18 | 19 | int ans = 0; 20 | int sum = 0; 21 | rep(i, n) 22 | { 23 | if (arr[i] == 0) 24 | { 25 | ans++; 26 | sum++; 27 | } 28 | else sum += arr[i]; 29 | } 30 | 31 | if (sum == 0) ans++; 32 | 33 | cout << ans << '\n'; 34 | } 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/NumberOfWays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int N; 8 | vector A, S, S1, S2; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | A.resize(N); 15 | rep(i, N) cin >> A[i]; 16 | 17 | S.assign(N + 1, 0); 18 | rep(i, N) S[i + 1] = S[i] + A[i]; 19 | 20 | if (S[N] % 3ll) { cout << 0 << '\n'; return 0; } 21 | ll T = S[N] / 3; 22 | 23 | ll p = 0; S1.resize(N); S2.resize(N); 24 | rep(i, N) 25 | { 26 | S1[i] = p + (S[i + 1] == T); 27 | S2[N - i - 1] = (S[N] - S[N - i] == T); 28 | 29 | p = S1[i]; 30 | } 31 | 32 | ll ans = 0; 33 | rep(i, N - 1) if (i && S2[i]) ans += S1[i - 1]; 34 | 35 | cout << ans << '\n'; 36 | } -------------------------------------------------------------------------------- /Problems/Codeforces/OmkarAndInfinityClock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll T, N, K; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N >> K; 16 | 17 | ll m = LLONG_MAX, M = LLONG_MIN; 18 | A.resize(N); 19 | rep(i, N) { cin >> A[i]; m = min(m, A[i]), M = max(M, A[i]); } 20 | 21 | if (K & 1ll) rep(i, N) cout << M - A[i] << ' '; 22 | else rep(i, N) cout << A[i] - m << ' '; 23 | cout << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/OmkarAndPassword.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, A; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | set s; 16 | rep(i, N) { cin >> A; s.insert(A); } 17 | 18 | if (s.size() == 1) cout << N << '\n'; 19 | else cout << 1 << '\n'; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Codeforces/OmkarAndWaterslide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll T, N; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | A.resize(N); 18 | rep(i, N) cin >> A[i]; 19 | 20 | ll ans = 0; 21 | rep(i, N - 1) ans += max(A[N - i - 2] - A[N - i - 1], 0ll); 22 | 23 | cout << ans << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/OracAndFactors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 5 | typedef long long ll; 6 | 7 | int T; 8 | ll n, k; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> n >> k; 16 | 17 | rep(i, n) if (i && n % (i + 1ll) == 0ll) 18 | { 19 | n += (i + 1ll); 20 | break; 21 | } 22 | 23 | n += (k - 1ll) * 2ll; 24 | 25 | cout << n << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/OracAndMedians.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, k; 7 | vector a; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n >> k; 15 | 16 | a.resize(n); 17 | rep(i, n) cin >> a[i]; 18 | 19 | bool can1 = false, can2 = false; 20 | if (n == 1 && k == a[0]) can2 = true; 21 | 22 | int l1 = -1, l2 = -1; 23 | rep(i, n) 24 | { 25 | if (a[i] == k) can1 = true; 26 | if (a[i] >= k && max(l1, l2) >= k) can2 = true; 27 | l2 = l1; l1 = a[i]; 28 | } 29 | 30 | if (can1 && can2) cout << "yes\n"; 31 | else cout << "no\n"; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Codeforces/OracAndModels.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 1; i < (int)n + 1; i++) 5 | #define rep_(i, n) for (int i = (int)n; i > 0; i--) 6 | 7 | int T, n; 8 | vector s, c; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> n; 16 | 17 | s.resize(n); c.assign(n, 0); 18 | rep(i, n) cin >> s[i - 1]; 19 | 20 | int M = -1; 21 | rep_(i, n) 22 | { 23 | rep(j, (n / i) + 2) if (j > 1) 24 | { 25 | if (i * j <= n && s[(i * j) - 1] > s[i - 1]) 26 | c[i - 1] = max(c[i - 1], c[(i * j) - 1] + 1); 27 | } 28 | M = max(M, c[i - 1]); 29 | } 30 | 31 | cout << M + 1 << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Palindromifier.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | string S; 7 | 8 | int main() 9 | { 10 | cin >> S; 11 | 12 | int N = S.size(); 13 | 14 | cout << 3 << '\n'; 15 | cout << 'R' << ' ' << N - 1 << '\n'; 16 | cout << 'L' << ' ' << N << '\n'; 17 | cout << 'L' << ' ' << 2 << '\n'; 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ParkLightning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, m; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n >> m; 14 | 15 | cout << n * m / 2 + bool((n * m) % 2) << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PassportControlGates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int q, n; 5 | 6 | int main() 7 | { 8 | 9 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PermutationForgery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, p; 7 | stack s; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | rep(_, N) { cin >> p; s.push(p); } 17 | while (!s.empty()) { cout << s.top() << ' '; s.pop(); } 18 | cout << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PermutationPartitions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll n, k, p; 8 | 9 | int main() 10 | { 11 | cin >> n >> k; 12 | 13 | ll ans1 = 0; 14 | rep(i, k) ans1 += (n - i); 15 | 16 | ll ans2 = 1; 17 | ll aux = 0; 18 | bool add = false; 19 | rep(i, n) 20 | { 21 | cin >> p; 22 | if (p > n - k) 23 | { 24 | if (add) ans2 = (ans2 * (aux + 1)) % 998244353; 25 | aux = 0; 26 | add = true; 27 | } 28 | else aux++; 29 | } 30 | 31 | cout << ans1 << ' ' << ans2 << '\n'; 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PhoenixAndBalance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n; 14 | int a = 0, b = 0; 15 | rep(i, n) 16 | { 17 | if (i < (n / 2 - 1)) a += (1 << (i + 1)); 18 | else if (i < n - 1) b += (1 << (i + 1)); 19 | else a += (1 << (i + 1)); 20 | } 21 | cout << a - b << '\n'; 22 | } 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PlusesAndMinuses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T; 8 | string s; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> s; 16 | 17 | ll ans = 0, sum = 0, id = 0; 18 | for (char ch : s) 19 | { 20 | id++; 21 | if (ch == '-') sum--; 22 | else sum++; 23 | 24 | if (sum < 0) ans += id, sum++; 25 | } 26 | ans += (ll)s.size(); 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PollandBallAndHypothesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | 6 | int main() 7 | { 8 | cin >> n; 9 | if (n == 1) cout << 3 << '\n'; 10 | else if (n == 2) cout << 4 << '\n'; 11 | else cout << (n - 2) << '\n'; 12 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PoweredAddition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | int T, n; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n; 15 | 16 | ll prev = LLONG_MIN, a, ans = 0; 17 | rep(i, n) 18 | { 19 | cin >> a; 20 | if (a < prev) 21 | { 22 | ll dif = prev - a, b = 0; 23 | while ((1ll << b) <= dif) b++; 24 | ans = max(ans, b); 25 | } 26 | else prev = a; 27 | } 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PrimeSquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | const int MAX = 1e5; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | 14 | while (T--) 15 | { 16 | cin >> N; 17 | 18 | rep(i, N) 19 | { 20 | rep(j, N) 21 | { 22 | if (j == N - i - 1) cout << 1 << ' '; 23 | else if (j == N - i - 2) cout << 1 << ' '; 24 | else if (i == N - 1 && j == N - 1) cout << 1 << ' '; 25 | else cout << 0 << ' '; 26 | } 27 | cout << '\n'; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/PuzzlePieces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, m; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> n >> m; 14 | 15 | if (min(n, m) <= 1) cout << "YES\n"; 16 | else if (max(n, m) == 2) cout << "YES\n"; 17 | else cout << "NO\n"; 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RPGProtagonist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll T, P, F, cs, cw, s, w; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> P >> F >> cs >> cw >> s >> w; 15 | 16 | if (s > w) swap(s, w), swap(cs, cw); 17 | 18 | ll ans = 0; 19 | rep(i, min(cs, P / s) + 1) 20 | { 21 | ll wp = (P - i * s) / w, sf = min(cs - i, F / s), wf = min((F - s * sf) / w, cw - wp); 22 | ans = max(ans, i + wp + sf + wf); 23 | } 24 | 25 | cout << ans << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RadioTower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | const ll MOD = 998244353; 8 | 9 | ll N; 10 | ll F[200010], P[200010]; 11 | 12 | ll inv(ll b) 13 | { 14 | ll a = 1, p = MOD - 2; b %= MOD; 15 | while (p) 16 | { 17 | if (p & 1ll) a = (a * b) % MOD; 18 | b = (b * b) % MOD, p >>= 1ll; 19 | } 20 | return (a + MOD) % MOD; 21 | } 22 | 23 | int main() 24 | { 25 | F[0] = 1, F[1] = 1; 26 | rep(i, 200000) F[i + 2] = (F[i] + F[i + 1]) % MOD; 27 | 28 | P[0] = 1; 29 | rep(i, 200000) P[i + 1] = (P[i] * 2) % MOD; 30 | 31 | cin >> N; 32 | 33 | cout << (F[N - 1] * inv(P[N])) % MOD << '\n'; 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RainbowDash,FuttershyAndChessColoring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | cout << N / 2 + 1 << '\n'; 15 | } 16 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RandomEvents.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, M, x; double p; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cout.setf(ios::fixed); 12 | cout.precision(10); 13 | 14 | cin >> T; 15 | while (T--) 16 | { 17 | cin >> N >> M; 18 | 19 | A.resize(N); 20 | rep(i, N) cin >> A[i]; 21 | 22 | int l = N; 23 | while (l && A[l - 1] == l) l--; 24 | 25 | double ans = 0, n = 1; 26 | rep(_, M) 27 | { 28 | cin >> x >> p; 29 | if (x >= l) ans += n * p, n *= (1. - p); 30 | } 31 | 32 | if (l) cout << ans << '\n'; 33 | else cout << 1. << '\n'; 34 | } 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RandomMood.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef vector> matrix; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int N; double P; 8 | 9 | matrix mul(matrix &A, matrix &B) 10 | { 11 | matrix ans = {{0, 0}, {0, 0}}; 12 | rep(i, 2) rep(j, 2) rep(k, 2) ans[i][k] += A[i][j] * B[j][k]; 13 | return ans; 14 | } 15 | 16 | int main() 17 | { 18 | cout.setf(ios::fixed); 19 | cout.precision(10); 20 | 21 | cin >> N >> P; 22 | 23 | matrix M = {{1 - P, P}, {P, 1 - P}}, A = {{1, 0}, {0, 1}}; 24 | while (N) 25 | { 26 | if (N & 1) A = mul(A, M); 27 | M = mul(M, M); 28 | N >>= 1; 29 | } 30 | 31 | cout << A[0][0] << '\n'; 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RegularBracketSequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, N; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | while (T--) 10 | { 11 | cin >> N; 12 | for (int i = 0; i < N; i++) 13 | { 14 | for (int j = 0; j < i; j++) cout << '('; 15 | for (int j = 0; j < (N - i); j++) cout << "()"; 16 | for (int j = 0; j < i; j++) cout << ')'; 17 | cout << '\n'; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RegularBracketSequences.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | for _ in range(T): 4 | 5 | N = int(input()) 6 | 7 | for i in range(N): 8 | print('(' * i + '()' * (N - i) + ')' * i) 9 | -------------------------------------------------------------------------------- /Problems/Codeforces/RemoveOneElement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n; 7 | vector a, r, l; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | a.resize(n); 14 | rep(i, n) cin >> a[i]; 15 | 16 | r.assign(n, 1); 17 | rep(i, n) if (i > 0 && a[i] > a[i - 1]) r[i] += r[i - 1]; 18 | 19 | l.assign(n, 1); 20 | rep(i, n) if (i > 0 && a[n - i - 1] < a[n - i]) l[n - i - 1] += l[n - i]; 21 | 22 | int ans = 0; 23 | 24 | rep(i, n) 25 | { 26 | ans = max({ans, r[i], l[i]}); 27 | if (i > 0 && i < n - 1) 28 | { 29 | if (a[i - 1] < a[i + 1]) 30 | ans = max(ans, r[i - 1] + l[i + 1]); 31 | } 32 | } 33 | 34 | cout << ans << '\n'; 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RemoveSmallest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | A.resize(N); 16 | rep(i, N) cin >> A[i]; 17 | 18 | sort(A.begin(), A.end()); 19 | 20 | int l = -1, ans = 0; 21 | for (int x : A) 22 | { 23 | if (x > l + 1) ans++; 24 | l = x; 25 | } 26 | 27 | cout << (ans == 1 ? "YES" : "NO") << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RemoveSmallest.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | 4 | for _ in range(T): 5 | 6 | N = int(input()) 7 | A = list(map(int, input().split())) 8 | 9 | A.sort() 10 | 11 | l = -1; ans = 0 12 | for x in A: 13 | if x > l + 1: 14 | ans += 1 15 | l = x 16 | 17 | if ans == 1: 18 | print("YES") 19 | else: 20 | print("NO") 21 | -------------------------------------------------------------------------------- /Problems/Codeforces/Reorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, M, x; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> M; 14 | 15 | int ans = 0; 16 | rep(i, N) { cin >> x; ans += x; } 17 | 18 | cout << (ans == M ? "YES\n" : "NO\n"); 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ReverseBinaryStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; string s; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> s; 14 | 15 | int c = 0, l = -1; 16 | rep(i, N) 17 | { 18 | if (l == s[i] && s[i] == '1') c++; 19 | l = s[i]; 20 | } 21 | if (l == s[0] && s[0] == '1') c++; 22 | 23 | cout << c << '\n'; 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ReviewSite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, r; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | int ans = 0; 16 | rep(_, N) 17 | { 18 | cin >> r; 19 | if (r != 2) ans++; 20 | } 21 | 22 | cout << ans << '\n'; 23 | } 24 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RoadToZero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, x, y, a, b; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> x >> y >> a >> b; 15 | 16 | ll ans = a * x + a * y; 17 | ans = min(ans, b * min(x, y) + a * abs(x - y)); 18 | cout << ans << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RobotProgram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, x, y; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> x >> y; 14 | 15 | cout << x + y + abs(x - y) - bool(abs(x - y)) << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RockAndLever.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, a; 7 | vector C; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | C.assign(30, 0); 17 | rep(i, N) 18 | { 19 | cin >> a; 20 | int m = 0; 21 | rep(j, 31) if (a & (1 << j)) m = j; 22 | C[m]++; 23 | } 24 | 25 | long long ans = 0; 26 | rep(i, 30) ans += C[i] * (C[i] - 1ll) / 2ll; 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/RotationMatching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, x; 7 | vector a, b, c; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | a.resize(n); 14 | rep(i, n) { cin >> x; a[x - 1] = i; } 15 | 16 | b.resize(n); 17 | rep(i, n) { cin >> x; b[x - 1] = i; } 18 | 19 | c.assign(n, 0); 20 | rep(i, n) c[(a[i] - b[i] + n) % n]++; 21 | 22 | int ans = -1; 23 | rep(i, n) ans = max(ans, c[i]); 24 | 25 | cout << ans << '\n'; 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SagesBirthday.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | vector A, B; 8 | 9 | int main() 10 | { 11 | cin >> N; 12 | 13 | A.resize(N); 14 | rep(i, N) cin >> A[i]; 15 | 16 | sort(A.begin(), A.end()); 17 | 18 | int i = 0, j = N - 1; 19 | B.resize(N); 20 | rep(k, N) 21 | { 22 | if (k & 1) B[k] = A[i], i++; 23 | else B[N - k - 1 - (N % 2 == 0)] = A[j], j--; 24 | } 25 | 26 | int c = 0; 27 | rep(i, N) if (i && i < N - 1 && B[i - 1] > B[i] && B[i + 1] > B[i]) c++; 28 | cout << c << '\n'; 29 | rep(i, N) cout << B[i] << ' '; 30 | cout << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SantaClausAndCandies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | 8 | int main() 9 | { 10 | cin >> N; 11 | 12 | int ans = 1; 13 | while (((ans * (ans + 1)) / 2) <= N) ans++; 14 | ans--; 15 | 16 | cout << ans << '\n'; 17 | rep(i, ans - 1) cout << i + 1 << ' '; 18 | cout << ans + (N - ((ans * (ans + 1)) / 2)) << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /Problems/Codeforces/SashaAndHisTrip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, V; 7 | int DP[110][110]; 8 | 9 | int dp(int i, int f) 10 | { 11 | if (DP[i][f] != -1) 12 | return DP[i][f]; 13 | 14 | if (i == N - 1) 15 | return 0; 16 | 17 | int ans = INT_MAX; 18 | for (int j = 0; j <= V - f; j++) 19 | if (f + j > 0) 20 | ans = min(ans, j * (i + 1) + dp(i + 1, f + j - 1)); 21 | return DP[i][f] = ans; 22 | } 23 | 24 | int main() 25 | { 26 | cin >> N >> V; 27 | 28 | memset(DP, -1, sizeof DP); 29 | 30 | cout << dp(0, 0) << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SequenceWithDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, a, K; 8 | 9 | ll step(ll a) 10 | { 11 | ll x = a; 12 | ll m = 10, M = 0; 13 | while (x) 14 | { 15 | ll d = x % 10; 16 | m = min(m, d); M = max(M, d); 17 | x /= 10; 18 | } 19 | return m * M; 20 | } 21 | 22 | int main() 23 | { 24 | cin >> T; 25 | while (T--) 26 | { 27 | cin >> a >> K; K--; 28 | 29 | while (K--) 30 | { 31 | ll s = step(a); 32 | if (s == 0) break; 33 | a = a + step(a); 34 | } 35 | 36 | cout << a << '\n'; 37 | } 38 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ShovelsAndSwords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll T, a, b; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> a >> b; 15 | cout << min({a, b, (a + b) / 3}) << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Shuffle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n, x, m; 7 | vector l, r; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n >> x >> m; x--; 15 | 16 | l.resize(m); r.resize(m); 17 | rep(i, m) { cin >> l[i] >> r[i]; l[i]--; r[i]--; } 18 | 19 | int l_ = x, r_ = x; 20 | rep(i, m) 21 | { 22 | if (l[i] < l_ && r[i] >= l_) l_ = l[i], r_ = max(r_, r[i]); 23 | if (r[i] > r_ && l[i] <= r_) l_ = min(l_, l[i]), r_ = r[i]; 24 | } 25 | 26 | cout << r_ - l_ + 1 << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SignFlipping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, A; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while(T--) 12 | { 13 | cin >> N; 14 | 15 | int last = INT_MIN; 16 | vector B; 17 | rep(i, N) 18 | { 19 | cin >> A; 20 | if (i % 2) B.push_back(abs(A)); 21 | else B.push_back(-abs(A)); 22 | } 23 | 24 | rep(i, N) cout << B[i] << ' '; 25 | cout << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Snowball.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int w, h, u, d, u_, d_; 7 | 8 | int main() 9 | { 10 | cin >> w >> h >> u >> d >> u_ >> d_; 11 | 12 | if (d < d_) 13 | { 14 | swap(d, d_); 15 | swap(u, u_); 16 | } 17 | 18 | while (h > d) w += h, h--; 19 | w += h; h--; 20 | w = max(0, w - u); 21 | while (h > d_) w += h, h--; 22 | w += h; h--; 23 | w = max(0, w - u_); 24 | while (h > 0) w += h, h--; 25 | 26 | cout << w << '\n'; 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SoftDrinking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, k, l, c, d, p, nl, np; 5 | 6 | int main() 7 | { 8 | cin >> n >> k >> l >> c >> d >> p >> nl >> np; 9 | 10 | int drinks = k * l / nl; 11 | int limes = c * d; 12 | int salt = p / np; 13 | 14 | cout << min({drinks, limes, salt}) / n << '\n'; 15 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SoftDrinking.py: -------------------------------------------------------------------------------- 1 | 2 | n, k, l, c, d, p, nl, np = list(map(int, input().split())) 3 | 4 | drinks = k * l // nl 5 | limes = c * d 6 | salt = p // np 7 | 8 | print(min(drinks, limes, salt) // n) 9 | -------------------------------------------------------------------------------- /Problems/Codeforces/SongsCompression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, M, a, b; 7 | vector S; 8 | 9 | int main() 10 | { 11 | cin >> N >> M; 12 | 13 | long long sum = 0; 14 | rep(i, N) { cin >> a >> b; sum += a; S.push_back(a - b); } 15 | 16 | sort(S.rbegin(), S.rend()); 17 | 18 | int ans = 0; 19 | for (int s : S) 20 | { 21 | if (sum <= M) break; 22 | sum -= s, ans++; 23 | } 24 | 25 | cout << (sum <= M ? ans : -1) << '\n'; 26 | } 27 | -------------------------------------------------------------------------------- /Problems/Codeforces/SortedAdjacentDifferences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, n; 7 | vector a; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> n; 15 | 16 | a.resize(n); 17 | rep(i, n) cin >> a[i]; 18 | 19 | sort(a.begin(), a.end()); 20 | 21 | bool d = true; 22 | int i = (n - 1) / 2, j = 1; 23 | cout << a[i] << ' '; 24 | while (j < n) 25 | { 26 | if (d) i += j; 27 | else i -= j; 28 | cout << a[i] << ' '; 29 | j++; d = !d; 30 | } 31 | cout << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SpecificTastesOfAndre.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | rep(i, N) cout << 1 << ' '; 15 | cout << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Stairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll T, X; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> X; X--; 15 | 16 | ll ans = 1, s = 1, b = 1; 17 | while (X >= 4ll * b + s + 1ll) 18 | { 19 | X -= 4ll * b + s + 1ll; 20 | b = 4ll * b + s + 1ll; 21 | s = 2ll * s + 1ll; 22 | ans++; 23 | } 24 | 25 | cout << ans << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/StatisticsOfRecompressingVideos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll n, k, s, m; 8 | priority_queue, greater> q; 9 | 10 | int main() 11 | { 12 | ios::sync_with_stdio(0); cin.tie(0); 13 | cin >> n >> k; 14 | 15 | rep(i, k) q.push(0); 16 | 17 | rep(i, n) 18 | { 19 | cin >> s >> m; 20 | 21 | ll nxt = q.top(); q.pop(); 22 | cout << max(s, nxt) + m << '\n'; 23 | q.push(max(s, nxt) + m); 24 | } 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/StonedGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | A.resize(N); 17 | rep(i, N) cin >> A[i]; 18 | 19 | sort(A.rbegin(), A.rend()); 20 | 21 | int s = 0; 22 | rep(i, N - 1) s += A[i + 1]; 23 | 24 | int t = 0, c = N - 1; 25 | if (A[0] > s || ((s + A[0]) & 1ll)) cout << "T\n"; 26 | else cout << "HL\n"; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/StonesOnTheTable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | string s; 8 | 9 | int main() 10 | { 11 | cin >> N >> s; 12 | 13 | int ans = 0; 14 | 15 | char l = '.'; 16 | for (char c : s) 17 | { 18 | if (c == l) ans++; 19 | l = c; 20 | } 21 | 22 | cout << ans << '\n'; 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/StonesOnTheTable.py: -------------------------------------------------------------------------------- 1 | 2 | N = int(input()) 3 | s = input().strip() 4 | 5 | ans = 0 6 | 7 | l = '.' 8 | for c in s: 9 | if l == c: 10 | ans += 1 11 | l = c 12 | 13 | print(ans) 14 | -------------------------------------------------------------------------------- /Problems/Codeforces/StringGeneration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, K; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> K; 14 | 15 | rep(i, N) 16 | { 17 | if (i % 3 == 0) cout << 'a'; 18 | if (i % 3 == 1) cout << 'b'; 19 | if (i % 3 == 2) cout << 'c'; 20 | } 21 | cout << '\n'; 22 | } 23 | } -------------------------------------------------------------------------------- /Problems/Codeforces/StringMood.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef vector> matrix; 6 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 7 | 8 | const ll MOD = 1e9 + 7; 9 | 10 | matrix mul(matrix &A, matrix &B) 11 | { 12 | matrix ans = {{0, 0}, {0, 0}}; 13 | rep(i, 2) rep(j, 2) rep(k, 2) ans[i][k] = (ans[i][k] + A[i][j] * B[j][k]) % MOD; 14 | return ans; 15 | } 16 | 17 | ll N; 18 | 19 | int main() 20 | { 21 | cin >> N; 22 | 23 | matrix A = {{19, 7}, {6, 20}}, ans = {{1, 0}, {0, 1}}; 24 | while (N) 25 | { 26 | if (N & 1ll) ans = mul(ans, A); 27 | A = mul(A, A), N >>= 1ll; 28 | } 29 | 30 | cout << ans[0][0] << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/StringSimilarity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll T, N; 8 | string s; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N >> s; 16 | 17 | int c = 0; 18 | vector ans(N, 0); 19 | rep(i, 2 * N - 1) 20 | { 21 | if (s[i] == '1') c++; 22 | else c = 0; 23 | 24 | if (i >= N - 1) cout << (c >= N); 25 | } 26 | cout << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Suborray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | rep(i, N) cout << i + 1 << ' '; 15 | cout << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SubsetMex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, a, A[101]; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | memset(A, 0, sizeof A); 14 | 15 | cin >> N; 16 | rep(i, N) { cin >> a; A[a]++; } 17 | 18 | int ans = 0, c = 0; 19 | rep(i, 101) 20 | { 21 | if (A[i] == 0) ans += i, c++; 22 | if (A[i] < 2 - c) ans += i, c++; 23 | if (c == 2) break; 24 | } 25 | 26 | cout << ans << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SubstractOrDivide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | if (N == 1) cout << 0 << '\n'; 15 | else if (N == 2) cout << 1 << '\n'; 16 | else if (N == 3) cout << 2 << '\n'; 17 | else if (N % 2 == 0) cout << 2 << '\n'; 18 | else cout << 3 << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SubstringRemovalGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string s; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> s; 15 | 16 | priority_queue q; 17 | 18 | int c = 0, l = 0; 19 | for (char ch : s) 20 | { 21 | if (ch == '1') c++; 22 | if (ch == '0' && l == 1) { q.push(c); c = 0; } 23 | l = ch - '0'; 24 | } 25 | if (l == 1) { q.push(c); c = 0; } 26 | 27 | int ans = 0; 28 | while (!q.empty()) { ans += q.top(); q.pop(); if (!q.empty()) q.pop(); } 29 | 30 | cout << ans << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string sa, sb; 5 | 6 | int main() 7 | { 8 | cin >> sa >> sb; 9 | 10 | int base = 0; 11 | for (char c : sa) base = max(base, c - '0' + 1); 12 | for (char c : sb) base = max(base, c - '0' + 1); 13 | 14 | int a = stoi(sa), b = stoi(sb); 15 | 16 | int ans = 0, prev = 0; 17 | while (a + b + prev) 18 | { 19 | prev = (a % 10 + b % 10 + prev) / base; 20 | a /= 10; b /= 10; 21 | ans++; 22 | } 23 | 24 | cout << ans << '\n'; 25 | } -------------------------------------------------------------------------------- /Problems/Codeforces/Sum.py: -------------------------------------------------------------------------------- 1 | 2 | sa, sb = input().strip().split() 3 | 4 | base = max(int(c) for c in sa + sb) + 1 5 | 6 | a = int(sa); b = int(sb) 7 | 8 | ans = 0; prev = 0 9 | while a + b + prev: 10 | prev = (a % 10 + b % 10 + prev) // base 11 | a //= 10; b //= 10 12 | ans += 1 13 | 14 | print(ans) 15 | -------------------------------------------------------------------------------- /Problems/Codeforces/SumOf2050.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T; 8 | ll x; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> x; 16 | 17 | ll ans = 0; 18 | while (x >= 2050ll) 19 | { 20 | ll p = 1; 21 | while (x >= (2050ll * p)) p = p * 10; 22 | p /= 10ll; 23 | x -= (2050ll * p); 24 | ans++; 25 | } 26 | 27 | if (x) cout << -1 << '\n'; 28 | else cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SumOfOddIntegers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll T, n, k; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(_, T) 13 | { 14 | cin >> n >> k; 15 | 16 | if (n % 2ll == k % 2ll and n >= k * k) cout << "YES\n"; 17 | else cout << "NO\n"; 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/SweetProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, r, g, b; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | while (T--) 10 | { 11 | cin >> r >> g >> b; 12 | 13 | vector nums = {r, g, b}; 14 | sort(nums.begin(), nums.end()); 15 | 16 | int ans = nums[0]; 17 | if (nums[2] - nums[1] >= ans) 18 | { 19 | nums[2] -= ans; 20 | ans += nums[1]; 21 | cout << ans << '\n'; 22 | } 23 | else 24 | { 25 | int dif = nums[2] - nums[1]; 26 | nums[2] -= dif; 27 | int dans = ans - dif; 28 | 29 | nums[1] -= dans - dans / 2; 30 | ans += nums[1]; 31 | cout << ans << '\n'; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TediousLee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | const int MOD = 1e9 + 7; 7 | 8 | int T, N; 9 | int DP[2000010]; 10 | 11 | int dp(int n) 12 | { 13 | if (n < 0) return 0; 14 | 15 | if (DP[n] != -1) return DP[n]; 16 | 17 | int ans = 4 * (n % 3 == 0); 18 | 19 | ans = (ans + 2 * dp(n - 2)) % MOD; 20 | ans = (ans + dp(n - 1)) % MOD; 21 | return DP[n] = ans; 22 | } 23 | 24 | int main() 25 | { 26 | cin >> T; 27 | 28 | memset(DP, -1, sizeof DP); 29 | 30 | while (T--) 31 | { 32 | cin >> N; 33 | cout << dp(N - 3) << '\n'; 34 | } 35 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TemporarilyUnavailable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, a, b, c, r; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(t, T) 12 | { 13 | cin >> a >> b >> c >> r; 14 | 15 | if (a > b) swap(a, b); 16 | 17 | if (c + r < a || c - r > b) 18 | cout << b - a << '\n'; 19 | else 20 | cout << max(0, (c - r) - a) + max(0, b - (c + r)) << '\n'; 21 | } 22 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TemporarilyUnavailable.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | 4 | for _ in range(T): 5 | 6 | a, b, c, r = list(map(int, input().split())) 7 | 8 | if a > b: 9 | a, b = b, a 10 | 11 | if c + r < a or c - r > b: 12 | print(b - a) 13 | else: 14 | print(max(0, (c - r) - a) + max(0, b - (c + r))) 15 | -------------------------------------------------------------------------------- /Problems/Codeforces/TernarySequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll T, x, y, z, x_, y_, z_, t; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> x >> y >> z; 15 | cin >> x_ >> y_ >> z_; 16 | 17 | ll ans = 0; 18 | ans += min(z, y_) * 2ll; 19 | 20 | t = min(z, y_); 21 | z -= t, y_ -= t; 22 | 23 | t = min(z, z_); 24 | z -= t, z_ -= t; 25 | 26 | t = min(x, z_); 27 | x -= t, z_ -= t; 28 | 29 | ans -= min(z_, y) * 2ll; 30 | cout << ans << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TheMeetingPlaceCannotBeChanged.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n; 7 | vector x, v; 8 | 9 | int main() 10 | { 11 | cout.precision(10); 12 | 13 | cin >> n; 14 | 15 | x.resize(n); v.resize(n); 16 | rep(i, n) cin >> x[i]; 17 | rep(i, n) cin >> v[i]; 18 | 19 | double l = 0, r = 1e9 + 1; 20 | rep(i, 1000) 21 | { 22 | double m = (l + r) * 0.5; 23 | 24 | double L = DBL_MIN, R = DBL_MAX; 25 | rep(i, n) 26 | { 27 | L = max(L, x[i] - v[i] * m); 28 | R = min(R, x[i] + v[i] * m); 29 | } 30 | 31 | if (R >= L) r = m; 32 | else l = m; 33 | } 34 | 35 | cout << (l + r) * 0.5 << '\n'; 36 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TheMeetingPlaceCannotBeChanged.py: -------------------------------------------------------------------------------- 1 | 2 | n = int(input()) 3 | 4 | x = list(map(int, input().split())) 5 | v = list(map(int, input().split())) 6 | 7 | l = 0; r = 1e9 + 1 8 | for _ in range(1000): 9 | 10 | m = (l + r) * 0.5 11 | 12 | L = -2e9; R = 2e9 13 | for i in range(n): 14 | L = max(L, x[i] - v[i] * m) 15 | R = min(R, x[i] + v[i] * m) 16 | 17 | if R >= L: 18 | r = m 19 | else: 20 | l = m 21 | 22 | print(round((l + r) / 2, 10)) 23 | -------------------------------------------------------------------------------- /Problems/Codeforces/TheSportsFestival.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll N; 8 | vector S; 9 | 10 | ll DP[2010][2010]; 11 | 12 | ll dp(int i, int j) 13 | { 14 | if (i == j) return 0; 15 | if (DP[i][j] != -1) return DP[i][j]; 16 | return DP[i][j] = S[j] - S[i] + min(dp(i + 1, j), dp(i, j - 1)); 17 | } 18 | 19 | int main() 20 | { 21 | cin >> N; 22 | 23 | S.resize(N); 24 | rep(i, N) cin >> S[i]; 25 | 26 | sort(S.begin(), S.end()); 27 | 28 | memset(DP, -1, sizeof DP); 29 | 30 | cout << dp(0, N - 1) << '\n'; 31 | } 32 | -------------------------------------------------------------------------------- /Problems/Codeforces/TheyAreEverywhere.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | string S; 6 | 7 | int main() 8 | { 9 | cin >> N >> S; 10 | 11 | set s; 12 | for (char c : S) s.insert(c); 13 | 14 | int total = s.size(); 15 | 16 | map M; 17 | int ans = N, cnt = 0, pos = 0; 18 | for (int i = 0; i < N; i++) 19 | { 20 | while (pos < N && cnt < total) 21 | { 22 | M[S[pos]]++; 23 | if (M[S[pos]] == 1) cnt++; 24 | pos++; 25 | } 26 | 27 | if (cnt == total) ans = min(ans, pos - i); 28 | 29 | M[S[i]]--; 30 | if (M[S[i]] == 0) cnt--; 31 | } 32 | 33 | cout << ans << '\n'; 34 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TheyAreEverywhere.py: -------------------------------------------------------------------------------- 1 | 2 | N = int(input()) 3 | S = input() 4 | 5 | total = len({c for c in S}) 6 | 7 | M = {c : 0 for c in S} 8 | ans = N; cnt = 0; pos = 0 9 | for i in range(N): 10 | 11 | while pos < N and cnt < total: 12 | M[S[pos]] += 1 13 | if M[S[pos]] == 1: 14 | cnt += 1 15 | pos += 1 16 | 17 | if cnt == total: 18 | ans = min(ans, pos - i) 19 | 20 | M[S[i]] -= 1 21 | if M[S[i]] == 0: 22 | cnt -= 1 23 | 24 | print(ans) 25 | -------------------------------------------------------------------------------- /Problems/Codeforces/ThreeFriends.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int q, a, b, c; 5 | 6 | int main() 7 | { 8 | cin >> q; 9 | while (q--) 10 | { 11 | cin >> a >> b >> c; 12 | cout << max(abs(a - b) + abs(b - c) + abs(c - a) - 4, 0) << '\n'; 13 | } 14 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ThreeStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string a, b, c; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> a >> b >> c; 15 | int n = a.size(); 16 | 17 | bool pos = true; 18 | rep(i, n) 19 | { 20 | if (a[i] == b[i] and c[i] != a[i]) pos = false; 21 | if (a[i] != b[i] and c[i] != a[i] and b[i] != c[i]) pos = false; 22 | } 23 | 24 | if (pos) cout << "YES\n"; 25 | else cout << "NO\n"; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TitForTat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, K; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N >> K; 15 | 16 | A.resize(N); rep(i, N) cin >> A[i]; 17 | 18 | int i = 0, k = 0; 19 | while (i < N - 1 && k < K) 20 | { 21 | if (A[i] == 0) { i++; continue; } 22 | A[i]--, A[N - 1]++, k++; 23 | } 24 | 25 | rep(i, N) cout << A[i] << ' '; 26 | cout << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ToyBlocks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int T, N; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | while (T--) 14 | { 15 | cin >> N; 16 | 17 | A.resize(N); 18 | rep(i, N) cin >> A[i]; 19 | 20 | if (N == 2) { cout << 0 << '\n'; continue; } 21 | 22 | sort(A.rbegin(), A.rend()); 23 | 24 | ll s = 0; 25 | rep(i, N) s += A[i]; 26 | ll m = (s / (N - 1)) + bool(s % (N - 1)); 27 | 28 | if (A[0] >= m) cout << A[0] * (ll)(N - 1) - s << '\n'; 29 | else cout << m * (ll)(N - 1) - s << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TwoArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | const long long MOD = 1e9 + 7; 7 | int n, m; 8 | long long DP[11][1001]; 9 | 10 | long long dp(int ind, int d) 11 | { 12 | if (DP[ind][d] != -1) 13 | return DP[ind][d]; 14 | 15 | if (ind == m) 16 | return 1; 17 | 18 | long long ans = 0; 19 | rep(i, d + 1) ans = (ans + (d - i + 1) * dp(ind + 1, i)) % MOD; 20 | 21 | return DP[ind][d] = ans; 22 | } 23 | 24 | int main() 25 | { 26 | cin >> n >> m; 27 | 28 | memset(DP, -1, sizeof DP); 29 | 30 | cout << dp(0, n - 1) << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TwoBrackets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string S; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> S; 15 | 16 | int p1 = 0, p2 = 0, ans = 0; 17 | for (char c : S) 18 | { 19 | if (c == '(') p1++; 20 | if (c == '[') p2++; 21 | if (c == ')') ans += bool(p1), p1 = max(p1 - 1, 0); 22 | if (c == ']') ans += bool(p2), p2 = max(p2 - 1, 0); 23 | } 24 | 25 | cout << ans << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TwoDifferent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | vector> A; 8 | 9 | void solve(int l, int r) 10 | { 11 | if (l == r) return; 12 | int mid = (l + r) / 2, m = (r - l + 1) / 2; 13 | solve(l, mid), solve(mid + 1, r); 14 | for (int i = l; i <= mid; i++) A.emplace_back(i, i + m); 15 | } 16 | 17 | int main() 18 | { 19 | cin >> N; 20 | 21 | int b = 0; 22 | rep(i, 30) if (N & (1 << i)) b = i; 23 | 24 | solve(0, (1 << b) - 1), solve(N - 1 - ((1 << b) - 1), N - 1); 25 | 26 | cout << A.size() << '\n'; 27 | for (auto &e : A) cout << e.first + 1 << ' ' << e.second + 1 << '\n'; 28 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TwoDistinctPoints.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Q, l1, r1, l2, r2; 5 | 6 | int main() 7 | { 8 | cin >> Q; 9 | while (Q--) 10 | { 11 | cin >> l1 >> r1 >> l2 >> r2; 12 | cout << l1 << ' ' << (l1 != l2 ? l2 : r2) << '\n'; 13 | } 14 | } -------------------------------------------------------------------------------- /Problems/Codeforces/TwoDistinctPoints.py: -------------------------------------------------------------------------------- 1 | 2 | Q = int(input()) 3 | 4 | for _ in range(Q): 5 | 6 | l1, r1, l2, r2 = list(map(int, input().split())) 7 | 8 | if l1 != l2: 9 | print(l1, l2) 10 | else: 11 | print(l1, r2) 12 | -------------------------------------------------------------------------------- /Problems/Codeforces/TwoRabbits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, x, y, a, b; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | rep(t, T) 12 | { 13 | cin >> x >> y >> a >> b; 14 | 15 | int d = y - x; 16 | if (d % (a + b) == 0) cout << d / (a + b) << '\n'; 17 | else cout << -1 << '\n'; 18 | } 19 | } -------------------------------------------------------------------------------- /Problems/Codeforces/ValeriiAgainstEveryone.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, x; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | set S; 16 | bool done = 0; 17 | rep(i, N) 18 | { 19 | cin >> x; 20 | if (S.count(x)) done = 1; 21 | else S.insert(x); 22 | } 23 | if (done) cout << "YES\n"; 24 | if (!done) cout << "NO\n"; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/Codeforces/VanyaAndLanterns.py: -------------------------------------------------------------------------------- 1 | 2 | N, L = list(map(int, input().split())) 3 | 4 | a = list(map(int, input().split())) 5 | a.sort() 6 | 7 | l = 0; r = 1e9 8 | for _ in range(1000): 9 | 10 | m = (l + r) * 0.5 11 | 12 | check = True 13 | t = 0 14 | for i in range(N): 15 | if t < a[i] - m: 16 | check = False 17 | t = a[i] + m 18 | if t < L: 19 | check = False 20 | 21 | if check: 22 | r = m 23 | else: 24 | l = m 25 | 26 | print(round((l + r) / 2, 12)) 27 | -------------------------------------------------------------------------------- /Problems/Codeforces/Vitamins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, ans; 7 | string S; 8 | vector C, V; 9 | 10 | void solve(int i, int v, int c) 11 | { 12 | if (v == 7) { ans = min(ans, c); return; } 13 | if (i == N) return; 14 | 15 | if ((V[i] & ~v)) solve(i + 1, v | V[i], c + C[i]); 16 | solve(i + 1, v, c); 17 | } 18 | 19 | int main() 20 | { 21 | cin >> N; 22 | 23 | C.resize(N), V.assign(N, 0); 24 | rep(i, N) 25 | { 26 | cin >> C[i] >> S; 27 | for (char c : S) V[i] |= (1 << (c - 'A')); 28 | } 29 | 30 | ans = INT_MAX; solve(0, 0, 0); 31 | cout << (ans != INT_MAX ? ans : -1) << '\n'; 32 | } -------------------------------------------------------------------------------- /Problems/Codeforces/WalkOnMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int k; 7 | 8 | int main() 9 | { 10 | cin >> k; 11 | 12 | int l; 13 | rep(i, 30) if (k & (1 << i)) l = i; 14 | 15 | cout << "2 3\n"; 16 | cout << (1 << (l + 2)) - 1 << ' ' << k << ' ' << 0 << '\n'; 17 | cout << (1 << (l + 1)) << ' ' << (1 << (l + 2)) - 1 << ' ' << k << '\n'; 18 | } -------------------------------------------------------------------------------- /Problems/Codeforces/WizardOfOrz.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N; 14 | 15 | cout << 9; 16 | if (N > 1) cout << 8; 17 | if (N > 2) rep(i, N - 2) cout << (8 + i + 1) % 10; 18 | cout << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/XORwice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, a, b; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> a >> b; 14 | 15 | cout << (a ^ b) << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/Codeforces/XXXXX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, X, a; 7 | 8 | int main() 9 | { 10 | cin >> T; 11 | while (T--) 12 | { 13 | cin >> N >> X; 14 | 15 | int d = INT_MAX, s = 0; 16 | rep(i, N) 17 | { 18 | cin >> a; 19 | s += a; 20 | if (a % X) d = min({d, i, N - i - 1}); 21 | } 22 | if (s % X == 0) 23 | { 24 | if (d == INT_MAX) cout << -1 << '\n'; 25 | else cout << N - d - 1 << '\n'; 26 | } 27 | else cout << N << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Codeforces/YetAnotherBrokenKeyboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | int n, k; 8 | char c; 9 | string s; 10 | set keys; 11 | 12 | int main() 13 | { 14 | cin >> n >> k; 15 | cin >> s; 16 | 17 | rep(i, k) 18 | { 19 | cin >> c; 20 | keys.insert(c); 21 | } 22 | 23 | ll ans = 0; 24 | ll counter = 0; 25 | 26 | for (char ch : s) 27 | { 28 | if (keys.count(ch)) 29 | counter++; 30 | else 31 | { 32 | ans += (counter * (counter + 1)) / 2; 33 | counter = 0; 34 | } 35 | } 36 | ans += (counter * (counter + 1)) / 2; 37 | 38 | cout << ans << '\n'; 39 | } -------------------------------------------------------------------------------- /Problems/Codeforces/YetAnotherMemeProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long T, A, B; 5 | 6 | int main() 7 | { 8 | cin >> T; 9 | 10 | while (T--) 11 | { 12 | cin >> A >> B; 13 | 14 | long long B_ = log10(B + 1); 15 | 16 | long long ans = B_ * A; 17 | 18 | cout << ans << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/Codeforces/YoungExplorers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector e; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N; 15 | 16 | e.resize(N); 17 | rep(i, N) cin >> e[i]; 18 | 19 | sort(e.begin(), e.end()); 20 | 21 | int g = 0, ans = 0; 22 | for (int p : e) 23 | { 24 | g++; 25 | if (g >= p) { ans++; g = 0; } 26 | } 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Facebook/2020/Alchemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, A; 7 | char aux; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cout << "Case #" << t + 1 << ": "; 15 | 16 | cin >> N; A = 0; 17 | rep(i, N) { cin >> aux; A += (aux == 'A'); } 18 | if (abs(A - (N - A)) == 1) cout << "Y\n"; 19 | else cout << "N\n"; 20 | } 21 | } -------------------------------------------------------------------------------- /Problems/Google/CodeJam/NestingDepth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T; 7 | string s; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> s; 15 | string A; 16 | 17 | int l = 0; 18 | for (char c : s) 19 | { 20 | int d = (c - '0') - l; 21 | if (d > 0) rep(_, d) A += '('; 22 | else rep(_, -d) A += ')'; 23 | A += c; 24 | l = (c - '0'); 25 | } 26 | rep(_, l) A += ')'; 27 | 28 | cout << "Case #" << t + 1 << ": " << A << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Google/CodeJam/Vestigium.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N; 7 | vector> M; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(t, T) 13 | { 14 | cin >> N; 15 | 16 | M.assign(N, vector(N)); 17 | rep(i, N) rep(j, N) cin >> M[i][j]; 18 | 19 | int R = 0, C = 0; 20 | rep(i, N) 21 | { 22 | set sr, sc; 23 | rep(j, N) { sr.insert(M[i][j]); sc.insert(M[j][i]); } 24 | if (sr.size() < N) R++; 25 | if (sc.size() < N) C++; 26 | } 27 | 28 | int k = 0; 29 | rep(i, N) k += M[i][i]; 30 | 31 | cout << "Case #" << t + 1 << ": " << k << ' ' << R << ' ' << C << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Google/Kickstart/ATMQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N, X; 7 | vector A, C; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | rep(tt, T) 13 | { 14 | cout << "Case #" << tt + 1 << ": "; 15 | 16 | cin >> N >> X; 17 | 18 | A.resize(N); 19 | rep(i, N) cin >> A[i]; 20 | 21 | vector> S; 22 | rep(i, N) S.emplace_back((A[i] - 1) / X, i); 23 | 24 | sort(S.begin(), S.end()); 25 | 26 | for (auto &e : S) cout << e.second + 1 << ' '; 27 | cout << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/HackerRank/TheCoinChangeProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | using ll = long long; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | int N, M; 8 | vector C; 9 | 10 | ll DP[51][251]; 11 | 12 | ll dp(int i, int c) 13 | { 14 | if (c < 0) return 0; 15 | if (c == 0) return 1; 16 | if (i == M) return 0; 17 | 18 | if (DP[i][c] != -1) return DP[i][c]; 19 | 20 | return DP[i][c] = dp(i, c - C[i]) + dp(i + 1, c); 21 | } 22 | 23 | int main() 24 | { 25 | cin >> N >> M; 26 | 27 | C.resize(M); 28 | rep(i, M) cin >> C[i]; 29 | 30 | memset(DP, -1, sizeof DP); 31 | cout << dp(0, N) << '\n'; 32 | } -------------------------------------------------------------------------------- /Problems/ICPC/AutomatedCheckingMachine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | bool v1[5], v2[5]; 7 | 8 | int main() 9 | { 10 | while (cin >> v1[0]) 11 | { 12 | rep(i, 4) cin >> v1[i + 1]; 13 | rep(i, 5) cin >> v2[i]; 14 | bool done = false; 15 | rep(i, 5) 16 | { 17 | if (v1[i] == v2[i]) 18 | { 19 | cout << "N\n"; 20 | done = true; 21 | break; 22 | } 23 | } 24 | if (!done) 25 | cout << "Y\n"; 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/ICPC/Bingo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define repx(i, a, b) for (int i = (int)a; i < (int)b; i++) 6 | 7 | int n, b; 8 | vector numbers; 9 | set diffs; 10 | 11 | int main() 12 | { 13 | while (cin >> n >> b) 14 | { 15 | if (n == 0 && b == 0) 16 | break; 17 | 18 | numbers.resize(b); 19 | rep(i, b) cin >> numbers[i]; 20 | 21 | rep(i, b) repx(j, i + 1, b) 22 | diffs.insert(abs(numbers[i] - numbers[j])); 23 | 24 | if (diffs.size() == n) 25 | cout << "Y\n"; 26 | else 27 | cout << "N\n"; 28 | 29 | diffs.clear(); 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/ICPC/BlackAndWhiteStones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | typedef long long ll; 6 | 7 | ll A, B; 8 | string s; 9 | 10 | int main() 11 | { 12 | while (cin >> A >> B >> s) 13 | { 14 | int b = 0; 15 | rep(i, s.size()) if (s[i] == 'B') b++; 16 | 17 | ll ans = LLONG_MAX; 18 | rep(i, b + 1) 19 | { 20 | ll m = (ll)i * A; 21 | int ind = i; 22 | rep(j, s.size()) if (s[j] == 'B' && ind < b && j > ind++) 23 | m += (A - B) * (ll)(j - (ind - 1)); 24 | 25 | ans = min(ans, m); 26 | } 27 | 28 | cout << ans << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/ICPC/CodeLock.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BenjaminRubio/CompetitiveProgramming/866c9ead9f7682707003f0047d17ed5f3e834758/Problems/ICPC/CodeLock.cpp -------------------------------------------------------------------------------- /Problems/ICPC/DAsInDaedalus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, M, B, first, x; 7 | int c[5] = {1, 10, 100, 1000, 10000}; 8 | 9 | int main() 10 | { 11 | while (cin >> N >> M) 12 | { 13 | long long ans = 0; 14 | rep(i, M) 15 | { 16 | int aux = 0; 17 | cin >> B >> first; 18 | int sum = first; 19 | rep(j, N - 1) 20 | { 21 | cin >> x; 22 | sum += x; 23 | } 24 | 25 | rep(k, 5) if (sum - first + c[k] <= B) aux = max(aux, c[k] - first * (sum <= B)); 26 | 27 | ans += aux; 28 | } 29 | 30 | cout << ans << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/ICPC/FlowersFlourishFromFrance.py: -------------------------------------------------------------------------------- 1 | 2 | while True: 3 | 4 | line = list(input().lower().split()) 5 | 6 | if line == ['*']: 7 | break 8 | 9 | yes = True 10 | for word in line: 11 | if word[0] != line[0][0]: 12 | print("N") 13 | yes = False 14 | break 15 | if yes: 16 | print("Y") 17 | -------------------------------------------------------------------------------- /Problems/ICPC/HardChoice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int C, B, P, c, b, p; 5 | 6 | int main() 7 | { 8 | while (cin >> C >> B >> P) 9 | { 10 | cin >> c >> b >> p; 11 | 12 | cout << max(c - C, 0) + max(b - B, 0) + max(p - P, 0) << '\n'; 13 | } 14 | } -------------------------------------------------------------------------------- /Problems/ICPC/HeIsOffside.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int A, D, x; 7 | 8 | int main() 9 | { 10 | while (cin >> A >> D) 11 | { 12 | if (A == 0 && D == 0) 13 | break; 14 | 15 | int a = 1e5; 16 | rep(i, A) 17 | { 18 | cin >> x; 19 | a = min(a, x); 20 | } 21 | 22 | int c = 0; 23 | rep(i, D) 24 | { 25 | cin >> x; 26 | c += (a < x); 27 | } 28 | 29 | if (c > D - 2) 30 | cout << "Y\n"; 31 | else 32 | cout << "N\n"; 33 | } 34 | } -------------------------------------------------------------------------------- /Problems/ICPC/HelpCupid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | vector T; 8 | 9 | int dist(int i, int j) 10 | { 11 | return min(abs(i - j), 24 - abs(i - j)); 12 | } 13 | 14 | int main() 15 | { 16 | while (cin >> N) 17 | { 18 | T.resize(N); 19 | rep(i, N) cin >> T[i]; 20 | 21 | sort(T.begin(), T.end()); 22 | 23 | int ans1 = 0; 24 | rep(i, N / 2) ans1 += dist(T[2 * i], T[(2 * i + 1) % N]); 25 | int ans2 = 0; 26 | rep(i, N / 2) ans2 += dist(T[(2 * i + 1) % N], T[(2 * i + 2) % N]); 27 | 28 | cout << min(ans1, ans2) << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/ICPC/HoursAndMinutes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | set s; 7 | int ang; 8 | 9 | int main() 10 | { 11 | rep(i, 24) rep(j, 60) 12 | { 13 | int p1 = j; 14 | int p2 = 5 * i + j / 12; 15 | 16 | s.insert(min(6 * abs(p1 - p2), 6 * (p1 + p2 - 60))); 17 | } 18 | 19 | while (cin >> ang) 20 | { 21 | if (s.count(ang)) 22 | cout << "Y\n"; 23 | else 24 | cout << "N\n"; 25 | } 26 | } -------------------------------------------------------------------------------- /Problems/ICPC/IdentifyingTea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, x; 5 | 6 | int main() 7 | { 8 | while (cin >> T) 9 | { 10 | int ans = 0; 11 | for (int i = 0; i < 5; i++) 12 | { 13 | cin >> x; 14 | if (x == T) 15 | ans++; 16 | } 17 | 18 | cout << ans << '\n'; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/ICPC/JingleComposing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | map duration = {{'W', 64}, {'H', 32}, {'Q', 16}, {'E', 8}, {'S', 4}, {'T', 2}, {'X', 1}}; 5 | string composition; 6 | 7 | int main() 8 | { 9 | while (cin >> composition) 10 | { 11 | if (composition == "*") 12 | break; 13 | 14 | int sum = 0; 15 | int ans = 0; 16 | for (char c : composition) 17 | { 18 | if (c == '/') 19 | { 20 | if (sum == 64) 21 | ans++; 22 | sum = 0; 23 | } 24 | else 25 | { 26 | sum += duration[c]; 27 | } 28 | } 29 | 30 | cout << ans << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/ICPC/JumpingFrog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | string S; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> N >> S; 13 | 14 | A.assign(N, 0); 15 | rep(i, N) A[i] = S[i] == 'R'; 16 | 17 | vector M(N, -1); 18 | rep(K, N) if 19 | } -------------------------------------------------------------------------------- /Problems/ICPC/OddOrEven.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, x; 7 | 8 | int main() 9 | { 10 | while (cin >> N) 11 | { 12 | if (N == 0) 13 | break; 14 | 15 | int a = 0, b = 0; 16 | rep(i, N) 17 | { 18 | cin >> x; 19 | if (x % 2 == 0) 20 | a++; 21 | } 22 | rep(i, N) 23 | { 24 | cin >> x; 25 | if (x % 2 == 1) 26 | b++; 27 | } 28 | 29 | cout << max(a, b) - min(a, b) << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/ICPC/PascalLibrary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, d, x; 7 | vector c; 8 | 9 | int main() 10 | { 11 | while (cin >> n >> d) 12 | { 13 | if (n == 0 && d == 0) 14 | break; 15 | 16 | c.assign(n, 0); 17 | 18 | rep(i, d) rep(j, n) 19 | { 20 | cin >> x; 21 | if (x) 22 | c[j]++; 23 | } 24 | 25 | rep(i, n) if (c[i] == d) 26 | { 27 | cout << "yes\n"; 28 | goto next; 29 | } 30 | 31 | cout << "no\n"; 32 | 33 | next: 34 | continue; 35 | } 36 | } -------------------------------------------------------------------------------- /Problems/Kattis/Antiarithmetic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, p; char t; 7 | vector P; 8 | 9 | int main() 10 | { 11 | ios::sync_with_stdio(0); cin.tie(0); 12 | 13 | while (cin >> N >> t && N) 14 | { 15 | P.resize(N); 16 | rep(i, N) { cin >> p; P[p] = i; } 17 | 18 | rep(i, N) for (int c = -(i / 2); c <= (N - i - 1) / 2; c++) 19 | { 20 | if (P[i + c] > P[i] && P[i + 2 * c] > P[i + c]) 21 | { cout << "no\n"; goto next; } 22 | } 23 | 24 | cout << "yes\n"; 25 | 26 | next: 27 | continue; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Kattis/CrazyDriver.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 5 | typedef long long ll; 6 | 7 | ll N; 8 | vector C, T; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | C.resize(N); T.resize(N); 15 | rep(i, N - 1) cin >> C[i]; 16 | rep(i, N) cin >> T[i]; 17 | 18 | ll ans = 0, m = 1e9, t = 0; 19 | rep(i, N) 20 | { 21 | ans += C[i]; 22 | while (T[i] - t > 0) { t += 2; ans += 2ll * m; } 23 | m = min(m, C[i]); t++; 24 | } 25 | 26 | cout << ans << '\n'; 27 | } -------------------------------------------------------------------------------- /Problems/Kattis/DistributingBallotBoxes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, B; 7 | vector P; 8 | 9 | int main() 10 | { 11 | while (cin >> N >> B && N != -1) 12 | { 13 | P.resize(N); 14 | rep(i, N) cin >> P[i]; 15 | 16 | int l = 1, r = 5000000; 17 | while (l < r) 18 | { 19 | int m = (l + r) / 2; 20 | 21 | int aux = 0; 22 | rep(i, N) aux += P[i] / m + bool(P[i] % m); 23 | 24 | if (aux <= B) r = m; 25 | else l = m + 1; 26 | } 27 | 28 | cout << l << '\n'; 29 | } 30 | } -------------------------------------------------------------------------------- /Problems/Kattis/DistributingBallotBoxes.py: -------------------------------------------------------------------------------- 1 | 2 | while True: 3 | 4 | N, B = list(map(int, input().split())) 5 | 6 | if N == -1: 7 | break 8 | 9 | P = [0] * N 10 | for i in range(N): 11 | P[i] = int(input()) 12 | 13 | l = 1; r = 5000000 14 | while l < r: 15 | 16 | m = (l + r) // 2 17 | 18 | aux = 0 19 | for i in range(N): 20 | aux += P[i] // m + bool(P[i] % m) 21 | 22 | if aux <= B: 23 | r = m 24 | else: 25 | l = m + 1 26 | 27 | print(l) 28 | input() 29 | -------------------------------------------------------------------------------- /Problems/Kattis/EvenUpSolitaire.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, A; 7 | 8 | int main() 9 | { 10 | cin >> N; 11 | 12 | int ans = 0; 13 | 14 | int l = -1, c = 0; 15 | rep(_, N) 16 | { 17 | cin >> A; 18 | if ((A & 1) == l) c++; 19 | else 20 | { 21 | if (c & 1) ans++, c = 1; 22 | else if (ans) c = 2, ans--; 23 | else c = 1; 24 | l = (A & 1); 25 | } 26 | } 27 | if (c & 1) ans++; 28 | 29 | cout << ans << '\n'; 30 | } -------------------------------------------------------------------------------- /Problems/Kattis/ForestForTheTrees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll xb, yb, x_1, y_1, x_2, y_2, x_, y_, m; 8 | 9 | int main() 10 | { 11 | cin >> xb >> yb >> x_1 >> y_1 >> x_2 >> y_2; 12 | 13 | ll g = __gcd(xb, yb); 14 | 15 | if (g == 1) { cout << "Yes\n"; return 0; } 16 | 17 | x_ = xb / g, y_ = yb / g; 18 | if (x_ < x_1 || y_ < y_1 || x_ > x_2 || y_ > y_2) 19 | { cout << "No\n" << x_ << ' ' << y_ << '\n'; return 0; } 20 | m = min(x_2 / x_, y_2 / y_) + 1, x_ *= m, y_ *= m; 21 | if (m < g && (x_ < x_1 || y_ < y_1 || x_ > x_2 || y_ > y_2)) 22 | { cout << "No\n" << x_ << ' ' << y_ << '\n'; return 0; } 23 | cout << "Yes\n"; 24 | } -------------------------------------------------------------------------------- /Problems/Kattis/GreedyPolygons.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | const double PI = 3.141592653589793238462643383279502884L; 7 | 8 | int T; 9 | double n, l, d, g; 10 | 11 | int main() 12 | { 13 | cout.setf(ios::fixed); 14 | cout.precision(10); 15 | 16 | cin >> T; 17 | while (T--) 18 | { 19 | cin >> n >> l >> d >> g; 20 | d *= g; 21 | 22 | double a = l * l * n / (4. * tan(PI / n)); 23 | 24 | a += n * l * d; 25 | a += PI * d * d; 26 | 27 | cout << a << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /Problems/Kattis/H-Index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n; 7 | vector c; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | c.resize(n); 14 | rep(i, n) cin >> c[i]; 15 | 16 | sort(c.begin(), c.end(), greater<>()); 17 | 18 | int H = -1; 19 | rep(i, n) H = max(H, min(c[i], i + 1)); 20 | 21 | cout << H << '\n'; 22 | } -------------------------------------------------------------------------------- /Problems/Kattis/H-Index.py: -------------------------------------------------------------------------------- 1 | 2 | n = int(input()) 3 | 4 | c = [] 5 | for i in range(n): 6 | c.append(int(input())) 7 | 8 | c.sort(reverse=True) 9 | 10 | H = -1 11 | for i in range(n): 12 | H = max(H, min(c[i], i + 1)) 13 | 14 | print(H) -------------------------------------------------------------------------------- /Problems/Kattis/MagicalCows.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (ll i = 0ll; i < (ll)n; i++) 5 | typedef long long ll; 6 | 7 | ll C, N, M, D; 8 | vector c; 9 | 10 | int main() 11 | { 12 | cin >> C >> N >> M; 13 | 14 | c.resize(N); 15 | rep(i, N) cin >> c[i]; 16 | 17 | vector d(N); 18 | rep(i, N) rep(j, 12) if (c[i] * (1ll << (j + 1ll)) > C) 19 | { 20 | d[i] = j + 1ll; 21 | break; 22 | } 23 | 24 | rep(_, M) 25 | { 26 | cin >> D; 27 | 28 | ll ans = 0ll; 29 | rep(i, N) ans += (1ll << (max(0ll, D - d[i] + 1))); 30 | 31 | cout << ans << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/Kattis/NeedForSpeed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | double T; 8 | vector D, V; 9 | 10 | int main() 11 | { 12 | cout.setf(ios::fixed); cout.precision(10); 13 | 14 | cin >> N >> T; 15 | 16 | double l = DBL_MAX, r = 10000000; 17 | D.resize(N); V.resize(N); 18 | rep(i, N) cin >> D[i] >> V[i]; 19 | 20 | rep(i, N) l = min(l, V[i]); 21 | l = -l; 22 | 23 | rep(_, 1000) 24 | { 25 | double m = (l + r) * 0.5; 26 | 27 | double t = 0; 28 | rep(i, N) t += D[i] / (V[i] + m); 29 | 30 | if (t < T) r = m; 31 | else l = m; 32 | } 33 | 34 | cout << (l + r) * 0.5 << '\n'; 35 | } -------------------------------------------------------------------------------- /Problems/Kattis/NeedForSpeed.py: -------------------------------------------------------------------------------- 1 | 2 | N, T = list(map(int, input().split())) 3 | 4 | l = 1e9; r = 1e7 5 | D = [0] * N; V = [0] * N 6 | for i in range(N): 7 | D[i], V[i] = list(map(int, input().split())) 8 | 9 | for i in range(N): 10 | l = min(l, V[i]) 11 | l = -l 12 | 13 | for _ in range(1000): 14 | 15 | m = (l + r) * 0.5 16 | 17 | t = 0 18 | for i in range(N): 19 | t += D[i] / (V[i] + m) 20 | 21 | if t < T: 22 | r = m 23 | else: 24 | l = m 25 | 26 | print(round((l + r) / 2, 10)) 27 | -------------------------------------------------------------------------------- /Problems/Kattis/PizzaHawaii.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | T = int(input()) 4 | 5 | for _ in range(T): 6 | 7 | N = int(input()) 8 | 9 | S1 = defaultdict(list); S2 = defaultdict(list) 10 | for i in range(N): 11 | name = input() 12 | 13 | line = input().strip().split() 14 | for name in line[1:]: 15 | S1[name].append(i) 16 | 17 | line = input().strip().split() 18 | for name in line[1:]: 19 | S2[name].append(i) 20 | 21 | ans = [] 22 | for e1 in S1.items(): 23 | for e2 in S2.items(): 24 | if e1[1] == e2[1]: 25 | ans.append((e1[0], e2[0])) 26 | 27 | ans.sort() 28 | 29 | for e in ans: 30 | print("({}, {})".format(*e)) 31 | print() 32 | -------------------------------------------------------------------------------- /Problems/Kattis/PolynomialMultiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int T, N1, N2; 7 | vector C1, C2, ans; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | while (T--) 13 | { 14 | cin >> N1; C1.resize(N1 + 1); 15 | rep(i, N1 + 1) cin >> C1[i]; 16 | 17 | cin >> N2; C2.resize(N2 + 1); 18 | rep(i, N2 + 1) cin >> C2[i]; 19 | 20 | ans.assign(N1 + N2 + 1, 0); 21 | rep(i, N1 + 1) rep(j, N2 + 1) 22 | ans[i + j] += C1[i] * C2[j]; 23 | 24 | cout << N1 + N2 << '\n'; 25 | rep(i, N1 + N2 + 1) cout << ans[i] << ' '; 26 | cout << '\n'; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Kattis/PolynomialMultiplication.py: -------------------------------------------------------------------------------- 1 | 2 | T = int(input()) 3 | 4 | for _ in range(T): 5 | N1 = int(input()) 6 | C1 = list(map(int, input().split())) 7 | N2 = int(input()) 8 | C2 = list(map(int, input().split())) 9 | 10 | ans = [0] * (N1 + N2 + 1) 11 | for i in range(N1 + 1): 12 | for j in range(N2 + 1): 13 | ans[i + j] += C1[i] * C2[j] 14 | 15 | print(N1 + N2) 16 | print(' '.join(map(str, ans))) 17 | -------------------------------------------------------------------------------- /Problems/Kattis/PrimeReduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define rep(i, n) for (ll i = 0; i < (ll)n; i++) 6 | 7 | ll N; 8 | 9 | ll comp(ll n) 10 | { 11 | ll factors = 0; 12 | for (ll d = 2; d * d <= n; d++) while (n % d == 0) 13 | { 14 | factors += d; 15 | if ((n /= d) == 1) return factors; 16 | } 17 | if (n > 1) factors += n; 18 | return factors; 19 | } 20 | 21 | int main() 22 | { 23 | while (cin >> N && N != 4) 24 | { 25 | int c = 1; 26 | while (1) 27 | { 28 | ll aux = comp(N); 29 | if (aux == N) break; 30 | N = aux, c++; 31 | } 32 | cout << N << ' ' << c << '\n'; 33 | } 34 | } -------------------------------------------------------------------------------- /Problems/Kattis/QuickEstimates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; string S; 5 | 6 | int main() 7 | { 8 | cin >> N; 9 | while (N--) 10 | { 11 | cin >> S; 12 | cout << S.size() << '\n'; 13 | } 14 | } -------------------------------------------------------------------------------- /Problems/Kattis/ReversedBinaryNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | 8 | int main() 9 | { 10 | cin >> N; 11 | 12 | int ans = 0; 13 | while (N) 14 | { 15 | ans <<= 1; 16 | if (N & 1) ans++; 17 | N >>= 1; 18 | } 19 | 20 | cout << ans << '\n'; 21 | } -------------------------------------------------------------------------------- /Problems/Kattis/ReversedBinaryNumbers.py: -------------------------------------------------------------------------------- 1 | 2 | N = int(input()) 3 | 4 | ans = 0 5 | while N: 6 | ans <<= 1 7 | if N & 1: 8 | ans += 1 9 | N >>= 1 10 | 11 | print(ans) 12 | -------------------------------------------------------------------------------- /Problems/Kattis/StopCounting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | vector A, S; 8 | 9 | int main() 10 | { 11 | cout.setf(ios::fixed); 12 | cout.precision(10); 13 | 14 | cin >> N; 15 | 16 | double ans = 0; A.resize(N); 17 | rep(i, N) cin >> A[i]; 18 | 19 | S.assign(N + 1, 0); 20 | rep(i, N) S[i + 1] = S[i] + A[i]; 21 | 22 | rep(i, N + 1) if (i) 23 | ans = max({ans, S[i] / (double)i, (S[N] - S[N - i]) / (double)i}); 24 | 25 | cout << ans << '\n'; 26 | } -------------------------------------------------------------------------------- /Problems/Kattis/SumOfTheOthers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string line; 5 | 6 | int main() 7 | { 8 | while (getline(cin, line)) 9 | { 10 | stringstream ss(line); 11 | 12 | int sum = 0, aux; 13 | while (ss >> aux) sum += aux; 14 | cout << sum / 2 << '\n'; 15 | } 16 | } -------------------------------------------------------------------------------- /Problems/Kattis/SumOfTheOthers.py: -------------------------------------------------------------------------------- 1 | 2 | while True: 3 | try: 4 | s = sum(map(int, input().split())) # suma de todos 5 | print(s // 2) 6 | except: 7 | break -------------------------------------------------------------------------------- /Problems/Kattis/SumOfTheOthers2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | print('\n'.join([str(sum([int(x) for x in line.split()]) // 2) for line in sys.stdin])) -------------------------------------------------------------------------------- /Problems/Kattis/TouchscreenKeyboard.py: -------------------------------------------------------------------------------- 1 | 2 | K = [['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'], 3 | ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'], 4 | ['z', 'x', 'c', 'v', 'b', 'n', 'm']] 5 | 6 | X = {}; Y = {} 7 | for i in range(3): 8 | for j in range(len(K[i])): 9 | X[K[i][j]] = i 10 | Y[K[i][j]] = j 11 | 12 | T = int(input()) 13 | 14 | for _ in range(T): 15 | 16 | s, N = input().split() 17 | 18 | N = int(N) 19 | 20 | ans = [] 21 | for __ in range(N): 22 | 23 | w = input().strip() 24 | 25 | score = 0 26 | for i in range(len(w)): 27 | score += abs(X[w[i]] - X[s[i]]) + abs(Y[w[i]] - Y[s[i]]) 28 | 29 | ans.append((score, w)) 30 | 31 | ans.sort() 32 | 33 | for e in ans: 34 | print(e[1], e[0]) 35 | -------------------------------------------------------------------------------- /Problems/Kattis/Zagrade.py: -------------------------------------------------------------------------------- 1 | 2 | S = input().strip() 3 | N = len(S) 4 | 5 | s = [] 6 | P = [] 7 | for i in range(N): 8 | if S[i] == '(': 9 | s.append(i) 10 | if S[i] == ')': 11 | P.append((s.pop(), i)) 12 | 13 | M = len(P) 14 | 15 | ans = set() 16 | for b in range((1 << M) - 1): 17 | out = set() 18 | for i in range(M): 19 | if not (b & (1 << i)): 20 | out.add(P[i][0]) 21 | out.add(P[i][1]) 22 | 23 | aux = "" 24 | for i in range(N): 25 | if i not in out: 26 | aux += S[i] 27 | 28 | ans.add(aux) 29 | 30 | for aux in sorted(list(ans)): 31 | print(aux) 32 | -------------------------------------------------------------------------------- /Problems/LeetCode/InsertDeleteGetRandom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class RandomizedSet 5 | { 6 | unordered_map m; 7 | public: 8 | RandomizedSet() {} 9 | 10 | bool insert(int val) 11 | { 12 | if (m[val]) return 0; 13 | else m[val] = 1; 14 | return 1; 15 | } 16 | 17 | bool remove(int val) 18 | { 19 | if (!m[val]) return 0; 20 | else m[val] = 0; 21 | return 1; 22 | } 23 | 24 | int getRandom() 25 | { 26 | auto it = m.begin(); 27 | advance(it, rand() % m.size()); 28 | while (!it->second) 29 | { 30 | it = m.begin(); 31 | advance(it, rand() % m.size()); 32 | } 33 | return it->first; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Problems/Matcomgrader/EggfruitCake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int S; 7 | string B; 8 | 9 | int main() 10 | { 11 | cin >> B >> S; 12 | int n = B.size(); 13 | 14 | long long ans = 0; 15 | int last = -1; 16 | rep(i, n + S) 17 | { 18 | if (last != -1 && last <= i - S) 19 | last = -1; 20 | if (B[i % n] == 'E') 21 | { 22 | if (i >= S) 23 | ans += S; 24 | last = i; 25 | } 26 | else if (last != -1 && i >= S) 27 | ans += S - (i - last); 28 | } 29 | 30 | cout << ans << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/Matcomgrader/FabricatingSculptures.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 1e9 + 7; 5 | int s, b; 6 | vector> DP; 7 | 8 | int dp(int b, int s) 9 | { 10 | if (b < 0 || s <= 0) 11 | return 0; 12 | if (b == 0 || s == 1) 13 | return 1; 14 | if (DP[b][s] != -1) 15 | return DP[b][s]; 16 | int ans = dp(b - s, s); 17 | ans = (ans + dp(b, s - 1)) % MOD; 18 | ans = (ans + dp(b, s - 1)) % MOD; 19 | return DP[b][s] = ((ans - dp(b, s - 2)) % MOD + MOD) % MOD; 20 | } 21 | 22 | int main() 23 | { 24 | cin >> s >> b; 25 | DP.assign(b + 1, vector(s + 1, -1)); 26 | 27 | cout << dp(b - s, s) << '\n'; 28 | } -------------------------------------------------------------------------------- /Problems/Matcomgrader/MountainRanges.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define repx(i, a, b) for (int i = (int)a; i < (int)b; i++) 6 | 7 | int N, X; 8 | vector A; 9 | 10 | int main() 11 | { 12 | cin >> N >> X; 13 | 14 | A.resize(N); 15 | rep(i, N) cin >> A[i]; 16 | 17 | int ans = 0; 18 | rep(i, N) 19 | { 20 | bool done = false; 21 | repx(j, i, N - 1) if (A[j + 1] - A[j] > X) 22 | { 23 | ans = max(ans, j - i + 1); 24 | done = true; 25 | break; 26 | } 27 | if (!done) 28 | ans = max(ans, N - i); 29 | } 30 | 31 | cout << ans << '\n'; 32 | } -------------------------------------------------------------------------------- /Problems/Otros/MemoryAndTrident.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string s; 5 | 6 | map m = {{'L', 0}, {'R', 0}, {'U', 0}, {'D', 0}}; 7 | 8 | int main() 9 | { 10 | cin >> s; 11 | 12 | if (s.size() % 2) 13 | { 14 | cout << -1 << '\n'; 15 | return 0; 16 | } 17 | 18 | for (char c : s) m[c]++; 19 | 20 | int ans = 0; 21 | 22 | ans += abs(m['L'] - m['R']) / 2; 23 | ans += abs(m['U'] - m['D']) / 2; 24 | if ((m['L'] + m['R']) % 2) 25 | ans++; 26 | 27 | cout << ans << '\n'; 28 | } -------------------------------------------------------------------------------- /Problems/Otros/NeighborhoodWatch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | long long n, k; 7 | vector w; 8 | 9 | int main() 10 | { 11 | cin >> n >> k; 12 | 13 | w.resize(k); 14 | rep(i, n) cin >> w[i]; 15 | 16 | long long prev = 0LL; 17 | long long ans = n * (n + 1LL) / 2LL; 18 | for (long long i : w) 19 | { 20 | ans -= (i - prev - 1LL) * (i - prev) / 2LL; 21 | prev = i; 22 | } 23 | ans -= (n - prev) * (n - prev + 1LL) / 2LL; 24 | 25 | cout << ans << '\n'; 26 | } -------------------------------------------------------------------------------- /Problems/Otros/Staircases.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | using ll = long long; 5 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 6 | 7 | ll N, DP[510][510]; 8 | 9 | ll dp(int r, int h) 10 | { 11 | if (r == 0) return 1; 12 | if (r <= h) return 0; 13 | if (DP[r][h] != -1) return DP[r][h]; 14 | 15 | ll ans = 0; 16 | for (ll x = h + 1; x <= r; x++) ans += dp(r - x, x); 17 | 18 | return DP[r][h] = ans; 19 | } 20 | 21 | int main() 22 | { 23 | cin >> N; 24 | 25 | memset(DP, -1, sizeof DP); 26 | cout << dp(N, 0) - 1 << '\n'; 27 | } -------------------------------------------------------------------------------- /Problems/README.md: -------------------------------------------------------------------------------- 1 | # Problems 2 | 3 | Here I saved all code for solved or unsolved competitive programming problems. 4 | -------------------------------------------------------------------------------- /Problems/SPOJ/BusquedaBinaria.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, Q, q; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> N >> Q; 12 | 13 | A.resize(N); 14 | rep(i, N) cin >> A[i]; 15 | 16 | while (Q--) 17 | { 18 | cin >> q; 19 | 20 | int l = 0, r = N; 21 | while (l < r) 22 | { 23 | int m = (l + r) / 2; 24 | 25 | if (A[m] >= q) r = m; 26 | else l = m + 1; 27 | } 28 | 29 | if (l == N || A[l] != q) cout << -1 << '\n'; 30 | else cout << l << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/SPOJ/BusquedaBinaria.py: -------------------------------------------------------------------------------- 1 | 2 | N, Q = list(map(int, input().split())) 3 | A = list(map(int, input().split())) 4 | 5 | for _ in range(Q): 6 | 7 | q = int(input()) 8 | 9 | l = 0; r = N 10 | while l < r: 11 | m = (l + r) // 2 12 | 13 | if A[m] >= q: 14 | r = m 15 | else: 16 | l = m + 1 17 | 18 | if l == N or A[l] != q: 19 | print(-1) 20 | else: 21 | print(l) 22 | -------------------------------------------------------------------------------- /Problems/SPOJ/BusquedaBinaria2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, Q, q; 7 | vector A; 8 | 9 | int main() 10 | { 11 | cin >> N >> Q; 12 | 13 | A.resize(N); 14 | rep(i, N) cin >> A[i]; 15 | 16 | while (Q--) 17 | { 18 | cin >> q; 19 | 20 | int l = 0, r = N; 21 | while (l < r) 22 | { 23 | int m = (l + r) / 2; 24 | 25 | if (A[m] >= q + 1) r = m; 26 | else l = m + 1; 27 | } l--; 28 | 29 | if (A[l] != q) cout << -1 << '\n'; 30 | else cout << l << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /Problems/SPOJ/BusquedaBinaria2.py: -------------------------------------------------------------------------------- 1 | 2 | N, Q = list(map(int, input().split())) 3 | A = list(map(int, input().split())) 4 | 5 | for _ in range(Q): 6 | 7 | q = int(input()) 8 | 9 | l = 0; r = N 10 | while l < r: 11 | m = (l + r) // 2 12 | 13 | if A[m] >= q + 1: 14 | r = m 15 | else: 16 | l = m + 1 17 | l -= 1 18 | 19 | if A[l] != q: 20 | print(-1) 21 | else: 22 | print(l) 23 | -------------------------------------------------------------------------------- /Problems/SPOJ/LandAcquisition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int main() 7 | { 8 | 9 | } -------------------------------------------------------------------------------- /Problems/SPOJ/LeftRightOrCenter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | typedef ll T; struct P 7 | { 8 | T x, y; 9 | P() {} P(T x, T y) : x(x), y(y) {} 10 | 11 | P operator-(const P &p) const { return P(x - p.x, y - p.y); } 12 | T operator^(const P &p) const { return x * p.y - y * p.x; } 13 | }; 14 | istream &operator>>(istream &s, P &p) { return s >> p.x >> p.y; } 15 | 16 | int N; P a, b, p; 17 | 18 | int main() 19 | { 20 | cin >> a >> b >> N; 21 | while (N-- && cin >> p) 22 | { 23 | ll dir = (b - a) ^ (p - a); 24 | if (dir > 0) cout << "I\n"; 25 | else if (dir < 0) cout << "D\n"; 26 | else cout << "C\n"; 27 | } 28 | } -------------------------------------------------------------------------------- /Problems/Timus/StringTale.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int match(string &p, string &t) 7 | { 8 | int n = p.size(), m = t.size(), L[n]; L[0] = 0; 9 | rep(j, n - 1) 10 | { 11 | int i = L[j]; while (p[i] != p[j + 1] && i) i = L[i - 1]; 12 | L[j + 1] = p[i] == p[j + 1] ? i + 1 : 0; 13 | } 14 | int i = 0; rep(j, m) 15 | { 16 | while (p[i] != t[j] && i) i = L[i - 1]; 17 | if (p[i] == t[j] && ++i == n) return j - n + 1; 18 | } 19 | return -1; 20 | } 21 | 22 | int N; 23 | string S, T; 24 | 25 | int main() 26 | { 27 | cin >> N >> S >> T; 28 | rep(i, N) T.push_back(T[i]); 29 | 30 | cout << match(S, T) << '\n'; 31 | } -------------------------------------------------------------------------------- /Problems/URI/AssigningTeams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int a, b, c, d; 5 | 6 | int main() 7 | { 8 | cin >> a >> b >> c >> d; 9 | cout << abs(a + d - b - c) << '\n'; 10 | } -------------------------------------------------------------------------------- /Problems/URI/BuildingAField.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int n, x; 7 | set accums; 8 | 9 | int main() 10 | { 11 | cin >> n; 12 | 13 | int sum = 0; 14 | rep(i, n) 15 | { 16 | cin >> x; 17 | accums.insert(sum + x); 18 | sum += x; 19 | } 20 | 21 | if (sum % 2) 22 | { 23 | cout << "N\n"; 24 | return 0; 25 | } 26 | 27 | int c = 0; 28 | for (int i : accums) 29 | { 30 | if (accums.count(i + sum / 2)) 31 | c++; 32 | } 33 | 34 | if (c >= 2) 35 | cout << "Y\n"; 36 | else 37 | cout << "N\n"; 38 | } -------------------------------------------------------------------------------- /Problems/URI/DatabaseOfClients.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | string email_in, email; 6 | set s; 7 | 8 | int main() 9 | { 10 | cin >> N; 11 | 12 | while (N--) 13 | { 14 | cin >> email_in; 15 | 16 | email = ""; 17 | bool plus = false; 18 | bool arroba = false; 19 | for (char c : email_in) 20 | { 21 | if (!plus && !arroba && c != '.' && c != '+' && c != '@') 22 | email += c; 23 | if (!plus && !arroba && c == '+') 24 | plus = true; 25 | if (c == '@') 26 | arroba = true; 27 | if (arroba) 28 | email += c; 29 | } 30 | 31 | s.insert(email); 32 | } 33 | 34 | cout << s.size() << '\n'; 35 | } -------------------------------------------------------------------------------- /Problems/URI/DifferentDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int A, B, C[5001]; 7 | 8 | int main() 9 | { 10 | rep(i, 5001) if (i) 11 | { 12 | bool flag = 0; 13 | int N = i; 14 | 15 | set s; 16 | while (N) 17 | { 18 | int d = N % 10; N /= 10; 19 | if (s.count(d)) flag = 1; 20 | s.insert(d); 21 | } 22 | 23 | if (flag) C[i] = C[i - 1]; 24 | else C[i] = C[i - 1] + 1; 25 | } 26 | 27 | while (cin >> A >> B) 28 | { 29 | cout << C[B] - C[A - 1] << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /Problems/URI/FarmRobot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, C, S, c; 5 | 6 | int main() 7 | { 8 | cin >> N >> C >> S; 9 | 10 | int pos = 0; 11 | int ans = 0; 12 | while (C--) 13 | { 14 | if ((pos % N + N) % N == S - 1) 15 | ans++; 16 | cin >> c; 17 | pos += c; 18 | } 19 | if ((pos % N + N) % N == S - 1) 20 | ans++; 21 | 22 | cout << ans << '\n'; 23 | } -------------------------------------------------------------------------------- /Problems/URI/Feynman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N; 7 | 8 | int main() 9 | { 10 | while (cin >> N && N) 11 | { 12 | int ans = 0; 13 | rep(i, N) ans += (i + 1) * (i + 1); 14 | 15 | cout << ans << '\n'; 16 | } 17 | } -------------------------------------------------------------------------------- /Problems/URI/Football.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, G, s, r; 7 | vector v; 8 | 9 | int main() 10 | { 11 | while (cin >> N >> G) 12 | { 13 | int ans = 0; 14 | v.resize(N); 15 | rep(i, N) 16 | { 17 | cin >> s >> r; v[i] = s - r; 18 | ans += (v[i] > 0 ? 3 : (!v[i] ? 1 : 0)); 19 | } 20 | 21 | sort(v.rbegin(), v.rend()); 22 | 23 | int i = -1; 24 | while (G && ++i < N) 25 | { 26 | if (v[i] == 0) G--, ans += 2; 27 | if (v[i] < 0 && G == -v[i]) G = 0, ans += 1; 28 | if (v[i] < 0 && G > -v[i]) G -= -v[i] + 1, ans += 3; 29 | } 30 | 31 | cout << ans << '\n'; 32 | } 33 | } -------------------------------------------------------------------------------- /Problems/URI/HotelRewards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | int N, K, P; 7 | 8 | int main() 9 | { 10 | cin >> N >> K; 11 | 12 | priority_queue, greater> q; 13 | 14 | int ans = 0; 15 | rep(i, N) 16 | { 17 | cin >> P; 18 | 19 | if (i - (K + 1) * q.size() >= K) q.push(P); 20 | else if (i - (K + 1) * q.size() < K and !q.empty() and P > q.top()) 21 | { 22 | ans += q.top(); 23 | q.pop(); 24 | q.push(P); 25 | } 26 | else ans += P; 27 | } 28 | 29 | cout << ans << '\n'; 30 | } -------------------------------------------------------------------------------- /Problems/URI/MountMarathon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | #define repinv(i, n) for (int i = (int)n - 1; i >= 0; i--) 6 | 7 | int N; 8 | vector C; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | C.resize(N); 15 | rep(i, N) cin >> C[i]; 16 | 17 | rep(i, 52) repinv(j, C.size() - 1) 18 | if (C[j] == i + 1 && C[j + 1] <= C[j]) 19 | C.erase(C.begin() + j + 1); 20 | 21 | cout << C.size() << '\n'; 22 | } -------------------------------------------------------------------------------- /Problems/URI/SymmetricalPizza.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | scanf( "%d.%d", &a, &b); 8 | int R = 100 * a + b; 9 | cout << 36000 / __gcd(36000, R) << '\n'; 10 | } -------------------------------------------------------------------------------- /Problems/UVA/SoyaMilk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i, n) for (int i = 0; i < (int)n; i++) 5 | 6 | const double PI = acos(-1.0); 7 | 8 | double l, w, h, t; 9 | 10 | int main() 11 | { 12 | cout.setf(ios::fixed); 13 | cout.precision(3); 14 | 15 | while (cin >> l >> w >> h >> t) 16 | { 17 | t *= PI / 180.; 18 | double a = atan2(h, l); 19 | 20 | if (t < a) cout << w * l * (h - l * tan(t) * 0.5); 21 | else cout << w * h * h * tan(PI * 0.5 - t) * 0.5; 22 | 23 | cout << " mL\n"; 24 | } 25 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive Programming Repository 2 | 3 | This repository contains all the material I used while preparing for ICPC competitions. 4 | --------------------------------------------------------------------------------