Goedel-LM/MathOlympiadBench huggingface dataset:
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 N6

A multiset $S$ of positive rational numbers is called *nice* if
  both $\sum_{q ∈ S} q$ and $\sum_{q ∈ S} 1/q$ are integers.
Find all $n ∈ ℕ$ such that there exists infinitely many nice multisets $S$ of size $n$.
-/
/- special open -/ open Multiset
structure nice (S : Multiset ℚ) : Prop where
  pos : ∀ q : ℚ, q ∈ S → 0 < q
  sum_eq_int : ∃ k : ℤ, S.sum = k
  sum_inv_eq_int : ∃ k : ℤ, (S.map (·⁻¹)).sum = k

def good (n : ℕ) := {S : Multiset ℚ | card S = n ∧ nice S}.Infinite


theorem imo_sl_2017_N6 : good n ↔ 3 ≤ n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2018 A2 (P2)

Let $R$ be a totally ordered commutative ring.
Find all periodic sequences $(a_k)_{k ≥ 0}$ such that for any $k ≥ 0$,
$$ a_{k + 2} = a_k a_{k + 1} + 1. $$

Original problem: Find all possible periods of such sequence.
-/
/- special open -/ open Finset
def good [NonAssocSemiring R] (a : ℕ → R) := ∀ k, a (k + 2) = a k * a (k + 1) + 1

variable (R) [NonAssocRing R] (d : Fin 3)

def stdGoodSeq : ℕ → R := λ n ↦ ![2, -1, -1] (n + d)

variable [LinearOrderedCommRing R] {a : ℕ → R} (ha : good a) (hn : a.Periodic (n + 1))

theorem imo_sl_2018_A2 [LinearOrderedCommRing R] {n : ℕ} :
    (∃ a : ℕ → R, good a ∧ a.Periodic (n + 1)) ↔ 3 ∣ n + 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2018 C1

Let $n ≥ 4$ be an integer and $S ⊆ ℕ^+$.
We say that $S$ is *good* if for each $m ∈ ℕ$ with $2 ≤ m ≤ n - 2$, there exists
  $T ⊆ S$ of size $m$ such that the sum of all elements in $T$ and $S \ T$ are equal.
Prove that for any $n ≥ 4$, there exists a good set of size $n$.
-/
/- special open -/ open Finset
def good (S : Finset ℕ) :=
  ∀ m ≥ 2, m + 2 ≤ S.card → ∃ T ⊆ S, T.card = m ∧ T.sum id = (S \ T).sum id

theorem imo_sl_2018_C1 (n : ℕ) : ∃ S : Finset ℕ, S.card = n ∧ (∀ x ∈ S, 0 < x) ∧ good S := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2018 N2

Let $n$ and $k$ be positive integers.
Consider an $k × k$ table, where each cell contains an integer $1 \pmod{n}$.
Suppose that the sum of all numbers in an arbitrary row or column is $k \pmod{n^2}$.
For each $i ≤ n$, let $R_i$ and $C_i$ be the product of
  numbers in the $i$th row and $i$th column, respectively.
Prove that
$$ \sum_{i = 1}^n R_i ≡ \sum_{i = 1}^n C_i \pmod{n^4}. $$
-/
/- special open -/ open Finset
theorem imo_sl_2018_N2 {n : ℤ} {f : ι → ι → ℤ} (h : ∀ i ∈ S, ∀ j ∈ S, f i j ≡ 1 [ZMOD n])
    (h0 : ∀ i ∈ S, ∑ j ∈ S, f i j ≡ S.card [ZMOD n ^ 2])
    (h1 : ∀ j ∈ S, ∑ i ∈ S, f i j ≡ S.card [ZMOD n ^ 2]) :
    (S.sum λ i ↦ ∏ j ∈ S, f i j) ≡ (S.sum λ j ↦ ∏ i ∈ S, f i j) [ZMOD n ^ 4] := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2018 N4 (P5)

