brain

tamnd's digital brain — notes, problems, research

43815 notes

binary searchconstructive algorithmsgamesgreedyinteractive
binary search
*specialconstructive algorithms
bitmaskscombinatoricsdpmathnumber theory
constructive algorithmsgraphsmath
mathnumber theory
greedyimplementation
constructive algorithmsgreedymathnumber theory
implementation
*specialprobabilities
binary searchgraph matchingsgreedy
geometry
dp
data structuresgraphsshortest paths
implementation
implementation
greedysortings
brute force
dpgreedy
CF 896A - Nephren gives a riddle

The construction in this problem generates a sequence of strings where each level wraps the previous one inside a fixed template. The base string, call it $f0$, is a fixed sentence.

codeforcescompetitive-programmingbinary-searchdfs-and-similar
CF 897B - Chtholly's request

We are asked to list special numbers in increasing order and take a prefix sum. A number is considered valid if it reads the same forward and backward in decimal notation and its length is even.

codeforcescompetitive-programmingbrute-force
CF 898F - Restoring the Expression

We are given a single long string consisting only of digits. This string is known to come from a correct arithmetic identity of the form a + b = c, but the symbols + and = were removed and the digits were concatenated.

codeforcescompetitive-programmingbrute-forcehashingmath
CF 898A - Rounding

We are given a single non-negative integer, and we are asked to transform it into a nearby number whose last digit is zero.

codeforcescompetitive-programmingimplementationmath
CF 899F - Letters Removing

We are given a string whose characters are indexed from left to right, and a sequence of operations that repeatedly remove certain characters from specified segments of the current string.

codeforcescompetitive-programmingdata-structuresstrings
CF 899A - Splitting in Teams

We are given several groups of students. Each group has either one student or two students, and these groups are indivisible. The coach wants to form as many teams as possible, where each team must contain exactly three students.

codeforcescompetitive-programmingconstructive-algorithmsgreedymath
CF 900B - Position in Fraction

We are given a rational number formed by dividing two integers, and we are interested in its decimal representation after the decimal point. The task is to determine the earliest position where a specific digit appears in that infinite (or terminating) decimal expansion.

codeforcescompetitive-programmingmathnumber-theory
CF 922F - Divisibility

We are asked to build a subset of the numbers from 1 to n such that a very specific quantity computed on this subset equals k exactly. The quantity counts ordered pairs of distinct elements (a, b) where a appears earlier in value than b and a divides b evenly.

codeforcescompetitive-programmingconstructive-algorithmsdpgreedynumber-theory
CF 922A - Cloning Toys

We start with a single original plush toy and no copies. A machine can be applied repeatedly, and each application changes the inventory depending on what type of toy it is applied to.

codeforcescompetitive-programmingimplementation
CF 923F - Public Service

We are given two different trees on the same number of cities. One tree describes bus connections between cities labeled from 1 to N. The other tree describes train connections between cities labeled from N+1 to 2N.

codeforcescompetitive-programmingconstructive-algorithmsgraphstrees
CF 923A - Primal Sport

We are given the value of the game after two moves, call it $X2$. The game starts from some unknown integer $X0 ge 3$, and two players alternately modify this value.

codeforcescompetitive-programmingmathnumber-theory
CF 924C - Riverside Curio

We are given a sequence of observations over consecutive days. Each day corresponds to a water level, and over time Arkady leaves marks at the distinct water levels he has seen so far. If a water level repeats, no new mark is added.

codeforcescompetitive-programmingdata-structuresdpgreedy
CF 925B - Resource Distribution

We are given a pool of servers, each with a fixed capacity measured in resource units. Two independent services must be deployed, and each service can use several servers at the same time.

codeforcescompetitive-programmingbinary-searchimplementationsortings
CF 926F - Mobile Communications

We are simulating a bank account over a sequence of days. Each day starts with a fixed charge of $p$ rubles applied to the account. On some specific days, before this charge happens, Arkady deposits money into the account.

codeforcescompetitive-programming
CF 926A - 2-3-numbers

We are asked to count how many integers in a closed interval $[l, r]$ can be written using only the prime factors 2 and 3. Any valid number must have the form $2^x cdot 3^y$, where both exponents are non-negative integers.

codeforcescompetitive-programmingimplementationmath
CF 927A - BuberPool Taxi Optimization

