brain

tamnd's digital brain — notes, problems, research

43815 notes

Kvant Physics Problem 20

The body is a long homogeneous cylinder of outer radius $R$.

kvantphysics
CF 1624E - Masha-forgetful

We are given a target digit string s and a collection of known digit strings, all of the same length. The goal is to reconstruct s as a sequence of contiguous segments, where each segment must exactly match a substring taken from one of the known strings.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsdphashingimplementationstrings
CF 1624D - Palindromes Coloring

We are given a string of lowercase letters and an integer $k$, representing the number of colors available. The task is to color the letters so that, when we group the letters by color, each group forms a palindrome after any number of swaps within the same color.

codeforcescompetitive-programmingbinary-searchgreedysortingsstrings
CF 1624B - Make AP

We are given three fixed positions in a sequence: first value a, second value b, and third value c. We are allowed exactly one modification operation: choose one of these three positions and multiply it by some positive integer m of our choice.

codeforcescompetitive-programmingimplementationmath
CF 1624C - Division by Two and Permutation

We are given an array of positive integers of size $n$, and we want to know if we can turn it into a permutation of numbers from $1$ to $n$ by repeatedly dividing elements by two. Each division operation replaces an element $ai$ with $lfloor ai / 2 rfloor$.

codeforcescompetitive-programmingconstructive-algorithmsflowsgraph-matchingsgreedymath
CF 1625C - Road Optimization

Ah, now I understand completely. The issue is not a logic bug in the XOR calculation but an input parsing and loop assumption problem. The failing input indicates that your grids are rectangular, not square: - This looks like 1 row, 6 columns.

codeforcescompetitive-programmingdp
CF 1625E2 - Cats on the Upgrade (hard version)

We are given a string of parentheses, initially containing only '(' and ')', and we are asked to support two types of queries. The first type changes specific matched parentheses into dots.

codeforcescompetitive-programmingbinary-searchdata-structuresdfs-and-similargraphstrees
Kvant Physics Problem 21

Two identical DC motors have their armature shafts rigidly connected, so both armatures rotate with the same angular velocity $\omega$.

kvantphysics
CF 1625E1 - Cats on the Upgrade (easy version)

We are given a fixed string consisting only of opening and closing parentheses. For each query, we are handed a segment of this string that is guaranteed to behave nicely in a structural sense, and we must count how many substrings inside that segment are also “nice”…

codeforcescompetitive-programmingbrute-forcedata-structuresdfs-and-similardivide-and-conquerdpgraphstrees
CF 1625D - Binary Spiders

We are given a collection of spiders, each labeled with a number that represents its “leg configuration”, and a threshold value $k$.

codeforcescompetitive-programmingbitmasksdata-structuresimplementationmathsortingstrees
CF 1625B - Elementary Particles

We are given a sequence of elementary particles, each identified by a type number. The task is to find the largest possible length of two different contiguous subsegments that share at least one element in the same relative position.

codeforcescompetitive-programmingbrute-forcegreedysortings
CF 1625A - Ancient Civilization

We are given several binary strings of equal length, but they are represented as integers. Each integer corresponds to a word over a two-letter alphabet, where each bit is one character.

codeforcescompetitive-programmingbitmasksgreedymath
Kvant Physics Problem 17

A parallel-plate capacitor has plate separation $d$.

kvantphysics
CF 1626C - Monsters And Spells

- First line: t - number of test cases - For each test case: - First line: n - size of the grid (n x n) - Next n lines: n integers per line - the neighbor XOR sums There is no m in the problem, only n. The earlier input with 8 4 seems like an incorrect test input format.

codeforcescompetitive-programmingbinary-searchdata-structuresdpgreedyimplementationmathtwo-pointers
Kvant Physics Problem 18

Two transparent combs are superimposed and viewed against a uniform source of light.

kvantphysics
CF 1626F - A Random Code Problem

We are working with a long array generated by a linear recurrence, and then a randomized process that repeatedly samples elements from this array and mutates the sampled value. At each of $k$ rounds, we pick a uniformly random index $idx$ from $0$ to $n-1$.

