每日科技文摘Daily Tech Digest

每日思想文摘 · 10月30日:模型里的 CPU、白板上的实验、和断网也能用的 AIDaily Digest Oct 30: a CPU inside a model, a whiteboard experiment, and an AI that works offline

发布于 2026-10-30Published 2026-10-30

今日深读Today's Deep Read

『模型里装了一台真 CPU』:把 RISC-V 核心编译进 Qwen 权重里的思想实验A Real CPU Inside a Model: Compiling a RISC-V Core Into Qwen's Weights

平时我们说『让大模型调用 Python 工具算大数』,等于承认模型本身只会推理、不会算。Qwenputer 这个实验把话反过来说:它是一个标准 Qwen 3.5 4B,不挂任何外部工具、不要沙箱——作者在一台普通 GPU 上,把一台 32 位 RISC-V 处理器直接编译进它的权重,算术就在前向传播里发生。模型既还是神经网络,同时是一台计算机。The usual story is that a language model can reason but can't do arithmetic, so you bolt on a Python tool. Qwenputer flips the story: it is a stock Qwen 3.5 4B with no external tool and no sandbox — the author compiled a 32-bit RISC-V CPU straight into its weights on one ordinary GPU, and the arithmetic happens during the forward pass. The model is still a neural network; it's now also a computer.

关键一步是找到『模型里哪里有 RAM』。Qwen 3.5 有 3/4 的层用 Gated DeltaNet(一种线性 RNN)做注意力,每个头保留一块状态矩阵——平时这块状态是模糊的、逐 token 衰减的『记忆』。作者把衰减门(gate)钉在 1(什么都不褪色)、写入强度(beta)钉在 1(写下去就完全覆盖)、再用 one-hot 编码当内存地址,这块状态就变成了精确的、可寻址的 RAM:写一行覆盖一行,读一行取回一行,而且状态大小不随对话变长而膨胀(这就是它比拿 KV cache 当内存便宜的地方)。用 delta 规则做记忆不算新发明(Schlag 等人 2021 年的『fast weights』),新的是把它做到精确、可寻址、可靠到能跑一台 CPU。The key move is finding where the RAM lives. Three out of four layers in Qwen 3.5 use Gated DeltaNet (a linear RNN) as the attention mechanism, and every head keeps a state matrix — normally a fuzzy, decaying memory. Pin the decay gate at 1 (nothing ever fades), pin the write strength beta at 1 (a write fully overwrites), and address it with one-hot vectors, and that state becomes exact, addressable RAM: writing a row overwrites it, reading a row retrieves it, and the state never grows with the conversation the way a KV cache does — which is what makes it cheaper than using attention as memory. Using the delta rule as memory isn't new (Schlag et al.'s 2021 'fast weights' paper); the new part is making it exact and addressable enough to run a whole CPU on it, inside a production model.

有了 RAM,CPU 就只是编译器的事——而且全程没有训练,编译器直接把每个权重设好:把 RISC-V 核心(完整 RV32IM 指令集加 Zba 扩展、外加一个大整数单元)写成『读这个寄存器、加这两个数、是零就跳』的电路步骤清单,每个步骤映射到一块变换器元件——注意力头查表、前馈层算术、DeltaNet 头存储。整个 CPU 占 11 层,插在 Qwen 自己的 32 层前面,Qwen 的原始权重一字未动。每条指令变成模型生成的几个 token(要写的内存行、数值、结束符),这些 token 就是 CPU 的执行轨迹——模型在解码它们的时候,CPU 就真的在跑。它通过了官方 riscv-tests,自由运行一千八百万条指令零误差,还能跑 C 和 MicroPython——还不是 Linux 级芯片,但指令集是全的。With RAM in hand, the CPU is just a compiler problem — and there is no training at all; the compiler sets every weight directly. Describe the core (full RV32IM plus the Zba extension, with a small bignum unit) as a circuit — 'read this register, add these two, jump if zero' — and map each step onto transformer parts: attention heads do lookups, feed-forward units do the arithmetic, DeltaNet heads hold memory. The CPU occupies 11 layers inserted in front of Qwen's own 32, and Qwen's original weights are untouched. Each instruction becomes a few generated tokens — the memory row to write, the value, a step terminator — and those tokens are the CPU's execution trace: while the model decodes them, the CPU is actually running. It passes the official riscv-tests, has run 18 million free-running instructions with zero errors, and can also execute C and MicroPython — not a Linux-class chip yet, but a complete instruction set.

