import Mathlib.Data.Fintype.Cardimport Mathlib.Data.Finset.Cardimport Mathlib.Tactic.Linarithnamespace ProgressionColours/-- The arithmetic progression with `k+1` terms. Positions start at zero. -/def progression (a d k : ℕ) : Finset ℕ := (Finset.range (k+1)).image (fun j => a+j*d)/-- This progression is inside `[0,N)` and extending it at either end leaves that interval. -/def Blocked (N k a d : ℕ) : Prop :=0 < d ∧ a+k*d < N ∧ a < d ∧ N ≤ a+(k+1)*ddef Hits (N k : ℕ) (T : Finset ℕ) : Prop :=∀ a d, Blocked N k a d → ∃ x ∈ T, x ∈ progression a d k/-- `N = (k+1)*q+r`, with `r ≤ k`. Natural subtraction truncates at zero. -/def count (k q r : ℕ) := max (q-r) (q-(k-r))def strip (k q r : ℕ) : Finset ℕ :=(Finset.range (count k q r)).image (fun i => r*(q+1)+i)theorem card_strip (k q r : ℕ) : (strip k q r).card = count k q r := bysimp [strip,Finset.card_image_of_injective, Function.Injective]theorem pivot_covered {k q r a d : ℕ} (hr : r ≤ k)(h : Blocked ((k+1)*q+r) k a d) :r*(q+1) ≤ a+r*d ∧ a+r*d < r*(q+1)+count k q r := byobtain ⟨_,hin,hl,hh⟩ := hhave ee : ∀ t : ℕ, (k-r)*t+r*t=k*t := fun t => by rw [←Nat.add_mul, Nat.sub_add_cancel hr]have el := ee dhave eq := ee qrcases Nat.le_total d q with hd | hd· have u := Nat.mul_le_mul_left (k-r+1) hdhave v := Nat.mul_le_mul_left r hdhave m := le_max_left (q-r) (q-(k-r))dsimp [count]; simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *; omega· by_cases he : d=q· subst dhave m := le_max_left (q-r) (q-(k-r))dsimp [count]; simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *; omega· have hq : q+1 ≤ d := by omegahave u := Nat.mul_le_mul_left (k-r) hqhave v := Nat.mul_le_mul_left r hqhave m := le_max_right (q-r) (q-(k-r))dsimp [count]; simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *; omegatheorem strip_hits {k q r : ℕ} (hr : r ≤ k) : Hits ((k+1)*q+r) k (strip k q r) := byintro a d hobtain ⟨lo,hi⟩ := pivot_covered hr hrefine ⟨a+r*d, Finset.mem_image.mpr ⟨a+r*d-r*(q+1), Finset.mem_range.mpr (by omega), by omega⟩, ?_⟩exact Finset.mem_image.mpr ⟨r, Finset.mem_range.mpr (by omega), rfl⟩/-- A set hitting a family of blocked progressions with distinct initial residues has at leastas many elements as that family. -/private theorem packing_bound {N k o d m : ℕ} (T : Finset ℕ) (ht : Hits N k T)(hb : ∀ i < m, Blocked N k (o+i) d) : m ≤ T.card := byclassicalhave h : ∀ i : Fin m, ∃ x ∈ T, x ∈ progression (o+i.val) d k := fun i => ht _ _ (hb _ i.isLt)choose f hf hg using hlet g : Fin m → ↥T := fun i => ⟨f i,hf i⟩have res (i : Fin m) : f i % d = o+i.val := byobtain ⟨j,_,hj⟩ := Finset.mem_image.mp (hg i)rw [← hj, Nat.add_mul_mod_self_right, Nat.mod_eq_of_lt (hb i.val i.isLt).2.2.1]have inj : Function.Injective g := byintro i j hijhave he : f i = f j := congrArg Subtype.val hijhave := (res i).symm.trans ((congrArg (· % d) he).trans (res j))exact Fin.ext (by omega)simpa using Fintype.card_le_of_injective g injtheorem exact_cover_lower {k q r : ℕ} (hr : r ≤ k) (T : Finset ℕ)(ht : Hits ((k+1)*q+r) k T) : count k q r ≤ T.card := byapply max_le· apply packing_bound (o:=r) (d:=q) T htintro i hiunfold Blockedsimp only [Nat.add_mul, Nat.one_mul]omega· apply packing_bound (o:=0) (d:=q+1) T htintro i hiunfold Blockedsimp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul,Nat.zero_add]omegatheorem sharp_transversal (N L : ℕ) (hL : 2 ≤ L) :∃ T : Finset ℕ, Hits N (L-1) T ∧ T.card = count (L-1) (N/L) (N%L) ∧∀ S : Finset ℕ, Hits N (L-1) S → T.card ≤ S.card := bylet k := L-1let q := N/Llet r := N%Lhave hk : k+1=L := by dsimp [k]; omegahave hr : r≤k := by have := Nat.mod_lt N (by omega : 0<L); dsimp [r,k]; omegahave hn : (k+1)*q+r=N := by rw [hk]; exact Nat.div_add_mod N Lrefine ⟨strip k q r, hn ▸ strip_hits hr, card_strip .., ?_⟩intro S hsrw [card_strip]exact exact_cover_lower hr S (hn.symm ▸ hs)end ProgressionColours