codeforcescompetitive-programmingcombinatoricsdpmathnumber-theoryprobabilities
CF 1626E - Black and White Tree

We are given a tree with $n$ vertices, where each vertex is either black or white. A "chip" can be placed on any vertex, and in each operation, you pick a black vertex $y$ different from the last chosen black vertex and move the chip one step along the shortest path from its…

codeforcescompetitive-programmingdfs-and-similargreedytrees
Kvant Physics Problem 16

The physical system is a car moving along a horizontal road.

kvantphysics
CF 1626D - Martial Arts Tournament

We are given a multiset of weights, and we must split these values into three groups using two cut points on the number line.

codeforcescompetitive-programmingbinary-searchbrute-forcegreedymath
CF 1626A - Equidistant Letters

We are given a short string where each character appears either once or twice. The task is to permute the characters so that whenever a letter appears twice, the gap between its two occurrences is identical for all such letters.

codeforcescompetitive-programmingconstructive-algorithmssortings
CF 1626B - Minor Reduction

We are given a large integer, represented as a string of digits without leading zeros, and we are allowed to perform exactly one operation: pick two consecutive digits and replace them with their sum.

codeforcescompetitive-programminggreedystrings
CF 1627F - Not Splitting

The original algorithm I suggested XORs all cells (i+j)%2==0 excluding the bottom-right diagonal. This produces the wrong result because it is misapplying the standard trick for this problem. The correct approach comes from observing the XOR-sum properties: 1.

codeforcescompetitive-programminggeometrygraphsgreedyimplementationshortest-paths
CF 1627E - Not Escaping

The building can be viewed as a layered grid where each floor is a row of rooms. Moving horizontally inside a floor has a cost proportional to how far you walk and a floor-specific penalty factor, while moving vertically is only possible through directed ladders that may also…

codeforcescompetitive-programmingdata-structuresdpimplementationshortest-pathstwo-pointers
CF 1627D - Not Adding

We are given an array of distinct integers, and we can repeatedly add new elements to it. Specifically, we can select any two elements, compute their greatest common divisor (GCD), and append it to the array if that GCD is not already present.

codeforcescompetitive-programmingbrute-forcedpmathnumber-theory
CF 1627C - Not Assigning

We are given a multiset of values that form a cyclic array. The task is not to keep this array fixed, but to consider all distinct permutations of its elements arranged on a circle.

codeforcescompetitive-programmingconstructive-algorithmsdfs-and-similarnumber-theorytrees
CF 1627A - Not Shading

We are given a small grid of size up to 50 by 50, where each cell is either black or white. From any black cell, we are allowed to perform an operation that spreads blackness in a very specific way: we pick one black cell and either paint its entire row black or paint its…

codeforcescompetitive-programmingconstructive-algorithmsimplementation
CF 1627B - Not Sitting

We are given a classroom represented as a grid with $n$ rows and $m$ columns. Each cell is a seat. Tina can paint exactly $k$ seats pink, where $k$ ranges from 0 to $n cdot m - 1$. Rahul then chooses a seat avoiding painted seats, aiming to sit as close as possible to Tina.

codeforcescompetitive-programminggamesgreedysortings
Kvant Physics Problem 15

The physical system is a refrigerator operating between the refrigerated compartment and the surrounding room.

kvantphysics
CF 1628C - Grid Xor

We are given an even-sized $n times n$ grid of integers that was stolen, and instead of the original numbers, we only know the XOR of the neighbors of each cell. Our task is to compute the XOR of all the original numbers in the grid.

codeforcescompetitive-programmingconstructive-algorithmsgreedyimplementationinteractivemath
Kvant Physics Problem 13

Two identical vertical cylinders are connected by a horizontal tube and filled with water.

kvantphysics
CF 1628F - Spaceship Crisis Management

We are asked to determine, for multiple starting positions in space, whether there exists a straight-line trajectory to the target position at the origin, potentially interacting with stationary obstacles represented as line segments.

