---
title: "proofs"
date: "13/06/2025"
useKatex: true
useD3: true
---

import Pseudocode from '@components/Pseudocode.astro';

A computer science student attempting to learn proofs.

# 1032

## E

Minimize-Digit-Diff($l, r$):
1. Initialize $ans=18$, the largest possible value of $f(l, x) + f(x, r)$ with $l \leq r\leq 10^9$
2. For $i:=1$ to $200$:
  - Let $x$ be a random sample from the open interval $[l, r]$
  - Let $cost:=f(l, x) + f(x, r)$, computed in $O(1)$ time
  - $ans:=min(ans,cost)$
3. Return $ans$

---

*Proof.*

We are interested in minimizing the expected probability of failure over $t\leq10^4$ tests. Firstly, consider a single test (i.e. a single $(l,r)$ input):

An $n$ digit candidate $x$ has a $\frac{1}{10}\cdot 2=\frac{1}{5}$ possibility of colliding with the corresponding digit from $l$ or $r$[^1].
Thus, a digit has approximately a $1-\frac{1}{5}=\frac{4}{5}$ probability of minimizing the score for a digit.
It follows that the candidate then has roughly a $(\frac{4}{5})^9\approx13\%$ chance of minimizing the entire score $f(l,x)+f(x,r)$.
So, one random sample $x$ has a $100-13\approx87\%$ chance of being a suboptimal candidate.


Consider sampling $n$ times--the probability of all random samples being suboptimal is $0.87^n$. Let's only settle for a probability of failure near $1/10^{10}$ and solve for our number of trials:

$$
0.87^n=1/10^{10}\rightarrow n\ln(0.87)=\ln(1/10^{10})\rightarrow n=\frac{\ln(1/10^{10})}{\ln(0.87)}\rightarrow n\approx165
$$

The probability all tests succeed is $(1-1/10^{10})^{10^4}$, so the probability that at least one test fails is $1-((1-1/10^{10})^{10^4})\approx 9.99\cdot10^{-7}$.

$\blacksquare$

---

## G

Maximize-F(s):

1. Compute the prefix sum of zeroes $Z$ and ones $O$ in $s$
2. Concatenate and sort $Z$ and $O$ into array $S$
3. Let $first:=\sum_{1\leq i\leq n}i\cdot(n-i+1)$, computed in $O(|s|)$ time
4. Let $second:=\sum_{1\leq i\leq n}S_i\cdot (2\cdot i-n)$, computed in $O(|s|)$ time
5. Return $\frac{first+second}{2}$

---

*Proof.*

Let $X$ and $Y$ be the number of occurrences of zeroes and ones in the substring $p$ of $s$. Then, $f(p)=max(X, Y)=\frac{X+Y+|X-Y|}{2}$. The goal is to compute:

$$
\begin{align*}
\sum_{1\leq r\leq n}\sum_{1\leq l<r} f(s[l:r+1])&=\sum_{1\leq r\leq n}\sum_{1\leq l<r} \frac{X+Y+|X-Y|}{2}
&=\frac{1}{2}(\sum_{1\leq r\leq n}\sum_{1\leq l<r} X+Y+\sum_{1\leq r\leq n}\sum_{1\leq l<r} |X-Y|)
\end{align*}
$$

Consider the first term, which counts the number of occurrences of ones and zeroes across all one-indexed bounds $l,r$.
Rather than enumerate all $n^2$ subarrays, consider how much each index $1\leq i\leq n$ contributes to the sum.
Exactly $i$ substrings start at or before $i$, including the character, each doing so $n-i+1$ times.
Thus, the character contributes $i\cdot(n-i+1)$ to the term.
So, the first term aggregates this quantity across all indices and can be computed in linear time: $\sum_{1\leq i\leq n}i\cdot(n-i+1)$.

For the second term, consider the sum more abstractly--it computes $|X-Y|$, the absolute difference of the number of zeroes and ones in the substring across all index pairs.
Because $|a-b|=|b-a|$, we are free to rearrange $X$ and $Y$ as we see fit.
Sort all $X$ and $Y$ values into some array $S$ and compute the difference across all pairs.
As $S_i<S_{j>i}$, $|S_{j>i}-S_i|=S_{j>i}-S_i$--the sum is unchanged. Rewriting the second term:

