brain

tamnd's digital brain — notes, problems, research

41230 notes

CF 104361D - Перевернутые родословные

We are given a structure on $n$ labeled people where each person has either zero or exactly one child. If a person has no child, their outgoing pointer is 0. Otherwise, every person points to exactly one child index in $[1, n]$.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 17

A reduced ordered binary decision diagram (BDD) for a Boolean function $f(x_1,\dots,x_n)$ consists of a rooted directed acyclic graph in which each non-sink node $v$ has a variable index $V(v)\in{1,\d...

taocpmathematicsalgorithmsvolume-4hard
CF 104361B - Выбор цветов для букета

We are given a bouquet that must contain exactly n flowers, and there are m flower types. Each type can be used any number of times. The reward model for a type is not constant per flower.

codeforcescompetitive-programming
CF 104361C - Межпланетные электрички

We are working with a cyclic daily timetable split into minutes. A passenger railway service must run forever with a fixed periodic pattern: trains depart exactly every m/2 minutes, and each departure occupies the platform for a fixed interval before it.

codeforcescompetitive-programming
CF 104361A - Подготовка к экзамену

We are asked to construct three integers $a, b, c$, all chosen from a fixed interval $[l, r]$, together with a positive integer $n$, such that a linear expression holds exactly: $$n cdot a + b - c = m.$$ The input gives us the bounds $l$ and $r$, and a target value $m$.

codeforcescompetitive-programming
CF 104363K - Turn-based Game

We are simulating a sequence of battles where each battle consists of fighting a small group of identical monsters. Each monster has a fixed amount of health, and every attack reduces the chosen monster’s health by exactly one.

codeforcescompetitive-programming
CF 104363L - Subxor

We are given an array of integers and a fixed integer $K$. For each query, we look at a subsegment of the array, from index $l$ to $r$, and we want to choose a contiguous subarray inside this segment that maximizes its length under a constraint on XOR.

codeforcescompetitive-programming
CF 104363J - XOR String

We are given a circular string, so substrings are allowed to wrap from the end back to the beginning. Each position in this circle has a lowercase character and an associated integer value.

codeforcescompetitive-programming
CF 104363I - Club

We are given a collection of n clubs, and we must assign each club one of m badge types. Multiple clubs can share the same badge type, but every badge type must appear at least once. After the assignment is fixed, a participant repeatedly visits clubs.

codeforcescompetitive-programming
CF 104363H - KingZ

We are given a fixed 10×10 board where each cell represents a battlefield tile. Some cells are walls and cannot be used. Every other cell may initially contain a number of troops and also belongs to a category such as core, keep, lawn, or neutral territory.

codeforcescompetitive-programming
CF 104363G - Gravity

We are given a set of points in the plane, each representing an asteroid of equal mass. We must split these points into two nonempty groups.

codeforcescompetitive-programming
CF 104363F - Folder

We are given a rooted tree where each node represents a folder. Folder 1 is fixed as the root, and every other folder has exactly one parent. So the structure is a hierarchy of directories.

codeforcescompetitive-programming
CF 104363D - Pandemic

We are asked to count how many valid “distribution plans” Kanade can follow while serving a row of $n$ rooms arranged in a line. The process always moves strictly from left to right, never revisiting rooms.

codeforcescompetitive-programming
CF 104363E - Ethernet

We are simulating a very small randomized process on a fixed set of ports and cables, where each cable ultimately ends up plugged into exactly one port. There are n ports labeled from 1 to n and n cables also labeled from 1 to n.

codeforcescompetitive-programming
CF 104363C - Compass

We are dealing with a system of three rotating components. Each component has a position on a circular scale, and each full rotation brings it back to the starting point after a fixed number of steps. The twist is that we do not directly choose how much each component rotates.

codeforcescompetitive-programming
CF 104363B - Chevonne's Game

We are given a binary string representing a row of pearls, where each pearl is either white or black. The system supports two operations over time. One operation flips the colors of all pearls in a range, turning white into black and black into white.

codeforcescompetitive-programming
CF 104363A - Magic Computer

We are given a process involving a collection of USB disks, where each disk initially holds a unique file. The computer has a very unusual constraint: at any moment, it only interacts with the two most recently inserted disks, and when two disks are inserted together they…

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 169

Let $f$ be a read-once Boolean function of $22m+1$ variables, represented by a rooted formula tree in which each variable appears exactly once.

taocpmathematicsalgorithmsvolume-4math-research
CF 104366L - Spatial Quantum Energy Theory

We are given a system of atoms, where each atom is defined by a subset of at most 20 possible elementary particle types. Each particle type has a fixed energy value.

codeforcescompetitive-programming
CF 104366M - Easy Problem of Prime

We are given many independent queries. Each query provides a number $n$, and for that value we first imagine all integers from $2$ up to $n$. For each integer $i$, we define a value $f(i)$, where $f(i)$ is the smallest number of prime numbers whose sum equals exactly $i$.

codeforcescompetitive-programming
CF 104366J - Less Time on the Road

We are given a directed graph where every edge has unit cost, and the graph is guaranteed to be strongly connected. Two workers start at vertex 1. A sequence of requests arrives, and each request specifies a vertex that must be visited to perform a repair.

codeforcescompetitive-programming
CF 104366K - The Secret Comparison

Two competitors each have a single integer score. The task is to compare these two numbers and declare who has the higher score, or whether they are equal. The input consists of exactly two integers, representing the scores of the two players.

codeforcescompetitive-programming
CF 104366I - Subsetting and Summing

We are given a collection of 3D vectors. From these vectors we may choose any subset, including the empty set, and sum the chosen vectors component-wise to obtain a single resultant vector.

codeforcescompetitive-programming
CF 104366H - Light the Street

We are placing streetlights along a one-dimensional segment of length $n$. We are allowed to choose up to $k$ positions for these lights.

codeforcescompetitive-programming
CF 104366G - Expected Sum

We are given a long decimal string, interpreted as a sequence of digits placed left to right. Between every adjacent pair of digits, we independently decide whether to insert a plus sign.

codeforcescompetitive-programming
CF 104366E - Triangle Pick

We are given a collection of triangular surfaces in 3D space. Each triangle is a flat solid piece defined by three vertices.

codeforcescompetitive-programming
CF 104366F - MPFT

We are simulating a chat group that changes over time. People either join the group or send messages, and the group has strict rules that can forcibly remove members. The first rule is a capacity constraint. The group can hold at most N people.

codeforcescompetitive-programming
CF 104366D - Concrete Painting

We are given a collection of segments on a number line. From these segments, we consider every possible subset of them. For any chosen subset, we imagine painting all of its segments onto the number line, where overlapping parts are still counted only once.

codeforcescompetitive-programming
CF 104366B - Problem B

The town is a rectangular grid of intersections with roads connecting adjacent intersections in the usual four-direction structure. Vehicles enter from any boundary road endpoint and must eventually leave through some boundary endpoint.

codeforcescompetitive-programming
CF 104366C - Abstract Painting

We are given a collection of axis-aligned line segments on an infinite 2D plane. Each segment is either vertical or horizontal. Two segments are considered connected if they physically intersect at any point, including touching at endpoints.

codeforcescompetitive-programming
CF 104366A - Cask Effect

We are given several wooden boards, each with a fixed length. The “strength” or “capacity” of a cask built from these boards is defined as the length of the shortest board used in it.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 168

Let a read-once Boolean function $f(x_1,\dots,x_n)$ be given, and let $B(f)$ denote the number of BDD nodes including sinks, as defined in Section 7.

taocpmathematicsalgorithmsvolume-4hm-project
CF 104369M - Computational Geometry

We are given a convex polygon described by its vertices in counterclockwise order. The task is to choose two distinct vertices and draw a chord between them, but not every pair is allowed: the chord must actually split the polygon into two regions, and both resulting pieces…

codeforcescompetitive-programming
CF 104369L - Classic Problem

We are given a very large graph on vertices labeled from 1 to n, where every pair of vertices is connected by an edge, so the graph is complete.

codeforcescompetitive-programming
CF 104369J - X Equals Y

We are asked to choose two bases, one for $x$ and one for $y$, so that when both numbers are written in their respective bases, the resulting digit sequences are identical when read from least significant digit to most significant digit.

codeforcescompetitive-programming
CF 104369K - Peg Solitaire

We are given a very small chessboard, at most six rows by six columns, with up to six pegs placed on distinct cells.

codeforcescompetitive-programming
CF 104369I - Path Planning

We are given a grid where every cell contains a distinct integer from the range $[0, n cdot m - 1]$. We start at the top-left cell and can only move right or down until reaching the bottom-right cell.

codeforcescompetitive-programming
CF 104369H - Canvas

We are given a sequence of length $n$, initially filled with zeros. We also have $m$ operations. Each operation selects two positions $li < ri$ and assigns values $xi$ and $yi$ to those positions, overwriting whatever is currently there.

codeforcescompetitive-programming
CF 104369G - Swapping Operation

We are given an array of non-negative integers. For any split point $k$, we can divide the array into a prefix and a suffix. For that split, we compute the bitwise AND of the prefix and the bitwise AND of the suffix, then sum the two results.

codeforcescompetitive-programming
CF 104369F - Traveling in Cells

We are given a line of cells, each cell having a fixed position, a color, and a value associated with a single removable ball. The structure changes over time because both colors and values of individual cells can be updated.

codeforcescompetitive-programming
CF 104369E - New but Nostalgic Problem

We are given a collection of strings and we are allowed to pick exactly k of them. Once the subset is fixed, we look at every pair inside it and compute their longest common prefix. Among all these pairwise LCP values, we take the lexicographically largest one.

codeforcescompetitive-programming
CF 104369D - New Houses

We are given a line of houses indexed from 1 to m, and we must place n people into distinct houses. Two people are considered neighbors only when they occupy adjacent house indices.

codeforcescompetitive-programming
CF 104369C - Trading

We are given a street with several shops, each shop offering the same type of product at a fixed price. In each shop, the price is symmetric: if you buy or sell one unit there, the cost or revenue is exactly the same value.

codeforcescompetitive-programming
CF 104369B - Base Station Construction

We are given a line of positions from 1 to n, where each position has a cost for building a base station. We are allowed to choose any subset of positions to build base stations, paying the sum of their costs.

codeforcescompetitive-programming
CF 104369A - Programming Contest

We are looking at a yearly event that starts at some initial year y1. From that year onward, the contest is intended to happen once every year. However, there is a small list of exceptional years where the contest did not take place.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 167

We restart from the structural characterization of read-once Boolean functions and the defining property of reduced ordered binary decision diagrams.

taocpmathematicsalgorithmsvolume-4medium
CF 104373K - Link-Cut Tree

We are given an undirected graph where each edge has a very special weight: the i-th edge in input order has weight $2^i$.

codeforcescompetitive-programming
CF 104373J - Colorful Tree

We are building a tree incrementally. The structure starts with a single node, and each operation either attaches a new node to an existing node with a weighted edge or changes the color of an existing node.

codeforcescompetitive-programming
CF 104373I - LCS Spanning Tree

We are given a collection of strings, each representing a vertex in a complete undirected graph. Every pair of vertices is connected, and the weight of an edge is defined by how similar the two strings are: specifically, it is the length of the longest substring that appears…

codeforcescompetitive-programming
CF 104373H - Permutation on Tree

We are given a rooted tree with $n$ labeled vertices. We consider all permutations of the vertices, but we only keep those permutations that respect the tree’s ancestor structure: whenever a node $u$ is an ancestor of node $v$, then $u$ must appear earlier in the permutation.

codeforcescompetitive-programming
CF 104373G - Cyclic Buffer

We are given a circular array that contains a permutation of the numbers from 1 to n. There is a fixed window of size k that represents the “visible” part of the buffer, specifically the first k positions at any moment.

codeforcescompetitive-programming
CF 104373D - Shortest Path Fast Algorithm

The algorithm in the statement is a modified shortest path routine that behaves like SPFA but uses a priority queue instead of a FIFO queue. Every time a vertex is extracted from the queue, it relaxes its outgoing edges.

codeforcescompetitive-programming
CF 104373F - Sandpile on Clique

We are given a complete graph with $n$ vertices, where every vertex is connected to every other vertex. Each vertex starts with some number of chips.

codeforcescompetitive-programming
CF 104373E - Pass the Ball!

We are given a directed mapping over n children. Each child always passes whatever ball they currently hold to exactly one fixed destination child p[i].

codeforcescompetitive-programming
CF 104373B - The Matching System

We are given a length $n$, and we must construct two binary strings of that length: a pattern string and a target string. The pattern is not purely binary; it also contains two special symbols that define a recursive matching process against the binary string.

codeforcescompetitive-programming
CF 104373A - So I'll Max Out My Constructive Algorithm Skills

We are given a square grid of size $n times n$, where every cell contains a distinct integer from 1 to $n^2$. You can think of this grid as a weighted graph laid out on a lattice: each cell is a node, and edges exist between orthogonally adjacent cells.

codeforcescompetitive-programming
CF 104373C - Laser Trap

We are given a set of points in the plane. Each point acts as a laser generator, and every pair of generators is connected by a straight laser segment. So for n points, the system forms a complete geometric graph where every edge is a segment between two given coordinates.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 166

We restart the argument from the formal semantics of ordered binary decision diagrams and build canonicity directly from evaluation, without assuming any uniqueness of a construction procedure.

taocpmathematicsalgorithmsvolume-4math-hard
CF 104375K - Kingdom Power C.

The game can be modeled as a directed graph where each level is a node and each prerequisite relation is a directed edge. If there is an edge from level u to level v, then finishing u unlocks v, so v becomes playable after u is completed.

codeforcescompetitive-programming
CF 104375J - Jumping Reaction

We are given an array of integers where each value represents a “jumping energy” of a substance. When two substances with energies $a$ and $b$ are mixed, they contribute an energy of $ab$.

codeforcescompetitive-programming
CF 104375I - Improving Chewing Candy

We are given a circular arrangement of candy blocks, where each block has a flavor represented by a lowercase letter. The circularity means the first and last positions are adjacent, so any segment we take can wrap around the end of the string.

codeforcescompetitive-programming
CF 104375F - Finding the Best Guess

We are given a tree where every node carries a positive weight. A process runs for exactly $n$ rounds. In each round, one remaining node is chosen uniformly at random.

codeforcescompetitive-programming
CF 104375H - Hell or paradise?

We are given a collection of words and a long string written on a monster’s body. The task is to determine how many ways we can cut this long string into consecutive pieces so that each piece exactly matches one of the given words.

codeforcescompetitive-programming
CF 104375G - Growing game

We are given a pile of chips and two players who alternate turns, starting with Jane. On each turn, the player removes between 1 and a bounded number of chips, where the bound grows with the turn index.

codeforcescompetitive-programming
CF 104375E - Employees Bonus

We are given a company hierarchy that forms a rooted tree. Each employee corresponds to a node, and every node has a subtree consisting of all employees they supervise directly or indirectly, including themselves. The company processes a sequence of bonus events.

codeforcescompetitive-programming
CF 104375D - Dynamic Collection

We maintain a multiset of integers with two operations: inserting or modifying the structure in a specific ordered way, and answering how many elements lie inside a numeric interval. The collection is not just a static bag.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 165

We restart the argument from the ROBDD construction rules and avoid manipulating the coupled recurrences in an unjustified way.

taocpmathematicsalgorithmsvolume-4math-medium
CF 104375C - Counting Stars

We are given a set of points in the plane, each representing a star. We want to count how many valid “spoke constellations” can be formed. A configuration is defined by choosing one star as a center and then selecting other stars around it in a very structured way.

codeforcescompetitive-programming
CF 104375A - Aliases

Each person’s full name is given as a sequence of words. From that name, a compact identifier called a NAME is constructed by taking the first letter of every word and concatenating these letters in order. So a name like “jose osorio jimenez orozco” becomes “jojo”.

codeforcescompetitive-programming
CF 104375B - Bucket storing

We are given a situation where coffee is first packed into several identical small containers. Each small container always holds exactly $K$ units of coffee, and there are $N$ such containers. So the total amount of coffee is simply $N times K$.

codeforcescompetitive-programming
CF 104377O - 捕鱼达人!

We are given a set of points in the plane, each point carrying a positive or negative value. A fisherman stands at the origin and can deploy a net in a very flexible way.

codeforcescompetitive-programming
CF 104377N - 解密

We are given a sequence of 2×2 matrices that play the role of convolution kernels, and another implicit sequence of 2×2 matrices that acts as the inverse under the same convolution rule. More concretely, each input test case gives a list of matrices $A0, A1, dots, A{n-1}$.

codeforcescompetitive-programming
CF 104377M - 数字模拟

We are given a fixed-height ASCII picture consisting of 5 rows and 18 columns. Each column contains either a star character or a dot-like blank representation, and together these characters encode three digits written side by side.

codeforcescompetitive-programming
CF 104377L - MEX问题

We are given a sequence of integers, and we are asked to count how many of its subsequences satisfy a structural constraint that depends on the MEX of every prefix of that subsequence.

codeforcescompetitive-programming
CF 104377K - 字符串游戏

We are given two players, each owning a collection of string “tiles”. The first player has $n$ distinct tile types, and each type can be used an unlimited number of times. The second player has $m$ tile types, also with unlimited supply.

codeforcescompetitive-programming
CF 104377J - BLG冲冲冲!

We are given a full matrix describing the outcome probabilities between every pair of 8 teams. For any two teams $i$ and $j$, the entry $a{i,j}$ gives the probability that $i$ defeats $j$ in a single match, with the complementary probability $a{j,i}$ ensuring that exactly one…

codeforcescompetitive-programming
CF 104377H - 账号已注销,我想账号已注销了

We are given a sequence of $n$ pillars, each with a height. From this sequence we are allowed to choose a non-empty subsequence while preserving the original order. After selecting, we only keep the chosen pillars.

codeforcescompetitive-programming
CF 104377I - 这真的是签到题

We are given a list of integers and asked to select the largest value in that list that satisfies a specific structural property: every prime factor of that value must also appear somewhere in the list.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 164

A Boolean function $f(x_1,\dots,x_n)$ is **read-once** if it can be expressed by a formula in which each variable $x_i$ appears exactly once.

taocpmathematicsalgorithmsvolume-4math-hard
CF 104377G - 我的车呢?

We are standing in a 2D plane and there is a hidden point representing a car. We are given one crucial parameter, a radius r, and we can interactively move a point anywhere in the plane and receive a binary answer: whether our current position is within distance r of the…

codeforcescompetitive-programming
CF 104377F - 珂朵莉树

We start with an array of length $n$. The array is repeatedly modified by choosing a random interval $[l, r]$ uniformly among all $frac{n(n+1)}{2}$ possible subsegments, and assigning all elements in that interval a new value that has never been used before.

codeforcescompetitive-programming
CF 104377E - 传感器对齐

We are given two ordered sequences of integers, both of the same length. Each sequence comes from a sensor reading over time, so the index order is fixed and meaningful.

codeforcescompetitive-programming
CF 104377D - 随机数生成器

We are given an array and a random process that repeatedly samples indices uniformly from a chosen segment of this array. After taking k independent samples, we look at the smallest and largest sampled indices and return the sum of the array over that interval.

codeforcescompetitive-programming
CF 104377A - 计算异或和

We are asked to look at all ordered arrays of length $m$ consisting of non-negative integers whose total sum is fixed to $n$. Every such array contributes a value equal to the bitwise XOR of all its elements, and we need the sum of these XOR values over all valid arrays.

codeforcescompetitive-programming
CF 104377C - 二维数组变换

We are given an n by n matrix and a sequence of operations. Each operation selects a square submatrix using its top-left and bottom-right coordinates, then applies one geometric transformation to that submatrix.

codeforcescompetitive-programming
CF 104377B - 最大价值

We are given an undirected graph where each edge carries a weight that behaves like a threshold. You start at node S holding a value k, and want to reach node T.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 163

A read-once Boolean function $f(x_1,\ldots,x_n)$ is represented by a formula tree in which each variable occurs exactly once.

taocpmathematicsalgorithmsvolume-4medium
TAOCP 7.1.4 Exercise 162

Let $X = (x_{ij})$ be a $6 \times 6$ matrix with entries in ${0,1}$.

taocpmathematicsalgorithmsvolume-4hard
CF 104380R - Deque 2 (Hard Version)

We are given a sequence of numbers and we build a deque by processing them in order. For each element, we decide independently whether it is inserted at the front or at the back.

codeforcescompetitive-programming
CF 104380Q - Deque 2 (Easy Version)

We are repeatedly building a sequence of length n by processing the array from left to right. At each step, the current element is inserted either at the front or at the back of an initially empty deque.

codeforcescompetitive-programming
CF 104380P - Dungeon

The dungeon is a straight line, and the knight walks from position 0 to position D without ever turning back. Along this path there are two kinds of encounters: monsters and shops, each placed at fixed positions.

codeforcescompetitive-programming
CF 104380O - Rabbit Jump

We start at the origin in a grid and want to reach a target coordinate $(x, y)$. From any current position, the rabbit has three possible moves: it can move one step right for cost $A$, one step up for cost $B$, or it can scale both coordinates by a factor of two for cost $C$.

codeforcescompetitive-programming
CF 104380N - Robot

We are given a grid representing a maze where some cells are blocked and others are free. A robot starts in the top-left cell and must execute a sequence of moves of fixed length.

codeforcescompetitive-programming
CF 104380M - Tower

Each tower in this problem acts like a light source placed on a number line. A tower at position $ai$ emits a brightness that starts at $pi$ at its own location and then decreases linearly as you move away from it.

codeforcescompetitive-programming
CF 104380L - Equation

We are given a consecutive list of integers starting from 0 up to n. Between every pair of adjacent numbers, we are allowed to insert either a plus or a minus sign, effectively deciding whether each number contributes positively or negatively to a running sum.

codeforcescompetitive-programming
CF 104380K - glimmerypond

We are given a binary grid that represents a pond, where each cell is either water or empty ground. On top of this grid, we consider every possible square region of fixed size $k times k$.

codeforcescompetitive-programming
CF 104380J - No 7

We are given a single positive integer x, and we need to move strictly downward to find the closest smaller integer that avoids a specific digit constraint: none of its decimal digits may be 7.

codeforcescompetitive-programming
CF 104380H - 01 (Hard Version)

We are given a binary string that evolves over time. Two kinds of operations happen: flipping a single character, and answering a query on a substring.

codeforcescompetitive-programming
CF 104380I - Bad Grades

We are given a sequence of exam grades for a student, each grade being an integer between 0 and 100. The task is to produce a cleaned version of this sequence where every grade below 60 is removed, while keeping the relative order of the remaining grades exactly the same as in…

codeforcescompetitive-programming
CF 104380G - Social Network

We are given a set of people labeled from 1 to $n$, but $n$ can be extremely large, so we cannot afford to explicitly build any structure over all individuals.

codeforcescompetitive-programming