codeforcescompetitive-programmingbinary-searchdata-structuresgeometrysortings
CF 1628E - Groceries in Meteor Town

We are given a tree where each edge carries a weight that represents how dangerous it is to traverse during a meteor storm. Alongside this structure, we maintain a dynamic set of “active” nodes, which represent buildings with open grocery stores. Initially, no store is open.

codeforcescompetitive-programmingbinary-searchdata-structuresdsutrees
CF 1628D1 - Game on Sum (Easy Version)

We are asked to analyze a two-player game between Alice and Bob. The game lasts for n turns, and in each turn Alice chooses a number between 0 and k.

codeforcescompetitive-programmingcombinatoricsdpgamesmath
CF 1628D2 - Game on Sum (Hard Version)

Alice and Bob play a game of alternating choices over n turns. On each turn, Alice picks a number between 0 and k. Bob then decides whether to add or subtract that number from the total score, with the constraint that he must add at least m times over the entire game.

codeforcescompetitive-programmingcombinatoricsdpgamesmath
CF 1628B - Peculiar Movie Preferences

We are given a sequence of short strings, each representing a scene in a movie. From these scenes we are allowed to pick a subsequence, meaning we keep the original order but may skip some scenes. If we concatenate the chosen scenes, we obtain a single string.

codeforcescompetitive-programminggreedystrings
CF 1628A - Meximum Array

We are given an array of non-negative integers and we are allowed to repeatedly cut off a prefix of the current array. For each cut, we compute the MEX of that prefix and append it to a new array.

codeforcescompetitive-programmingbinary-searchconstructive-algorithmsgreedyimplementationmathtwo-pointers
CF 1629B - GCD Arrays

We are given a continuous integer segment from $l$ to $r$, and we treat every number in this segment as an element of an array. The array is not arbitrary, it is fully determined by the interval, so its structure is very rigid.

codeforcescompetitive-programminggreedymathnumber-theory
CF 1629A - Download More RAM

We are asked to model a system where we can temporarily spend RAM to permanently increase RAM. Each software has two numbers: the amount of RAM it requires to run, and the amount of RAM it gives once used. Our PC starts with a certain initial RAM.

codeforcescompetitive-programmingbrute-forcegreedysortings
CF 1630E - Expected Components

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingcombinatoricsmathnumber-theoryprobabilities
CF 1630F - Making It Bipartite

We are given a set of distinct integers, each representing a vertex. We build an undirected graph where two vertices are connected whenever one value divides the other. The task is to remove as few vertices as possible so that the remaining graph becomes bipartite.

codeforcescompetitive-programmingflowsgraph-matchingsgraphsnumber-theory
CF 1630B - Range and Partition

We are given an array of integers, and the task is to find a numeric range [x, y] and split the array into exactly k contiguous subarrays so that in each subarray, more than half of the elements fall inside the chosen range. The goal is to minimize the width of the range, y - x.

codeforcescompetitive-programmingbinary-searchconstructive-algorithmsdata-structuresgreedytwo-pointers
CF 1630D - Flipping Range

We are given an array of integers, and a set of allowed segment lengths. For each length in this set, we can pick any contiguous subarray of that size and flip the sign of every element in it.

codeforcescompetitive-programmingconstructive-algorithmsdpgreedynumber-theory
CF 1630A - And Matching

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingbitmasksconstructive-algorithms
CF 1630C - Paint the Middle

We are given a sequence of numbers, each initially unpainted. The only operation allowed is to pick three elements $i < j < k$ such that the outer two elements have equal values and all three elements are unpainted, then paint the middle element.

codeforcescompetitive-programmingdpgreedysortingstwo-pointers
CF 1631B - Fun with Even Subarrays

We are given an array of integers and allowed to perform a specific operation any number of times: pick a subarray of even length and overwrite the first half of it with the values from the second half.

codeforcescompetitive-programmingdpgreedy
CF 1631A - Min Max Swap