$$
\begin{align*}
\sum_{1\leq r\leq n}\sum_{1\leq l<r} |X-Y| &=\sum_{1\leq r\leq n}\sum_{1\leq l<r}S_r-\sum_{1\leq r\leq n}\sum_{1\leq l<r}S_l
&=\sum_{1\leq r\leq n}r\cdot S_r-\sum_{1\leq l<r\leq n} S_l
&=\sum_{1\leq r\leq n}r\cdot S_r-\sum_{1\leq r\leq n} (n-r)\cdot S_r
\end{align*}
$$

which can be computed in linear time.

$\blacksquare$

# 993

## A

Count-Pairs($n$):
1. Return $n-1$

---

*Proof.*

Suppose $(a,b)\in\mathbb{N}^2$. Because $a=n-b$ and $a\geq1$, it follows that $1\leq b\leq n-1$. Each choice of $b$ yields a unique $a=n-b$, so there are $n-1$ unique solutions.

$\blacksquare$

## B

Mirror-String($s$):
1. Reverse $s$
2. For each character $c$ in $s$:
  - If $c$ is "w": Print($c$)
  - If $c$ is "p": Print($q$)
  - If $c$ is "q": Print($p$)

---

*Proof.*

The string appears fipped on the y-axis from within the score due to the perspective
shifting. Structurally, it is read right-to-left. "p"/"q"/"w" appear as "q"/"p"/"w" when flipped on its y-axis.

$\blacksquare$

## C

Seat-Monkeys($a$, $b$, $c$, $m$):
1. Return $min(m, a)+min(m, b)+min(c, 2\cdot m-(min(m, a) + min(m, b)))$

---

*Proof.*

Consider an assignment of monkeys $S$ that sits the $a$ and $b$ monkeys in the first and second row and then fills the remaining seats with the $c$ monkeys.

Assume there exists a more optimal assignment of monkeys $S^{'}$. WLOG, assume $S^{'}$ sits $a$ and $b$ monkeys first in their respective rows.

