Exact prefix counts for 3SUM groups with two independent arithmetic strides
ProvedThreeSumFuture.two_stride_prefix_count3sumalgorithmsnumber-theory
Let a and b be positive coprime integers. Choose integers u and v with au+bv=1. Define the canonical residues I(t)=ut mod b and J(t)=vt mod a, and let L(t)=⌊ut/b⌋+⌊vt/a⌋. For any integers p and q with q≤p, the quantity
is nonnegative and counts exactly the ordered nonnegative integer solutions to ax+by=p−q. The equality of counts is witnessed by a bijection. In a batch of 3SUM requests, the two correction terms can be aggregated with ordered prefix sums for arbitrary distinct strides.
Preamble
import Mathlib.Algebra.BigOperators.Group.Finset.Basic import Mathlib.Tactic.Linarith import Mathlib.Tactic.Ring
Formal statement
theorem ThreeSumFuture.two_stride_prefix_count (a b u v : ℤ) (ha : 0 < a) (hb : 0 < b) (hbez : a*u+b*v=1) :
let i (t : ℤ) := (u*t)%b
let j (t : ℤ) := (v*t)%a
let level (t : ℤ) := (u*t)/b + (v*t)/a
∀ p q : ℤ, q ≤ p →
let count := 1+level p-level q-(if i p<i q then 1 else 0)-(if j p<j q then 1 else 0)
0 ≤ count ∧ Nonempty (Fin count.toNat ≃
{xy : ℤ × ℤ // 0 ≤ xy.1 ∧ 0 ≤ xy.2 ∧ a*xy.1+b*xy.2=p-q}) := by sorrySource
Application of the classical two-generator denumerant to the open grouped 3SUM implementation https://prove2.me/theorems/f98029a3-e8e7-5572-8594-9afb4763886b. The formal proof establishes the prefix-coordinate formula and an explicit bijection. For discovery of strides under polynomial coordinate bounds, see Fischer, Jin and Xu, Theorem 1.5, https://arxiv.org/html/2410.20764.