brain

tamnd's digital brain — notes, problems, research

41230 notes

TAOCP 7.1.4 Exercise 49

For each $m \ge 2$, an $m$-ary de Bruijn cycle of order $n$ is a cyclic sequence $C_{m,n}$ of length $m^n$ over ${0,1,\dots,m-1}$ in which every $m$-ary string of length $n$ occurs exactly once as a c...

taocpmathematicsalgorithmsvolume-4medium
CF 104076H - Set of Intervals

We start with a collection of intervals. Each interval represents a continuous range on the number line. The process repeatedly merges two existing intervals into one new interval.

codeforcescompetitive-programming
CF 104076F - Grid Points

Let $f(x1,ldots,xn)$ have truth table $tau$, and let $f^Z$ have truth table $tau^Z$. For $0 le k le n$, let $Sk(x1,ldots,xn)$ denote the subfunction obtained by fixing $x1=cdots=xk=1$, so its truth table is the subtable of $tau$ of order $n-k$ starting at position $2^k$ in the…

codeforcescompetitive-programming
CF 104076G - Quick Sort

We are given a deterministic quicksort implementation that always chooses the middle element of the current segment as the pivot and uses a Hoare-style partition procedure.

codeforcescompetitive-programming
CF 104076E - Identical Parity

We are given a permutation of numbers from 1 to n and a fixed segment length k. For every contiguous block of length k inside the permutation, we compute its sum.

codeforcescompetitive-programming
CF 104076D - Frozen Scoreboard

We are given a contest with up to 1000 teams and at most 13 problems. For each team, we know two kinds of information that must be made consistent. The first is the final official result: how many problems the team solved and the total penalty time across those solved problems.

codeforcescompetitive-programming
CF 104076C - DFS Order 2

Let $tau$ be the truth table of $f(x1,ldots,xn)$, and let $f^Z$ be the Boolean function whose truth table is $tau^Z$, where $tau^Z$ is defined by the recursive Z-transform in Exercise 192.

codeforcescompetitive-programming
CF 104076A - Tower

We are given a collection of tower heights, where each tower has an integer height. Before doing anything else, we are allowed to permanently delete exactly $m$ towers.

codeforcescompetitive-programming
CF 104076B - Torch

Two people are walking through a narrow cave in a fixed order. The first person, Pang, is always ahead, and the second person, Shou, follows exactly one unit behind at the start.

codeforcescompetitive-programming
CF 104077G - Perfect Word

We are given a collection of strings. From this collection we want to build a new string, and we call it valid if every contiguous piece of it appears somewhere in the given collection as one of the input strings.

codeforcescompetitive-programming
CF 104077L - Tree

We are given a rooted tree where node 1 is the root and every other node has a parent pointer, so the structure is fixed and acyclic. For each node, we can talk about its subtree, meaning all nodes whose path to the root passes through that node.

codeforcescompetitive-programming
CF 104077K - Streets

We are working on a geometric selection problem defined on a grid that is not explicitly built, but implicitly formed by vertical and horizontal lines.

codeforcescompetitive-programming
CF 104077J - Strange Sum

We are given an array of values, and we want to pick a subset of positions to maximize the sum of selected values. The twist is that selection is constrained in a non-uniform way: every chosen element imposes a restriction that depends on its index.

codeforcescompetitive-programming
CF 104077I - Square Grid

We are working on a grid graph formed by lattice points from coordinates $(0,0)$ to $(n,n)$. Each point is connected to its four orthogonal neighbors whenever those neighbors stay inside the grid. A move is one step along one of these edges.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 48

The previous solution failed at three precise points: it never constructed the module network in the sense of Fig.

taocpmathematicsalgorithmsvolume-4math-medium
CF 104077F - Hotel

The Z-transform is defined recursively on binary strings with special behavior depending on whether the second argument is a block of zeros, identical to the first half, or a general concatenation case.

