import Mathlib.Algebra.BigOperators.Group.Finset.Basicimport Mathlib.Data.List.Countimport Mathlib.Tactic/-! Counting assignments subject to a per-label capacity. -/namespace Erdos824Greedyopen Finsetopen scoped BigOperatorsprivate theorem sum_counts_le {α : Type*} [DecidableEq α] (S : Finset α) (xs : List α) :∑ x ∈ S, xs.count x ≤ xs.length := byinduction xs with| nil => simp| cons a xs ih =>simp only [List.count_cons, List.length_cons, Finset.sum_add_distrib]by_cases ha : a ∈ S <;> simp [beq_iff_eq, ha] at * <;> omega/-- At least r choices remain whenever the alphabet has at least r+n/bmembers and n positions have been assigned. -/theorem room {α : Type*} [DecidableEq α] (S : Finset α) (xs : List α)(b r : ℕ) (hb : 0 < b) (hS : r + xs.length / b ≤ S.card) :r ≤ (S.filter fun x => xs.count x < b).card := bylet full := S.filter fun x => b ≤ xs.count xhave hh : full.card * b ≤ xs.length := calc_ = ∑ _x ∈ full, b := by simp_ ≤ ∑ x ∈ full, xs.count x := Finset.sum_le_sum fun x hx => (Finset.mem_filter.mp hx).2_ ≤ xs.length := sum_counts_le full xshave hfull : full.card ≤ xs.length / b := (Nat.le_div_iff_mul_le hb).mpr hhhave heq : (S.filter fun x => xs.count x < b).card + full.card = S.card := bysimpa [full, not_lt] using S.card_filter_add_card_filter_not (fun x => xs.count x < b)omega/-- The assignments in reverse order, with coordinate i drawn from S i. -/def assignments {α : Type*} [DecidableEq α] (S : ℕ → Finset α) (b : ℕ) : ℕ → Finset (List α)| 0 => {[]}| i+1 => (assignments S b i).biUnion fun xs =>((S i).filter fun x => xs.count x < b).image (fun x => x::xs)theorem assignment_length {α : Type*} [DecidableEq α] {S : ℕ → Finset α} {b n : ℕ}{xs} (h : xs ∈ assignments S b n) : xs.length = n := byinduction n generalizing xs with| zero =>have : xs = [] := by simpa [assignments] using hsimp [this]| succ n ih =>obtain ⟨ys,hy,hx⟩ := Finset.mem_biUnion.mp hobtain ⟨a,_,rfl⟩ := Finset.mem_image.mp hxsimp [ih hy]theorem assignment_counts {α : Type*} [DecidableEq α] {S : ℕ → Finset α} {b n : ℕ}{xs} (h : xs ∈ assignments S b n) : ∀ x, xs.count x ≤ b := byinduction n generalizing xs with| zero =>have : xs = [] := by simpa [assignments] using hsimp [this]| succ n ih =>obtain ⟨ys,hy,hx⟩ := Finset.mem_biUnion.mp hobtain ⟨a,ha,rfl⟩ := Finset.mem_image.mp hxhave hc := (Finset.mem_filter.mp ha).2intro xby_cases hxa : x = a· subst xsimpomega· simpa [List.count_cons, hxa, Ne.symm hxa] using ih hy xtheorem many_assignments {α : Type*} [DecidableEq α] (S : ℕ → Finset α) (b r n : ℕ)(hb : 0 < b) (hS : ∀ i<n, r + i/b ≤ (S i).card) :r^n ≤ (assignments S b n).card := byinduction n with| zero => simp [assignments]| succ n ih =>let F (xs : List α) := ((S n).filter fun x => xs.count x < b).image fun x => x::xshave hdis : ((assignments S b n : Finset (List α)) : Set (List α)).PairwiseDisjoint F := byintro xs _ ys _ hnerefine Finset.disjoint_left.mpr ?_intro ls hl hrobtain ⟨_,_,rfl⟩ := Finset.mem_image.mp hlobtain ⟨_,_,he⟩ := Finset.mem_image.mp hrexact hne (List.cons.inj he).2.symmhave hc (xs : List α) (hx : xs ∈ assignments S b n) : r ≤ (F xs).card := byrw [show (F xs).card = _ from Finset.card_image_of_injective _ (fun _ _ h => (List.cons.inj h).1)]apply room _ _ _ _ hbrw [assignment_length hx]exact hS n (by omega)rw [pow_succ, assignments, Finset.card_biUnion hdis]calc_ ≤ (assignments S b n).card * r :=Nat.mul_le_mul_right r (ih fun i hi => hS i (by omega))_ = ∑ _xs ∈ assignments S b n, r := by simp_ ≤ _ := Finset.sum_le_sum hc#print axioms many_assignmentsend Erdos824Greedy