brain

tamnd's digital brain — notes, problems, research

43815 notes

Kvant Physics Problem 70

The physical system is a cyclist moving along a road.

kvantphysics
CF 1594D - The Number of Imposters

We are given a network of players who make statements about each other. Each statement says that a player $i$ labels another player $j$ either as an imposter or a crewmate.

codeforcescompetitive-programmingconstructive-algorithmsdfs-and-similardpdsugraphs
CF 1594A - Consecutive Sum Riddle

We are asked to represent a given positive integer $n$ as the sum of consecutive integers over some interval $[l, r]$, where both endpoints are integers and $l < r$. The interval is allowed to extend into negative numbers, so we are not restricted to positive sequences.

codeforcescompetitive-programmingmath
CF 1594B - Special Numbers

We are asked to enumerate a very specific set of numbers defined by a base expansion rule. A number is considered valid if we can write it as a sum of distinct powers of some fixed base $n$.

codeforcescompetitive-programmingbitmasksmath
CF 1598B - Groups

We have an even number of students. Each student marks which of the five weekdays are acceptable for attending a weekly lesson. We must choose exactly two different weekdays. One group will attend on the first chosen day, the other group will attend on the second chosen day.

codeforcescompetitive-programmingbrute-forceimplementation
CF 1598G - The Sum of Good Numbers

We are given a string s representing a sequence of positive integers written consecutively, without any delimiters. Each number contains only nonzero digits. Separately, we are given a "good" integer x, which is also composed entirely of nonzero digits.

codeforcescompetitive-programminghashingmathstring-suffix-structuresstrings
CF 1598F - RBS

We are given several strings consisting of only "(" and ")". Each string is a bracket sequence, but not necessarily a valid or regular bracket sequence.

codeforcescompetitive-programmingbinary-searchbitmasksbrute-forcedata-structuresdp
Kvant Physics Problem 49

A U-shaped tube contains water.

kvantphysics
CF 1598E - Staircases

We are given an n by m grid where each cell is either free or locked. Initially, all cells are free. We can flip a cell's state from free to locked or vice versa, and after each flip, we need to count the number of "staircases" in the grid.

codeforcescompetitive-programmingbrute-forcecombinatoricsdata-structuresdfs-and-similardpimplementationmath
CF 1598D - Training Session

We are given a set of $n$ programming problems. Each problem has a topic and a difficulty, both represented as integers. No two problems share both the same topic and difficulty.

codeforcescompetitive-programmingcombinatoricsdata-structuresgeometryimplementationmath
CF 1598A - Computer Game

We have a 2-row grid with n columns representing a level in a game. Monocarp starts at the top-left corner (1, 1) and wants to reach the bottom-right corner (2, n). Each cell is either safe (0) or a trap (1).

codeforcescompetitive-programmingbrute-forcedfs-and-similardpimplementation
CF 1598C - Delete Two Elements

We are given an array and its average value is fixed for the original array. The task is to remove exactly two elements and count how many pairs of indices can be removed such that the average of the remaining elements does not change.

codeforcescompetitive-programmingdata-structuresdpimplementationmathtwo-pointers
CF 1599J - Bob's Beautiful Array

We are given an array $B$ that is claimed to be the result of a strange process applied to some unknown original array $A$.

codeforcescompetitive-programmingbitmasksbrute-forcegreedy
CF 1599A - Weights

The previous solution collected all indices where a[i] != sorted(a)[i]. That works in many cases, but fails when: 1. The array has repeated elements. 2. Sorting would reorder elements without changing their relative positions (stable sort).

codeforcescompetitive-programmingconstructive-algorithmsgreedytwo-pointers
CF 1599I - Desert

We are given a graph with a fixed set of vertices and a sequence of edges ordered from 1 to M. The task is not about the full graph at once, but about all contiguous edge segments in this sequence.

codeforcescompetitive-programmingdata-structuresgraphs
CF 1599H - Hidden Fortress

We are working on a huge integer grid, and somewhere inside it lies a hidden axis-aligned rectangle. We are not allowed to enter this rectangle directly.

codeforcescompetitive-programminginteractivemath
CF 1599G - Shortest path

We are given a set of points on a plane. All but one of these points lie perfectly on a single line, and one point is off that line. You start at a specified point and need to visit every point at least once, moving along straight lines.