The system simulates a fleet of taxis moving on a large rectangular grid where distance is measured in Manhattan terms, but movement has an additional constraint: whenever a car is instructed to go to a point, it first adjusts its x-coordinate fully and only then adjusts its…

codeforcescompetitive-programming*specialinteractive
CF 928A - Login Verification

We are given a proposed username and a collection of already-registered usernames. The task is to decide whether the new one is “safe” to use, meaning it is not considered equivalent to any existing username under a set of transformation rules.

codeforcescompetitive-programming*specialstrings
CF 929A - Прокат велосипедов

We are given a sequence of positions along a straight line where bike stations are located. The first station coincides with Arkady’s starting point (school), and the last station coincides with his destination (home).

codeforcescompetitive-programming*specialgreedyimplementation
CF 930E - Coins Exhibition

We are given a line of $k$ coins, each independently oriented either “obverse” (call it O) or “reverse” (call it R). A full configuration is simply a binary string of length $k$, but $k$ can be extremely large, so we cannot enumerate configurations.

codeforcescompetitive-programmingdata-structuresdpmath
CF 930C - Teodor is not a liar!

We are given a collection of integer segments on the line from 1 to m. Each segment contributes coverage to every integer point inside it, including endpoints. For every integer position x, we can compute how many segments cover it; call this value cnt(x).

codeforcescompetitive-programmingdata-structuresdp
CF 930A - Peculiar apple-tree

We are given a rooted tree with vertices numbered from 1 to n, where vertex 1 is the root. Every vertex i greater than 1 has exactly one parent p[i], and that parent always has a smaller index, which implicitly guarantees that the structure is a rooted tree without cycles and…

codeforcescompetitive-programmingdfs-and-similargraphstrees
CF 931B - World Cup

We are given a single-elimination tournament with $n$ teams labeled from 1 to $n$. The structure of the tournament is fixed and mechanical: in every round, the remaining teams are sorted by their original labels, then paired consecutively, so team 1 plays 2, 3 plays 4, and so on.

codeforcescompetitive-programmingconstructive-algorithmsimplementation
CF 931A - Friends Meeting

Two people stand on a number line at integer coordinates $a$ and $b$. They want to end up at the same integer position, and each of them can move one step left or right any number of times. The twist is that movement cost is not linear.

codeforcescompetitive-programmingbrute-forcegreedyimplementationmath
CF 932C - Permutation Cycle

We are asked to construct a permutation of the numbers from 1 to N such that every position behaves in a very specific cyclic way.

codeforcescompetitive-programmingbrute-forceconstructive-algorithms
CF 932F - Escape Through Leaf

We are working on a rooted tree where each node carries two numerical attributes, one acting like a “multiplier when leaving a node” and the other acting like a “weight when entering a node”.

codeforcescompetitive-programmingdata-structuresdpgeometry
CF 932A - Palindromic Supersequence

We are given a single lowercase string $A$. The task is to construct another string $B$ such that two conditions hold at the same time: $B$ must read the same forward and backward, and the string $A$ must appear inside $B$ as a subsequence, meaning we can delete some…

codeforcescompetitive-programmingconstructive-algorithms
CF 933E - A Preponderant Reunion

We are given a sequence of non-negative integers. The only allowed operation is to pick a pair of adjacent positions where both values are still positive, subtract the smaller value from both, and pay a cost equal to that smaller value.

codeforcescompetitive-programmingconstructive-algorithmsdp
CF 933A - A Twisty Movement

We are given a sequence made only of two values, 1 and 2, arranged in a line. We are allowed to choose a single contiguous segment of this sequence, reverse it, and then we want to measure how long a non-decreasing subsequence becomes after this operation.

codeforcescompetitive-programmingdp
CF 933B - A Determined Cleanup

We are asked to construct a polynomial whose coefficients are non-negative integers strictly less than a given base $k$, such that when this polynomial is divided by $x + k$, the remainder is exactly the constant polynomial $p$.

codeforcescompetitive-programmingmath
CF 934B - A Prosperous Lot

We are asked to construct a positive integer not exceeding $10^{18}$ such that when it is written in base 10, the total number of “loops” formed by its digits is exactly $k$.

codeforcescompetitive-programmingconstructive-algorithmsimplementation
CF 934A - A Compatible Pair

