brain

tamnd's digital brain — notes, problems, research

41641 notes

CF 105381C - Trip Counting III

We are given a simple undirected graph with up to 300 vertices, where each input edge is guaranteed to exist and no duplicates appear. The graph represents travel routes between countries.

codeforcescompetitive-programming
CF 105381D - Rearrangement

We are given a rectangular grid with $n$ rows and $m$ columns. Each cell contains an integer, and the only operation allowed is to permute values independently inside each column.

codeforcescompetitive-programming
CF 105380C - Dhrumil The Pados Wali Aunty

We are given an array of size $2n$ representing fighting strengths of $2n$ friends. The task is to split them into two disjoint teams so that every person belongs to exactly one team, and both teams must contain an odd number of members.

codeforcescompetitive-programming
CF 105380F - Inversion Sum

We are asked to look at every permutation of numbers from 1 to n, compute how many inversions each permutation contains, and then sum those inversion counts over all permutations.

codeforcescompetitive-programming
CF 105380E - String Palindrome Game

We are given a string and multiple independent queries. Each query specifies a segment of the string, and we are only allowed to look inside that segment.

codeforcescompetitive-programming
CF 105380A - Who Hates Abhishek?

We are asked to construct a special kind of permutation of size n. A permutation here means we arrange the numbers from 1 to n exactly once each. The twist is that the permutation must behave like an involution without fixed points.

codeforcescompetitive-programming
CF 105364B - Papalindromes!

We are given a starting integer and a deterministic process that repeatedly transforms it. At each step, we take the current number, reverse its decimal representation, average the two values, and round the result down to an integer.

codeforcescompetitive-programming
IMO 1968 Problem 1

Let the triangle have consecutive integer side lengths.

imomathematicsolympiad
CF 105364F - Gold Cubes

Each test case describes a very small “production system” that assembles gold cubes from three types of nuggets. Every nugget type has a fixed weight in milligrams, and we are also given a maximum available count for each type.

codeforcescompetitive-programming
CF 105364E - Painting Crosswalks

We are given several independent road segments, each containing a set of existing painted crosswalk stripes. Every stripe is described by its starting position and its width, so each one occupies a continuous interval on a number line. These stripes may overlap or leave gaps.

codeforcescompetitive-programming
CF 105364D - Colored Towers

We are given several test cases. In each case, María owns disks of different colors, and for each color we know how many identical disks she has. She wants to partition all disks into several vertical towers.

codeforcescompetitive-programming
CF 105364A - Pairs

We are given several independent test cases. In each one, we receive an even-length list of integers, and we must decide whether it is possible to partition the numbers into pairs such that every pair has the same sum.

codeforcescompetitive-programming
CF 105363B - Closed by Subtraction

We are given several independent test cases. Each test case provides a finite set of distinct integers, and we need to decide whether this set satisfies a very specific structural property involving differences between elements.

codeforcescompetitive-programming
IMO 1967 Problem 6

Let $m$ be the total number of medals and let $n$ be the number of days.

imomathematicsolympiad
CF 105363F - Coloring the Grid

We are given a geometric construction that can be reinterpreted as a graph problem. There are two families of segments.

codeforcescompetitive-programming
CF 105363E - Beautiful Board

We are given a rectangular grid of $n times m$ cells, and we must assign each cell one of two colors. The coloring must satisfy two global conditions at the same time. First, exactly half of the cells must be black and the other half white.

codeforcescompetitive-programming
CF 105363C - Squares in the Notebook

The page is drawn with a fixed set of horizontal guide lines, equally spaced one centimeter apart, and a set of vertical guide lines placed at arbitrary x-coordinates.

codeforcescompetitive-programming
CF 105363D - Connecting Villages

We are given a network of villages connected by roads, where each road is initially unusable and becomes usable only after a certain number of hours. All roads “unlock” in parallel according to their own schedules. Once a road is unlocked, it can be used permanently.

codeforcescompetitive-programming
CF 105358L - 502 Bad Gateway

We are simulating a system that holds a single countdown timer whose initial value is random. At time zero, the timer is set to a uniformly chosen integer between 1 and T. Time then advances in discrete seconds. Every second, the timer decreases by one.

codeforcescompetitive-programming
IMO 1967 Problem 5