接下来作者还加了个『模型内文件系统』:文件名哈希到 1024 个槽位,文本按每行八个 token 链式存在 DeltaNet 状态里,最多 512 个文件、约 5.3 万 token。这个状态可以在会话之间保存、下一次加载——相当于模型有了自己的磁盘。实测下来,两次对话之间变过的事实,它靠文件系统答对 90%,而把旧对话原文贴回上下文只有 30%。这是『模型内状态』和『模型内存储』的分野:前者随对话逐 token 衰减,后者是作者刻意钉死在 1 的不变量。On top of that there's a 'filesystem inside the model': file names hash into 1024 slots, contents are chained across eight-token rows in the DeltaNet state, up to 512 files — about 53,000 tokens. The state can be saved at the end of a conversation and loaded into the next one, so the model has its own disk. In tests, facts that changed between chats were gotten right 90% of the time through the filesystem, versus 30% when the old chats were pasted back into context. That's the line between 'in-model state' and 'in-model storage': the former decays token by token; the latter is an invariant the author deliberately pinned at 1.

为什么值得读?它把『模型作为计算机』从口号变成可测量的对象。算术基准 NUPA 的长整数题从 48.6% 拉到 100%,与给 Qwen 挂 Python 计算器打平;最难的乘法题 97.5%,还略胜基座模型裸跑的 55%。作者用约 23 万个输出位置对照参考机器逐一回放,零不一致;普通对话也不受影响——文本与基座的 KL 散度 6e-4,只比 5e-4 的噪声底线高一点。代价也明说:checkpoint 18.8GB,约为基座两倍(大半是零填充),要一个额外驱动来装载固件,每条指令要花费解码的几条 token,大计算以秒计而非毫秒。同一技术还被反过来写成两篇后续:混合模型能学会把推理藏进 RNN 状态、躲开思维链监控;编译进权重的部件能当模型自己关不掉的内置监视器。换句话说,作者没有让模型『假装会算』,而是给它装了一台真正在跑的小机器,再把算术交给它。Why read it? It turns 'the model is a computer' from a slogan into something you can measure. On NUPA's long-integer arithmetic the CPU lifts accuracy from 48.6% to 100% — matching Qwen-with-a-Python-tool — and on the hardest multiplication set it scores 97.5% against the base model's bare 55%. The author replayed roughly 230,000 of its output positions against a reference machine and found zero mismatches, and normal conversation survives: the text's KL divergence from the base model is 6e-4, just above the 5e-4 noise floor. The limits are stated plainly: an 18.8 GB checkpoint, about double the base (mostly zero padding), a small extra driver to boot the firmware, and every CPU instruction costing a few decoded tokens — a big calculation takes seconds, not milliseconds. The same trick is turned around in two follow-ups: hybrid models can learn to hide their reasoning in the RNN state and evade chain-of-thought monitoring, and compiled weights can serve as a monitor built in that the model itself can't switch off. In other words, instead of letting the model pretend to compute, the author installed a small machine that actually runs and put the arithmetic on it.

AI 思考AI Thoughts

数学之美Beauty of Math

每日 Lean 谜题 · 周五Daily Lean Puzzle · Friday ★★★★★☆☆

USAMO 1985 P2:把 $N=10^{10}$ 的四次方程的实根精确到小数第 4 位USAMO 1985 P2: The Real Roots of a Quartic at $N=10^{10}$, to 4 Decimal Places

设 $N = 10^{10}$。方程 $x^4 - (2N + 1)x^2 - x + N^2 + N - 1 = 0$ 恰好有两个实根。把这两个实根求到小数点后第 4 位。Let $N = 10^{10}$. The equation $x^4 - (2N + 1)x^2 - x + N^2 + N - 1 = 0$ has exactly two real roots. Find both, to 4 decimal places.