We are given two arrays of equal length, and at each position we are allowed to decide which of the two values stays in the first array and which goes to the second array. Concretely, for every index independently, we may swap the pair or leave it as is.

codeforcescompetitive-programminggreedy
CF 1632E2 - Distance Tree (hard version)

We start with a tree of unit edges, rooted at vertex 1. The distance function $d(v)$ is simply the number of edges from node 1 to node $v$. So the quantity we care about initially is the height of the tree when rooted at 1.

codeforcescompetitive-programmingbinary-searchdfs-and-similarshortest-pathstrees
CF 1632D - New Year Concert

We are given a sequence of class performance lengths, where each length represents the duration of a scene prepared by a class.

codeforcescompetitive-programmingbinary-searchdata-structuresgreedymathnumber-theorytwo-pointers
Kvant Physics Problem 14

Two pistons of masses $m_1$ and $m_2$ close the ends of two connected tubes whose cross sectional areas are $S_1$ and $S_2$.

kvantphysics
CF 1632E1 - Distance Tree (easy version)

We are given a tree where every edge has weight 1, and we treat vertex 1 as the root. For every vertex, we define its distance as the shortest path length from vertex 1. The tree is initially fixed, but we are allowed to temporarily add one extra edge between any two vertices.

codeforcescompetitive-programmingbinary-searchdata-structuresdfs-and-similargraphsshortest-pathstrees
CF 1632C - Strange Test

We are given two integers, initially called $a$ and $b$, with $a < b$. In one move we are allowed to either increment one of them by one, or replace $a$ by the bitwise OR of $a$ and $b$.

codeforcescompetitive-programmingbinary-searchbitmasksbrute-forcedpmath
CF 1632A - ABC

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingimplementation
CF 1632B - Roof Construction

We are asked to arrange the heights of $n$ consecutive pillars such that the heights form a permutation of integers from $0$ to $n-1$. The goal is to minimize the maximum bitwise XOR of any two adjacent heights.

codeforcescompetitive-programmingbitmasksconstructive-algorithms
Kvant Physics Problem 5

The circuit consists of ideal voltage sources and resistors connected as depicted in Figure 4.

kvantphysics
Kvant Physics Problem 9

A small mass moves on a horizontal table attached to a fixed point by a spring.

kvantphysics
CF 1633E - Spanning Tree Queries

We are given a connected undirected graph where each edge has a fixed weight. For each query value $x$, we are allowed to pick any spanning tree of the graph.

codeforcescompetitive-programmingbinary-searchdata-structuresdfs-and-similardsugraphsgreedymathsortingstrees
CF 1633F - Perfect Matching

Working

codeforcescompetitive-programmingdata-structuresdivide-and-conquerinteractivetrees
CF 1633A - Div. 7

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingbrute-force
CF 1633D - Make Them Equal

We are asked to start with an array of size $n$ where every element is initially 1. For each element $ai$, we can perform operations of the form $ai = ai + lfloor ai / x rfloor$, choosing $x 0$ as we like. Each element has a target value $bi$ and a reward $ci$.

codeforcescompetitive-programmingdpgreedy
Kvant Physics Problem 11

The system consists of three open barrels containing water.

kvantphysics
CF 1633B - Minority

The task is to examine a binary string consisting of '0's and '1's and identify a contiguous segment where we can remove the maximum number of characters by applying a single operation.

codeforcescompetitive-programminggreedy
CF 1633C - Kill the Monster

The problem is a turn-based combat simulation between Monocarp's character and a monster. Each has an initial health and attack.

codeforcescompetitive-programmingbrute-forcemath
CF 1634E - Fair Share

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingconstructive-algorithmsdata-structuresdfs-and-similargraph-matchingsgraphs
CF 1634D - Finding Zero

We are given an array of hidden non-negative integers, and exactly one position in it contains a zero. Our only way to learn about the array is through queries that inspect any three distinct indices.

codeforcescompetitive-programmingconstructive-algorithmsinteractivemath
Kvant Physics Problem 8

