brain

tamnd's digital brain — notes, problems, research

41561 notes

CF 105143I - Cyclic Apple Strings

We are given a binary string and allowed to repeatedly perform a very flexible operation: pick any contiguous segment and rotate it cyclically.

codeforcescompetitive-programming
CF 105143G - Pack

We are given two types of items. There are n items of type A, each contributing value a, and m items of type B, each contributing value b. We want to repeatedly assemble identical “products”.

codeforcescompetitive-programming
CF 105143F - Custom-Made Clothes

We are given a hidden $n times n$ grid filled with positive integers in the range $[1, n^2]$. The grid is not arbitrary: values are monotone in both directions, meaning they never decrease as we move right or down.

codeforcescompetitive-programming
CF 105143E - Boomerang

We are given a tree where a “fake message” starts at a fixed node $r$ and spreads outward one edge per unit time. At time $t$, every node within distance at most $t$ from $r$ has received it, so the infected set is exactly a metric ball centered at $r$.

codeforcescompetitive-programming
CF 105143D - ICPC

We are standing on a line of seats, each seat holding a non-negative value. From a chosen starting seat, we may move left, right, or stay in place once per second. Whenever we land on a seat for the first time, we collect its value. Re-visiting a seat later gives nothing new.

codeforcescompetitive-programming
CF 105143A - Shaking Trees

We are given a rooted tree with node 1 as the root. Each move lets us pick a node $u$, detach it from its parent, and then perform a “leaf pruning” process inside the component rooted at $u$.

codeforcescompetitive-programming
CF 105143B - Countless Me

We are given an array of non-negative integers and a very flexible operation that allows us to move any amount of value from one position to another, as long as no element becomes negative.

codeforcescompetitive-programming
CF 105143C - TreeBag and LIS

We are asked to construct a string of decimal digits whose length does not exceed one hundred thousand, but the string is not arbitrary. The requirement is tied to all longest strictly increasing subsequences of that string.

codeforcescompetitive-programming
CF 105158K - 树上问题

We are given a tree with weighted nodes. The tree is undirected and can be rooted at any node we choose. Once a root is fixed, every other node has exactly one parent defined by the rooted tree structure.

codeforcescompetitive-programming
CF 105158G - 扫雷 2

We are asked to construct an $n times n$ binary grid, where each cell is either a mine or an empty cell. The grid must contain exactly $m$ mines.

codeforcescompetitive-programming
CF 105145F - Дерево на Манхеттене

We are given a rooted tree with root at vertex 1. Every non-root node has a parent, and each edge from a node to its parent has a non-negative weight. We must assign to every vertex a distinct integer from 1 to n, forming a permutation of the vertices.

codeforcescompetitive-programming
CF 105145A - Максимальная прочность

We are given a continuous range of integers from L to R, where each integer represents a material. From this range, we must choose two materials (they may be the same) and compute a score based on how different their decimal representations are.

codeforcescompetitive-programming
CF 105145E - Печатная машинка

We are given a permutation of size n, representing numbers placed in n cells in a line. The goal is to understand how many times we must “reset” a special typing machine in order to restore the permutation into the identity arrangement where number i sits in cell i, but we…

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 205

Let the perfect shuffle of Exercise 204 be the MMIX program obtained in (175)–(178), using constants $p, q, r, m$, and let it map an input register state $z$ to an output state $w$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 204

The failure in the previous construction comes from a false invariant: masking with x \,\&\, 0xFFFFFFFF00000000 does not produce a normalized 32-bit quantity.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 203

We restart from the actual MMIX semantics used in TAOCP.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 201

Let $x = (x_{15}\ldots x_1 x_0)_{16}$, where each $x_i \in {0,\ldots,15}$ is a hexadecimal digit.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 200

In MMIX, register $0$ is the constant zero register, so its contents are $0$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 20

Let $x>0$ and define $u = x \,\&\, (-x), \qquad v = x + u.$ Let $k$ be the unique index such that $u = 2^k$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 199

We restart from the actual existence condition.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 198

Let $l$ be the number of bytes in the UTF-8 encoding of $x$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 197

Let $x$ be a codepoint in $0 \le x < 2^{20}+2^{16}$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 196

The solution must be rebuilt from the actual definitions, not from byte-range heuristics.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 195

Let $A$ be the binary matrix with rows $\alpha_1,\ldots,\alpha_m \in {0,1}^n$.