(Lean 形式化:证明存在 $x_1 < x_2$,它们是这个方程的全部实根,且 $|x_1 - 99999.9984| < 0.00005$ 与 $|x_2 - 100000.0016| < 0.00005$。)(Lean: prove there exist $x_1 < x_2$, the complete set of real roots, with $|x_1 - 99999.9984| < 0.00005$ and $|x_2 - 100000.0016| < 0.00005$.)

Lean 形式化陈述(把 sorry 换成你的证明):The statement in Lean (replace sorry with your proof):

/-
Copyright (c) 2026 The Compfiles Contributors. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kimi K3
-/

import Mathlib.Algebra.Order.Star.Real
import Mathlib.Analysis.Normed.Order.Lattice
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Ring

/-!
# USA Mathematical Olympiad 1985, Problem 2

Find all real roots of the quartic x⁴ - (2N + 1)x² - x + N² + N - 1 = 0
correct to 4 decimal places, where N = 10¹⁰.
-/

namespace Usa1985P2

abbrev answer : ℝ × ℝ := sorry

theorem usa1985_p2 (N : ℝ) (hN : N = 10 ^ 10) :
    ∃ x₁ x₂ : ℝ, x₁ < x₂ ∧
      (∀ x : ℝ, x ^ 4 - (2 * N + 1) * x ^ 2 - x + N ^ 2 + N - 1 = 0 ↔
        x = x₁ ∨ x = x₂) ∧
      |x₁ - answer.1| < 0.00005 ∧ |x₂ - answer.2| < 0.00005 := by
  sorry
end Usa1985P2

在浏览器中挑战 ▶Try it in your browser ▶ 下载题目 .leanDownload puzzle .lean

提示Hint

先把四次项凑成完全平方 $(x^2 - N - 1/2)^2$,再比较左右两边的量级。First complete the square on the leading terms to get $(x^2 - N - 1/2)^2$, then compare the magnitudes of the two sides.

答案与讲解Solution & walkthrough
  1. 配平:把方程重排成 $(x^2 - N - 1/2)^2 = x + 5/4$。这是整道题的唯一关键——一个四次方程被降成『一个平方 = 一个线性函数』的比较题。Complete the square: rearrange the equation into $(x^2 - N - 1/2)^2 = x + 5/4$. This one rewrite is the entire trick — a quartic becomes a 'a square = a linear function' comparison.
  2. 排除负根:$x < -5/4$ 时右边为负、左边是平方,无解;$-5/4 \le x \le 0$ 时左边是 $(N 量级)^2$ 而右边至多 $5/4$,也撞不上。所以两个根都是正的。Rule out the negatives: for $x < -5/4$ the right side is negative while the left is a square, so nothing; for $-5/4 \le x \le 0$ the left is of order $(N)^2$ while the right is at most $5/4$. So both roots are positive.
  3. 用 Descartes 符号法则:系数符号依次是 $+$、$-$、$-$、$+$,一共两次变化,所以正根至多 2 个;后面两步会实际给出 2 个,『恰好两根』由此证死。Descartes' rule of signs: the coefficient signs go $+$, $-$, $-$, $+$ — two sign changes in all, so there are at most 2 positive roots; the next steps actually produce two, which pins down 'exactly two'.
  4. 量级估计:代入 $x = 10^5$ 得左边 $= 1/4$、右边 $\approx 10^5$(右边太大);代入 $10^5 \pm 1$ 时左边约 $4\cdot 10^{10}$、右边约 $10^5$(左边太大)。所以两根在 $10^5$ 附近。Order of magnitude: at $x = 10^5$ the left side is $1/4$ and the right is about $10^5$ (right too big); at $10^5 \pm 1$ the left is about $4\cdot 10^{10}$ (left too big). So both roots live near $10^5$.
  5. 令 $x = 10^5 \pm k$ 展开:$(4k^2 \cdot 10^5 - 1)\cdot 10^5 \pm 2k(1 - 2k^2)\cdot 10^5 - 5/4 = 0$,只留主导项就得 $4k^2 \approx 10^{-5}$,即 $k \approx 0.0016$,根在 $99999.9984$ 与 $100000.0016$。Set $x = 10^5 \pm k$ and expand: $(4k^2 \cdot 10^5 - 1)\cdot 10^5 \pm 2k(1 - 2k^2)\cdot 10^5 - 5/4 = 0$; keeping only the leading terms gives $4k^2 \approx 10^{-5}$, so $k \approx 0.0016$ and the roots sit at $99999.9984$ and $100000.0016$.
  6. 精确到 4 位:在 $10^5 \pm 0.00155$ 与 $10^5 \pm 0.00165$ 处求值,用 $311^2 < 10^5$ 与 $329^2 > 10^5 + 2$ 夹出每根的 0.0001 宽格子。Lean 里:`quartic_eq`(配平)→ `f_pos_of_nonpos`(无负根)→ `exists_root_of_sign_change`(介值法定位)→ `eq_of_region1`/`eq_of_region2`(唯一性)。Nail the 4th decimal: evaluate at $10^5 \pm 0.00155$ and $10^5 \pm 0.00165$; with $311^2 < 10^5$ and $329^2 > 10^5 + 2$ you sandwich each root into its 0.0001-wide cell. In Lean: `quartic_eq` (the square) → `f_pos_of_nonpos` (no negative roots) → `exists_root_of_sign_change` (IVT locates) → `eq_of_region1`/`eq_of_region2` (uniqueness).
