brain
tamnd's digital brain — notes, problems, research
41650 notes
Codeforces 1994F: Stardew Valley
We are given several independent forests, where each forest consists of multiple rooted trees. Every tree has a fixed root at vertex 1, and each vertex defines a natural subtree consisting of itself and all descendants in this rooted structure.
We are given two binary sequences of equal length, s and t. Vanya can perform a specific operation on s repeatedly: he chooses a contiguous subarray from position l to r, and for each position i in that subarray, replaces s[i] with s[i] XOR s[i - l + 1].
We are given a grid that already contains every integer from 1 to $n cdot m$ exactly once, arranged in some arbitrary order. The task is to construct another grid of the same dimensions using the same set of numbers such that no number stays in its original cell.
We are asked to construct a connected graph on $n$ vertices, starting from an empty graph, by performing $n-1$ operations. Each operation is numbered from 1 to $n-1$.
We are given a row of values, and we want to count how many contiguous segments behave “well” under a very specific simulation rule. When we pick a segment $[l, r]$, we process its elements from left to right, maintaining a running sum $g$.
We are given a permutation of integers from 1 to $n$ and a string of instructions of length $n$, where each instruction is either L, R, or ?. Each position in the permutation can either connect to its nearest larger number to the left (L) or to the right (R), and ?
We are asked to find a hidden cell in a rectangular matrix of size $n times m$, where each cell contains either -1, 0, or 1.
We are given a permutation p of length n and a string s of the same length consisting only of ?. Each position i in the permutation defines two special indices: li is the last index before i where the permutation value is larger, and ri is the first index after i where the…
We are given an array of integers representing a sequence of numbers that we want to make non-decreasing. The only operation allowed is to choose a set of k indices and increment each of those selected elements by one, paying k + 1 coins for the operation.
We have a two-player game between Alice and Bob played over a set of cakes, each with an integer tastiness. Alice goes first.
We are given an array of integers where each element lies between 1 and the array's length. The operation we can perform requires finding an index i such that the value at that position equals i. When we do, we remove both a[i] and the following element a[i+1] from the array.
We are given an array of integers of length $n$. At each step, we can choose an index $i$ where $ai = i$ and $i < n$, and remove both $ai$ and $a{i+1}$. The goal is to maximize the number of times we can perform this operation until no more valid indices exist.
We are asked to determine the minimum time required to upload a certain amount of RAM, measured in gigabytes. You can upload either 0 or 1 GB per second, but there is a constraint on the network: in any consecutive block of $k$ seconds, the total upload cannot exceed 1 GB.
The problem asks us to reverse-engineer a Fenwick tree, or binary indexed tree, operation. A Fenwick tree s is derived from an array a by summing a specific range for each index k.
We are given a multiset of cards, each labeled with an integer from 1 to $n$, and for each type $i$ we know how many cards $ai$ we already own. We also have $k$ coins to buy additional cards, and the shop has unlimited supply of each type.
Connection interrupted. Waiting for the complete answer
We are given a circular arrangement of coins. Each coin is either facing up or facing down. Two players alternate turns, and on each turn a player must pick one coin that is currently facing up, remove it, and then flip its two neighbors.
We shall prove that the only integer $n>3$ for which a configuration of points $A_1,\dots,A_n$ and real numbers $r_1,\dots,r_n$ satisfying the problem conditions exists is
We are asked to count ordered pairs of positive integers (a, b) such that 1 ≤ a ≤ n and 1 ≤ b ≤ m, with an additional divisibility condition: b gcd(a, b) must be divisible by a + b.
We are asked to count how many ordered pairs of integers $(a, b)$ satisfy a divisibility condition under bounded ranges. For each test case, we are given two limits $n$ and $m$.
We are asked to reconstruct a hidden array of positive integers that is a palindrome, given nearly all of its subarray sums. Each subarray sum corresponds to the sum of a contiguous segment of the array, and exactly one subarray sum is missing.
The task is to construct a small sequence of non-negative integers such that every sum from 1 to $n$, except a single forbidden value $k$, can be represented as the sum of some subsequence.
We are given a hand of cards, each labeled with a number, and a fixed integer $k$. The operation allowed is to take any $k$ identical cards and exchange them for $k-1$ cards of any number we choose. The goal is to reduce the total number of cards as much as possible.
We are given a binary string representing a strip of paper with 0s and 1s. We can fold the strip at any position between adjacent characters.
We are asked to analyze a two-player game with multiple piles of stones. On a player's turn, they choose a number $k$ that does not exceed the smallest non-empty pile, then remove $k$ stones from every pile that still has stones.
We are given a grid of size $n times m$ filled with black and white tiles. Each tile is either 'B' (black) or 'W' (white). The task is to determine whether it is possible, using a series of rectangle-filling operations, to make all tiles in the grid the same color.
We are asked to count arrays of length n with elements between 0 and k that can be reduced to all zeros using a specific operation. The operation allows selecting two distinct indices l and r and subtracting 1 from all elements between l and r inclusive.
We are given an array of length $n$, where each position can independently take any value from $0$ to $k$. So the total universe is all $(k+1)^n$ arrays. The question is not about simulating the process directly.
Alice and Bob play on a multiset of numbers. Alice and Bob alternately remove elements from the array, with Alice moving first. The difference is that whenever Alice removes a value, she also keeps it in her personal array c, while Bob simply deletes a value.
We are asked to color all nodes of a tree black using the fewest operations. Each operation lets us pick a node $v$ and a distance $d$, and it colors all nodes that are exactly distance $d$ from $v$.
This is a Type A problem: find all natural numbers $x$ satisfying a given condition. For a Type A problem, the solution must do two things: 1. Derive necessary conditions and prove that every solution must satisfy them. 2.
We are given a string of lowercase English letters and a set of queries, each specifying a contiguous substring of the string. For each query, we are asked to compute the sum of all lengths $k$ for which there exists at least one substring of length $k$ that is not a palindrome.
We are given two integer arrays, a and b, of length n, along with a fixed integer v. We are asked to process queries of two types. The first type updates a single element in b.
We have a grid with exactly two rows and n columns. Every cell contains either 0 or 1. The path always starts at the top-left cell and ends at the bottom-right cell.
We can think of this problem as walking through a sequence of Pokémon, where each Pokémon is a node with a cost to “enter combat” and several strength values across different attributes.
We are interacting with a hidden permutation of numbers from 0 to n-1, but we never see it directly. Instead, we are allowed to compare expressions of the form (p[a] Our goal is not to reconstruct the permutation, but to find two indices i and j such that the XOR of their…
We are given a line of cells, each cell containing a direction character that behaves like a deterministic instruction for a moving token. When a token is dropped onto a cell, it repeatedly moves one step left or right depending on the current cell’s character.
We are dealing with a tree, but the real structure we care about is hidden behind an interactive layer. In every test case, we must output two permutations of node labels.
We build an array incrementally. After every insertion we must compute $$f(a)=maxx left(maxi(aimid x)-mini(aimid x)right).$$ The value inserted at each step is encrypted by the previous answer, so the queries must be processed online.
We are asked to count the number of distinct prefix maximum sequences that can appear over all valid pre-order traversals of a rooted tree.
We are asked to construct a permutation of numbers from 1 to n such that a specific forbidden pattern never appears. The pattern involves two different starting positions i and j.
We are given a binary string, and for each of its substrings, we want to compute the minimum number of ones required to form a “good” string with respect to that substring.
We are given an array a of length n. The task is to repeatedly select an element from a, add its value plus its current index to a set S, remove it from the array, and continue until a is empty. After all insertions, we sort the set S in decreasing order to form array b.
We are asked to count how many ways we can assign integer values to the vertices of a rooted binary tree such that it forms a valid binary search tree, with all values constrained between $1$ and $C$.
We are given a tree with n vertices and a set of k pairs of vertices. Sasha wants to ensure that for each pair (ai, bi), there is at least one colored edge on the simple path connecting ai and bi.
Sasha plays a repeated betting game where each bet either multiplies his stake by a factor or loses it entirely. The only control he has is the size of each bet, and he is allowed to adapt it based on past outcomes.
We are asked to count the number of subsets of intersections in a tree such that, if we declare exactly the intersections in the subset as dangerous, no simple path in the tree contains three or more dangerous intersections.
We have an $n times n$ grid. Every cell belongs to exactly one diagonal of each of the two diagonal directions. The problem counts both directions, so the grid contains a total of $2n-1$ diagonals of one type and $2n-1$ diagonals of the other type, for a total of $4n-2$…
We repeatedly perform the same fold on a square sheet. Each operation folds all four corners to the center, producing a smaller square rotated by $45^circ$. After doing this $N$ times and unfolding the paper, every crease is either a mountain fold or a valley fold.
This is a Type B (prove/disprove) problem. The task is to determine whether a stated property is always true: Given a closed ski track that passes through each village exactly twice, is it always possible to traverse along the track so as to visit each village exactly once?
We are asked to build a single string over the first k lowercase letters such that every possible string of length n formed from those k letters appears somewhere inside it as a subsequence.
We have a line with n positions, each containing a ship. Certain positions have harbours, each with an associated value. Every ship must eventually "move" to the next harbour to its right.
We are asked to construct an $n times n$ matrix of zeros and ones with two simultaneous constraints on the bitwise XORs of rows and columns, and an overall sum constraint.
We are given an array of integers and the ability to decrease each number to any integer between zero and its current value. For positive numbers, this means we can reduce them to zero; for negative numbers, we can increase them toward zero.
We start with an array a of length n. For each of the next d days we must choose exactly one action. The first action increases the prefix [1..bi] by one, where the sequence b is generated by repeating the given array v forever. The second action scores points.
We are building a long array by interleaving two independent permutations in a structured way. One permutation, call it $p$, provides odd base values. The second permutation, $q$, determines powers of two.
Each card can end up in one of three states. A card may be placed in your deck, placed in your friend's deck, or discarded entirely. Let the XOR of your deck be X and the XOR of your friend's deck be Y.
We are working in a geometric setting where a large convex polygon represents a forbidden region. A spaceship starts outside this region and must travel to another point, with the constraint that it is never allowed to enter the interior of the polygon, though touching its…
The network is a tree, so initially there is exactly one simple path between every pair of computers. Each edge, when put into emergency mode, becomes a directed constraint: one endpoint must come before the other in a global ordering of all nodes.
We are given two strings, $A$ of length $N$ and $B$ of length $M$ with $N le M$, representing the names of two twins. We want to create nicknames $A'$ and $B'$ for them.
We are asked to reverse-engineer the merge step of a Merge Sort-like routine, but with a twist: the two input arrays may not be sorted. Concretely, we receive a single array C of length 2N containing every integer from 1 to 2N exactly once.
We have a line of lamps, each either on or off, and a line of buttons, one per lamp. Each button affects only the lamps immediately adjacent to it: pressing button $i$ toggles lamps $i-1$ and $i+1$, if they exist.
We are given a string of lowercase English letters, and our goal is to transform it into a non-decreasing sorted string using a specific operation.
We are asked to work with a segment tree built over an array of length $n$, where the segment tree is the standard binary recursive construction. Each node of the tree corresponds to a segment of the array, and the root node is labeled $1$.
We are given a tree and a special operation. In one operation, we choose any two vertices and look at the unique path between them. Every vertex on that path is merged into a single new vertex.
We are given a permutation of numbers from 0 to n−1. We are allowed to rotate this array cyclically, and for each possible rotation we compute a score as we scan from left to right.
We observe a sequence of match outcomes where each character represents a single play won by either player A or player B. These plays are grouped into sets: a set ends as soon as one player accumulates $X$ wins inside that set.
Working
We are given two sequences. The first sequence is already fixed in its internal order, and we are not allowed to reorder it.
We are given an array of integers and need to construct a parallel array of labels containing only 1, 2, or 3. The goal is to satisfy exactly two out of three pairing conditions, each involving two indices where the original numbers are equal and the assigned labels form one…
We are given an array $b$ of length $n$ and a number $k$, which represents the number of operations performed on some hidden original array $a$. Each operation consists of choosing a fixed point $x$ in $a$ and cyclically left-shifting $a$ by $x$ positions.
We have $n$ cities arranged along a line and a forecast of rain for $m$ consecutive days. Each day's rain covers a continuous interval of cities $[li, ri]$. A city is dry if it never experiences rain across all $m$ days.
We are given a sequence of cities numbered from 1 to $n$ and a forecast of rain for the next $m$ days. On day $i$, rain will cover the cities in the interval $[li, ri]$. A city is dry if no rain ever falls there during the $m$ days. Doremy can prevent rain on exactly two days.
Each stack belongs to a vertex. If we are currently at vertex u, we look at the top element of stack u. If the stack is empty, the process stops and returns u. Otherwise we pop the top element, jump to the vertex written on that element, and repeat.
We are given two binary strings, s and t. Qingshan wants to transform s into a string where no two consecutive characters are the same, called a "good" string. She can repeatedly insert string t anywhere in s, including at the start or end, any number of times.
We are given a binary string consisting only of 0s and 1s. The task is to transform it into a string that satisfies the following "good" property: for every position i in the string, the character at i must differ from the character at the symmetric position counted from the…
We are asked to count the number of ways to colour an array of integers either blue or red, subject to two constraints.
We are given a tree where some edges already have a fixed direction and some edges are still undirected. We must choose directions for the remaining edges and assign a color to every edge. A walk may traverse an edge either along its direction or against it.
We are given a sequence of positive integers indexed from left to right. We are allowed to repeatedly choose positions and “circle” elements. The key twist is that circling does not remove elements immediately; it only marks them.
We choose a value $a{n+1}$ between $0$ and $m$. After that, every earlier position is determined uniquely by $$ai = a{i+1} bmod i$$ for $i=n,n-1,dots,1$. The entire array is generated from a single starting value.
We are given an array indexed from 1 to n. We repeatedly choose a non-empty subset of indices and mark them as special. Those chosen indices are colored black. After that, any still-unselected index becomes green if it is a multiple of at least one black index.
We are tasked with spreading an announcement to all residents of a village in the cheapest way possible. There are two ways to inform residents: Pak Chanek can directly tell someone at a fixed cost p, or a resident who already knows can inform others using a magical helmet…
We are given a collection of green apple pieces, each weighing exactly 1 kilogram. The goal is to distribute these pieces among a fixed number of people so that each person ends up with the same total weight.
The solution correctly restates the exercise in terms of the frequency test and the chi-square statistic for $b$ categories. It identifies the expected value $E(chi^2)=b-1$ and explains why the observed value $1.06$ for the first 2000 decimal digits is unusually small.
We are asked to maximize the time until a bomb explodes. The bomb has a timer b initially, which decreases by 1 every second. You have n tools, each capable of increasing the timer by xi when used, but the timer cannot exceed a maximum value a.
Codeforces 1875E: Jellyfish and Math
We are given an array of nonnegative integers. The task is to remove elements from the array one by one, and after each removal, we add the MEX of the remaining array to a running total. The goal is to minimize this total by choosing the deletion order optimally.
We are asked to simulate a turn-based exchange game between two players, Jellyfish and Gellyfish. Jellyfish owns an array of apples with integer values, and Gellyfish owns a different array.
We are asked to consider a country with $n$ cities connected in a tree-like structure that mirrors a complete binary tree. Each city $i$ has edges to $2i$ and $2i+1$ if those indices do not exceed $n$.
This problem is a Codeforces 3000-rated constructive graph problem. Producing a correct editorial requires reconstructing and proving the entire degree-sequence characterization of flower-like pseudotrees, together with the linear-time construction used in the official solution.
We have a party with n people, each bringing a certain number of candies. Each person can give exactly a power-of-two number of candies to another person, and every person can give to at most one person and receive from at most one person.
For Codeforces 1869A, the entire solution is a constructive observation. If n is even, perform: After the first operation, every element becomes X, where X is the XOR of the whole array.
We are asked to construct an $n times m$ matrix where every row is a permutation of the numbers from $0$ to $m-1$. This means each row contains all values exactly once, but rows can differ from each other. Once the matrix is built, each column induces a multiset of $n$ values.
We have a party with n people, each holding a certain number of candies ai. Each person will give a positive number of candies to exactly one other person, and each person will receive candies from exactly one other person.
The problem presents a magical device called "The Great Equalizer" which takes an array of integers and repeatedly applies a transformation.
We are asked to determine the minimum number of ice cream balls Tema needs to buy to make exactly n distinct two-ball ice cream cones. Each cone consists of two balls, which may be of the same flavor.
We are asked to reconstruct a possible original sequence a given a sequence b that was derived by a simple filtering rule. The sequence b always starts with the first element of a, and then includes every element of a that is greater than or equal to its immediate predecessor.
We are given a rectangular carpet of size $n times m$ where each cell contains a lowercase letter. Vika likes to read her name "vika" from the carpet by selecting one letter from each column, moving strictly left to right, and choosing exactly four columns for the letters 'v'…
We are given a tree on $n$ vertices, where each edge has an integer weight. The task is to count the number of weighted simple graphs such that the given tree is the unique minimum spanning tree (MST), and all edge weights in the graph are integers not exceeding $S$.