Testing small configurations clarifies what structure is forced by infinitely many vanishing power sums.

imomathematicsolympiad
CF 105358K - Match

We are given a complete bipartite setup with two groups of size $n$, which we can think of as left vertices indexed by $i$ and right vertices indexed by $j$. An edge between $i$ and $j$ exists only when the XOR of their values, $ai oplus bj$, is at least $k$.

codeforcescompetitive-programming
CF 105358I - Strange Binary

We are given a non-negative integer and asked to express it as a sum of powers of two, but instead of standard binary digits, each bit position can take the value −1, 0, or 1. The contribution of position i is ai · 2^i, and the total sum must equal the given number.

codeforcescompetitive-programming
CF 105358J - Stacking of Goods

We are given a sequence of goods, each with three attributes: a weight, an initial volume, and a compression factor. We must arrange all goods in a single stack. Once stacked, each item’s final volume is reduced depending on how much total weight is placed above it.

codeforcescompetitive-programming
CF 105358G - Game

Two players start with piles of chips. In each round they either have no change in position (a draw) or exactly one of them wins the round. A win is not just a point, it can immediately end the game if the winner already has at least as many chips as the opponent.

codeforcescompetitive-programming
CF 105358F - Tourist

We are tracking a single evolving value, the rating of a user. The rating starts at a fixed initial value, 1500, and then changes after each of n upcoming contests.

codeforcescompetitive-programming
CF 105358E - Escape

We are given an undirected, connected graph of rooms and passages. Sneaker starts at room 1 and wants to reach room n using as few passages as possible. The graph is simple in the sense that there are no self-loops and no multiple edges between the same pair of rooms.

codeforcescompetitive-programming
CF 105358A - Gambling on Choosing Regionals

Each team belongs to a university and has a fixed strength. Inside any contest, all participating teams are ranked strictly by strength, so stronger teams always appear ahead of weaker ones.

codeforcescompetitive-programming
CF 105358D - Query on Tree

We are given a rooted tree where node 1 acts as the root, and each node stores an integer weight. The structure of the tree stays fixed, but the weights change over time due to updates.

codeforcescompetitive-programming
CF 105358C - Prefix of Suffixes

We are building an array step by step. At each operation, a new value is appended to the end of the array. After each insertion, we must compute a running expression that depends on all suffixes ending at the current position and on a special prefix-suffix overlap value.

codeforcescompetitive-programming
CF 105350C - Yet Another Cool Pair Problem

We are given a single integer $n$ per test case, and we want to choose two different numbers $a$ and $b$ in the range $[1, n]$. The constraint is that the binary representations of $a$ and $b$ must not share any position where both have a 1, meaning their bitwise AND is zero.

codeforcescompetitive-programming
CF 105350G - Not An SQRT Problem

We are working on a rooted tree where every node initially holds value zero. Over time, we apply updates that either affect entire subtrees or only the immediate children of a node, and we also need to answer queries asking for maximum values over subtrees or over children sets.

codeforcescompetitive-programming
CF 105350E - Fun is Counting

We are given an array $a$ of size $n$. We want to count how many distinct multisets of size $n$ over values $1$ to $n$ can appear as follows. Imagine we construct an array $b$ of length $n$, where values are in the range $[1,n]$.

codeforcescompetitive-programming
CF 105350F - Mad MAD Sum II

We are given an array and asked to look at every contiguous subarray. For each subarray, we compute a value called the MAD, which is the largest number that appears at least twice inside that subarray. If no number repeats, the MAD is zero.

codeforcescompetitive-programming
CF 105350D - Tuples Fusion

We are given several independent test cases. In each test case there is a collection of pairs of numbers. Each pair behaves like a small container holding two values, and we are allowed to repeatedly perform operations that either destroy a container while collecting a…

codeforcescompetitive-programming
CF 105350A - An OK Problem

We are given a rectangular grid of size $n times m$, initially empty. We must choose a set of cells to color red and blue, with two strict structural constraints.

codeforcescompetitive-programming
CF 105348B - And Xor Pair

We are asked to count how many ordered pairs of integers $(x, y)$ can be formed from a given number $n$, under two binary constraints applied bit by bit.

codeforcescompetitive-programming
TAOCP 2.5 Exercise 43