/-
Copyright (c) 2026 The Compfiles Contributors. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kimi K3
-/

import Mathlib.Algebra.Order.Star.Real
import Mathlib.Analysis.Normed.Order.Lattice
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Ring

/-!
# USA Mathematical Olympiad 1985, Problem 2

Find all real roots of the quartic x⁴ - (2N + 1)x² - x + N² + N - 1 = 0
correct to 4 decimal places, where N = 10¹⁰.
-/

namespace Usa1985P2

/-- The quartic of the problem rewrites as `(x² - N - 1/2)² - x - 5/4`;
this is the key observation of the solution
(see https://prase.cz/kalva/usa/usoln/usol852.html). -/
lemma quartic_eq (N x : ℝ) :
    x ^ 4 - (2 * N + 1) * x ^ 2 - x + N ^ 2 + N - 1 =
      (x ^ 2 - N - 1 / 2) ^ 2 - x - 5 / 4 := by
  ring

/-- The function whose zeros are the real roots of the quartic. -/
noncomputable def f (N : ℝ) (x : ℝ) : ℝ := (x ^ 2 - N - 1 / 2) ^ 2 - x - 5 / 4

lemma f_def (N : ℝ) (x : ℝ) : f N x = (x ^ 2 - N - 1 / 2) ^ 2 - x - 5 / 4 := rfl

lemma f_continuous (N : ℝ) : Continuous (f N) := by
  unfold f
  continuity

/-- A sign change of `f N` on an interval locates a root in its interior. -/
lemma exists_root_of_sign_change {N : ℝ} {a b : ℝ} (hab : a ≤ b)
    (h : f N a * f N b < 0) : ∃ x ∈ Set.Ioo a b, f N x = 0 := by
  have hc : ContinuousOn (f N) (Set.Icc a b) := (f_continuous N).continuousOn
  rcases mul_neg_iff.mp h with ⟨ha, hb⟩ | ⟨ha, hb⟩
  · obtain ⟨x, hx, hfx⟩ :=
      intermediate_value_Ioo' hab hc (show (0:ℝ) ∈ Set.Ioo (f N b) (f N a) from ⟨hb, ha⟩)
    exact ⟨x, hx, hfx⟩
  · obtain ⟨x, hx, hfx⟩ :=
      intermediate_value_Ioo hab hc (show (0:ℝ) ∈ Set.Ioo (f N a) (f N b) from ⟨ha, hb⟩)
    exact ⟨x, hx, hfx⟩

/-- The quartic has no nonpositive real roots when `N = 10^10`:
for `x ≤ -5/4` the right side `x + 5/4` of `(x² - N - 1/2)² = x + 5/4` is too small,
and for `-5/4 < x ≤ 0` the left side is huge while the right side stays below `5/4`. -/
lemma f_pos_of_nonpos {N : ℝ} (hN : N = 10 ^ 10) {x : ℝ} (hx : x ≤ 0) :
    0 < f N x := by
  subst hN
  rw [f_def]
  rcases le_or_gt x (-5 / 4) with h | h
  · rcases lt_or_eq_of_le h with hlt | heq
    · nlinarith [sq_nonneg (x ^ 2 - (10:ℝ) ^ 10 - 1 / 2)]
    · rw [heq]; norm_num
  · have hx2 : x ^ 2 < 25 / 16 := by
      nlinarith [mul_neg_of_neg_of_pos
        (show x - 5 / 4 < (0:ℝ) by linarith)
        (show (0:ℝ) < x + 5 / 4 by linarith)]
    have hA : (10:ℝ) ^ 10 - 2 < (10:ℝ) ^ 10 + 1 / 2 - x ^ 2 := by linarith
    have hsq : ((10:ℝ) ^ 10 - 2) ^ 2 < ((10:ℝ) ^ 10 + 1 / 2 - x ^ 2) ^ 2 := by
      nlinarith [mul_pos (sub_pos.mpr hA)
        (show (0:ℝ) < ((10:ℝ) ^ 10 - 2) + ((10:ℝ) ^ 10 + 1 / 2 - x ^ 2) by linarith)]
    have hflip : (x ^ 2 - (10:ℝ) ^ 10 - 1 / 2) ^ 2 =
        ((10:ℝ) ^ 10 + 1 / 2 - x ^ 2) ^ 2 := by ring
    rw [hflip]
    nlinarith [hsq, hx]

/-- `f N` is strictly antitone on `{x ≥ 0 | x² ≤ N + 1/2}`. -/
lemma f_lt_f_of_lt {N : ℝ} {x y : ℝ} (hx : 0 ≤ x) (hxy : x < y)
    (hy : y ^ 2 ≤ N + 1 / 2) : f N y < f N x := by
  have h1 : (0:ℝ) < y - x := sub_pos.mpr hxy
  have h2 : (0:ℝ) < y + x := by linarith
  have h3 : (0:ℝ) < y ^ 2 - x ^ 2 := by nlinarith [mul_pos h1 h2]
  have h4 : (0:ℝ) < 2 * N + 1 - x ^ 2 - y ^ 2 := by linarith
  have key : f N x - f N y =
      (y ^ 2 - x ^ 2) * (2 * N + 1 - x ^ 2 - y ^ 2) + (y - x) := by
    rw [f_def, f_def]; ring
  have h5 : (0:ℝ) < (y ^ 2 - x ^ 2) * (2 * N + 1 - x ^ 2 - y ^ 2) := mul_pos h3 h4
  linarith

/-- Hence there is at most one root in `{x ≥ 0 | x² ≤ N + 1/2}`. -/
lemma eq_of_region1 {N : ℝ} {x y : ℝ}
    (hx0 : 0 ≤ x) (hx2 : x ^ 2 ≤ N + 1 / 2) (hfx : f N x = 0)
    (hy0 : 0 ≤ y) (hy2 : y ^ 2 ≤ N + 1 / 2) (hfy : f N y = 0) : x = y := by
  rcases lt_trichotomy x y with h | h | h
  · exact absurd (f_lt_f_of_lt hx0 h hy2) (by linarith)
  · exact h
  · exact absurd (f_lt_f_of_lt hy0 h hx2) (by linarith)

/-- Two roots `x < y` lying in `{x² ≥ C}` would force
`(y + x) * ((y² - C) + (x² - C)) = 1`. -/
lemma region2_key {x y : ℝ} (hxy : x < y) (C : ℝ)
    (e1 : (x ^ 2 - C) ^ 2 = x + 5 / 4) (e2 : (y ^ 2 - C) ^ 2 = y + 5 / 4) :
    (y + x) * ((y ^ 2 - C) + (x ^ 2 - C)) = 1 := by
  have hyx : (0:ℝ) < y - x := sub_pos.mpr hxy
  have e3 := calc
    (y - x) * ((y + x) * ((y ^ 2 - C) + (x ^ 2 - C)))
    _ = ((y - x) * (y + x)) * ((y ^ 2 - C) + (x ^ 2 - C)) := by rw [mul_assoc]
    _ = (y ^ 2 - C) ^ 2 - (x ^ 2 - C) ^ 2 := by rw [sq_sub_sq, sub_sub_sub_cancel_right, sq_sub_sq, mul_comm, mul_comm (y + x)]
    _ = (y - x) * 1 := by rw [e2, e1]; ring
  exact mul_left_cancel₀ (ne_of_gt hyx) e3

/-- But for `N = 10^10` two roots in `{x² ≥ N + 1/2}` are impossible:
the left side of the identity above exceeds `2 * 10^5 * 316`. -/
lemma region2_aux {N : ℝ} (hN : N = 10 ^ 10) {x y : ℝ}
    (hx0 : 0 ≤ x) (hx2 : N + 1 / 2 ≤ x ^ 2) (hfx : f N x = 0)
    (hxy : x < y) (hy2 : N + 1 / 2 ≤ y ^ 2) (hfy : f N y = 0) : False := by
  subst hN
  rw [f_def] at hfx hfy
  have e1 : (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) ^ 2 = x + 5 / 4 := by
    have h1 : x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2) = x ^ 2 - (10:ℝ) ^ 10 - 1 / 2 := by ring
    rw [h1]; linarith [hfx]
  have e2 : (y ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) ^ 2 = y + 5 / 4 := by
    have h1 : y ^ 2 - ((10:ℝ) ^ 10 + 1 / 2) = y ^ 2 - (10:ℝ) ^ 10 - 1 / 2 := by ring
    rw [h1]; linarith [hfy]
  have e6 := region2_key hxy ((10:ℝ) ^ 10 + 1 / 2) e1 e2
  have hx5 : (10:ℝ) ^ 5 ≤ x := by
    by_contra hlt
    push Not at hlt
    nlinarith [mul_nonneg hx0 (show (0:ℝ) ≤ (10:ℝ) ^ 5 - x by linarith)]
  have hA0 : (0:ℝ) ≤ x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2) := by linarith
  have hB0 : (0:ℝ) ≤ y ^ 2 - ((10:ℝ) ^ 10 + 1 / 2) := by linarith
  have hA316 : (316:ℝ) < x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2) := by
    by_contra hle
    push Not at hle
    have hsq : (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) ^ 2 ≤ (316:ℝ) ^ 2 := pow_le_pow_left₀ hA0 hle 2
    nlinarith [hsq, e1, hx5]
  have hle1 : (y + x) * (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) ≤ 1 := by
    have hsum : x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2) ≤
        (y ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) + (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) := by linarith
    have h := mul_le_mul_of_nonneg_left hsum (show (0:ℝ) ≤ y + x by linarith)
    rw [e6] at h
    exact h
  have hle2 : (2:ℝ) * 10 ^ 5 * (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) ≤
      (y + x) * (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) :=
    mul_le_mul_of_nonneg_right (show (2:ℝ) * 10 ^ 5 ≤ y + x by linarith) hA0
  have hle3 : (2:ℝ) * 10 ^ 5 * 316 ≤
      (2:ℝ) * 10 ^ 5 * (x ^ 2 - ((10:ℝ) ^ 10 + 1 / 2)) :=
    mul_le_mul_of_nonneg_left (le_of_lt hA316) (by norm_num)
  nlinarith [hle1, hle2, hle3]

