brain

tamnd's digital brain — notes, problems, research

41358 notes

TAOCP 3.2.2 Exercise 17

Let $m, k \in \mathbb{Z}^+$, and define the sequence $(X_n)$ by X_1 = X_2 = \cdots = X_k = 0, and, for $n \ge 1$,

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.2 Exercise 18

Let $X_n$ be the binary sequence generated by method (10) with $k=35$ and CONTENTS$(A)=(a_1a_2\ldots a_{35})_2$, where $a_{35}=1,\quad a_{31}=a_{33}=a_{35}=1,\quad a_i=0 \text{ otherwise in the final...

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.2 Exercise 16

Let the binary representation of $\mathrm{CONTENTS}(A_n)$ be \mathrm{CONTENTS}(A_n) = (c_{n,1} c_{n,2} \ldots c_{n,k})_2, where $c_{n,i} \in {0,1}$ for $1 \le i \le k$, and $c_{n,1}$ is the most signi...

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.2 Exercise 14

Let $(X_n)$ and $(Y_n)$ be integer sequences modulo $m$, with periods $\lambda_1$ and $\lambda_2$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.2 Exercise 15

The previous solution fails because it never constructs a valid global structure linking the return-time function $q_n$ with the indexing of the base period of $X_n$, and it incorrectly treats periodi...

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.2 Exercise 12

The sequence is defined modulo $2^e$ by $X_{n+1} = aX_n + bX_{n-1} + c \pmod{2^e}, \qquad n \ge 1.$ The goal is to choose integers $a,b,c,X_0,X_1$ so that the resulting sequence has maximal possible p...

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.2 Exercise 13

Let $(X_n)$ and $(Y_n)$ be sequences of integers modulo $m$ with periods $\lambda_1$ and $\lambda_2$, respectively.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.2 Exercise 10

Let X_{n+1}=X_n+X_{n-1}\pmod{2^e} and write the state vector

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.2 Exercise 8

Let $m = 2^e$ and consider the modified middle-square sequence defined by Coveyou: $X_0 \text{ given}, \qquad X_{n+1} = \operatorname{middle}(X_n^2 + 2^{e-1} X_n), \eqno(4)$ where the function $\opera...

taocpmathematicsalgorithmsvolume-1math-project
TAOCP 3.2.2 Exercise 11

Let $R_{p^r} = (\mathbb{Z}/p^r\mathbb{Z})[z]/(f(z))$ with $f(0)=1$, and denote by $\overline{z}$ the residue class of $z$ in $R_{p^r}$.

taocpmathematicsalgorithmsvolume-1math-project
TAOCP 3.2.2 Exercise 6

Let the binary method (10) be the scheme in which a word $X$ is updated by shifting and inserting a random bit, so that each step effectively appends a new random least significant bit while discardin...

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.2 Exercise 7

Let the MIX machine have accumulator $A$, index register $X$, and overflow toggle $O$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.2 Exercise 3

Work modulo $8$ throughout.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.2 Exercise 5

**Corrected Solution to Exercise 3.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.2 Exercise 1

We consider the linear congruential generator (LCG) in its standard integer form: X_{n+1} = (a X_n + c) \bmod m, \quad X_0 \in \{0,1,\dots,m-1\}.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.2 Exercise 2

From the Fibonacci generator, X_{n+1} = (X_n + X_{n-1}) \bmod m, there exists an integer $t \in {0,1}$ such that

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.3 Exercise 7

Since \(a\) satisfies the conditions of Theorem 3.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.3 Exercise 8

The generator is X_{n+1}\equiv aX_n \pmod{2^{35}}, \qquad a=2^{17}+3, \qquad

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.3 Exercise 6

By Exercise 5, if m=p_1^{e_1}\cdots p_r^{e_r}, \qquad a=1+k\,p_1^{f_1}\cdots p_r^{f_r},

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.1.3 Exercise 5

Let m=p_1^{e_1}p_2^{e_2}\cdots p_r^{e_r}, and let

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.3 Exercise 4

Let $m = 2^e$ with $e \ge 3$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.1.3 Exercise 3

We consider the multiplicative linear congruential sequence modulo $m = 2^{35}$, so we study the multiplicative order of $a$ in the unit group $(\mathbb{Z}/2^{35}\mathbb{Z})^\times$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.1.3 Exercise 2

In (3) the multiplier is $a=B^2+1$, hence $b=a-1=B^2$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.1.2 Exercise 14

Assume $e>1$ and that $a$ is a primitive element modulo $p$.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.2.1.3 Exercise 1

Let $B$ be the byte size of MIX, so that $m = B^e$ is the word size.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.2.1.2 Exercise 11

Let $x$ be an odd integer with $x>1$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.2 Exercise 12

Let $p$ be an odd prime and let $e>1$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.2 Exercise 10

Write m=2^{e}p_1^{e_1}\cdots p_t^{e_t}, where $p_1,\dots,p_t$ are distinct odd primes.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.2 Exercise 9

**Corrected Solution for Exercise 3.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.2 Exercise 6

By Theorem A, the multipliers that yield the maximum period are characterized by the conditions a-1 \equiv 0 \pmod p for every prime divisor $p$ of $m$, together with the additional condition

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.1.2 Exercise 8

We are asked to show that if a \equiv 3 \pmod 4, then, for every integer $e>1$,

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.2 Exercise 7

Let the modulus be m = p_1^{e_1} p_2^{e_2} \cdots p_t^{e_t}, and let $(X_n)$ denote the linear congruential sequence defined by $(X_0, a, c, m)$:

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.2 Exercise 4

Let $m = 2^e$, and let $(X_n)$ be the linear congruential sequence defined by $X_{n+1} \equiv a X_n + c \pmod{2^e}, \qquad X_0 = 0,$ where $a$ and $c$ satisfy the conditions of Theorem A.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.2 Exercise 5

We are asked to find all multipliers $a$ satisfying the conditions of Theorem A when $m = 2^{35} + 1$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.1.2 Exercise 3

Let $m = 10^e$ with $e \ge 2$, and let $c$ be odd and not a multiple of 5.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.1.2 Exercise 2

Let \(x_{n+1} \equiv a x_n + c \pmod{m}\) with \(m = 2^k\), and consider the conditions \[ c \text{ is odd}, \qquad a \equiv 1 \pmod{4}.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.1.1 Exercise 14

**Exercise 3.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.2 Exercise 1

We first verify the conditions of Theorem A for the given parameters.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.2.1.1 Exercise 13

We are asked to perform computations modulo $m = 9999999001$, with multipliers $a = 10$ and $a = 9999999101$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.1 Exercise 12

Let m=9999999999=10^{10}-1.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.1 Exercise 11

**Exercise 3.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.1 Exercise 8

Let aX=qw+r,\qquad 0\le r<w.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.1.1 Exercise 10

Let $m$ be a positive integer modulus.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1.1 Exercise 7

The flawed solution attempts to describe specific factorizations, but the actual question is to identify structural patterns visible in the table of factorizations of numbers of the form $w \pm 1$, wh...

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1.1 Exercise 6

Let $m$ be a positive integer modulus and let $a, c, X_0$ be integers with $0 \le X_0 < m$.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.2.1.1 Exercise 4

We are asked to discuss the calculation of linear congruential sequences with modulus $m = 2^{32}$ on two's-complement machines such as the IBM System/370 series.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.1.1 Exercise 5

Let $m$ be a positive integer less than the computer word size $w$, and let $x$ and $y$ be nonnegative integers satisfying $0 \le x, y < m$.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.2.1.1 Exercise 2

Let $w$ be the word size and let $X$ be stored in location $\texttt{XRAND}$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.2.1.1 Exercise 1

We are asked to compute (aX + c) \bmod w in MIX using **three instructions** when $m = w$ and $\gcd(a,w)=1$, with the result ending in register X.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.2.1.1 Exercise 3

Let $w$ be the word size, $0 \le a,x < m < w$, and $\gcd(m,w)=1$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.2.1 Exercise 4

Equation (2) defines the linear congruential sequence by X_{n+1}\equiv aX_n+c \pmod m.

taocpmathematicsalgorithmsvolume-2simple
TAOCP 3.2.1 Exercise 5

Equation (6) asserts that, for $k \ge 0$, $X_{n+k} = \bigl(a^k X_n + (a^k - 1)c/b\bigr) \bmod m, \qquad b = a-1. \eqno(6)$ We seek an expression for $X_{n+k}$ when $k < 0$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.2.1 Exercise 2

Assume that $(a,m)=1$.

taocpmathematicsalgorithmsvolume-2math-medium
TAOCP 3.2.1 Exercise 3

If $a$ and $m$ are not relatively prime, there exists a nontrivial common factor $d > 1$ such that $d \mid a$ and $d \mid m$.

taocpmathematicsalgorithmsvolume-2math-simple
TAOCP 3.2.1 Exercise 1

A linear congruential sequence has the form X_{n+1} \equiv aX_n + c \pmod m.

taocpmathematicsalgorithmsvolume-2simple
TAOCP 3.1 Exercise 21

**Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 23

Let $f$ be an arbitrary function from ${0,1,\ldots,m-1}$ into itself.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.1 Exercise 20

Let K(X) denote one application of Algorithm K.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 19

Let $N=m^k$.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 17

**Solution.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 18

**Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 15

**Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 14

**Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 16

**Solution to Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 12

Let the trajectory be X_0,\;X_1=f(X_0),\;X_2=f(X_1),\ldots and let the eventual cycle have length $\lambda$.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 13

Let $L_m$ denote the length of the longest cycle in the functional digraph of a random mapping $f$ on an $m$-element set.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 11

Let X_{n+1}=f(X_n),\qquad X_n\in\{1,\ldots,m\}, where $f$ is chosen uniformly from the $m^m$ mappings of $\{1,\ldots,m\}$ into itself, and $X_0$ is chosen uniformly from the $m$ possible starting valu...

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 9

Let a number in the middle-square method have $2n$ digits in base $b$, and let $X_k$ denote the $k$th number in the sequence.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.1 Exercise 10

**Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 7

Let X_0,X_1,X_2,\ldots be a sequence generated by

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 8

**Exercise 3.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.1 Exercise 5

Algorithm K generates each new value of $X$ by a fixed deterministic rule applied to the preceding value.

taocpmathematicsalgorithmsvolume-2simple
TAOCP 3.1 Exercise 6

The sequence takes its values from the finite set \{0,1,\ldots,m-1\}, which contains exactly $m$ elements.

taocpmathematicsalgorithmsvolume-2math-medium
TAOCP 3.1 Exercise 3

In the middle-square method for $10$-digit numbers, we square the current value and take the middle $10$ digits of the resulting $20$-digit number.

taocpmathematicsalgorithmsvolume-2simple
TAOCP 3.1 Exercise 4

Step K11 is \text{K11.

taocpmathematicsalgorithmsvolume-2medium
TAOCP 3.1 Exercise 2

Let $X_i$ denote the number of occurrences of digit $i$ in a random sequence of $1{,}000{,}000$ decimal digits.

taocpmathematicsalgorithmsvolume-2math-medium
TAOCP 3.1 Exercise 1

The desired outcome is a digit distributed as uniformly as possible on the set ${0,1,\ldots,9}$.

taocpmathematicsalgorithmsvolume-2medium
CF 104665I - Riddle Me This (Hard Version)

Each input item is a permutation of a finite length, and you are allowed to cyclically rotate it. A rotation means taking the last element and moving it to the front, repeated any number of times.

codeforcescompetitive-programming
CF 104665H - Alice Learns Eertree!

We are given a tree with $N$ nodes, and each node carries a single uppercase letter. The structure of the tree is fixed, but we are allowed to choose any node $u$ as a root. Once rooted, every node defines a rooted subtree consisting of itself and all nodes below it.

codeforcescompetitive-programming
CF 104665F - Noodles and Random Walk

We are given a process that starts at position 0 and evolves for $T$ steps. At every second, we either increase the position by 1 or decrease it by 1. The sequence of positions over time forms a walk on the integers, starting at 0.

codeforcescompetitive-programming
CF 104665G - Spaghetti Game

Two players are playing a turn-based game that changes a single integer, the current number of spaghetti strands in a shared pile. The game always starts from zero. Lario moves first, then Muigi, and they alternate for up to 100 moves each.

codeforcescompetitive-programming
CF 104665D - Noodling with Knights

We are given a square chess board of size $N times N$. Each cell is identified by integer coordinates, and a single knight piece starts on one cell while a target cell is fixed elsewhere on the board.

codeforcescompetitive-programming
CF 104665E - Riddle Me This (Easy Version)

We are given an even number of permutations, all of the same length. Each permutation represents a cyclic object: we are allowed to rotate it any number of times, meaning we can choose any cyclic shift of its elements.

codeforcescompetitive-programming
CF 104665C - Hatter's Party

We are given a collection of noodle strands, each carrying a numeric flavor value. We need to divide these strands into several dishes. Every dish must contain at least $K$ strands, and the value of a dish is defined as the maximum flavor among the strands placed into it.

codeforcescompetitive-programming
CF 104666K - Screamers in the Storm

We are given a building footprint in the plane, described as an axis-aligned simple polygon. Above every point inside this footprint there is a piecewise linear roof surface.

codeforcescompetitive-programming
CF 104666J - Saba1000kg

We are given an undirected graph representing islands and direct influence paths between some pairs of islands. Influence is transitive, meaning if island A can influence B and B can influence C, then A and C are in the same connected environment even without a direct edge.

codeforcescompetitive-programming
CF 104666L - The Bugs

Codeforces 104666L: The Bugs

codeforcescompetitive-programming
CF 104666I - Ponk Warshall

We are given two strings of equal length over the alphabet {A, C, G, T}. The second string is a permutation of the first, meaning both contain exactly the same multiset of characters.

codeforcescompetitive-programming
CF 104666H - K==S

We are asked to count how many sequences of length $N$ can be formed from an alphabet of 26 symbols, while avoiding a set of forbidden substrings.

codeforcescompetitive-programming
CF 104666G - Light Emitting Hindenburg

Each musician can be viewed as a 30-bit mask describing availability across the days of November. For a given day, the corresponding bit is set if the musician is available on that day, and unset otherwise.

codeforcescompetitive-programming
CF 104666F - Zeldain Garden

We are looking at all integers in a range from $N$ to $M$. For each integer $x$ in this range, we define its “variability” as the number of ways to split $x$ identical items into a convoy of identical lorries such that every lorry carries the same number of items and all…

codeforcescompetitive-programming
CF 104666E - Deep800080

We are given a straight pier in the plane, defined by a line passing through the origin and a second point $(A, B)$. We are allowed to choose any point on this infinite line as the location of a barbecue grill.

codeforcescompetitive-programming
CF 104666D - Crimson Sexy Jalapeños

The game is played on a large rectangular grid that behaves like a chocolate bar. Some cells are contaminated. The two players repeatedly cut the current remaining rectangle along grid lines and discard one side of the cut, keeping the other side as the new active region.

codeforcescompetitive-programming
CF 104666B - Be Geeks!

We are given a sequence of positive integers and we consider every contiguous subarray. For each subarray, two values are extracted: the greatest common divisor of all elements inside it and the maximum element inside it.

codeforcescompetitive-programming
CF 104666C - Bob in Wonderland

We are given a connected structure of $N$ labeled nodes, where each pair in the input describes an undirected link between two nodes. This structure is guaranteed to be a tree, so it has exactly $N-1$ edges and no cycles.

codeforcescompetitive-programming
CF 104666A - ABB

We are given a sequence of colored bungalows arranged in a straight line from the lake toward the forest. Each bungalow contributes one character to a string, so the whole street is represented as a string where position 1 is closest to the lake and position N is at the forest…

codeforcescompetitive-programming
CF 104668L - Game of Stones

We are given several independent piles of stones. Two players alternate turns, starting with Petyr. On each turn, the active player chooses exactly one pile and removes between one stone and a player-specific maximum: Petyr can take at most A stones, while Varys can take at…

codeforcescompetitive-programming
CF 104668K - Mirrority Report

We are given a rectangular board and a single chess-like piece placed on one cell. The piece is described by its type, such as K, Q, or R.

codeforcescompetitive-programming
CF 104668I - The Silence of the Lamps

We are counting geometric shapes that are rectangular boxes with integer side lengths. Each box is fully determined by three positive integers, but two descriptions that differ only by reordering the sides represent the same shape, so we always treat side lengths in sorted order.

codeforcescompetitive-programming
CF 104668H - The Lord of the Kings

The grid represents a country split into small cells. One cell contains the king’s palace, several cells contain cities that must be visited, and every other cell is just farmland. We are allowed to build helipads on some cells.

codeforcescompetitive-programming