Lean / Reader
Lean reader

ProgressionColours.lean

101 lines11 declarationsno sorryimports Mathlib.Data.Fintype.Card Mathlib.Data.Finset.Card Mathlib.Tactic.Linarith
  1. import Mathlib.Data.Fintype.Card
  2. import Mathlib.Data.Finset.Card
  3. import Mathlib.Tactic.Linarith
  4. namespace ProgressionColours
  5. /-- The arithmetic progression with `k+1` terms. Positions start at zero. -/
  6. def progression (a d k : ℕ) : Finset ℕ := (Finset.range (k+1)).image (fun j => a+j*d)
  7. /-- This progression is inside `[0,N)` and extending it at either end leaves that interval. -/
  8. def Blocked (N k a d : ℕ) : Prop :=
  9. 0 < d ∧ a+k*d < N ∧ a < d ∧ N ≤ a+(k+1)*d
  10. def Hits (N k : ℕ) (T : Finset ℕ) : Prop :=
  11. ∀ a d, Blocked N k a d → ∃ x ∈ T, x ∈ progression a d k
  12. /-- `N = (k+1)*q+r`, with `r ≤ k`. Natural subtraction truncates at zero. -/
  13. def count (k q r : ℕ) := max (q-r) (q-(k-r))
  14. def strip (k q r : ℕ) : Finset ℕ :=
  15. (Finset.range (count k q r)).image (fun i => r*(q+1)+i)
  16. theorem card_strip (k q r : ℕ) : (strip k q r).card = count k q r := by
  17. simp [strip,Finset.card_image_of_injective, Function.Injective]
  18. theorem pivot_covered {k q r a d : ℕ} (hr : r ≤ k)
  19. (h : Blocked ((k+1)*q+r) k a d) :
  20. r*(q+1) ≤ a+r*d ∧ a+r*d < r*(q+1)+count k q r := by
  21. obtain ⟨_,hin,hl,hh⟩ := h
  22. have ee : ∀ t : ℕ, (k-r)*t+r*t=k*t := fun t => by rw [←Nat.add_mul, Nat.sub_add_cancel hr]
  23. have el := ee d
  24. have eq := ee q
  25. rcases Nat.le_total d q with hd | hd
  26. · have u := Nat.mul_le_mul_left (k-r+1) hd
  27. have v := Nat.mul_le_mul_left r hd
  28. have m := le_max_left (q-r) (q-(k-r))
  29. dsimp [count]; simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *; omega
  30. · by_cases he : d=q
  31. · subst d
  32. have m := le_max_left (q-r) (q-(k-r))
  33. dsimp [count]; simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *; omega
  34. · have hq : q+1 ≤ d := by omega
  35. have u := Nat.mul_le_mul_left (k-r) hq
  36. have v := Nat.mul_le_mul_left r hq
  37. have m := le_max_right (q-r) (q-(k-r))
  38. dsimp [count]; simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul] at *; omega
  39. theorem strip_hits {k q r : ℕ} (hr : r ≤ k) : Hits ((k+1)*q+r) k (strip k q r) := by
  40. intro a d h
  41. obtain ⟨lo,hi⟩ := pivot_covered hr h
  42. refine ⟨a+r*d, Finset.mem_image.mpr ⟨a+r*d-r*(q+1), Finset.mem_range.mpr (by omega), by omega⟩, ?_⟩
  43. exact Finset.mem_image.mpr ⟨r, Finset.mem_range.mpr (by omega), rfl⟩
  44. /-- A set hitting a family of blocked progressions with distinct initial residues has at least
  45. as many elements as that family. -/
  46. private theorem packing_bound {N k o d m : ℕ} (T : Finset ℕ) (ht : Hits N k T)
  47. (hb : ∀ i < m, Blocked N k (o+i) d) : m ≤ T.card := by
  48. classical
  49. have h : ∀ i : Fin m, ∃ x ∈ T, x ∈ progression (o+i.val) d k := fun i => ht _ _ (hb _ i.isLt)
  50. choose f hf hg using h
  51. let g : Fin m → ↥T := fun i => ⟨f i,hf i⟩
  52. have res (i : Fin m) : f i % d = o+i.val := by
  53. obtain ⟨j,_,hj⟩ := Finset.mem_image.mp (hg i)
  54. rw [← hj, Nat.add_mul_mod_self_right, Nat.mod_eq_of_lt (hb i.val i.isLt).2.2.1]
  55. have inj : Function.Injective g := by
  56. intro i j hij
  57. have he : f i = f j := congrArg Subtype.val hij
  58. have := (res i).symm.trans ((congrArg (· % d) he).trans (res j))
  59. exact Fin.ext (by omega)
  60. simpa using Fintype.card_le_of_injective g inj
  61. theorem exact_cover_lower {k q r : ℕ} (hr : r ≤ k) (T : Finset ℕ)
  62. (ht : Hits ((k+1)*q+r) k T) : count k q r ≤ T.card := by
  63. apply max_le
  64. · apply packing_bound (o:=r) (d:=q) T ht
  65. intro i hi
  66. unfold Blocked
  67. simp only [Nat.add_mul, Nat.one_mul]
  68. omega
  69. · apply packing_bound (o:=0) (d:=q+1) T ht
  70. intro i hi
  71. unfold Blocked
  72. simp only [Nat.add_mul,Nat.mul_add,Nat.mul_one,Nat.one_mul,Nat.zero_add]
  73. omega
  74. theorem sharp_transversal (N L : ℕ) (hL : 2 ≤ L) :
  75. ∃ T : Finset ℕ, Hits N (L-1) T ∧ T.card = count (L-1) (N/L) (N%L) ∧
  76. ∀ S : Finset ℕ, Hits N (L-1) S → T.card ≤ S.card := by
  77. let k := L-1
  78. let q := N/L
  79. let r := N%L
  80. have hk : k+1=L := by dsimp [k]; omega
  81. have hr : r≤k := by have := Nat.mod_lt N (by omega : 0<L); dsimp [r,k]; omega
  82. have hn : (k+1)*q+r=N := by rw [hk]; exact Nat.div_add_mod N L
  83. refine ⟨strip k q r, hn ▸ strip_hits hr, card_strip .., ?_⟩
  84. intro S hs
  85. rw [card_strip]
  86. exact exact_cover_lower hr S (hn.symm ▸ hs)
  87. end ProgressionColours