Two players are interacting with two arrays of integers. One player owns an array of length n, the other owns an array of length m. Each number represents a lantern’s brightness, and brightness can be positive, negative, or zero. The interaction is adversarial.

codeforcescompetitive-programmingbrute-forcegames
CF 936C - Lock Puzzle

We are given a string of length $n$, and we are allowed to transform it using a single operation that cuts the string into a suffix and a prefix. The suffix is reversed and moved to the front, and the prefix is appended after it.

codeforcescompetitive-programmingconstructive-algorithmsimplementationstrings
CF 936B - Sleepy Game

We are given a directed graph where each vertex represents a state of a game token. A token starts at a fixed vertex, and two players alternate moving it along outgoing edges. A player loses immediately if they are to move from a vertex that has no outgoing edges.

codeforcescompetitive-programmingdfs-and-similardpgamesgraphs
CF 937B - Vile Grasshoppers

We are given two integers that describe a vertical structure of branches on a tree. The branches are numbered in increasing order, starting from a low level up to some maximum height.

codeforcescompetitive-programmingbrute-forcemathnumber-theory
CF 937A - Olympiad

We are given a list of participant scores, and we need to count how many different ways we can choose a group of participants to receive diplomas under a very specific rule. A valid group is determined by picking a score threshold.

codeforcescompetitive-programmingimplementationsortings
CF 938F - Erasing Substrings

We start with a single string of lowercase letters and repeatedly perform a sequence of destructive operations. In the i-th operation, we are forced to delete a contiguous block whose length is fixed to be $2i - 1$, but we are free to choose where that block lies in the…

codeforcescompetitive-programmingbitmasksdpgreedy
CF 938E - Max History

We are given a multiset of values, where each value is attached to a distinct position. The task is not to process a single ordering of these values, but to consider every possible permutation of indices, treating identical values at different positions as distinct.

codeforcescompetitive-programmingcombinatoricsmath
CF 938A - Word Correction

The task describes a transformation on a string where certain characters are removed according to a local rule involving vowels. We are given a word and repeatedly modify it until no two adjacent vowels remain.

codeforcescompetitive-programmingimplementation
CF 938B - Run For Your Prize

We are given a set of distinct points on a number line representing prizes. Two people start from fixed positions, one from the left side at position 1 and the other far to the right at position 106.

codeforcescompetitive-programmingbrute-forcegreedy
CF 939F - Cutlet

Arkady cooks a cutlet for a total of $2n$ seconds, and the physics is simple: at every moment it is on exactly one side, and whenever he flips it, the side changes instantly.

codeforcescompetitive-programmingdata-structuresdp
CF 939E - Maximize!

We are maintaining a growing collection of positive integers. The collection starts empty, and we process two types of operations: we either insert a new number, or we ask a question about the current collection.

codeforcescompetitive-programmingbinary-searchgreedyternary-searchtwo-pointers
CF 939A - Love Triangle

We are given a directed structure over $n$ nodes, where each node has exactly one outgoing edge. Concretely, plane $i$ points to plane $fi$, meaning it “likes” exactly one other plane. Self-loops are explicitly disallowed, so no node points to itself.

codeforcescompetitive-programminggraphs
CF 939B - Hamster Farm

Dima has a fixed number of hamsters that will be ready for transport, and a collection of box types, each with a different capacity. Every box used must be completely filled, and all chosen boxes must be of a single type because buying mixed types removes a discount.

codeforcescompetitive-programmingimplementation
CF 940A - Points on the line

We are given several points placed on a number line. Each point is just a single integer coordinate. We want to keep a subset of these points such that the spread of the remaining points is controlled, specifically the difference between the largest and smallest kept point…

codeforcescompetitive-programmingbrute-forcegreedysortings
CF 940E - Cashback

We are given a sequence of numbers, and we need to break it into contiguous segments. Each segment is evaluated independently, and then we sum up the segment scores.

codeforcescompetitive-programmingdata-structuresdpgreedymath
CF 940B - Our Tanya is Crying Out Loud

We start with a single integer value x = n and repeatedly transform it until it becomes 1. Each move either reduces the value by one at a fixed cost A, or divides the current value by k at cost B, but division is only allowed when the current value is exactly divisible by k.

codeforcescompetitive-programmingdpgreedy
CF 946F - Fibonacci String Subsequences

