From f0c1c150028c36549dc667d66ce571bc0743b115 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 22 Jun 2025 14:22:12 -0500 Subject: [PATCH] feat(proofs): some proofs --- astro.config.mjs | 8 + public/posts/proofs/graph.webp | Bin 0 -> 8046 bytes src/components/Pseudocode.astro | 41 +++++ .../algorithms/practice-makes-perfect.mdx | 24 --- src/content/posts/algorithms/proofs.mdx | 149 ++++++++++++++++++ 5 files changed, 198 insertions(+), 24 deletions(-) create mode 100644 public/posts/proofs/graph.webp create mode 100644 src/components/Pseudocode.astro delete mode 100644 src/content/posts/algorithms/practice-makes-perfect.mdx create mode 100644 src/content/posts/algorithms/proofs.mdx diff --git a/astro.config.mjs b/astro.config.mjs index 0a64412..3ac90be 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -2,6 +2,7 @@ import { defineConfig } from "astro/config"; import mdx from "@astrojs/mdx"; import remarkMath from "remark-math"; import rehypeKatex from "rehype-katex"; +import path from "path"; export default defineConfig({ build: { @@ -13,6 +14,13 @@ export default defineConfig({ rehypePlugins: [rehypeKatex], }), ], + vite: { + resolve: { + alias: { + "@components": path.resolve(".", "src/components"), + }, + }, + }, markdown: { shikiConfig: { theme: "github-light", diff --git a/public/posts/proofs/graph.webp b/public/posts/proofs/graph.webp new file mode 100644 index 0000000000000000000000000000000000000000..c1d6827de40457cfdb9c0db834d01ca344a69549 GIT binary patch literal 8046 zcmWIYbaP9SXJ80-bqWXzuuzDSXJGg+fnhGAmKoD5M!P_@32uuQDK1p}6zNdF#hIS> zN2PRvgVMYY?DMsL24=0^rsw-9{k{FFs;cPvTmLh^1@2mZy-NL@{g3&9_LFNH{`>!r zw%t&_=xgNP_P_2c^q=j2TVH1T?*Ey(1J&FAPk+VxZ}#8w5&CbhZ|Hx+zU=tB<%ine zZhyCa%lZHR&el!*=TznP;r<8vN%ODPhX09wx_@n5Vtw^5pXRR|e_Fq@e(%4{ zUqe6f-!`A|pWS}TSEfJCKi+?v|0aG{{6m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*aOCD`KNx>p z4?dG6d1;$KUy_vShsi8*7IkhNi*GLz^|E+krP}mpepA=|%7^`vZ%%wO*(z)n54?_#hJi%eq5gS+XCZ3n^Gj)4IHK*!k* literal 0 HcmV?d00001 diff --git a/src/components/Pseudocode.astro b/src/components/Pseudocode.astro new file mode 100644 index 0000000..39d4d67 --- /dev/null +++ b/src/components/Pseudocode.astro @@ -0,0 +1,41 @@ +--- +interface Props { + code: string; +} + +const lines = Astro.props.code + .trim() + .split(/\r?\n/); +--- + + + { console.log(lines) } + +

+  {lines.map((line, i) => (
+    
+ {i + 1}. + {line} +
+ ))} +
+ diff --git a/src/content/posts/algorithms/practice-makes-perfect.mdx b/src/content/posts/algorithms/practice-makes-perfect.mdx deleted file mode 100644 index 7b10062..0000000 --- a/src/content/posts/algorithms/practice-makes-perfect.mdx +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: "practice makes perfect" -date: "05/07/2025" ---- - -Today I improved my implementation skills with [Codeforces Round 874 Div. 3 Problem G](https://codeforces.com/contest/1833/problem/G). Despite not solving the problem after a full 45 minutes, I came across to the following realizations: - -1. Don't jump into coding. _Fully_ flesh out your implementation in your head before you begin. This is tempting to do, especially in a "competitive" environment. I tend to do this to avoid thinking about troublesome aspects of the problem that I _know_ I'll have to face later. Going into problems with a plan makes things much easier when coding but much harder up front. It is easy (for me) to get lost in the black-boxing four layers deep. Write it out, visualize it, and practice practice practice. - - > Considering my solution would've led to me uncover my core misinterpretation of the problem: **the tree does not have to binary**. I developed a solution for binary trees but the greedy logic cannot be extended to trees. - -2. Complex problems are, well, hard. You _have_ to practice to internalize patterns so you can focus on the _crux_ of the problem. - - > I spent 10 minutes debugging retrieving the leaves of a tree before even beginning to code the actual algorithm. **1800 is out of my skill range** (for now!). - -3. **Do not let a single thought/assertion/fact go unturned**. I made a litany of erroneous assertions in my time thinking about this problem, some of which include: - -- The tree has to be binary (it does not). -- I can gather the leaves in arbitrary order (once again, this doesn't generalize to trees). -- Ignore all cuts between identical nodes—it's fine! (I didn't know why this was the case) -- A set shouldn't be needed to track visited nodes in a tree— slap it on anyway (this was superfluous and should've immediately set off red flags that my parent-ignoring policy in my BFS was wrong). -- When processing a node in the "child-parent-child" pattern, just pop off the next node from the queue (within binary/n-ary trees, this is wrong—the leaves are gathered by _level_, so the next node in the queue is not guaranteed to be the current's sibling). - -5. Just because the solution passes the test cases does not mean it is right. This specifically applies to problems near/outside your skill range—create your own test cases. diff --git a/src/content/posts/algorithms/proofs.mdx b/src/content/posts/algorithms/proofs.mdx new file mode 100644 index 0000000..39dc3ad --- /dev/null +++ b/src/content/posts/algorithms/proofs.mdx @@ -0,0 +1,149 @@ +--- +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.* + +For some choice of $a$ there is only one +choice of $b$: $a=n-b\rightarrow b=n-a$. Consider all $a$ from $1,2,\cdots,n$. There are +$n$ such pairs: + +$$(1,n-1),(2,n-2),...,(n,0)$$ + +Excluding the last pair formed when $a=n\rightarrow b=0$, there are $n-1$ possible +ordered pairs. + +$\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. +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=Length(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. + +For each index $1\leq i\leq n$: +1. If $i$ is the first occurrence of $a[i]$, it is assigned to $b[i]$. +2. Otherwise, $a[i]$ is present in $a[:i]$. By the pigeonhole principle, there must be an unused integer $x\in\{1,2,\cdots,n\}.x\notin a[:i]\land x\notin b[:i]$. + +Therefore, all elements of $b$ are unique; every element of $b$ is a mode. + +$\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_i a_i)+a_i\cdot(\sum_j b_j)-a_i\cdot b_j)$ +$=((\sum_i a_i)-a_i)\cdot((\sum_j b_j)-b_j)$ + +If such $a_i$ and $b_j$ exist, the operation can be performed.