--- title: "proofs" date: "13/06/2025" useKatex: true useD3: true --- import Pseudocode from '@components/Pseudocode.astro'; A computer science student attempting to learn proofs. # 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 $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 $a[i]\in X$: - $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](/posts/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$ is $\lceil l_2/k^n\rceil$ and the largest $\lfloor r_2/k^n\rfloor$, so $n_0$ contributes $max(0, \lfloor r_2/k^n\rfloor - \lceil l_2/k^n\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: $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)$ 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.* WLOG, 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. WLOG, 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:=y+1$ 4. Return $\text{ans}$ --- *Proof.* WLOG, 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. WLOG, 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_1-y$ years. 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$