We are given a binary pattern string s and we want to measure how often this pattern appears inside many different strings derived from a very specific construction. The large string we care about is not arbitrary. It comes from a Fibonacci-style concatenation process.

codeforcescompetitive-programmingcombinatoricsdpmatrices
CF 946E - Largest Beautiful Number

We are given a large number represented as a string, and for each query we need to construct a strictly smaller number that satisfies a structural property. A number is considered valid if it has an even number of digits and its digits can be rearranged to form a palindrome.

codeforcescompetitive-programminggreedyimplementation
CF 946A - Partition

We are given a list of integers, and we are allowed to split this list into two groups in any way we like, with the only rule that every element must belong to exactly one of the two groups.

codeforcescompetitive-programminggreedy
CF 946C - String Transformation

We are given a string made of lowercase English letters. We are allowed to perform an operation that only increases a character by one step in the alphabet, for example turning c into d or a into b. Characters cannot decrease, and z is terminal.

codeforcescompetitive-programminggreedystrings
CF 948A - Protect Sheep

The grid can be viewed as a rectangular graph where each cell is a node connected to its four orthogonal neighbors. Some nodes contain sheep, some contain wolves, and the rest are empty.

codeforcescompetitive-programmingbrute-forcedfs-and-similargraphsimplementation
CF 949C - Data Center Maintenance

Codeforces 949C: Data Center Maintenance

codeforcescompetitive-programmingdfs-and-similargraphs
CF 949D - Curfew

There are n rooms arranged in a line. Each room must end up with exactly b students being counted by inspectors, but initially the distribution is arbitrary as an array a[i], and the total number of students equals n · b, so globally there is just enough “mass” to satisfy…

codeforcescompetitive-programmingbinary-searchbrute-forcegreedysortings
CF 949F - Astronomy

We are given 2n lattice points in the plane, each representing a star. Somewhere in the original astronomical configuration there existed a special point, the Moon, with integer coordinates, such that the stars could be partitioned into n pairs with a very rigid geometric…

codeforcescompetitive-programminggeometryprobabilities
CF 949E - Binary Cards

We are allowed to build a multiset of “coins”, where each coin has a value that is either a positive or negative power of two. In every round, a target integer is announced, and we must be able to select some subset of our fixed coins so that their sum equals that target.

codeforcescompetitive-programmingbrute-force
CF 949B - A Leapfrog in the Array

We start with numbers from 1 to $n$. Instead of placing them directly into positions $1$ through $n$, each number $i$ is initially placed at position $2i-1$. So the array of length $2n$ has numbers only in odd positions, while even positions are empty.

codeforcescompetitive-programmingconstructive-algorithmsmath
CF 949A - Zebras

We are given a binary string representing a chronological sequence of days, where each character is either 0 or 1.

codeforcescompetitive-programminggreedy
CF 950A - Left-handers, Right-handers and Ambidexters

We are given three groups of players at a training session: people who can only use the left hand, people who can only use the right hand, and ambidextrous players who can be assigned to either hand.

codeforcescompetitive-programmingimplementationmath
CF 950B - Intercepted Message

We are given two sequences of positive integers, each sequence representing how a long stream of bytes was transmitted in chunks. Each sequence is a segmentation of the same total length, but the segmentation points are different.

codeforcescompetitive-programminggreedyimplementation
CF 952E - Cheese Board

We are given a set of cheeses, each cheese has a unique name and a label indicating whether it is soft or hard. The names are just identifiers, but they can still be ordered lexicographically and used to define structure if needed.

codeforcescompetitive-programming*special
CF 952F - 2 + 2 != 4

The input is a short arithmetic expression containing small non-negative integers and only two operations: addition and subtraction. There are no parentheses, no multiplication, and no hidden formatting rules beyond the usual infix notation.

codeforcescompetitive-programming*special
CF 952A - Quirky Quantifiers

We are given a single integer a, guaranteed to be a three-digit or two-digit number between 10 and 999. The task is to output either 0 or 1 based on a hidden property of this number.

codeforcescompetitive-programming*specialmath
CF 952B - A Map of the Cat

We are interacting with a single unknown “cat”, but we are not told directly whether it behaves like a normal cat or a grumpy one. Instead, the cat is split into 10 labeled regions from 0 to 9, and we can probe the cat by choosing a region to pet.