codeforcescompetitive-programming
CF 104077H - Power of Two

We are given a multiset of numbers where each number is a power of two, meaning each value is of the form $2^{ci}$. Alongside these numbers, we are also given a fixed number of bitwise operators: some AND operations, some OR operations, and some XOR operations.

codeforcescompetitive-programming
CF 104077D - Contests

We are given multiple complete rankings of the same set of contestants. Each contest produces a permutation of all contestants, so for any contest we can compare two contestants and decide which one ranks ahead of the other.

codeforcescompetitive-programming
CF 104077E - Find Maximum

We are given a recursively defined function over non-negative integers. The function assigns a value to every integer starting from zero, where zero has a fixed value, and every positive number is computed from a smaller number.

codeforcescompetitive-programming
CF 104077A - Bridge

Connection interrupted. Waiting for the complete answer

codeforcescompetitive-programming
CF 104077C - Clone Ranran

We are simulating a contest preparation process that evolves over time. At the start there is a single worker, Ranran, and there is no work done yet. The goal is to reach a state where at least c problems have been prepared as quickly as possible.

codeforcescompetitive-programming
CF 104077B - Cells Coloring

We are given a grid where each cell is either blocked or available. On the available cells we want to assign colors, but the coloring rule is restrictive: for every color other than 0, no two cells sharing the same row or the same column are allowed to have that color.

codeforcescompetitive-programming
CF 104081L - 彩色的树

We are given a rooted tree with nodes labeled from 1 to n. Each node has a color. The tree is rooted at node 1, so every node has a well-defined subtree consisting of itself and all its descendants. We are also given several queries.

codeforcescompetitive-programming
CF 104081I - 宠物对战