Let the requests be processed in order, and let each request of size $s_i$ be placed by first-fit into the first available block that can contain it.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 44

Let $F$ be continuous and strictly increasing on its support, so it admits a generalized inverse $F^{-1}(u)=\inf\{x:F(x)\ge u\}$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 40

Let $N(n,m)$ denote the minimum memory guaranteeing successful allocation under the constraints of Exercise 38.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 39

Consider any admissible sequence of requests whose total simultaneously allocated memory never exceeds $n_1+n_2$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 41

Let level $k$ denote blocks of size $2^k$, for $0 \le k \le r$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 38

<Text>For blocksboxed{N(n,2)=\left\ of size at most 2, the exact value is</Text><lfloor \frac{3n}{2}\right\rfloor+O(Math block value="N(n,1)}, \] more precisely, \[ \boxed{N(n,2)= \begin{cases}

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 35

The dynamic storage allocation methods of Section 2.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 37

Consider any strategy of the hostess.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 33

Use three passes.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 29

No.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 23

In the buddy system, the buddy of a block of size $2^k$ is obtained by complementing bit $k$ of the block's starting address, counting the least significant bit as bit $0$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 26

The buddy system can be adapted to a memory of size $M$ that is not a power of two by partitioning the memory into the largest possible blocks whose sizes are powers of two.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 25

The criticism of this idea is that it conflates typical usage patterns with the correctness and generality of the buddy system.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 20

Doubly linking the `AVAIL[k]` lists in the buddy system allows a block to be removed from the list without traversing from the head.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 22

The buddy system depends on the invariant that every free block has size $2^k$ and begins at an address aligned for a block of that size.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 17

In formula (9), `AVAIL` serves as the header of the circular list of available blocks.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 13

Modify Algorithm A as follows.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 16

Algorithm C can be coded directly in MIX by following the structure of steps C1, C6 and incorporating the optimization of exercise 15.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 11

Use the same idea as the improvement to Algorithm A: retain a pointer to the place in the sorted `AVAIL` list where the most recent insertion occurred, and begin the search for the next insertion from...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 6

Modify Algorithm A so that the search does not always begin at the front of the `AVAIL` list.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 7

Suppose the `AVAIL` list contains three free blocks, in order of location, with sizes $100,\ 20,\ 20,$ and suppose two requests arrive consecutively, first for a block of size $18$, then for a block o...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 3

Computer simulations reported in the literature for dynamic storage allocation generally show that best-fit and first-fit have very similar memory utilization.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 2

Let x=\frac{l}{k-b}.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.5 Exercise 1

Under the last-in-first-out restriction, the dynamic storage problem degenerates into stack allocation.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 13

Algorithm A traverses the Data Table to find the first occurrence of an item with a given `NAME`.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 14

Algorithm C traverses the structure in preorder.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 15

In Algorithm A, every node whose address is placed on the stack has `SIB=\Lambda`.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 11

Algorithm B and Algorithm C can be accelerated by adding auxiliary links that reduce the number of nodes traversed in key searches.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 10

If there were no `NAME` link in the Data Table nodes, the test `NAME(S) = P_k` in step B6 could be performed by traversing the `DATAP` chain from `S` to find the data field corresponding to the name a...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 9

Algorithm C traverses the multilinked structure of a group item and relocates each element to its correct parent or next sibling according to the specification of the data hierarchy.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 12

To perform the function of Algorithm B with this two-link representation, traverse the tree by comparing the current node $P$ with the root $R$ of the desired subtree.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 7

The COBOL statement `MOVE CORRESPONDING SALES TO PURCHASES` means that each elementary data item in the group `SALES` that has the same name as an elementary data item in the group `PURCHASES` is copi...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 5

Algorithm B can be modified to accept a linked list of Symbol Table references by replacing the indexed access of $P_0, P_1, \ldots, P_n$ with a traversal of the list using the $\operatorname{RLINK}$...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 8

`MOVE CORRESPONDING \alpha TO \beta` transfers every subordinate item of $\alpha$ whose name also appears as a subordinate item of $\beta$, pairing items solely by identical names.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 3

Algorithm A must be modified to allow level numbers of items within a group to decrease by more than one between successive elements, rather than requiring strict increment by one as in COBOL.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 4

Algorithm A should be modified to verify that each group of items satisfies rule (c) at the point where a new item is read.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 2

Algorithm A performs a loop in which each iteration consists of a fixed number of arithmetic operations on the current data values, including a division step that determines the next state.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.4 Exercise 1

In COBOL data descriptions, a group item is written before any of its subordinate items, and each subordinate entry is listed immediately after the declaration of its parent group with increased inden...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 10

The copying procedure is defined on nodes reachable from $P_0$ using a depth-first traversal in which the field $\mathrm{REF}$ temporarily stores the correspondence between an original node $x$ and it...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 8

Let $P_0$ denote the initial node.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 7

Let $P$ denote the current node and $R$ the root.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 6

The term $c_2M$ arises from the need to traverse all the pointers contained within the nodes of the Lists during the marking phase of garbage collection.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 3

Algorithm E constructs the set of nodes reachable from the designated list heads by repeatedly following the structural fields `DLINK` and `RLINK`, marking each visited node by setting its mark bit `S...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 4

We represent each node of the List as a single MIX word, with the fields assigned as specified: `MARK` in $(0:0)$, `ATOM` in $(1:1)$, `ALINK` in $(2:3)$, `BLINK` in $(4:5)$, and $\Lambda = 0$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.6 Exercise 4

Label the $n$ nodes of a binary tree using an inorder traversal from left to right with the integers $1$ through $n$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.5 Exercise 2

List structures can be threaded in the same sense as threaded binary trees by using otherwise unused pointer fields to store links that represent the successor of a node in a chosen traversal order.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.6 Exercise 2

A configuration of $k$ noncrossing diagonals in an $r$-gon partitions the polygon into $k+1$ regions.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.6 Exercise 1

Number the vertices of an $(n+2)$-sided convex polygon consecutively as $V_1,V_2,\ldots,V_{n+2}$ in clockwise order.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.6 Exercise 3

A partition of the vertices of a convex $n$-gon with the property that no diagonal drawn inside one part crosses a diagonal drawn inside another part is equivalent to requiring that each part induces...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 15

Let $w_1,\ldots,w_m$ be the given weights and $l_1,\ldots,l_m$ the lengths of the paths to the corresponding external nodes in an extended binary tree.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 16

Let $F(w_1,\ldots,w_m)$ denote the minimum weighted external path length over all extended binary trees with external weights $w_1,\ldots,w_m$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 11

The Dewey decimal notation in Exercise 2.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 12

Let $T$ be a binary tree with $n$ nodes, and let $d(v)$ denote the distance from the root to a node $v$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 9

Let $T$ be an extended binary tree with external nodes carrying weights $w_1,\ldots,w_m$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 7

Let $E$ and $I$ denote the external and internal path lengths of an extended $t$-ary tree, and let $n$ be the number of internal (circular) nodes.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 5

Let $T$ be a binary tree with left subtree $T_L$ and right subtree $T_R$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 6

Let the extended $t$-ary tree contain $n$ circular (internal) nodes and $s$ square (external) nodes.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 1

Let $T$ be a binary tree with $n=12$ internal nodes and minimal internal path length $I$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.5 Exercise 3

For an extended binary tree, each external node corresponds to a unique path from the root, and thus to a binary string of length $l_j$ in which each left edge is encoded by $0$ and each right edge by...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.4 Exercise 25

Each configuration counted by $r(n,q)$ consists of a directed acyclic graph on ${1,2,\ldots,n}$ in which every designated vertex has outdegree $1$ and every nondesignated vertex has outdegree $0$, wit...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.4 Exercise 26

The $( (3,2,4),(1,4,2) )$-construction in the notation of the section determines a decomposition in which the first index sequence selects the root structure at level $t=8$, while the subsequent index...

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.4 Exercise 23

An ordered tree with $n$ vertices determines, by the correspondence in Section 2.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.4 Exercise 20

Let the vertices be ${1,2,\ldots,n}$ and let the oriented tree be directed toward its root $r$.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.3.4.4 Exercise 19

By the correspondence of equation (16), every labeled oriented tree on ${1,\ldots,n}$ corresponds uniquely to an $(n-1)$-tuple $(x_1,\ldots,x_{n-1})$, where each $x_i$ is an integer between $1$ and $n...

taocpmathematicsalgorithmsvolume-1