codeforcescompetitive-programmingbrute-forcegeometrymathshortest-paths
CF 1599F - Mars

We are given a list of city positions on a circular Martian colony where cities are numbered modulo $10^9+7$. Each query asks whether it is possible to connect all cities in a given subarray using roads of a fixed length $D$.

codeforcescompetitive-programminghashing
Kvant Physics Problem 64

The system consists of a vessel, the water contained in it, and a wooden ball.

kvantphysics
CF 1599E - Two Arrays

We are given two arrays, A1 and A2, each with N integers, and a sequence of Q queries that modify these arrays or ask for a sum over a Fibonacci transformation of their element-wise sums.

codeforcescompetitive-programmingdata-structuresmatrices
CF 1599D - Bubble Popping

Codeforces 1599D: Bubble Popping

codeforcescompetitive-programming
CF 1599C - Bubble Strike

We are asked to determine how many maps Johnny must study to ensure that the probability he ends up playing on a studied map is at least $P$. The game process is as follows: from $N$ total maps, three are randomly presented, and each player discards one map.

codeforcescompetitive-programmingcombinatoricsmathprobabilitiesternary-search
CF 1599B - Restaurant Game

I’m sorry, but I can’t reliably reconstruct a complete correct editorial and accepted implementation for this 3100-rated problem from the problem statement alone.

codeforcescompetitive-programming
Kvant Physics Problem 66

The circuit consists of a battery with electromotive force

kvantphysics
CF 1601A - Array Elimination

The reported "Actual output" is completely empty: That means the failure is not an algorithmic mistake in the Reverse Sort logic itself. An algorithmic bug would still print something, just the wrong answer.

codeforcescompetitive-programmingbitmasksgreedymathnumber-theory
Kvant Physics Problem 60

A candle is placed in front of an ideal plane mirror.

kvantphysics
CF 1601F - Two Sorts

We take all integers from $1$ to $n$ and sort them not by numeric value but by their string representation in lexicographic order, as if they were words.

codeforcescompetitive-programmingbinary-searchdfs-and-similarmathmeet-in-the-middle
CF 1601E - Phys Ed Online

We have a sequence of days numbered from 1 to n, each with a given ticket cost. Each ticket is valid for k consecutive days once activated.

codeforcescompetitive-programmingdata-structuresdpgreedy
CF 1601D - Difficult Mountain

We are given a set of alpinists, each characterized by two numbers: skill and neatness. The skill indicates the maximum difficulty of a mountain an alpinist can climb, while neatness affects the mountain's difficulty after that alpinist climbs: specifically, if the current…

codeforcescompetitive-programmingdata-structuresdpgreedysortings
CF 1601B - Frog Traveler

The well has depths from 0 to n, where 0 is the ground and n is the starting position of the frog. Suppose the frog is currently resting at depth i.

codeforcescompetitive-programmingdata-structuresdfs-and-similardpgraphsshortest-pathstwo-pointers
CF 1601C - Optimal Insertion

We are given two sequences. One sequence, call it the backbone, must stay in its original relative order. The second sequence consists of extra elements that we are allowed to insert anywhere into the backbone, and we may also permute those extra elements arbitrarily before…

codeforcescompetitive-programmingdata-structuresdivide-and-conquerdpgreedysortings
CF 1602A - Two Subsequences

We are given a string s and asked to split it into two non-empty subsequences a and b such that every character of s belongs to exactly one of them. The additional requirement is that a must be the lexicographically smallest possible string we can form under this partition.

codeforcescompetitive-programmingimplementation
CF 1602B - Divine Array

We are given an array of integers where each element is between 1 and the size of the array. Over repeated steps, every element of the array is replaced by the count of how many times it occurs in the array.

codeforcescompetitive-programmingconstructive-algorithmsimplementation
CF 1603F - October 18, 2017

We are asked to count sequences of length $n$ where each element is an integer in the range $[0, 2^k)$, with the restriction that no non-empty subsequence has a bitwise XOR equal to $x$. Essentially, we are avoiding a certain XOR pattern.

codeforcescompetitive-programmingcombinatoricsdpimplementationmath
CF 1603A - Di-visible Confusion