I can write the full editorial, but I need the actual problem statement in a clean, uncorrupted form first. Right now the statement you pasted is heavily broken by formatting loss (missing variables, constraints, and most importantly the exact definitions of the three…

codeforcescompetitive-programming
CF 104081K - 区间和

We are given an array of length $n$, and every element is a non-negative integer. From this array we consider every contiguous subarray, and each subarray has a weight equal to the sum of its elements.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 47

Let $f$ be a Boolean function with a reduced ordered binary decision diagram $G$.

taocpmathematicsalgorithmsvolume-4math-medium
CF 104081J - 瑞士轮

We are simulating a 32-team Swiss-system tournament where each match produces a winner and a loser according to fixed pairwise win probabilities derived from team strengths. Each team starts at state 0 wins and 0 losses.

codeforcescompetitive-programming
CF 104081H - 提瓦特之旅

We are given a weighted undirected graph and a traveler who wants to move from a fixed starting node to a fixed destination node. Each edge has a travel time.

codeforcescompetitive-programming
CF 104081G - 排队打卡

We are given a discrete-time queueing system where time is divided into seconds. At the start of some seconds, new people arrive and join the end of a queue. At the end of every second, a fixed number of people are admitted from the front of the queue and leave the system.

codeforcescompetitive-programming
CF 104081D - Devil May Cry

I can write the full editorial, but I need the actual problem statement in a clean, uncorrupted form first. Right now the statement you pasted is heavily broken by formatting loss (missing variables, constraints, and most importantly the exact definitions of the three…

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 46

Let $f(x_1,\dots,x_n)$ be the three-in-a-row function, that is, f(x_1,\dots,x_n)=1 iff there exists $i$ with $1\le i\le n-2$ such that either

taocpmathematicsalgorithmsvolume-4math-medium
CF 104081E - 睡觉

We are tracking a single scalar state that changes once per second while a music track is played in an infinite loop. The track has length $n$, and each second produces a fixed “loudness” value from this period.

codeforcescompetitive-programming
CF 104081F - 位运算谜题

We are given nine integers for each test case, but their meaning is partially hidden. Behind them are three unknown non-negative integers, call them $a$, $b$, and $c$. For every pair among these three numbers, we are told three bitwise results: XOR, OR, and AND.

codeforcescompetitive-programming
CF 104081A - 减肥计划

We are given a line of people, each with a fixed weight, and a game that repeatedly compares people at the front of the line. In each round, the first two people in the queue compete. The heavier one wins the round.

codeforcescompetitive-programming
CF 104081C - 测量学

We are looking at a geometric navigation problem in a circular campus layout. Everything is organized around a central library, with several concentric circular roads (think of them as rings).

codeforcescompetitive-programming
CF 104081B - 翻新道路 II

Connection interrupted. Waiting for the complete answer

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 45

We restart the construction from the actual BDD network underlying (33)–(34), where each module corresponds to a node of an ordered decision diagram and therefore represents a Boolean subfunction dete...

taocpmathematicsalgorithmsvolume-4medium
CF 104090K - Master of Both

In a ZDD, each level corresponds to a variable, and a node labeled $k$ represents a decision on $xk$, where the low edge excludes the variable and the high edge includes it in the represented family of sets.

codeforcescompetitive-programming
CF 104090M - Please Save Pigeland

We are given a weighted tree of up to 5×10^5 cities. A subset of k cities are infected. We must choose one city r as a hospital location and also choose a fixed integer parameter d for a special transport system.

codeforcescompetitive-programming
CF 104090L - Levenshtein Distance

We are given a pattern string S and a text string T. From T, we consider every contiguous substring. For each such substring X, we compute its Levenshtein distance to S, meaning the minimum number of insertions, deletions, or substitutions needed to transform one string into…

codeforcescompetitive-programming
CF 104090J - Painting

Working

codeforcescompetitive-programming
CF 104090I - Guess Cycle Length

We are interacting with a hidden directed structure that is actually a single cycle of unknown length. There are n vertices arranged in a loop, but we do not know n and we do not know the labeling order.

codeforcescompetitive-programming
CF 104090F - Da Mi Lao Shi Ai Kan De

In a ZDD, each level corresponds to a variable, and a node labeled $k$ represents a decision on $xk$, where the low edge excludes the variable and the high edge includes it in the represented family of sets.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 44

A symmetric Boolean function $f(x_1,\dots,x_n)$ depends only on the Hamming weight $t=x_1+\cdots+x_n$, so it is determined by a binary sequence \sigma = (f(0),f(1),\dots,f(n)), of length $n+1$.

taocpmathematicsalgorithmsvolume-4math-hard
CF 104090H - RPG Pro League

We are given a pool of players. Each player has a price and a set of roles they are capable of performing. A player can be used in at most one team and, within that team, occupies exactly one role from their allowed set.

codeforcescompetitive-programming
CF 104090G - Subgraph Isomorphism

We are given a connected undirected simple graph $G$. From this graph, consider all connected subgraphs that use all $n$ vertices and contain exactly $n-1$ edges.

codeforcescompetitive-programming
CF 104090D - Money Game

We are given a circular system of players, each holding a real-valued amount of money. The players are arranged in a fixed cycle, and during one round every player simultaneously transfers half of their current money to their clockwise neighbor, with the last player sending…

codeforcescompetitive-programming
CF 104090E - Oscar is All You Need

We are given a permutation of size $n$, and we are allowed to repeatedly rearrange it using a very specific block operation. Each operation selects two cut points that split the array into three consecutive non-empty segments.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 43

Let $f$ be a Boolean function on $2n$ variables and recall that $B(f)$ is the number of beads of $f$, equivalently the number of nodes in its reduced ordered BDD, including sinks.

taocpmathematicsalgorithmsvolume-4medium
CF 104090C - No Bug No Game

We are given a collection of items, each item having two pieces of information: a mandatory “size” value $pi$, and a list of possible bonus values $w{i,1}, w{i,2}, dots, w{i,pi}$. The player chooses an ordering of all items.

codeforcescompetitive-programming
CF 104090B - Useful Algorithm

We are given a small bit-width $m le 16$, so every value $ci$ is an $m$-bit binary number. The core operation is binary addition with full carry propagation exactly as in standard bitwise addition: each bit produces a sum bit and a carry to the next position.

codeforcescompetitive-programming
CF 104090A - Modulo Ruins the Legend

We are given an array of integers, and we are allowed to modify it using a very structured operation: choose two non-negative integers s and d, then add an arithmetic progression to the array so that position k (1-indexed) increases by s + (k-1)d.

codeforcescompetitive-programming
CF 104091F - Будь начеку! 2

We are counting how many valid numbers of length n can be formed under a very specific adjacency rule. A number is considered valid if every pair of consecutive digits forms a two-digit number that is prime.

codeforcescompetitive-programming
CF 104091E - Шахтёрское ремесло

We are simulating a simplified 2D world that behaves like a long 1D strip of width n and unlimited vertical height. Initially, every position in this strip is covered with grass. During the process, the game engine spawns horizontal segments of earth blocks.

codeforcescompetitive-programming
CF 104091D - Шахматный дозор

We are given a rectangular grid with $n$ rows and $m$ columns. Inside this grid there are $q$ special pieces called scouts. Each scout sits on a distinct cell $(x, y)$, where $x$ is the row index from top to bottom and $y$ is the column index from left to right.

codeforcescompetitive-programming
CF 104091B - Вступительное испытание в Коллегию Винтерхолда

We are given a positional numeral system with base $b$. Every integer has a representation in that base, and we are interested in the number of trailing zeros in that representation.

codeforcescompetitive-programming
CF 104091A - Градостроитель

We are given a total amount of unit area S, and we want to partition this area into several disjoint square plots.

codeforcescompetitive-programming
CF 104091C - Будь начеку!

We need to count how many decimal numbers with exactly n digits satisfy a special adjacency rule. A number is called beautiful if every pair of neighboring digits forms a two digit number divisible by 3.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 42

Let $f(x_1,x_2,x_3)$ be symmetric.

taocpmathematicsalgorithmsvolume-4medium
CF 104092E - Казнить нельзя помиловать

We are given a sequence of words, each carrying an integer value that can be positive or negative. We are allowed to insert up to k commas, which split the sequence into contiguous segments.

codeforcescompetitive-programming
CF 104092D - Опилки

The prompt asks for a complete editorial and verified implementation for a specific competitive programming problem. However, the problem statement you've provided is incomplete and internally inconsistent, so I can't derive or verify the correct algorithm.

codeforcescompetitive-programming
CF 104092B - Двое из ларца

We are given a dynamic array of length n, and we need to support two kinds of operations efficiently under a large number of queries. The first operation updates a single position in the array, replacing its value with a new number.

codeforcescompetitive-programming
CF 104092A - Котёнок Гав

We are given a collection of short stories split into two types: stories about a kitten and stories about a puppy. There are c kitten stories and d puppy stories in total.

codeforcescompetitive-programming
CF 104092C - Дворец (типовой)

Each palace has exactly n floors. The floors are numbered from the top, starting at 1. For the floor with index i, its base must be a square with integer side length. The area of that square cannot exceed i, and among all valid squares we always choose the largest possible one.

codeforcescompetitive-programming
CF 104094L - Wires Puzzle

I can't write a correct editorial for this problem because the prompt does not include the actual problem statement.

codeforcescompetitive-programming
CF 104094K - The Fortress Defense

I can write the full Codeforces-style editorial in the format you specified, but I’m missing the actual problem statement for 104094K - The Fortress Defense.

codeforcescompetitive-programming
CF 104094J - Pyramid Construction

I can write the full editorial in the exact style you want, but I’m missing the actual problem statement for Codeforces 104094J - Pyramid Construction. Right now the prompt section is empty, so there’s no way to reliably reconstruct the rules, constraints, or required output.

codeforcescompetitive-programming
CF 104094I - Soviet Kindergarden

The problem statement is not included in your message, so I don’t have enough information to reconstruct the task or produce a correct editorial.

codeforcescompetitive-programming
CF 104094H - One-dimensional Game

The problem statement for “Codeforces 104094H - One-dimensional Game” is not included in your message, so I don’t have the rules, input/output definition, or constraints needed to write a correct editorial.

codeforcescompetitive-programming
CF 104094G - Loop around Lake

I can’t reliably write a correct editorial for “Codeforces 104094G - Loop around Lake” without the actual problem statement. Right now I don’t have the definitions of the input, what “loop around lake” refers to, or what is being computed.

codeforcescompetitive-programming
CF 104094D - Gas Stations

We are given a straight road with several gas stations placed at increasing positions. Each station has a fixed price per liter of fuel. A car starts at position 0 with an empty tank, a limited tank capacity, and a fixed total budget.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 41

Let $F_1=1$, $F_2=1$, and $F_{k+2}=F_{k+1}+F_k$.

taocpmathematicsalgorithmsvolume-4math-medium
CF 104094F - Bob's Average

I can’t reliably write a correct editorial yet because the actual problem statement (what “Bob’s Average” asks for, input/output format, constraints) is missing from your prompt. Right now I only see the title and empty sections.

codeforcescompetitive-programming
CF 104094E - kex

I can write the full Codeforces-style editorial in your required format, but I’m missing the actual problem content.

codeforcescompetitive-programming
CF 104094C - Tournament

I’m missing the actual problem statement for Codeforces 104094C - Tournament. Without it, any “editorial” would necessarily involve guessing the rules of the tournament, which risks producing a completely incorrect solution and analysis.

codeforcescompetitive-programming
CF 104094A - Meeting Near the Fountain

I can’t reliably write a correct editorial without the actual problem statement. Right now the “Problem Statement”, input, and output sections are empty, so anything I write would be guesswork and very likely wrong.

codeforcescompetitive-programming
CF 104094B - GPS Hack

I cannot reliably reconstruct the full statement of Codeforces 104094B - GPS Hack from the available context, and without the exact rules of the process (what a “hack” operation does, what is being optimized or computed, and how inputs affect transitions), any editorial…

codeforcescompetitive-programming
CF 104095L - 送外卖

There are up to 14 locations connected by an undirected weighted graph. Each location has one delivery order that becomes available at a specific time. You start at node 1 at time 0 and move along roads at unit speed, so traveling along an edge takes time equal to its weight.

codeforcescompetitive-programming
CF 104095K - 子串翻转回文串

The problem statement in your prompt is incomplete because the PDF text has been corrupted during extraction. The crucial part describing the samples is missing, and more importantly, the statement does not include the official solution logic.

codeforcescompetitive-programming
CF 104095H - 林克与翻转排列

We are given two sequences, each a permutation of the integers from 1 to n. Think of them as an initial arrangement and a target arrangement. The only allowed operation is to pick a contiguous block of exactly k elements in the current array and reverse the order of that block.

codeforcescompetitive-programming
CF 104095J - 二进制与、平方和

We are maintaining an array of integers where each value fits in a fixed 24-bit range. The system must support two operations over subarrays.

codeforcescompetitive-programming
CF 104095I - 太阳轰炸

The problem statement in your prompt is incomplete for writing a correct editorial and solution. The crucial mathematical definition is missing: the statement only says that a missile "hits" if its damage circle intersects the target circle, but it does not include the full…

codeforcescompetitive-programming
CF 104095G - vvvvvvvim

We are given two rectangular text layouts, but each row is not stored as a raw string. Instead, each row is described in a compressed form as blocks of repeated characters. For example, a row like aaabccc is given as (a,3),(b,1),(c,3).

codeforcescompetitive-programming
CF 104095A - 班委竞选

Each student either competes for exactly one position or none of them matter for a given position. For every position, we must look at all students who applied for that position and select the one with the highest vote count.

codeforcescompetitive-programming
CF 104095F - 旅游胜地

We are given a connected undirected graph with up to one hundred thousand vertices and edges. Every vertex has two possible values: a normal value and a discounted value. For each vertex, we must choose exactly one of these two values as its final weight.

codeforcescompetitive-programming
CF 104095D - 园艺大师

We are given a row of $n$ plants, each starting with the same height $h$. For each plant, we are allowed to either keep it unchanged or cut it down to any integer height strictly less than $h$.

codeforcescompetitive-programming
CF 104095E - 发通知

We are given a set of students, each associated with a time interval during which they are actively checking messages. If a notification is sent at some chosen moment, a student receives it only if that moment lies inside their personal interval.

codeforcescompetitive-programming
TAOCP 7.1.4 Exercise 40

Let $f$ be a Boolean function of variables $x_1,\dots,x_n$ and let $g$ be obtained from $f$ by the condensation $x_{k+1} \leftarrow x_k$.

taocpmathematicsalgorithmsvolume-4medium
CF 104095C - 我得重新集结部队

We are simulating a sequence of events on a 2D battlefield. Two kinds of entities appear over time: bugs and warriors. Bugs spawn at fixed coordinates with a given health.

codeforcescompetitive-programming
CF 104095B - 广告投放

We are given a sequential process with n episodes, and an initial audience size m. Each episode may or may not run an advertisement.

codeforcescompetitive-programming
CF 104096E - Участок на берегу

I can’t write a correct editorial for this yet because the actual problem statement for Codeforces 104096E - “Участок на берегу” is missing from your prompt.

codeforcescompetitive-programming
CF 104096D - Суммарный XOR

I can’t reliably write a correct editorial for this yet because the actual problem content is missing from the prompt. Right now only the title (“Суммарный XOR”) is present, but the statement, constraints, and required computation are blank.

codeforcescompetitive-programming
CF 104096A - Праздничный торт

Let $Pm$ denote the Boolean predicate that encodes whether a length-$m$ assignment represents a valid permutation of ${1,dots,m}$.

codeforcescompetitive-programming
CF 104096C - Путешествие шахматного коня

The statement section is empty, so there isn’t enough information to produce a correct editorial. For a problem like “104096C - Путешествие шахматного коня”, small differences matter a lot: whether it’s shortest path on a grid, whether there are…

codeforcescompetitive-programming
CF 104096B - Транспортировка гравия

I don’t have the actual statement of Codeforces 104096B (“Транспортировка гравия”) available in this context, and I can’t reliably reconstruct the problem from the title alone without risking inventing details.

codeforcescompetitive-programming
CF 104097I - 子集合和 (SOS)

We are given an array indexed by bitmasks. Each position represents a subset of some universe of size $k$, so there are $2^k$ values in total. The task is to compute, for every subset, an aggregate over other subsets that are related to it by inclusion.

codeforcescompetitive-programming
CF 104097F - LCA 遊戲 (LCA)

I can’t reliably write a correct Codeforces editorial for this yet because the actual problem statement for 104097F - LCA 遊戲 (LCA) is missing from your prompt.

codeforcescompetitive-programming
CF 104097H - 圓規 (Compass)

The problem statement is missing, so there isn’t enough information to reconstruct the task or derive a correct solution.

codeforcescompetitive-programming
CF 104097B - 更加 Trivial 的題目 (Quadrivial)

Let the odd-indexed variables define a binary fraction $$A = (0.x1x3x5ldots)2,$$ and the even-indexed variables define $$B = (0.x2x4x6ldots)2.$$ The Boolean function is $$F = [AB ge 1/2].

codeforcescompetitive-programming
CF 104097G - 幸運數字 (Number)

I can write the full editorial in the exact format you specified, but I’m missing the only thing that makes it possible: the actual problem statement.

codeforcescompetitive-programming