Let $(a_n)_{n ≥ 1}$ be a sequence of positive integers such that for $n$ large enough,
$$ \frac{a_1}{a_2} + \frac{a_2}{a_3} + … + \frac{a_{n - 1}}{a_n} + \frac{a_n}{a_1} ∈ ℤ. $$
Prove that $(a_n)_{n ≥ 1}$ is eventually constant.
-/
/- special open -/ open Finset
theorem imo_sl_2018_N4 {a : ℕ → ℕ} (ha : ∀ n, 0 < a n)
    (ha0 : ∃ K, ∀ n ≥ K, ∃ z : ℤ, (z : ℚ) =
      (∑ i ∈ Finset.range n, (a i : ℚ) / a (i + 1)) + a n / a 0) :
    ∃ C N, ∀ n, a (n + N) = C := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2018 N5

Determine whether there exists $x, y, z, t ∈ ℕ^+$ such that
  $xy - zt = x + y = z + t$ and both $xy$ and $zt$ are perfect squares.
-/
/- special open -/ open Finset
def good (v : Fin 4 → ℤ) := v 0 * v 1 - v 2 * v 3 = v 0 + v 1 ∧ v 0 + v 1 = v 2 + v 3

variable (hv : good v)

theorem imo_sl_2018_N5 (hv0 : ∀ i, v i ≠ 0) :
    ¬((∃ x, v 0 * v 1 = x ^ 2) ∧ ∃ y, v 2 * v 3 = y ^ 2) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2018 N6

Let $f : \mathbb{N}^+ \to \mathbb{N}^+$ be a function such that $f(m + n)$ divides $f(m) + f(n)$
for all $m, n \in \mathbb{N}^+$.

Prove that there exists $n_0 \in \mathbb{N}^+$ such that $f(n_0)$ divides $f(n)$ for all
$n \in \mathbb{N}^+$.
-/
def IsGood (f : ℕ+ → ℕ+) : Prop :=
  ∀ m n, f (m + n) ∣ f m + f n

theorem imo_sl_2018_N6 (f : ℕ+ → ℕ+) (hf : IsGood f) :
  ∃ n₀, ∀ n, f n₀ ∣ f n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2019 A1 (P1)

Fix an integer $N ≠ 0$.
Find all functions $f : ℤ → ℤ$ such that, for any $a, b ∈ ℤ$,
$$ f(Na) + N f(b) = f(f(a + b)). $$
-/
theorem imo_sl_2019_A1 (h : N ≠ 0) {f : Int → Int} :
    (∀ a b : Int, f (N * a) + N * f b = f (f (a + b)))
      ↔ (f = λ _ ↦ 0) ∨ ∃ c : Int, f = (N * · + c) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2019 A2

Let $R$ be a totally ordered ring and $x_1, x_2, …, x_n ∈ R$ be elements with
$$ x_1 + x_2 + … + x_n = 0. $$
Let $a, b ∈ R$ such that $b ≤ x_i ≤ a$ for all $i ≤ n$.
Show that $$ nab + \sum_{i = 1}^n x_i^2 ≤ 0. $$
-/
/- special open -/ open Multiset
theorem imo_sl_2019_A2 [LinearOrderedCommRing R]
    {a b : R} {M : Multiset R} (hM : M.sum = 0) (ha : ∀ x ∈ M, x ≤ a) (hb : ∀ x ∈ M, b ≤ x) :
    card M • (a * b) + (M.map λ x ↦ x ^ 2).sum ≤ 0 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2019 C2

Let $G$ be a totally ordered abelian group, and fix a non-negative element $g \in G$.
For a multiset $S$ of elements of $G$, let $\Sigma_S$ denote the sum of the elements of $S$,
counting multiplicity.

Let $S$ be a multiset of elements of $G$ such that $\Sigma_S \le 2|S|g$. Suppose that
each element of $S$ is greater than or equal to $g$.