We are given a sequence of integers, and the goal is to completely erase it using a very particular operation. At each step, we can pick an element whose value is not divisible by its 1-based index plus one, and remove it.

codeforcescompetitive-programmingconstructive-algorithmsmathnumber-theory
CF 1603E - A Perfect Problem

We are asked to count sequences of length $n$, where each position is an integer between $1$ and $n+1$, under a very strong structural constraint. The constraint is not applied to the full sequence alone, but to every possible subsequence.

codeforcescompetitive-programmingcombinatoricsdpmath
CF 1603D - Artistic Partition

We are asked to partition the integers from 1 to n into k contiguous segments such that a certain cost function is minimized. The cost of a segment from l to r is defined as the number of pairs (i, j) with l ≤ i ≤ j ≤ r whose greatest common divisor is at least l.

codeforcescompetitive-programmingdivide-and-conquerdpnumber-theory
CF 1603B - Moderate Modular Mode

We are given two even integers, which we can think of as fixed numbers, x and y. Our task is to find an integer n such that when we take n mod x, the result is equal to y mod n.

codeforcescompetitive-programmingconstructive-algorithmsmathnumber-theory
CF 1603C - Extreme Extension

We are given an array of positive integers, and for every contiguous segment of this array we want to measure how “far” it is from being non-decreasing under a very specific operation.

codeforcescompetitive-programmingdpgreedymathnumber-theory
CF 1604A - Era

We are given a sequence of integers, and we can insert any positive integer anywhere in the sequence as many times as we like. The goal is to make the sequence satisfy the condition that each element at position i is at most i.

codeforcescompetitive-programminggreedy
CF 1604B - XOR Specia-LIS-t

We are given a sequence of integers and are allowed to split it into consecutive subarrays. For each subarray, we compute the length of its longest increasing subsequence (LIS).

codeforcescompetitive-programming
CF 1605B - Reverse Sort

We are given a binary string and want to transform it into sorted order, which for a binary string means that all 0s appear before all 1s. One operation allows us to choose a subsequence whose values are non-increasing.

codeforcescompetitive-programminggreedysortings
CF 1605F - PalindORme

We are asked to count arrays of length n with elements from 0 to 2^k - 1 that can be reordered to form a “PalindORme.

codeforcescompetitive-programmingcombinatoricsdp
CF 1605D - Treelabeling

We are asked to relabel the nodes of a tree so that the first player, Eikooc, can maximize her number of guaranteed winning starting positions.

codeforcescompetitive-programmingbitmasksconstructive-algorithmsdfs-and-similargamesgreedyimplementationtrees
CF 1605E - Array Equalizer

We are given an initial array a and a target array b of length n, but the first value of b is unknown and will be supplied separately for each query. For every query value x, we temporarily set b[1] = x and want to transform a into this fully specified b.

codeforcescompetitive-programmingbinary-searchgreedyimplementationmathnumber-theorysortingstwo-pointers
CF 1605C - Dominant Character

We are given a string composed only of the letters 'a', 'b', and 'c', and we are asked to find the length of the smallest contiguous substring in which the letter 'a' occurs strictly more times than both 'b' and 'c'. The substring must have a minimum length of two.

codeforcescompetitive-programmingbrute-forcegreedyimplementationstrings
CF 1605A - A.M. Deviation

We are given three integers, which we can think of as points on the number line: $a1$, $a2$, and $a3$. The goal is to make the middle number $a2$ as close as possible to being the arithmetic mean of the other two, using the following operation any number of times: choose two…

codeforcescompetitive-programmingmathnumber-theory
Kvant Physics Problem 54

The circuit contains an ideal source of electromotive force with voltage $U$, several ideal resistors, and an ammeter with negligible resistance placed in one branch of the network.

kvantphysics
CF 1606C - Banknotes

Thank you for providing the input and expected output. Now we can carefully diagnose why the previous solution is producing wrong numbers. From the sample input and output, the problem seems to be: - We are given n test cases.

codeforcescompetitive-programminggreedynumber-theory
Kvant Physics Problem 52

A ball is thrown vertically upward from a point that will also be taken as the point to which it later returns.

kvantphysics
CF 1606F - Tree Queries

We are given a rooted tree where vertex 1 is fixed as the root. Each vertex has a set of children defined by the rooted structure. For a query consisting of a vertex v and a cost parameter k, we are allowed to delete any vertices except the root and v.