$S^{'}$ can only differ from $S$ as follows:

1. Seats a $c$ monkey in row 1 instead of an $a$ monkey
  - $S^{'}$ leaves an $a$ monkey unseated. $S$ seats this monkey instead--the same number of monkeys are seated in $S$.
2. Seats a $c$ monkey in row 2 instead of a $b$ monkey
  - $S^{'}$ leaves a $b$ monkey unseated. $S$ seats this monkey instead--the same number of monkeys are seated in $S$.
3. Does not seat a monkey where $S$ has
  - $S$ seats more than $S^{'}$.

In all cases, $S^{'}$ is no better than S, therefore $S$ is optimal.

$\blacksquare$

## D

Construct-B($a$):
1. Let $b$ be an array of size $n=\#a$ and $X$ be the set of numbers in $a$.
2. For each element $x$ of $a$ at index $i$:
- If $x\in X$:
  - $b[i]:=x$
  - $X:=X \backslash \{x\}$
3. Let $Y=\{1,2,\cdots,n\}\backslash X$
4. For each element $x$ of $b$ at index $i$:
- If $b[i]$ is NIL:
  - $b[i]:=\text{first-element}(Y)$
  - $Y:=Y\backslash\{\text{first-element}{(Y)}\}$
5. Return $b$

---

*Proof.*

Consider the array $b$ from Construct-B.

If there are $x$ unique elements in $a$, the algorithm assigns each of those to the first $x$ positions in $b$. There are $n-x$ duplicate elements in $a$ and $n-x$ remaining positions in $b$.

Since $\forall a\in A:1\leq a\leq n$, there are $n-x$ unused unique elements in $\{1,2,\cdots,n\}$, each of which is assigned a unique position in $b$.

Therefore, all elements of $b$ are unique and thus a mode. As every unique element in $a$ is assigned to an index no later in $b$, all $a_i$ is a mode in $b[i+1:]$.

$\blacksquare$

## E

Count-Pairs($l_1$, $l_2$, $r_1$, $r_2$, $k$):
1. Let $A:=\lfloor log_k(r_2/l_1)\rfloor$
2. Let $B:=\lfloor max(0, log_k(l_2/r_1))\rfloor$
3. Let $\text{total}:=0$
4. For each $A\leq i\leq B$:
- Let $r=\lfloor r_2/ k^n\rfloor$
- Let $l=\lfloor l_2/k^n\rfloor$
- $\text{total} := \text{total} + max(0, r - l + 1)$
5. Return $\text{total}$
---

*Proof.*

Each value of $n$ corresponds to a line with slope $k^n$ because $y/x=k^n\leftrightarrow y=x\cdot k^n$. The problem can be visualized as follows:

![graph of problem](/proofs/graph.webp)

It is sufficient to count the number of ordered $(x,y)$ pairs for all valid $n$. Because $y=x\cdot k^n\leftrightarrow n=log_k(y/x)$, $n\in [log_k(l_2/r_1), log_k(r_2/l_1)]$.

For each $n_0$ in this range, the smallest $x$ satisfying $y=x\cdot k^n_0$ is $\lceil l_2/k^n_0\rceil$ and the largest $\lfloor r_2/k^n_0\rfloor$, so $n_0$ contributes $max(0, \lfloor r_2/k^n_0\rfloor - \lceil l_2/k^n_0\rceil + 1)$ ordered pairs.

## F

1. Let $A=\sum a$ and $B=\sum b$.
2. For each query with requested beauty $q$:
- If $\exists (i,j)\in(\{1,2,\cdots,n\},\{1,2,\cdots,m\}):(A-a[i])\cdot(B-b[j])=x$, print "YES"
- Otherwise, print "NO"

---

*Proof.*

The beauty of the grid equals $B=\sum_i \sum_j M_{i,j}=\sum_i\sum_j a_i\cdot b_j=\sum_i(a_i\cdot \sum_j b_j)=(\sum_i a_i)\cdot (\sum_j b_j)$.

Formulating setting row $i$ and column $j$ to zero, the new beauty is:

$$
\begin{align*}
q&=B-(b_j\cdot(\sum a)+a_i\cdot(\sum b)-a_i\cdot b_j) \\
&=((\sum a)-a_i)\cdot((\sum b)-b_j)
\end{align*}
$$

If such $a_i$ and $b_j$ exist, the operation can be performed.

## G1

1. Let $G$ be the input graph
2. Let $\text{ans}=0$
3. For each component $C$ in $G$:
  - Let $\text{cycle}$ be the set of nodes in the cycle of $C$
  - Let $d=max_{u\in \text{cycle}}\text{Distance-To-Cycle(C, u)}$
  - $\text{ans}:=max(\text{ans}, d)$
4. Return $\text{ans}$

---

*Proof.*

Consider some $v_i\in V$. There must exist an edge $e=(v_i,v_j),i\neq j$. Following this path from $v_i$, the each edge must map to a previously seen node, forming a cycle, or a new node. Because the graph is finite, the path starting any $v_i$ must contain a cycle. Thus, G is a graph of components with one cycle each.

Next, consider some component $C\in G$. For every spider $v_i$ in $C$:

- If $v_i$ is in the cycle:
  - The cycle itself will always be stable. Every spider has a plushie and each spider will give and receive a plushie.
- Otherwise, $v_i$ is not in the cycle.
  - Let $v_j$ be the furthest spider on a path with a plushie containing $v_i$ to the cycle. When $v_j$ gives a plushie to its child, the graph is unstable because $v_j$ transitions from state $0$ to $1$. However, the path containing $v_j$ and its ancestors become stable the next year because they never receive or give a plushie again. The path takes $d_j$ years to become stable, the distance from $v_j$ to the path.

Therefore, $C$ becomes stable in $d_C:=max_{v_j\in C}(d_j)$ time.

The entire graph becomes stable when each component becomes stable, which is the longest time any component takes to become stable. Thus, the graph becomes stable in $max_{C\in G}d_C$ years.

$\blacksquare$

## G2

1. Let $G$ be the input graph
2. Let $\text{ans}=0$
3. For each component $C$ in $G$:
  - Let $U$ be the set of all nodes not in the cycle of $C$
  - Let $\text{count}$ be the number of plushies each spider has
  - Let $\text{par}$ be the set of parents for each spider
  - Run a multi-source BFS simulating the state transitions on $U$. For each iteration at year $y$:
    - $\text{count[u]}:=\text{count[u]}+\sum_{p\in \text{par[u]}}\text{count[p]}$
    - $\text{ans}:=max(\text{ans}, \text{count[u]})$
    - $y:=y+1$
4. Return $\text{ans}$

---

*Proof.*

Consider some $v_i\in V$. There must exist an edge $e=(v_i,v_j),i\neq j$. Following this path from $v_i$, the each edge must map to a previously seen node, forming a cycle, or a new node. Because the graph is finite, the path starting any $v_i$ must contain a cycle. Thus, G is a graph of components with one cycle each.

Next, consider some component $C\in G$. For every spider $v_i$ in $C$:

- If $v_i$ is in the cycle:
  - The cycle itself will always be stable. If a spider has $x$ plushies in year $y$, it will give and receive one plushie and have $x$ in the next year as well.
- Otherwise, $v_i$ is not in the cycle.
  - Let $v_j$ be the furthest spider on a path containing $v_i$ to the cycle. When $v_j$ gives a plushie to its child, the graph is unstable because $v_j$ transitions from state $0$ to $1$. However, the path containing $v_j$ and its ancestors become stable the next year because they never receive or give a plushie again.
  - However, the child may have more than one plushie next year. If the child had $x_0$ plushies the on year $y$ received $x_1$ plushies the next, it must ultimately give $x_0+x_1$ plushies taking $x_0+x_1y$. The path becomes stable in the maximum time it takes any spider on the path to give its plushies, $d_i$. The algorithm gathers the $x_1$ term by considering all parents of the child and propagates the plushie counts by simulation.

Therefore, $C$ becomes stable in $d_C:=max_{v_i\in C}(d_i)$ time.

The entire graph becomes stable when each component becomes stable, which is the longest time any component takes to become stable. Thus, the graph becomes stable in $max_{C\in G}d_C$ years.

$\blacksquare$

## H

- Let $\text{Prefix}$ be the 2D-prefix sum matrix of $A$
- Let $\text{Colwise}$ be the column-wise 2D-prefix coefficient sum matrix of $A$
- Let $\text{Rowwise}$ be the row-wise 2D-prefix coefficient sum matrix of $A$
- Let $\text{Submatrix-Sum}(x_1,y_1,x_2,y_2)$ compute the sum of the submatrix of $M$ bounded by $(x_1,y_1)$ and $(x_2,y_2)$
1. For each query $x_1,x_2,y_1,y_2$:
- Let $w=y_2-y_1+1$
- Let $prefix=\text{Submatrix-Sum}(\text{Prefix},x_1,y_1,x_2,y_2)$
- Let $rowsum=\text{Submatrix-Sum}(\text{Rowwise},x_1,y_1,x_2,y_2)$
- Let $colsum=\text{Submatrix-Sum}(\text{Colwise},x_1,y_1,x_2,y_2)$
- Return $w\cdot rowsum+colsum-(x_1+y_1-1)\cdot \text{prefix}$

---

*Proof.*

Mathematically formulated:

$$
\begin{align*}
\sum_{i}i\cdot A_i
&= \sum_{i=x_1}^{x_2}\sum_{j=y_1}^{y_2}(i+(y_2-y_1+1)\cdot j)\cdot M_{i,j} \\
&= \sum_{i=x_1}^{x_2}\sum_{j=y_1}^{y_2}M_{i,j}\cdot i+(y_2-y_1+1)\sum_{i=x_1}^{x_2}\sum_{j=y_1}^{y_2} M_{i,j}\cdot j \\
&= \sum_{i=1}^{x_2} \sum_{j=1}^{y_2} i\cdot M_{i,j} + (y_2-y_1+1)\cdot\sum_{i=1}^{x_2} \sum_{j=1}^{y_2}j\cdot M_{i,j} - ( x_1 + y_1 - 1 ) \sum_{i=x_1}^{x_2} \sum_{j=y_1}^{y_2} M_{i,j}
\end{align*}
$$

Where the first term is $\text{Colwise}$ and the second $\text{Rowwise}$. Because the query matrix is offset by $x_1$ rows and $y_1$ cols, the algorithm avoids double-counting by subtracting $\text{prefix}$ matrix sum $x_1+y_1-1$.

$\blacksquare$

[^1]: This is a massive and inaccurate simplification made for pedagogical purposes. In reality, there are many less candidates, and thus optimal solutions, then all permutations of 10 digits between some $l$ and $r$. However, because the computation is so lightweight, one may increase the number of trials to 1000 or even 10000. While not rigorous, given that only $n=165$ candidates can provide a probability of failure of $10^{-7}$, running an order of magnitude greater number of trials is nearly certain to pass all test cases.