taocpmathematicsalgorithmsvolume-4hm-medium
TAOCP 7.1.3 Exercise 193

Let $A=(a_{i,j})$ be a perfect $m\times n$ parity pattern, so for every $i,j$, a_{i,j}\equiv \sum_{j'\ne j} a_{i,j'}+\sum_{i'\ne i} a_{i',j}\pmod 2, and no row or column of $A$ is identically zero.

taocpmathematicsalgorithmsvolume-4math-medium
CF 105145D - Разрезание торта

We are given a rectangular cake modeled as an $n times m$ grid. Inside this grid there are $k$ distinct cells, each containing exactly one candle.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 192

We restart from the actual combinatorial structure of parity patterns and only use identities for Fibonacci polynomials that can be derived directly from their defining recurrence.

taocpmathematicsalgorithmsvolume-4hm-project
TAOCP 7.1.3 Exercise 191

Work in the ring R=\mathbb{F}_2[x,x^{-1}]/(x^N+1), \qquad N=2n+2, so that $x^{-1}=x^{N-1}$.

taocpmathematicsalgorithmsvolume-4math-hard
TAOCP 7.1.3 Exercise 190

We correct the solution by rebuilding the argument from the linear structure of the parity condition and avoiding any invalid submatrix or periodicity assumptions.

taocpmathematicsalgorithmsvolume-4medium
CF 105145C - Опрос на уроке

Each student in the classroom is associated with a range of topics they understand. If the teacher asks about a topic, every student either reacts positively if the topic lies inside their learned interval or negatively if it lies outside.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 189

Let the bitmap be stored as $8$ consecutive rows of bytes per block column.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 188

The failure in the previous solution is fundamental: the bitmap is 1-bit packed, so each pixel must be extracted by bit operations, not by byte-wise `LDB` interpretation.

taocpmathematicsalgorithmsvolume-4medium
CF 105145B - Игра с переворотом

We are given two strings of equal length. One player can freely change any character of either string at any time, while the other player can flip a whole string end to end in a single move.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 187

The failure in the previous solution is the assumption that the right subsegment must be explicitly stored.

taocpmathematicsalgorithmsvolume-4math-hard
TAOCP 7.1.3 Exercise 186

Let B(t) = (1-t)^2 z_0 + 2(1-t)t z_1 + t^2 z_2, \qquad 0 \le t \le 1.

taocpmathematicsalgorithmsvolume-4hm-medium
TAOCP 7.1.3 Exercise 185

Let the endpoints be rational numbers (\xi,\eta)=\left(\frac{a}{c},\frac{b}{c}\right), \qquad (\xi',\eta')=\left(\frac{a'}{c'},\frac{b'}{c'}\right), where $a,b,a',b' \in \mathbb{Z}$ and $c,c' \in \mat...

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 184

Let Algorithm T be applied to the endpoints $(x,y)$ and $(x',y')$ with quadratic form $Q$, producing a sequence of edges determined by the sign changes of $Q$ along the digitized path from $(x,y)$ to...

taocpmathematicsalgorithmsvolume-4math-medium
TAOCP 7.1.3 Exercise 183

Let $F(x,y)$ be the integer-valued function defining the conic, as in Algorithm T.

taocpmathematicsalgorithmsvolume-4math-hard
TAOCP 7.1.3 Exercise 182

Let F(x,y)=ax^2+bxy+cy^2+dx+ey+g define the conic, and let the algorithm operate on a segment of the curve on which, say, $x$ is strictly increasing (the other case is symmetric).

taocpmathematicsalgorithmsvolume-4math-hard
TAOCP 7.1.3 Exercise 181

Let the conic be given by F(x,y)=ax^2+bxy+cy^2+dx+ey+f=0,\qquad a,b,c,d,e,f\in\mathbb{Q}.

taocpmathematicsalgorithmsvolume-4hm-medium
TAOCP 7.1.3 Exercise 180

Let F(x,y)=y^{2}-x^{2}-13.

taocpmathematicsalgorithmsvolume-4math-medium
CF 105158D - 距离之比

We are given a set of points in the plane, and for every pair of points we can measure two different distances: the Manhattan distance, which adds absolute horizontal and vertical displacement, and the Euclidean distance, which is the straight-line distance.

codeforcescompetitive-programming
CF 105151H - От 6 до 12?