Prove that for any $r \in G$ with $-2g \le r \le \Sigma_S$, there exists a sub-multiset
$S'$ of $S$ such that $r \le \Sigma_{S'} \le r + 2g$.
-/
/- special open -/ open Multiset
theorem imo_sl_2019_C2 [LinearOrderedAddCommGroup G] (g : G) (hg : 0 ≤ g) (S : Multiset G)
    (hS_elems : ∀ x ∈ S, g ≤ x)
    (hS_sum : S.sum ≤ (2 * card S) • g)
    (r : G) (hr_lower : -(2 • g) ≤ r) (hr_upper : r ≤ S.sum) :
    ∃ T ≤ S, r ≤ T.sum ∧ T.sum ≤ r + 2 • g := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2019 N3

A set $S ⊆ ℤ$ is called *rootiful* if for any $a_0, a_1, … a_n ∈ S$, not all zero,
  and $x ∈ ℤ$ such that $a_0 + a_1 x + … + a_n x^n = 0$, we have $x ∈ S$.

Fix an integer $N$ with $|N| > 1$.
Find all rootiful sets containing $N^{a + 1} - N^{b + 1}$ for all $a, b ∈ ℕ$.
-/
/- special open -/ open List
def rootiful (S : Set ℤ) :=
  ∀ (x : ℤ) (P : List ℤ) (_ : ∀ a : ℤ, a ∈ P → a ∈ S) (_ : ∃ a : ℤ, a ∈ P ∧ a ≠ 0),
    P.foldr (· + x * ·) 0 = 0 → x ∈ S

theorem imo_sl_2019_N3 {N : ℤ} (h : 1 < |N|) {S : Set ℤ} :
    (rootiful S ∧ ∀ a b : ℕ, N ^ (a + 1) - N ^ (b + 1) ∈ S) ↔ S = Set.univ := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2019 N4

Fix some $C ∈ ℕ$.
Find all functions $f : ℕ → ℕ$ such that $a + f(b) ∣ a^2 + b f(a)$
  for any $a, b ∈ ℕ$ satisfying $a + b > C$.
-/
/- special open -/ open List
def goodPNat (C : ℕ+) (f : ℕ+ → ℕ+) :=
  ∀ a b : ℕ+, C < a + b → a + f b ∣ a ^ 2 + b * f a

theorem imo_sl_2019_N4 : goodPNat C f ↔ ∃ k : ℕ+, f = (k * ·) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2020 A3

Let $a, b, c, d$ be positive real numbers such that $(a + c)(b + d) = ac + bd$.
Find the smallest possible value of
$$ \frac{a}{b} + \frac{b}{c} + \frac{c}{d} + \frac{d}{a}. $$
-/
variable {F : Type*} [LinearOrderedField F]

def IsGood (a b c d : F) : Prop :=
  (a + c) * (b + d) = a * c + b * d

def targetVal (a b c d : F) : F :=
  a / b + b / c + c / d + d / a

theorem imo_sl_2020_A3 :
  (∀ a b c d : F, 0 < a → 0 < b → 0 < c → 0 < d → IsGood a b c d → (8 : F) ≤ targetVal a b c d) ∧
  (∃ a b c d : F, 0 < a ∧ 0 < b ∧ 0 < c ∧ 0 < d ∧ IsGood a b c d ∧ targetVal a b c d = (8 : F)) :=
  by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2020 A4

Let $a, b, c, d$ be real numbers with $a \ge b \ge c \ge d > 0$ and $a + b + c + d = 1$.
Prove that
$$ (a + 2b + 3c + 4d) a^a b^b c^c d^d < 1. $$
-/
/- special open -/ open NNReal
theorem imo_sl_2020_A4 (a b c d : NNReal)
    (h_ord : a ≥ b ∧ b ≥ c ∧ c ≥ d ∧ d > 0)
    (h_sum : a + b + c + d = 1) :
    (a + 2 * b + 3 * c + 4 * d) *
      (a.toReal ^ a.toReal * b.toReal ^ b.toReal * c.toReal ^ c.toReal * d.toReal ^ d.toReal) < 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2020 A6