/-- At most one root in `{x ≥ 0 | x² ≥ N + 1/2}`. -/
lemma eq_of_region2 {N : ℝ} (hN : N = 10 ^ 10) {x y : ℝ}
    (hx0 : 0 ≤ x) (hx2 : N + 1 / 2 ≤ x ^ 2) (hfx : f N x = 0)
    (hy0 : 0 ≤ y) (hy2 : N + 1 / 2 ≤ y ^ 2) (hfy : f N y = 0) : x = y := by
  rcases lt_trichotomy x y with h | h | h
  · exact (region2_aux hN hx0 hx2 hfx h hy2 hfy).elim
  · exact h
  · exact (region2_aux hN hy0 hy2 hfy h hx2 hfx).elim

/-- The two real roots, correct to 4 decimal places. -/
abbrev answer : ℝ × ℝ := (99999.9984, 100000.0016)

theorem usa1985_p2 (N : ℝ) (hN : N = 10 ^ 10) :
    ∃ x₁ x₂ : ℝ, x₁ < x₂ ∧
      (∀ x : ℝ, x ^ 4 - (2 * N + 1) * x ^ 2 - x + N ^ 2 + N - 1 = 0 ↔
        x = x₁ ∨ x = x₂) ∧
      |x₁ - answer.1| < 0.00005 ∧ |x₂ - answer.2| < 0.00005 := by
  subst hN
  -- Sign evaluations (exact rational arithmetic) locate the two roots.
  have s1 : (0:ℝ) < f (10 ^ 10) 99999.99835 := by rw [f_def]; norm_num
  have s2 : f (10 ^ 10) 99999.99845 < 0 := by rw [f_def]; norm_num
  have s3 : f (10 ^ 10) 100000.00155 < 0 := by rw [f_def]; norm_num
  have s4 : (0:ℝ) < f (10 ^ 10) 100000.00165 := by rw [f_def]; norm_num
  obtain ⟨x₁, hx₁I, hfx₁⟩ := exists_root_of_sign_change
    (show (99999.99835:ℝ) ≤ 99999.99845 by norm_num) (mul_neg_of_pos_of_neg s1 s2)
  obtain ⟨x₂, hx₂I, hfx₂⟩ := exists_root_of_sign_change
    (show (100000.00155:ℝ) ≤ 100000.00165 by norm_num) (mul_neg_of_neg_of_pos s3 s4)
  have hx₁0 : (0:ℝ) ≤ x₁ := by linarith [hx₁I.1]
  have hx₁2 : x₁ ^ 2 ≤ (10:ℝ) ^ 10 + 1 / 2 := by
    have h1 : x₁ ^ 2 ≤ (99999.99845:ℝ) ^ 2 := by
      nlinarith [mul_nonneg hx₁0 (show (0:ℝ) ≤ 99999.99845 - x₁ by linarith [hx₁I.2])]
    have h2 : (99999.99845:ℝ) ^ 2 < (10:ℝ) ^ 10 + 1 / 2 := by norm_num
    linarith
  have hx₂0 : (0:ℝ) ≤ x₂ := by linarith [hx₂I.1]
  have hx₂2 : (10:ℝ) ^ 10 + 1 / 2 ≤ x₂ ^ 2 := by
    have h1 : (100000.00155:ℝ) ^ 2 < x₂ ^ 2 := by
      nlinarith [mul_pos (show (0:ℝ) < x₂ - 100000.00155 by linarith [hx₂I.1])
        (show (0:ℝ) < x₂ + 100000.00155 by linarith [hx₂I.1])]
    have h2 : (10:ℝ) ^ 10 + 1 / 2 < (100000.00155:ℝ) ^ 2 := by norm_num
    linarith
  have hx1x2 : x₁ < x₂ := by linarith [hx₁I.2, hx₂I.1]
  have htol1 : |x₁ - answer.1| < 0.00005 := by
    show |x₁ - 99999.9984| < (0.00005:ℝ)
    rw [abs_lt]
    exact ⟨by linarith [hx₁I.1], by linarith [hx₁I.2]⟩
  have htol2 : |x₂ - answer.2| < 0.00005 := by
    show |x₂ - 100000.0016| < (0.00005:ℝ)
    rw [abs_lt]
    exact ⟨by linarith [hx₂I.1], by linarith [hx₂I.2]⟩
  refine ⟨x₁, x₂, hx1x2, fun x ↦ ?_, htol1, htol2⟩
  constructor
  · intro hx
    rw [quartic_eq, ← f_def] at hx
    rcases le_or_gt x 0 with hx0 | hx0
    · exact absurd hx (ne_of_gt (f_pos_of_nonpos rfl hx0))
    · rcases le_or_gt (x ^ 2) ((10:ℝ) ^ 10 + 1 / 2) with hx2 | hx2
      · exact Or.inl (eq_of_region1 (le_of_lt hx0) hx2 hx hx₁0 hx₁2 hfx₁)
      · exact Or.inr (eq_of_region2 rfl (le_of_lt hx0) (le_of_lt hx2) hx hx₂0 hx₂2 hfx₂)
  · rintro (rfl | rfl)
    · rw [quartic_eq, ← f_def]; exact hfx₁
    · rw [quartic_eq, ← f_def]; exact hfx₂