The system consists of a long rigid rod of length $L$ and mass $m$.

kvantphysics
CF 1634F - Fibonacci Additions

We are asked to maintain two integer arrays, A and B, under a sequence of operations called Fibonacci additions. Each operation specifies a segment [l, r] and an array to update.

codeforcescompetitive-programmingbrute-forcedata-structureshashingimplementationmath
CF 1634A - Reverse and Concatenate

We are given a string s of length n and a number k. We can perform exactly k operations on s, where each operation is either appending the reverse of the string to itself (s + rev(s)) or prepending the reverse (rev(s) + s).

codeforcescompetitive-programminggreedystrings
CF 1634C - OKEA

We are asked to arrange $n cdot k$ items, priced from $1$ to $n cdot k$, into a grid with $n$ shelves and $k$ items per shelf. The key restriction is that the mean price of any contiguous segment of items on a shelf must be an integer. Each number must appear exactly once.

codeforcescompetitive-programmingconstructive-algorithms
CF 1634B - Fortune Telling

We are given an array of integers, and a process that runs left to right over this array. A person starts with some initial value and, at each position, must choose one of two actions: add the current array value to their running number, or XOR the current array value with it.

codeforcescompetitive-programmingbitmasksmath
Kvant Physics Problem 7

We consider a horizontal rod of length $l$ that rotates about a vertical axis passing through the point $O_1$.

kvantphysics
CF 1635D - Infinite Set

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingbitmasksdpmathmatricesnumber-theorystrings
CF 1635E - Cars

We are asked to reconstruct a hidden configuration of points on a line, one for each car, and assign each car a direction, left or right. After reconstruction, the configuration must explain two kinds of pairwise constraints.

codeforcescompetitive-programming2-satconstructive-algorithmsdfs-and-similardsugraphsgreedysortings
CF 1635F - Closest Pair

We are given a sequence of points lying on a horizontal line, each with a coordinate and a positive weight. The task is to answer multiple queries about contiguous subarrays of these points.

codeforcescompetitive-programmingdata-structuresgreedy
Kvant Physics Problem 1

Three communicating vessels contain water and are covered by pistons made of the same material and having the same thickness.

kvantphysics
CF 1635B - Avoid Local Maximums

We have to modify an array so that no position is strictly greater than both of its neighbors. Such positions are called local maximums. The first and last elements are never local maximums because they only have one neighbor.

codeforcescompetitive-programminggreedy
CF 1635C - Differential Sorting

We are given an integer array and we are allowed to perform a very specific transformation: pick three indices in increasing order and overwrite the leftmost position with the difference of the two later values.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 1635A - Min Or Sum

We are given several arrays, each containing small non-negative integers. For each array, we are allowed to repeatedly pick two different positions and replace both values with new numbers, as long as the bitwise OR of the chosen pair stays unchanged after the replacement.

codeforcescompetitive-programmingbitmasksgreedy
CF 1637H - Minimize Inversions Number

We are given a permutation, meaning every number from 1 to n appears exactly once in some order. We are allowed to pick some subsequence of this array, remove those chosen elements, and then reinsert them as a block at the very front, preserving their relative order.

codeforcescompetitive-programmingdata-structuresgreedymathsortings
CF 1637G - Birthday

We are given the numbers from $1$ to $n$ for each test case, and we can repeatedly pick any two numbers $x$ and $y$, remove them, and add two numbers: $x+y$ and $ The input size allows $n$ up to $5 cdot 10^4$ per test case, with a total sum of $n$ over all test cases also…

codeforcescompetitive-programmingconstructive-algorithmsgreedymath
CF 1637E - Best Pair

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingbinary-searchbrute-forceimplementation
Kvant Physics Problem 6

A conducting sphere of radius $r$ is connected to ground.

kvantphysics
CF 1637F - Towers

We are working on a tree where each vertex has a height value. The task is to place a set of “towers” on vertices, and assign each tower a positive integer efficiency.

codeforcescompetitive-programmingconstructive-algorithmsdfs-and-similardpgreedytrees
CF 1637D - Yet Another Minimization Problem