codeforcescompetitive-programming*specialbrute-forceinteractive
CF 954F - Runner's Problem

We are given a 3 by m grid where movement is always one column to the right and can also shift vertically by at most one row. The journey starts in the middle row of the first column and must end in the same middle row at the last column.

codeforcescompetitive-programmingdpmatricessortings
CF 954I - Yet Another String Matching Problem

We are given two strings, one long string and one pattern string, both over a very small alphabet of size six. We slide the pattern across the long string, and for each position we take a substring of the same length as the pattern.

codeforcescompetitive-programmingfftmath
CF 954E - Water Taps

Each tap contributes a controllable flow of water, but every tap has a fixed temperature. You are allowed to choose a real-valued flow rate for each tap between zero and its maximum capacity.

codeforcescompetitive-programmingbinary-searchgreedysortings
CF 954B - String Typing

We are given a string that we want to construct starting from an empty string. Each action costs one operation. The basic action is appending a single character to the end of what we already have.

codeforcescompetitive-programmingimplementationstrings
CF 954A - Diagonal Walking

We are given a sequence of moves on a grid where each move is either one step to the right or one step upward. The key operation allowed is merging a right move and an up move when they are adjacent in either order.

codeforcescompetitive-programmingimplementation
CF 955F - Heaps

We are given a rooted tree with $n$ nodes, rooted at node 1. For every node $u$, we consider a family of structures defined by an integer $k$, where a node can be thought of as the root of a “$k$-ary heap of depth $m$” if it has at least $k$ child-subtrees that themselves…

codeforcescompetitive-programmingdptrees
CF 955E - Icicles

We have a line of positions from 1 to n, each position containing an icicle with an initial height a[i]. Over time, two things happen: a sound wave spreads from a chosen starting point T, and the icicles touched by the wave begin to melt downward.

codeforcescompetitive-programming
CF 955A - Feed the cat

Andrew wakes up at a given time of day and realizes his cat is hungry. The cat starts with some initial hunger value, and this hunger grows steadily over time at a fixed rate per minute. Andrew can reduce the cat’s hunger only by buying buns.

codeforcescompetitive-programminggreedymath
CF 955B - Not simply beatiful strings

We are given a single lowercase string. The task is to decide whether we can split its positions into two disjoint subsequences such that each subsequence forms a string that can be rearranged into exactly two contiguous blocks of equal characters, and those two blocks must…

codeforcescompetitive-programmingimplementation
CF 957A - Tritonic Iridescence

We are given a one-dimensional strip of length $n$, where each position is either already painted in one of three colors or left blank. The blank positions must be filled using the same three colors so that no two adjacent positions end up sharing the same color.

codeforcescompetitive-programmingimplementation
CF 958F1 - Lightsabers (easy)

We are given a line of Jedi, each occupying a fixed position in an array, and each Jedi has one of several possible lightsaber colors. Alongside this, we are given a target specification that tells us how many Jedi of each color we must pick.

codeforcescompetitive-programmingimplementation
CF 958E3 - Guard Duty (hard)

We are given two sets of points in the plane, each containing the same number of points. One set represents spaceships, the other represents bases. Every point has a unique location, and no three points lie on a single straight line.

codeforcescompetitive-programminggeometry
CF 958E1 - Guard Duty (easy)

We are given two small point sets in the plane, one representing Rebel ships and the other representing bases. Each ship must be assigned to exactly one base, and each base must also receive exactly one ship, so the assignment is a bijection between the two sets.

codeforcescompetitive-programmingbrute-forcegeometrygreedymath
CF 958B2 - Maximum Control (medium)

We are given a tree with $N$ nodes, meaning every pair of nodes is connected by exactly one simple path. We are allowed to choose $K$ nodes as “active stations”.

codeforcescompetitive-programmingdata-structuresdfs-and-similargraphsgreedytrees
CF 958C2 - Encryption (medium)

We are given a sequence of integers and asked to cut it into exactly $k$ contiguous non-empty segments. Each element must belong to exactly one segment, and the order of elements is preserved. For any segment, we take the sum of its elements, then reduce that sum modulo $p$.

codeforcescompetitive-programmingdp
CF 958A2 - Death Stars (medium)

We are given two rectangular grids of characters. The first grid has size $N times M$, while the second grid has size $M times N$.

codeforcescompetitive-programminghashingstrings