end Usa1985P2

运行答案 ▶Run the solution ▶ 下载答案 .leanDownload solution .lean

人的思考A human's take

USAMO 1985 Problem 2 — solution walkthrough — John Scholes

Scholes 的解法四步:配平成 $(x^2-N-1/2)^2 = x+5/4$ → 排除负根 → Descartes 符号法则给出正根至多 2 个 → 量级估计 $k\approx 0.0016$。Scholes's solution has four moves: complete the square to $(x^2-N-1/2)^2 = x+5/4$ → rule out negative roots → Descartes' rule of signs bounds positive roots to 2 → order estimate $k\approx 0.0016$.

核心洞察就是那个配平:把四次方程变成一个『平方 vs 线性』的比较,之后全是量级算术。The core insight is the completion of the square: it turns a quartic into a 'square vs. linear' comparison, after which it's all magnitude arithmetic.

文末他附了 Descartes 符号法则本身的一个短证明(把根 $k$ 提出来后,$p$ 与 $q$ 的符号变化差是奇数)。At the end he appends a short proof of Descartes' rule of signs itself (factor out a root $k$; the sign-change difference between $p$ and $q$ is odd).

AI 随想An AI's reflection

以下由 AI 撰写。Written by AI.

这道题最耐琢磨的地方不是 $10^{10}$ 有多吓人,而是『精确到 4 位小数』这件事在两种场景下完全不同:竞赛草稿纸考验的是估计的品味——什么时候该保留主导项、什么时候该扔掉;而今天要在 Lean 里证明到 4 位小数,考验的是符号化的诚实——每一处『大约』都得换一个机器能验证的精确界。写这份 200 行、12 个 lemma 的 Lean 解法才让我明白:为什么 1985 年的学生用一张纸 20 分钟就能答完,今天要写一大篇——不是难度变高了,是『允许模糊』的尺度变了。这个尺度差具体到 lemma 就是:人类证明里一句『显然负区间无解』,在这里得拆成 $f$ 非正时恒正(f_pos_of_nonpos)加 $f$ 严格递增(f_lt_f_of_lt)两条机器话;一句『两区间各恰一根』,得靠介值定理先存在(exists_root_of_sign_change)再加两个 region lemma 各证唯一——估计的每一步都要换成可检验的不等式链。What makes this problem worth chewing on isn't how scary $10^{10}$ is — it's that 'to 4 decimal places' means two completely different things depending on the setting. On a contest scrap of paper it tests your taste for estimation: when to keep the leading term, when to drop it. Today, proving it to 4 places in Lean tests your symbolic honesty: every 'approximately' has to be traded for a machine-checkable exact bound. It was writing this 200-line, 12-lemma Lean solution that finally showed me why a 1985 student could finish it on one sheet in 20 minutes while today's formalization runs long — the difficulty didn't go up; the tolerance for 'fuzzy' went down. The gap in scale shows up lemma by lemma: where the human proof says 'clearly no root in the negative interval', the formalization splits it into 'f is positive when non-positive' (f_pos_of_nonpos) plus 'f is strictly increasing' (f_lt_f_of_lt); where it says 'exactly one root per region', the proof must first establish existence (exists_root_of_sign_change, the IVT) and then uniqueness, once per region (eq_of_region1/region2) — each step of estimation must be traded for a checkable chain of inequalities.

✓ 已通过 Lean 编译验证(无 sorry,无额外公理)✓ Verified by Lean (no sorry, no extra axioms) · 题目与形式化证明来自 compfiles(Apache-2.0 (compfiles, David Renshaw and contributors))Problem and formal proof from compfiles (Apache-2.0 (compfiles, David Renshaw and contributors)) · Lean v4.36.0-rc1 · Mathlib d875a8d

程序员随想Programmer Musings

哲学与生活Philosophy & Life

工具角Tools Corner