Find all functions $f : ℤ → ℤ$ such that, for any $a, b ∈ ℤ$,
$$ f^{a^2 + b^2}(a + b) = a f(a) + b f(b). $$
-/
/- special open -/ open Function
def good (f : ℤ → ℤ) := ∀ a b, f^[a.natAbs ^ 2 + b.natAbs ^ 2] (a + b) = a * f a + b * f b

theorem imo_sl_2020_A6 {f : ℤ → ℤ} : good f ↔ f = (· + 1) ∨ f = λ _ ↦ 0 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2020 N1

Prove that, for any positive integer $k$, there exists a prime $p$ and
  distinct elements $x_1, x_2, …, x_{k + 3} \in 𝔽_p^×$ such that for all $i ≤ k$,
$$ x_i x_{i + 1} x_{i + 2} x_{i + 3} = i. $$
-/
/- special open -/ open Function
abbrev ratSeq : ℕ → ℚ
  | 0 => 2
  | 1 => 2⁻¹
  | 2 => -4
  | 3 => -4⁻¹
  | n + 4 => (1 + (n.succ : ℚ)⁻¹) * ratSeq n

theorem imo_sl_2020_N1 (k : ℕ) :
    ∃ (p : ℕ) (_ : p.Prime) (a : Fin (k + 4) → ZMod p), a.Injective ∧ (∀ i, a i ≠ 0) ∧
        (∀ i ≤ k, a i * a (i + 1) * a (i + 2) * a (i + 3) = i.succ) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 A1

Let $n$ be an integer and $A$ be a subset of $\{0, 1, …, 5^n\}$ of size $4n + 2$.
Prove that there exists $a, b, c ∈ A$ such that $a < b < c$ and $c + 2a > 3b$.
-/
/- special open -/ open List
theorem imo_sl_2021_A1 (hn : n ≠ 0) {A : Finset ℕ}
    (hA : A.card = 4 * n + 2) (hA0 : ∀ a ∈ A, a ≤ 5 ^ n) :
    ∃ a ∈ A, ∃ b ∈ A, ∃ c ∈ A, a < b ∧ b < c ∧ 3 * b < c + 2 * a := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 A2

For any positive integer $n$, prove that
$$ 4 \sum_{i = 1}^n \sum_{j = 1}^n \left\lfloor \frac{ij}{n + 1} \right\rfloor
  ≥ n^2 (n - 1). $$
Determine the equality cases.
-/
/- special open -/ open Finset
abbrev targetSum (n : ℕ) := 4 * ∑ i ∈ range n, ∑ j ∈ range n, (i + 1) * (j + 1) / (n + 1)

theorem imo_sl_2021_A2 (hn : n ≠ 0) : targetSum n = n ^ 2 * (n - 1) ↔ (n + 1).Prime := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 A3

Find the smallest possible value of
$$ \sum_{j = 1}^n \left\lfloor \frac{a_j}{j} \right\rfloor $$
  across all permutations $(a_1, a_2, \ldots, a_n)$ of $(1, 2, \ldots, n)$.
-/
/- special open -/ open List
def targetSum : List ℕ → ℕ
  | [] => 0
  | a :: l => a / (a :: l).length + targetSum l