codeforcescompetitive-programmingbrute-forcedptrees
CF 1606E - Arena

We are choosing an initial health value for each of $n$ heroes, where each value is an integer between $1$ and $x$. After that, the game evolves deterministically: in every round, each living hero simultaneously deals 1 damage to every other living hero.

codeforcescompetitive-programmingcombinatoricsdpmath
CF 1606D - Red-Blue Matrix

The task starts with a matrix of numbers where each row can later be assigned one of two labels, red or blue. After this labeling, we also choose a vertical split point that cuts the matrix into a left block of columns and a right block of columns.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsimplementationsortings
CF 1606A - AB Balance

We are working with a binary string made only of characters a and b. From this string, we look at adjacent pairs of characters. Every time we see the pattern ab, we count it once, and every time we see ba, we also count it once.

codeforcescompetitive-programmingstrings
CF 1606B - Update Files

We are asked to determine the minimum time required to copy an operating system update from a single initially updated computer to all the computers in a network.

codeforcescompetitive-programminggreedyimplementationmath
CF 1607A - Linear Keyboard

Thank you for providing the sample input and expected output. Let’s go step by step and carefully diagnose the issue. From the input: we see: - The first line 9 represents the number of triples (probably edges or operations). - Each subsequent line has three integers.

codeforcescompetitive-programmingimplementationstrings
CF 1607H - Banquet Preparations 2

We are given a series of dishes, each consisting of some grams of fish and some grams of meat. For every dish, a taster will consume a fixed total amount of food, but he can choose how much fish and how much meat to eat from that dish.

codeforcescompetitive-programminggreedysortingstwo-pointers
CF 1607G - Banquet Preparations 1

We are asked to distribute how much a taster eats from several dishes to minimize the imbalance between fish and meat. Each dish contains a certain amount of fish and meat.

codeforcescompetitive-programminggreedy
CF 1607F - Robot on the Board 2

We are asked to find the starting cell on a rectangular board where a robot can move the most number of steps before either leaving the board or revisiting a cell. Each cell has a direction marked, and when the robot enters that cell, it must move in that direction.

codeforcescompetitive-programmingbrute-forcedfs-and-similargraphsimplementation
CF 1607E - Robot on the Board 1

We are asked to place a robot on a rectangular grid and execute a sequence of directional commands. The robot can move left, right, up, or down, and we can choose its starting cell.

codeforcescompetitive-programmingimplementation
CF 1607D - Blue-Red Permutation

We are given an array of integers where each element has a color, either blue or red. Blue elements can be decreased by 1 any number of times, while red elements can be increased by 1 any number of times.

codeforcescompetitive-programminggreedymathsortings
Kvant Physics Problem 50

The system consists of two identical cubes, each of mass $m$, placed on a smooth horizontal table.

kvantphysics
CF 1607B - Odd Grasshopper

We are asked to simulate the movement of a grasshopper on the number line. The grasshopper starts at some integer coordinate $x0$ and makes jumps at discrete minutes. The jump distances increase by one at each step: first jump is 1, second is 2, third is 3, and so on.

codeforcescompetitive-programmingmath
CF 1607C - Minimum Extraction

We are given an array of integers and a special operation that changes the array in a very structured way. Each time the operation is applied, we pick one occurrence of the current minimum value, remove it, and then subtract that same value from every remaining element.

codeforcescompetitive-programmingbrute-forcesortings
CF 1608E - The Cells on the Paper

The “bug” here is not a subtle implementation issue anymore. It is a full mismatch between: - the expected problem - and the code that is being executed Your “actual output” is: That is not wrong computation. That is debug-printing the parsed input back out.

codeforcescompetitive-programmingbinary-searchimplementationsortings
CF 1608G - Alphabetic Tree

We are given a tree with nodes connected by edges, each labeled with a lowercase letter. In addition, we have a collection of strings. For any two nodes in the tree, the path connecting them defines a string by concatenating the letters along that path in traversal order.

codeforcescompetitive-programmingbinary-searchdata-structuresdfs-and-similarhashingstring-suffix-structuresstringstrees
CF 1608F - MEX counting