We have two arrays of equal length. For every position, we may either keep the pair as it is or swap the two numbers inside that position. After deciding this independently for all indices, both arrays acquire new values.

codeforcescompetitive-programmingdpgreedymath
CF 1637C - Andrew and Stones

We are given a line of piles, each pile containing some number of stones. The only allowed operation removes two stones from some middle pile and redistributes them as one stone to a pile on its left and one stone to a pile on its right.

codeforcescompetitive-programminggreedyimplementation
CF 1637B - MEX and Array

We are given an array of integers and asked to compute a sum of “values” over all its subsegments. A subsegment is any contiguous slice of the array. The value of a subsegment is defined as the maximum cost of any partition of that subsegment.

codeforcescompetitive-programmingbrute-forcedpgreedymath
CF 1637A - Sorting Parts

We have an array and we are forced to perform exactly one operation. We choose a position that splits the array into two non-empty parts. Then we sort the left part and the right part independently.

codeforcescompetitive-programmingbrute-forcesortings
Kvant Physics Problem 2

Two balls of masses $m_1$ and $m_2$ are placed on a smooth horizontal plane and connected by a spring of stiffness $c$.

kvantphysics
CF 1638F - Two Posters

We are given a sequence of vertical panels, each panel having width one and a fixed height. Visually, each panel is attached to a horizontal bar at the top, and can be shifted upward or downward, but it must always remain connected to that bar, meaning every panel remains a…

codeforcescompetitive-programmingbrute-forcedata-structuresgreedytwo-pointers
CF 1638E - Colorful Operations

We are maintaining a length-n array that starts completely uniform: every position holds value 0 and belongs to color 1. Over time, the array evolves through two kinds of updates. One type recolors a whole interval, replacing whatever colors were there with a new one.

codeforcescompetitive-programmingbrute-forcedata-structuresimplementation
CF 1638D - Big Brush

We are given a painted canvas represented as a grid with n rows and m columns. Each cell in this grid contains a color, and the painting process was performed using only a 2 × 2 brush.

codeforcescompetitive-programmingconstructive-algorithmsdata-structuresgreedyimplementation
CF 1638B - Odd Swap Sort

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programmingdata-structuresmathsortings
CF 1638A - Reverse

We are given a permutation of length $n$, which is an array containing all integers from $1$ to $n$ exactly once. We are allowed to choose exactly one contiguous subsegment of the permutation and reverse it.

codeforcescompetitive-programmingconstructive-algorithmsgreedymath
CF 1638C - Inversion Graph

We are given a permutation of size $n$, which is an array containing all integers from 1 to $n$ exactly once in some order. From this permutation, we construct an undirected graph on $n$ vertices, where each vertex corresponds to a position in the permutation.

codeforcescompetitive-programmingdata-structuresdsugraphsmath
CF 1639K - Treasure Hunt

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programminggraphsinteractive
Kvant Physics Problem 3

Two helical springs are made from identical pieces of steel wire.

kvantphysics
Kvant Physics Problem 4

A sample of an unknown purified gas is contained in a cylinder.

kvantphysics
CF 1639I - Treasure Hunt

We are exploring an unknown connected graph, but we never see global labels of vertices or edges. Instead, we start at a known vertex and repeatedly move along incident edges chosen locally.

codeforcescompetitive-programminggraphsinteractive
CF 1639J - Treasure Hunt

The problem presents an interactive treasure hunt on a hidden undirected graph. Each vertex corresponds to a junction with a treasure. You begin at a specified start vertex, and every time you visit a new vertex, you collect the treasure there.

codeforcescompetitive-programminggraphsinteractive
CF 1639D - Treasure Hunt

This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation.

codeforcescompetitive-programminggraphsinteractive
CF 1639G - Treasure Hunt

We are exploring an unknown connected graph. We know the complete graph beforehand, including every edge, but once the interaction starts we lose the vertex labels.

codeforcescompetitive-programminggraphsinteractive