We are given a single integer $n$, and we want to split it into an ordered pair of positive integers $(a, b)$ such that $a + b = n$.

codeforcescompetitive-programming
CF 105151E - Циклические скобки

We are given a circular sequence of typed brackets, where each element is an integer. A positive value represents an opening bracket of a certain type, and the corresponding negative value represents its matching closing bracket.

codeforcescompetitive-programming
CF 105151C - Нижний Нижний Нижний Новгород

We are given a graph with stations as vertices and tunnels as undirected edges. Each station has a cost, and we also have a modulus value $k$. For any chosen starting station $s$, we consider all stations that are reachable from $s$ using at most $d$ edges.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 178

Let the columns of the original bitmap $X$ be indexed by $0,1,\ldots,N-1$.

taocpmathematicsalgorithmsvolume-4medium
CF 105150G - Объединение камней

We are given two collections of stones, one stored in an inventory and the other in a chest. Each stone has a size, and for every size we know how many stones of that size exist in each location.

codeforcescompetitive-programming
CF 105150F - Максим и пит-стоп

We are simulating a race where the cost of each lap depends on how worn the current tire set is. Each tire set starts with some initial wear value, and every time a lap is driven on that set, the lap takes exactly the current wear value in seconds, and then the wear increases…

codeforcescompetitive-programming
CF 105150E - Занавески

We are given a square office, but only its left and bottom walls exist. The top and right sides are open and act like a continuous source of incoming light.

codeforcescompetitive-programming
CF 105150D - Хронометраж и программирование