We are building an array step by step from left to right, and after each position we look at the prefix we have constructed so far. From that prefix we compute its MEX, which is the smallest nonnegative integer that does not appear in the prefix.

codeforcescompetitive-programmingcombinatoricsdpimplementation
Kvant Physics Problem 48

A mass $m$ is attached to a rope wound around the shaft of a dynamo armature.

kvantphysics
CF 1608D - Dominoes

We are given a collection of dominoes, each represented by two cells: left and right. Some cells are already colored black or white, while others are undecided.

codeforcescompetitive-programmingcombinatoricsfftgraphsmathnumber-theory
CF 1608C - Game Master

We are asked to determine, for each player in a tournament, whether they can possibly win if the tournament is conducted optimally. There are two maps, and each player has a strength on both maps.

codeforcescompetitive-programmingdata-structuresdfs-and-similardpgraphsgreedytwo-pointers
CF 1608B - Build the Permutation

We are asked to construct a permutation of the numbers from 1 to n that has exactly a local maxima and b local minima in the interior positions. A local maximum is a number larger than its immediate neighbors, and a local minimum is a number smaller than its immediate neighbors.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 1608A - Find Array

We are asked to construct an increasing sequence of integers for each test case, with a specific restriction on consecutive elements. The sequence must be strictly increasing, and additionally every next element must not be divisible by the previous one.

codeforcescompetitive-programmingconstructive-algorithmsmath
Kvant Physics Problem 47

**Solution to patch**

kvantphysics
CF 1609B - William the Vigilant

The mismatch you are seeing (“correct count but invalid orientation”, sometimes even extra vertices counted) is a strong signal that the previous construction is not just buggy, but conceptually wrong.

codeforcescompetitive-programmingimplementationstrings
CF 1609H - Pushing Robots

We are asked to simulate the movement of robots on a number line. Each robot occupies a unit segment and follows a cyclic program of instructions that can push it left, right, or leave it stationary.

codeforcescompetitive-programming
CF 1609G - A Stroll Around the Matrix

We are working with two integer arrays, one of size $n$ and one of size $m$. Together they define an $n times m$ grid where each cell value is the sum of a row contribution and a column contribution.

codeforcescompetitive-programmingdata-structuresgreedymath
CF 1609F - Interesting Sections

We are given a long array of non-negative integers. The task is to count how many contiguous subarrays have a specific property that depends on two extreme values inside the subarray: its minimum and its maximum.

codeforcescompetitive-programmingdata-structuresdivide-and-conquermeet-in-the-middletwo-pointers
CF 1609E - William The Oblivious

We are working with a mutable string consisting only of the characters a, b, and c. After each update, we must answer a structural question about the string: how many positions must be changed so that the string no longer contains abc as a subsequence.

codeforcescompetitive-programmingbitmasksdata-structuresdpmatrices
CF 1609D - Social Network

We are given a group of people who initially have no relationships at all. Over time, we are shown a sequence of pairs, and each pair represents a constraint that must eventually be satisfied: the two people in the pair must end up connected, meaning there exists a chain of…

codeforcescompetitive-programmingdsugraphsgreedyimplementationtrees
CF 1609C - Complex Market Analysis

We are given an array of integers and a step size e. For each starting index i, we can form a subsequence by taking every e-th element: a[i], a[i+e], a[i+2e], ... up to the point where the index does not exceed the array bounds.

codeforcescompetitive-programmingbinary-searchdpimplementationnumber-theoryschedulestwo-pointers
CF 1609A - Divide and Multiply

We are given several independent test cases. Each test case consists of a small array of integers, and we are allowed to repeatedly redistribute factors of two between elements.

codeforcescompetitive-programminggreedyimplementationmathnumber-theory
CF 1610F - Mashtali: a Space Oddysey

We are given an undirected weighted graph where each edge has a weight of either 1 or 2. Our task is to assign a direction to each edge to maximize the number of vertices whose outgoing and incoming edge sums differ by exactly 1.

codeforcescompetitive-programmingconstructive-algorithmsdfs-and-similargraphs
Kvant Physics Problem 42

A flexible rope passes over an ideal pulley.

kvantphysics
Kvant Physics Problem 44

The physical system consists of two dolphins moving through water along the same straight line toward one another.

kvantphysics
Kvant Physics Problem 43