theorem imo_sl_2021_A3 :
    IsLeast (targetSum '' {l : List ℕ | l ~ (List.range' 1 n).reverse}) n.size := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
### IMO 2021 A5

Let $F$ be a totally ordered field.
Let $a_1, a_2, …, a_n ∈ F$ be non-negative elements.
Let $r ∈ F$ be any positive element such that $r ≥ a_1 + a_2 + … + a_n$.
Prove that
$$ \sum_{k = 1}^n \frac{a_k}{r - a_k} (a_1 + a_2 + … + a_{k - 1})^2 < \frac{r^2}{3}. $$
-/
def targetSumPair [Field F] (r : F) (l : List F) : F × F :=
  l.foldr (λ a p ↦ (a / (r - a) * p.2 ^ 2 + p.1, a + p.2)) (0, 0)

theorem imo_sl_2021_A5 [LinearOrderedField F]
    {r : F} (hr : 0 < r) (l : List F)
    (hl : ∀ x ∈ l, 0 ≤ x) (h : l.foldr (· + ·) 0 ≤ r) :
    (targetSumPair r l).1 < r ^ 2 / 3 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 A6 (P6)

Let $m ∈ ℕ$ and $a_0, a_1, …, a_{k - 1}$ be integers.
Suppose that there exists subsets $B_0, B_1, …, B_{m - 1}$ of $[k]$
  such that for each $i ∈ [m]$, $$ \sum_{j ∈ B_i} a_j = m^{i + 1}. $$
Prove that $k ≥ m/2$.
-/
/- special open -/ open Finset
variable [Fintype κ] [DecidableEq κ] {a : κ → ℤ}

theorem imo_sl_2021_A6 {a : κ → ℤ} {B : Fin m → Finset κ}
    [∀ i j, Decidable (j ∈ B i)] (h : ∀ i : Fin m, (B i).sum a = m ^ (i.1 + 1)) :
    m ≤ 2 * Fintype.card κ := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 A7

Let $R$ be a totally ordered commutative ring.
Let $(x_n)_{n ≥ 0}$ be a sequence of elements of $R$ such that, for each $n ∈ ℕ$,
$$ x_{n + 1} x_{n + 2} ≥ x_n^2 + 1. $$
Show that for any $N ∈ ℕ$,
$$ 27 (x_0 + x_1 + … + x_{N + 1})^2 > 8 N^3. $$
-/
/- special open -/ open Finset
variable [LinearOrderedField R] [ExistsAddOfLE R]

theorem imo_sl_2021_A7 {x : ℕ → R} (hx : ∀ n, 0 ≤ x n)
    (hx0 : ∀ n, x n ^ 2 + 1 ≤ x (n + 1) * x (n + 2)) (N) :
    8 * N ^ 3 < 27 * (range (N + 2)).sum x ^ 2 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 C1

Consider a complete graph with an infinite vertex set $V$.
Each edge $xy$ is coloured such that for each vertex $v$, there exists only
  finitely many colours assigned to an edge incident with $v$.
Prove that if some of the edges has distinct colours, then there exists
  $x, y, z ∈ V$, pairwise distinct, such that $c_{xy} = c_{xz} ≠ c_{yz}$.
-/
structure FiniteIncidenceColouring (V α : Type*) where
  colour : V → V → α
  colour_symm (x y : V) : colour x y = colour y x
  incidence_finite (v : V) : Finite (Set.range (colour v))

variable [Infinite V] (C : FiniteIncidenceColouring V α)

theorem imo_sl_2021_C1 (h : ∀ c : α, ∃ x y : V, x ≠ y ∧ C.colour x y ≠ c) :
    ∃ x y z, y ≠ z ∧ C.colour x y = C.colour x z ∧ C.colour y z ≠ C.colour x z := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 C2

Fix some positive integer $n$, and denote $[n] = \{0, 1, …, n - 1\}$.
Find all positive integers $m ∈ ℕ$ such that there exists a
  function $f : ℤ/mℤ → [n]$ with the following property:
  for any $k ∈ ℤ/mℤ$ and $i ∈ [n]$, there exists $j ≤ n$ such that $f(k + j) = i$.
-/
/- special open -/ open Finset
def good (f : Fin (m + 1) → Fin n) := ∀ k i, ∃ j ≤ n, f (k + j) = i

theorem imo_sl_2021_C2 {n m : ℕ} :
    (∃ f : Fin m.succ → Fin n.succ, good f) ↔ m.succ % n.succ ≤ m.succ / n.succ := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 N1

Find all triplets $(a, b, n)$ of positive integers such that
* $a^2 + b + 3$ is cubefree; and
* $ab + 3b + 8 = n(a^2 + b + 3)$.
-/
@[mk_iff] structure good (a b n : ℕ+) : Prop where
  cubefree : ∀ p, (a ^ 2 + b + 3).factorMultiset.count p ≤ 2
  eqn : a * b + 3 * b + 8 = n * (a ^ 2 + b + 3)

theorem imo_sl_2021_N1 :
    good a b n ↔ n = 2 ∧ ∃ k : ℕ+,
      (∀ p, (k + 2).factorMultiset.count p ≤ 1) ∧ a = k + 1 ∧ b = 2 * k := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2021 N2 (P1)

Let $n ≥ 99$ be an integer.
The non-negative integers are coloured using two colours.
Prove that there exists $a, b ∈ ℕ$ of the same colour such that
  $n ≤ a < b ≤ 2n$ and $a + b$ is a square.
-/
def good (n : ℕ) :=
  ∀ x : ℕ → Bool, ∃ a b, n ≤ a ∧ a < b ∧ b ≤ 2 * n ∧ x a = x b ∧ ∃ k, a + b = k ^ 2

theorem imo_sl_2021_N2 (h : 99 ≤ n) : good n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2022 A1

Let $R$ be a totally ordered ring.
Let $(a_n)_{n ≥ 0}$ be a sequence of non-negative elements of $R$ such that for any $n ∈ ℕ$,
$$ a_{n + 1}^2 + a_n a_{n + 2} ≤ a_n + a_{n + 2}. $$
Show that $a_N ≤ 1$ for all $N ≥ 2$.
-/
variable [LinearOrderedRing R]

theorem imo_sl_2022_A1 {a : ℕ → R} (h : ∀ i, 0 ≤ a i)
    (h0 : ∀ i, a (i + 1) ^ 2 + a i * a (i + 2) ≤ a i + a (i + 2))
    (N : ℕ) (h1 : 2 ≤ N) : a N ≤ 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2022 A3 (P2)

Let $R$ be a totally ordered commutative ring, and let $R_{>0} = \{x ∈ R : x > 0\}$.
Find all functions $f : R_{>0} → R_{>0}$ such that for any $x ∈ R_{>0}$,
  there exists a unique $y ∈ R_{>0}$ such that $x f(y) + y f(x) ≤ 2$.
-/
variable [LinearOrderedField R]

def good (f : {x : R // 0 < x} → {x : R // 0 < x}) :=
  ∀ x, ∃! y, x * f y + y * f x ≤ ⟨2, two_pos⟩

theorem imo_sl_2022_A3 [ExistsAddOfLE R] {f : {x : R // 0 < x} → {x : R // 0 < x}} :
    good f ↔ ∀ x, x * f x = 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2022 A6

Let $G$ be a commutative group.
A function $f : G → G$ is called *infectious* if
$$ f(x + f(y)) = f(x) + f(y) \quad ∀ x, y ∈ G. $$
Find all pairs $(m, n)$ of integers such that for any infectious functions
  $f : G → G$, there exists $z ∈ G$ such that $m f(z) = nz$.
-/
structure InfectiousFun (G) [Add G] where
  toFun : G → G
  infectious_def' : ∀ x y, toFun (x + toFun y) = toFun x + toFun y

instance [Add G] : FunLike (InfectiousFun G) G G where
  coe f := f.toFun
  coe_injective' f g h := by rwa [InfectiousFun.mk.injEq]

def good (G) [AddGroup G] (m n : ℤ) := ∀ f : InfectiousFun G, ∃ z, m • f z = n • z

theorem imo_sl_2022_A6 [AddCommGroup G] :
    good G m n ↔ ∀ g : G, (m - n).gcd (addOrderOf g) ∣ m.natAbs := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2022 C7

Let $m$ be a positive integer and consider an arbitrary subset $S \subseteq \mathbb{Z}^m$.
We say that $S$ is *add-sup closed* if for any $v, w \in S$, their pointwise sum $v+w$ and
their pointwise maximum (or sup) $v \lor w$ are also in $S$.
A set $G \subseteq \mathbb{Z}^m$ is called an *add-sup generator* if the only add-sup
closed set containing $G$ is $\mathbb{Z}^m$ itself.

Find the smallest possible size of an add-sup generator, in terms of $m$.
-/
/- special open -/ open Finset Classical
class IsAddSupClosed {m : ℕ} (S : Set (Fin m → ℤ)) : Prop where
  add_mem : ∀ {v w}, v ∈ S → w ∈ S → v + w ∈ S
  sup_mem : ∀ {v w}, v ∈ S → w ∈ S → v ⊔ w ∈ S

def IsAddSupGenerator {m : ℕ} (G : Finset (Fin m → ℤ)) : Prop :=
  ∀ S : Set (Fin m → ℤ), ↑G ⊆ S → IsAddSupClosed S → S = Set.univ

def IsGoodSize (m n : ℕ) : Prop :=
  ∃ G : Finset (Fin m → ℤ), G.card ≤ n ∧ IsAddSupGenerator G

theorem imo_sl_2022_C7 (m n : ℕ) :
  IsGoodSize m n ↔ n ≥ (if m = 0 then 1 else if m ≤ 2 then 2 else 3) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2022 N8

Given $n ∈ ℕ$ such that $2^n + 65 ∣ 5^n - 3^n$, prove that $n = 0$.
-/
theorem imo_sl_2022_N8 (h : 5 ^ n ≡ 3 ^ n [MOD 2 ^ n + 65]) : n = 0 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2023 A2

Let $G$ be a $2$-divisible abelian group and $R$ be a totally ordered ring.
Let $f : G → R$ be a function such that
* $f(x + y) f(x - y) ≥ f(x)^2 - f(y)^2$ for all $x, y ∈ G$,
* $f(x_0 + y_0) f(x_0 - y_0) > f(x_0)^2 - f(y_0)^2$ for some $x_0, y_0 ∈ G$.

Prove that either $f ≥ 0$ or $f ≤ 0$.
-/
theorem imo_sl_2023_A2 [AddCommGroup G] (hG : ∀ x : G, ∃ y, 2 • y = x) [LinearOrderedRing R]
    {f : G → R} (hf : ∀ x y, f x ^ 2 - f y ^ 2 ≤ f (x + y) * f (x - y))
    (hf0 : ∃ x0 y0, f x0 ^ 2 - f y0 ^ 2 < f (x0 + y0) * f (x0 - y0)) :
    (∀ x, 0 ≤ f x) ∨ (∀ x, f x ≤ 0) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2023 A3

Let $F$ be a totally ordered field and $N$ be a positive integer.
Let $x_0, x_1, \dots, x_{N-1} \in F$ be distinct positive elements.
Suppose that for each $n \in \{0, \dots, N\}$, there exists an integer $a_n \in \mathbb{N}$ such that
$$ \left(\sum_{i=0}^{n-1} x_i\right) \left(\sum_{i=0}^{n-1} \frac{1}{x_i}\right) = a_n^2. $$
Prove that $a_N \ge \lfloor 3N/2 \rfloor$.
-/
/- special open -/ open Finset
structure GoodSeq (N : ℕ) (F : Type*) [LinearOrderedField F] where
  x : ℕ → F
  a : ℕ → ℕ
  x_pos : ∀ i < N, 0 < x i
  x_inj : ∀ i < N, ∀ j < N, x i = x j → i = j
  spec : ∀ i ≤ N, (a i : F) ^ 2 = (∑ j ∈ range i, x j) * (∑ j ∈ range i, (x j)⁻¹)

theorem imo_sl_2023_A3 {N : ℕ} {F : Type*} [LinearOrderedField F]
    (hN : 0 < N) (X : GoodSeq N F) :
  3 * N / 2 ≤ X.a N := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2023 A5

Let $N > 0$ be an integer and $(a_0, a_1, \dots, a_N)$ be a permutation of $(0, 1, \dots, N)$.
Suppose that the sequence of absolute differences $(|a_0 - a_1|, \dots, |a_{N - 1} - a_N|)$
is a permutation of $(1, 2, \dots, N)$.

Prove that $\max\{a_0, a_N\} \ge \lfloor (N + 1)/4 \rfloor + 1$.
-/
/- special open -/ open Fin
/--
A `NicePerm N` is a structure containing the permutations that satisfy the problem's conditions.
- `toPerm`: The permutation `a` of `{0, ..., N}`.
- `distPerm`: The permutation of `{1, ..., N}` given by the absolute differences.
- `distPerm_spec`: The proof that the differences `|aᵢ - aᵢ₊₁|` match the `distPerm`.
-/
structure NicePerm (N : ℕ) where
  toPerm : Equiv (Fin (N + 1)) (Fin (N + 1))
  distPerm : Equiv (Fin N) (Fin N)
  distPerm_spec : ∀ i : Fin N,
    Nat.dist (toPerm i.castSucc).val (toPerm i.succ).val = (distPerm i).val + 1

theorem imo_sl_2023_A5 (N : ℕ) (p : NicePerm N) :
  (if N = 0 then 0 else (N + 1) / 4 + 1) ≤ max (p.toPerm (last N)).val (p.toPerm 0).val := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2023 N2

Find all pairs $(a, p) ∈ ℕ^2$ with $a > 0$ and $p$ prime
  such that $p^a + a^4$ is a perfect square.
-/
def good (a p : ℕ) := ∃ b, p ^ a + a ^ 4 = b ^ 2

theorem imo_sl_2023_N2 {a p : ℕ} (ha : 0 < a) (hp : p.Prime) :
    good a p ↔ p = 3 ∧ (a = 1 ∨ a = 2 ∨ a = 6 ∨ a = 9) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2023 N3

For any positive integer $n$ and $k ≥ 2$, define $ν_k(n)$
  as the largest exponent $r$ such that $k^r ∣ n$.
Prove the following:
1. there are infinitely many $n$ such that $ν_{10}(n!) > ν_9(n!)$; and
2. there are infinitely many $n$ such that $ν_{10}(n!) < ν_9(n!)$.
-/
theorem imo_sl_2023_N3 (N : ℕ) :
    (∃ n > N, padicValNat 9 n.factorial < padicValNat 10 n.factorial) ∧ 
    (∃ n > N, padicValNat 10 n.factorial < padicValNat 9 n.factorial) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2023 N4

Let $a_0, a_1, …, a_{n - 1}, b_0, b_1, …, b_{n - 1} ∈ ℕ^+$ and
  $D$ be a positive integer such that for each $i ≤ n$,
$$ b_0 b_1 … b_i a_{i + 1} … a_{n - 1} = b_0 b_1 … b_{i - 1} a_i … a_{n - 1} + D. $$
Determine the smallest possible value of $D$.
-/
/- special open -/ open Finset
structure goodSeq (n : ℕ) where
  a : ℕ → ℕ
  a_pos : ∀ i, 0 < a i
  b : ℕ → ℕ
  b_pos : ∀ i, 0 < b i
  D : ℕ
  D_pos : 0 < D
  spec : ∀ i < n, (range (i + 1)).prod b * (Ico (i + 1) n).prod a
    = (range i).prod b * (Ico i n).prod a + D

theorem imo_sl_2023_N4 (n : ℕ) : IsLeast (Set.range (goodSeq.D (n := n))) n.factorial := by sorry