We are asked to imagine an infinite increasing sequence built from numbers that can be written in the form $$x = 2^k + 60m$$ where $k$ and $m$ are positive integers (or at least positive for $k$, and non-negative for $m$, depending on interpretation; the important part is that…

codeforcescompetitive-programming
CF 105150C - Карта кобры

We are given a line of segments, each segment indexed from 1 to n. The interesting part is that each segment i has a constraint value a[i] which controls how restrictive the next move becomes after visiting i.

codeforcescompetitive-programming
CF 105150A - Умный светофор

We are given a traffic light that alternates which of two one-way streets is allowed to pass. The pattern of the light is periodic and fully known in advance. Every minute belongs to either street 1 or street 2 depending on this repeating pattern. A set of cars arrives over time.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 176

Let $G$ be a graph on ${1,\ldots,n}$ and let $S={{u_j,v_j}\mid 1\le j\le r}$ be an $r$-family.

taocpmathematicsalgorithmsvolume-4math-medium
TAOCP 7.1.3 Exercise 175

The reviewer is correct that the original argument is invalid because it replaces pixel-level adjacency with an invented semantic decomposition.

taocpmathematicsalgorithmsvolume-4simple
TAOCP 7.1.3 Exercise 174

The previous argument correctly identifies a real obstruction: in three dimensions, simplicity of individual voxels is not preserved under simultaneous deletion.

taocpmathematicsalgorithmsvolume-4math-research
TAOCP 7.1.3 Exercise 173

We restate the definitions precisely and then rebuild the argument from first principles.

taocpmathematicsalgorithmsvolume-4math-hard
TAOCP 7.1.3 Exercise 172

Let the three black pixels be $a,b,c$ and assume they are pairwise king-neighbors.

taocpmathematicsalgorithmsvolume-4math-hard
TAOCP 7.1.3 Exercise 171

The previous solution failed because it did not use the actual definition of $g$ from (159).

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 170

The previous argument fails because it models Guo–Hall thinning as uniform geometric erosion.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 17

An expression $E(x_1,\ldots,x_m)$ is built from integer variables and integer constants using only $+$ and $\oplus$ (and possibly also $&$ in the second part).

taocpmathematicsalgorithmsvolume-4hm-project
TAOCP 7.1.3 Exercise 169

The state of a Life automaton on a finite torus is completely determined by the initial bitmap and the update rule given in Exercise 167.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 168

We start by separating three independent issues: the word packing geometry, the toroidal indexing, and the correctness of the bit-parallel update.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 167

Let the eight neighbors be $a_1,\dots,a_8 \in \{0,1\}$ and the center be $b\in\{0,1\}$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 166

Let $X = \operatorname{custer}(X)$, where \operatorname{custer}(X)(i,j)=\overline{X(i,j)} \;\&\; S(i,j), \quad S(i,j)=X(i-1,j)\lor X(i+1,j)\lor X(i,j-1)\lor X(i,j+1).

taocpmathematicsalgorithmsvolume-4math-medium
CF 105150B - Налоги

We are given two independent progressive tax systems and a fixed total income $X$. Dmitry and Anna must split this income into two parts: Dmitry declares $t$, and Anna declares $X - t$.

codeforcescompetitive-programming
CF 105151F - Double D

Two players simulate a deterministic game on a single integer. The state is just one number, initially $n$. Players alternate turns, starting with the first player. On each turn, the active player tries to apply a division move using their own fixed divisor.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 165

Let the $3\times 3$ configuration at time $t$ be represented by a bit matrix $X(t) = (x_{ij}(t))_{1 \le i,j \le 3}$, where each $x_{ij}(t) \in {0,1}$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 164

Let the eight neighbors of a cell $X$ be $X_{NW}, X_N, X_{NE}, X_W, X_E, X_{SW}, X_S, X_{SE}$.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.3 Exercise 163

The previous solution fails because it _assumes_ finiteness of triangle types without deriving it from the actual construction of Fig.

taocpmathematicsalgorithmsvolume-4hm-project
CF 105151G - Мой пешечный эндшпиль не удался, как я и ожидал

The board is extremely tall but only two columns wide, so every row is just a left or right cell. A white pawn starts at the bottom-left cell and moves upward row by row until it either gets stuck or reaches the top row at height $10^{18}$.

codeforcescompetitive-programming
CF 105151D - Скидки и точки

We are given a set of points on a plane, each representing a shop that yields exactly one collectible item. The key restriction is geometric: we are only allowed to pick items from shops that lie on a single straight line.

codeforcescompetitive-programming
CF 105151B - Капельки

We are given a set of rain droplets that each fall onto a point on a horizontal line. Each droplet appears at a specific coordinate and only starts expanding after its own falling time.

codeforcescompetitive-programming
CF 105151A - Чкаловская лестница

We are given five integers that describe how many steps exist in different segments of a staircase structure. The picture (which we do not need explicitly) encodes a set of possible routes from the bottom to the top, where each route corresponds to choosing a sequence of…

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 162

The previous solution fails because it replaces the actual object in Fig.

taocpmathematicsalgorithmsvolume-4hm-project
CF 105158M - 有效算法

We are given multiple independent test cases. In each test case, there are two integer arrays of the same length. For every index, we are allowed to “adjust” the value at that position, but the adjustment is not arbitrary.

codeforcescompetitive-programming
CF 105158L - Toxel 与 PCPC II

We are given a program consisting of $n$ lines, and a subset of $m$ of these lines contain bugs. The positions of all buggy lines are known in advance and are strictly increasing. Toxel repeatedly performs a debugging operation. In one operation, he chooses a prefix length $i$.

codeforcescompetitive-programming
CF 105158I - 378QAQ 和字符串

We are given a string consisting of lowercase letters, and we are allowed to change at most $k$ characters. The goal is to determine whether we can turn the string into a very rigid periodic structure.

codeforcescompetitive-programming
CF 105158J - 排列与合数

We are given a five-digit integer where all digits are different. From these five digits we are allowed to rearrange their order arbitrarily, but the resulting number must still be a valid five-digit integer, meaning it cannot start with zero.

codeforcescompetitive-programming
CF 105158H - 随机栈

We are given a process that builds a multiset dynamically. There are exactly n insert operations and n removal operations, interleaved in a fixed order. Each insertion adds a known value, while each removal deletes a uniformly random element from the current multiset.

codeforcescompetitive-programming
CF 105158F - 优秀字符串

We are given a collection of strings and we need to count how many of them satisfy a very specific structural pattern.

codeforcescompetitive-programming
CF 105158E - 保卫城邦

We are given a tree with $n$ vertices representing cities connected by $n-1$ roads. After each query, one existing road is removed and a new road is added, and the structure remains a tree. In each resulting tree, we must place troops on vertices.

codeforcescompetitive-programming
CF 105158C - 中二病也要打比赛

We are given an array of length $n$, where each element is an integer in the range $[1, n]$. We are allowed to apply a transformation defined by a function $f$, which maps every value in $[1, n]$ to another value in the same range.

codeforcescompetitive-programming
CF 105158B - 扫雷 1

We are given a sequence of $n$ game rounds. At the start of each round, exactly one coin is added to T0xel’s wallet, and coins are never lost except when they are spent.

codeforcescompetitive-programming
CF 105158A - Once In My Life

We are given a positive integer n and a digit d. We are allowed to choose another positive integer k, and we look at the product x = n · k. The goal is to make this resulting number satisfy a very specific digit pattern constraint.

codeforcescompetitive-programming
CF 105160M - 蔡光数组

We are given an array of exactly four positive integers, each between 1 and 9. The task is to decide whether this array matches a hidden pattern defined by a string “USST”, where identical characters in the string enforce equality constraints between corresponding positions…

codeforcescompetitive-programming
CF 105160L - 环形数组(hard)

We are given a rectangular grid of size $n times m$ whose cells are filled with the integers from $1$ to $n cdot m$. The filling order is not row-wise or column-wise.

codeforcescompetitive-programming
CF 105160J - 上学

We are given a tree with nodes labeled from 1 to n, plus an extra node 0. Node 0 is connected to node 1, so effectively node 0 acts like a root attached above the original tree. Every other edge connects the n student locations into a tree. Each student lives at a unique node i.

codeforcescompetitive-programming
CF 105160K - 环形数组(easy)

The task describes a deterministic way to assign numbers to an n by m grid. Imagine starting with an empty matrix and writing integers beginning from 1, increasing one by one, while always walking along the outer boundary of the remaining unfilled region in a clockwise spiral.

codeforcescompetitive-programming
CF 105160I - 纠缠之圆

We are given two circles in the plane. Each circle is defined by its center coordinates and radius. For every test case, we need to count how many distinct straight lines exist such that the line is tangent to both circles at the same time.

codeforcescompetitive-programming
CF 105160H - 十六进制的疑惑

We are given a collection of hexadecimal numbers written as strings. Each number is supposed to represent a valid non-negative integer in base 16, but the data set has a twist: some entries are correct results of hexadecimal subtraction problems, while others are wrong results…

codeforcescompetitive-programming
CF 105160G - 石子游戏

We start with a single pile of stones. Two players alternate turns, Alice moving first. On a turn, if the pile currently has $x$ stones, the player may add between $1$ and $x$ stones inclusive. After the move, the pile size must not exceed a fixed upper bound $k$.

codeforcescompetitive-programming
CF 105160F - 十六进制的异或

We are given a collection of distinct numbers written in hexadecimal, and a sequence of queries. For each query, we receive a decimal number $x$.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 161

We restate the problem in graph-theoretic form.

taocpmathematicsalgorithmsvolume-4medium
CF 105160E - 昨日方舟

The grid describes a map where each cell is either blocked or available for placing a unit. Over time, we receive a sequence of placement attempts. Each attempt tries to place a directional unit, a snake, on a specific cell facing up, down, left, or right.

codeforcescompetitive-programming
CF 105160D - 方块游戏

We are given an $n times m$ grid that represents a tiled game board. Each cell is either empty or colored with one of three colors labeled 1, 2, and 3.

codeforcescompetitive-programming
CF 105160B - 俄罗斯方块

We are given an $n times n$ grid and a multiset of rectangular tiles that can be placed either horizontally or vertically. Every tile is a $1 times k$ strip for some length $k$, and we are allowed to place each strip anywhere inside the grid as long as it stays inside bounds.

codeforcescompetitive-programming
CF 105160A - 我是组题人

We are given a list of problem difficulties, where each problem also has an implicit identifier given by its position in the input. The task is to reorder the problem indices according to difficulty from smallest to largest.

codeforcescompetitive-programming
CF 105160C - 小学题

We are given a large square $ABCD$ with side length $n$. Inside it sits a smaller square $AEFG$ whose side length is a variable integer $m$, restricted to an interval $[l, r]$.

codeforcescompetitive-programming
CF 105161K - Number Deletion Game

We are given a multiset or array of integers. Two players alternate turns in a game. On each turn, a player is allowed to remove one occurrence of the current maximum value present in the structure.

codeforcescompetitive-programming
TAOCP 7.1.3 Exercise 160

We restart from the definitions implicit in formulas (150) and (151) and prove directly that they generate identical labels, without introducing unproved intermediate tables.

taocpmathematicsalgorithmsvolume-4math-hard