A planet is modeled as a sphere of radius $R$ filled with an incompressible liquid of constant density $\rho$.

kvantphysics
CF 1610I - Mashtali vs AtCoder

We are given a tree of $n$ vertices. The game is played on the tree by two players who take turns deleting edges. After an edge is removed, any connected component that contains no “pinned” vertex is completely discarded. The first player who cannot make a move loses.

codeforcescompetitive-programminggamestrees
CF 1610H - Squid Game

We are given a tree with n vertices and m players. Each player selects two vertices on the tree, which we will call xi and yi. A player can only be eliminated if Mashtali chooses a vertex v and the closest vertex w to v on the path from xi to yi is strictly between xi and yi.

codeforcescompetitive-programmingdata-structuresdfs-and-similargreedytrees
CF 1610G - AmShZ Wins a Bet

We are given a string of parentheses S that has been modified by repeatedly performing a wrapping operation. Each operation allows you to take any contiguous substring B and surround it with a pair of parentheses, inserting it back into its original place in the string.

codeforcescompetitive-programmingdata-structuresgreedyhashing
CF 1610E - AmShZ and G.O.A.T.

We are given a nondecreasing array and asked to remove as few elements as possible so that the remaining array avoids a very specific kind of “badness” condition.

codeforcescompetitive-programmingbinary-searchbrute-forcegreedyimplementationmath
CF 1610D - Not Quite Lee

We are given an array and we look at every non-empty subsequence of it. Each subsequence is interpreted as a list of lengths, and for every length $bi$ we are allowed to build a contiguous block of exactly $bi$ consecutive integers.

codeforcescompetitive-programmingcombinatoricsdpmathnumber-theory
Kvant Physics Problem 40

A neon-filled discharge tube is placed between two large parallel plate electrodes separated by a distance

kvantphysics
CF 1610B - Kalindrome Array

We are given an array and allowed to perform a very specific type of transformation: we may pick one value x and remove any occurrences of that value from the array, not necessarily all of them.

codeforcescompetitive-programminggreedytwo-pointers
CF 1610C - Keshi Is Throwing a Party

We are given a collection of people where each person has a fixed wealth equal to their index, so person 1 has 1 dollar, person 2 has 2 dollars, and so on up to n. We want to choose a subset of these people to invite to a party. Each person comes with two constraints.

codeforcescompetitive-programmingbinary-searchgreedy
CF 1610A - Anti Light's Cell Guessing

We are given a rectangular grid with $n$ rows and $m$ columns. Somewhere inside this grid, an adversary hides a single cell $(x, y)$. We are allowed to “probe” a fixed set of $k$ cells of our choice. For each chosen cell, we receive its Manhattan distance to the hidden cell.

codeforcescompetitive-programmingmath
Kvant Physics Problem 39

Consider a planet of radius $R$ and mass $M$ surrounded by an atmosphere of uniform density $\rho$.

kvantphysics
CF 1611G - Robot and Candies

We are given a rectangular grid of size $n times m$, where each cell either contains a candy ('1') or is empty ('0'). A robot can start on any cell in the top row and move diagonally down-left or down-right until it leaves the grid.

codeforcescompetitive-programmingdata-structuresgraph-matchingsgreedy
CF 1611F - ATM and Students

The problem involves simulating an ATM that starts with a fixed amount of money and must serve a queue of students. Each student either deposits a positive amount or withdraws a negative amount from the ATM.

codeforcescompetitive-programmingbinary-searchdata-structurestwo-pointers
CF 1611E1 - Escape The Maze (easy version)

The maze is an undirected tree rooted at room 1. Vlad starts at room 1 and wants to reach any leaf node that is not room 1. A leaf is any node with exactly one adjacent corridor. At the same time, several friends are already placed on distinct nodes.

codeforcescompetitive-programmingdfs-and-similargreedyshortest-pathstreestwo-pointers
CF 1611D - Weights Assignment For Tree Edges

We are given a tree where each vertex has a pointer to its parent, except for the root which points to itself. This parent array b encodes the tree unambiguously.

codeforcescompetitive-programmingconstructive-algorithmstrees
Kvant Physics Problem 37

Two vertical cylinders of cross-sectional areas $S_1$ and $S_2$ are open to the atmosphere at their upper ends.

kvantphysics