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

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# International Mathematical Olympiad 2009, Problem 5

Determine all functions f: ℤ>0 → ℤ>0 such that for all positive integers a and b,
the numbers

  a, f(b), and f(b + f(a) - 1)

form the sides of a nondegenerate triangle.
-/
abbrev solution_set : Set (ℕ+ → ℕ+) := { id }

theorem imo2009_p5 (f : ℕ+ → ℕ+) :
    f ∈ solution_set ↔
    ∀ a b, (f (b + f a - 1) < f b + a ∧
            a < f b + f (b + f a - 1) ∧
            f b < f (b + f a - 1) + a) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# International Mathematical Olympiad 2023, Problem 4

Let x₁, x₂, ... x₂₀₂₃ be distinct positive real numbers.
Define

   aₙ := √((x₁ + x₂ + ... + xₙ)(1/x₁ + 1/x₂ + ... + 1/xₙ)).

Suppose that aₙ is an integer for all n ∈ {1,...,2023}.
Prove that 3034 ≤ a₂₀₂₃.
-/
noncomputable def a (x : Finset.Icc 1 2023 → ℝ) (n : Finset.Icc 1 2023) : ℝ :=
  √((∑ i ∈ Finset.univ.filter (· ≤ n), x i) *
    (∑ i ∈ Finset.univ.filter (· ≤ n), (1 / x i)))

theorem imo2023_p4
    (x : Finset.Icc 1 2023 → ℝ)
    (hxp : ∀ i, 0 < x i)
    (hxi : x.Injective)
    (hxa : ∀ i : Finset.Icc 1 2023, ∃ k : ℤ, a x i = k)
    : 3034 ≤ a x ⟨2023, by simp⟩ := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# International Mathematical Olympiad 1963, Problem 1

Find all real roots of the equation

  √(x²-p) + 2√(x²-1) = x

where *p* is a real parameter.
-/
abbrev f (p : ℝ) : Set ℝ :=
  if p ≥ 0 ∧ p ≤ (4 : ℝ) / 3
  then { (4 - p) / (2 * Real.sqrt (4 - 2 * p)) }
  else ∅

theorem imo1963_p1 : ∀ (p x : ℝ), (x ^ 2 - p) ≥ 0 → (x ^ 2 - 1) ≥ 0 →
  (Real.sqrt (x ^ 2 - p) + 2 * Real.sqrt (x ^ 2 - 1) = x ↔ (x ∈ f p)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# USA Mathematical Olympiad 2024, Problem 2

Let S₁, S₂, ..., Sₙ be finite sets of integers whose intersection
is not empty. For each non-empty T ⊆ {S₁, S₂, ..., Sₙ}, the size of
the intersection of the sets in T is a multiple of the number of
sets in T. What is the least possible number of elements that are in
at least 50 sets?
-/
abbrev solution : ℕ := 50 * Nat.choose 100 50

structure Good (S : Fin 100 → Set ℤ) : Prop where
  finite : ∀ i, (S i).Finite
  nonempty_inter : ⋂ i, S i ≠ ∅
  card : ∀ T : Finset (Fin 100), T.Nonempty →
                 ∃ k : ℕ, (⋂ i ∈ T, S i).ncard * k = T.card

-- z is in at least k of the sets S.
abbrev InAtLeastKSubsets (S : Fin 100 → Set ℤ) (k : ℕ) (z : ℤ) : Prop :=
  k ≤ {i : Fin 100 | z ∈ S i }.ncard

theorem usa2024_p2 (n : ℕ) :
    IsLeast
      { k | ∃ S, Good S ∧
             k = {z : ℤ | InAtLeastKSubsets S k z }.ncard } solution := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# International Mathematical Olympiad 2008, Problem 2

```
x^2 / (x-1)^2 + y^2 / (y-1)^2 + z^2 / (z-1)^2 ≥ 1
```

(b) Prove that equality holds above for infinitely many triples of rational numbers `x`, `y`, `z`,
each different from 1, and satisfying `xyz = 1`.
-/
def rationalSolutions :=
  {s : ℚ × ℚ × ℚ | ∃ x y z : ℚ, s = (x, y, z) ∧ x ≠ 1 ∧ y ≠ 1 ∧ z ≠ 1 ∧ x * y * z = 1 ∧
    x ^ 2 / (x - 1) ^ 2 + y ^ 2 / (y - 1) ^ 2 + z ^ 2 / (z - 1) ^ 2 = 1}

theorem imo2008_p2b : Set.Infinite rationalSolutions := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# International Mathematical Olympiad 2001, Problem 2

Let a, b, c be positive reals. Prove that

    a / √(a² + 8bc) + b / √(b² + 8ca) + c / √(c² + 8ab) ≥ 1.
-/
variable {a b c : ℝ}

theorem imo2001_p2 (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : 1 ≤
    a / Real.sqrt (a ^ 2 + 8 * b * c) + b / Real.sqrt (b ^ 2 + 8 * c * a) +
    c / Real.sqrt (c ^ 2 + 8 * a * b) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# International Mathematical Olympiad 2007, Problem 1

Real numbers a₁, a₂, ..., aₙ are fixed. For each 1 ≤ i ≤ n,
we let dᵢ = max {aⱼ : 1 ≤ j ≤ i} - min {aⱼ : i ≤ j ≤ n},
and let d = max {dᵢ : 1 ≤ i ≤ n}.

(a) Prove that for any real numbers x₁ ≤ ... ≤ xₙ, we have
   max { |xᵢ - aᵢ| : 1 ≤ i ≤ n } ≥ d / 2.
-/
noncomputable abbrev d {n : ℕ} (a : Fin n → ℝ) (i : Fin n) :=
  (⨆ j : {j // j ≤ i}, a j - ⨅ j : {j // i ≤ j}, a j)

theorem imo2007_p1a {n : ℕ} (hn : 0 < n) {a x : Fin n → ℝ} (h : Monotone x) :
    (⨆ i, d a i) / 2 ≤ ⨆ i, |x i - a i| := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# Singapore Math Olympiad (Senior) 2019 (Round 1), Problem 11

http://www.realsutra.com/limjeck/SMO_Senior_2019.pdf

Find the value of 448 * (sin 12 degrees) * (sin 39 degrees) * (sin 51 degrees) / sin 24 degrees
-/
noncomputable abbrev solution : ℝ := 112

theorem singapore2019_r1_p11 : 448 * Real.sin (12 * Real.pi / 180) * Real.sin (39 * Real.pi / 180) * Real.sin (51 * Real.pi / 180) / Real.sin (24 * Real.pi / 180) = solution := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 A1

Let $R$ be an archimedean ring with floor.
Define the function $f : R → R$ by $$ f(x) = ⌊x⌋ (x - ⌊x⌋). $$
Prove that for any $r ∈ R$, there exists $N ∈ ℕ$ such that for all $k ≥ N$,
$$ f^{k + 2}(r) = f^k(r). $$
-/
/- special open -/ open Finset
abbrev f [LinearOrderedRing R] [FloorRing R] (r : R) := ⌈r⌉ * (r - ⌈r⌉)

theorem imo_sl_2006_A1 [LinearOrderedRing R] [FloorRing R] [Archimedean R] (r : R) : 
  ∃ N, ∀ n ≥ N, f^[n + 2] r = f^[n] r := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 A2

Consider the sequence $(a_n)_{n ≥ 0}$ of rational nuimbers defined by $a_0 = 1$ and
$$ a_n = -\sum_{k = 0}^{n - 1} \frac{a_k}{n + 1 - k}. $$
Prove that $a_n > 0$ for all $n ≠ 0$.
-/
/- special open -/ open Finset
def a : ℕ → ℚ
  | 0 => -1
  | n + 1 => -(univ : Finset (Fin (n + 1))).sum λ i ↦ a i / (n + 2 - i : ℕ)

theorem imo_sl_2006_A2 (h : n ≠ 0) : 0 < a n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 A4

Let $F$ be a totally ordered field and $a_1, a_2, …, a_n ∈ F$ be positive.
Prove the inequality
$$ \sum_{i < j} \frac{a_i a_j}{a_i + a_j}
  ≤ \frac{n}{2(a_1 + a_2 + … + a_n)} \sum_{i < j} a_i a_j. $$
-/
theorem imo_sl_2006_A4 [LinearOrderedField F] [LinearOrder ι]
    (a : ι → F) {S : Finset ι} (hS : ∀ i ∈ S, 0 < a i) :
    let T := (S ×ˢ S).filter λ p ↦ p.1 < p.2
    T.sum (λ p ↦ a p.1 * a p.2 / (a p.1 + a p.2))
      ≤ S.card • T.sum (λ p ↦ a p.1 * a p.2) / (2 * S.sum a) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
International Mathematical Olympiad 2006, Problem 3

Find the smallest M ∈ ℝ such that for any a, b, c ∈ ℝ,
|ab(a² - b²) + bc(b² - c²) + ca(c² - a²)| ≤ M(a² + b² + c²)².
-/
def good [LinearOrderedCommRing R] (M : R) :=
  ∀ a b c : R, |a * b * (a ^ 2 - b ^ 2) + b * c * (b ^ 2 - c ^ 2) + c * a * (c ^ 2 - a ^ 2)|
    ≤ M * (a ^ 2 + b ^ 2 + c ^ 2) ^ 2

theorem good_iff : good M ↔ 9 * √2 ≤ 32 * M := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 N1 (P4)

Determine all pairs $(x, y) ∈ ℕ × ℤ$ such that $1 + 2^x + 2^{2x + 1} = y^2$.
-/
/- special open -/ open Finset
def good (x : ℕ) (y : ℤ) := 2 ^ (2 * x + 1) + 2 ^ x + 1 = y ^ 2

theorem imo_sl_2006_N1 :
    good x y ↔ (x = 0 ∧ (y = 2 ∨ y = -2)) ∨ (x = 4 ∧ (y = 23 ∨ y = -23)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 N3

For each $n ∈ ℕ$, define
$$ f(n) = \frac{1}{n} \sum_{k = 1}^n \left\lfloor \frac{n}{k} \right\rfloor. $$
1. Prove that $f(n + 1) > f(n)$ infinitely often.
2. Prove that $f(n + 1) < f(n)$ infinitely often.
-/
/- special open -/ open Finset
def g (n : ℕ) : ℕ := (range n).sum λ k ↦ n / (k + 1)
def f (n : ℕ) : ℚ := ((g n : ℤ) : ℚ) / ((n : ℤ) : ℚ)

theorem imo_sl_2006_N3 : {n : ℕ | f n < f n.succ}.Infinite ∧ {n : ℕ | f n.succ < f n}.Infinite := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 N5

Let $p > 3$ be a prime.
Determine all pairs $(x, y)$ of integers such that
$$ \sum_{k = 0}^{p - 1} x^k = y^{p - 2} - 1. $$
-/
/- special open -/ open Finset
theorem imo_sl_2006_N5 {p : ℕ} (hp : p.Prime) (h : 3 < p) (x y : ℤ) :
    ¬(range p).sum (x ^ ·) = y ^ (p - 2) - 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2006 N7

Prove that for any $b ∈ ℕ$ and $n ∈ ℕ^+$, there exists $m ∈ ℕ$ such that $n ∣ b^m + m$.
-/
theorem imo_sl_2006_N7 (hn : 0 < n) (b) : ∃ m, n ∣ b ^ m + m := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 A1, Part 1

Fix a linearly ordered abelian group $G$ and a positive integer $n$.
Consider a sequence $(a_i)_{i=0}^n$ of elements of $G$.

Let $(x_i)_{i=0}^n$ be a non-decreasing sequence in $G$, and let
$$ L = \max_{j \le n} |x_j - a_j|. $$
Prove that for any non-decreasing sequence $(x_i)$, the inequality $2L \ge a_k - a_m$ holds for any $k \le m \le n$.
-/
/- special open -/ open Finset
variable [LinearOrder α]

/--
The sequence `seqMax f` represents the running maximum of a sequence `f`.
`seqMax f n = max(f 0, f 1, ..., f n)`.
-/
def seqMax (f : Nat → α) : Nat → α
  | 0 => f 0
  | n + 1 => max (seqMax f n) (f n.succ)

theorem imo_sl_2007_A1a_part1 [LinearOrderedAddCommGroup G]
    (a : ℕ → G) (n : ℕ) (x : ℕ → G) (k m : ℕ)
    (h_mono : Monotone x) (h_le : k ≤ m) (h_n : m ≤ n) :
    a k - a m ≤ 2 • seqMax (λ i ↦ |x i - a i|) n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 A1, Part 2

Fix a linearly ordered abelian group $G$ and a positive integer $n$.
Consider a sequence $(a_i)_{i=0}^n$ of elements of $G$.
Let $L(x) = \max_{j \le n} |x_j - a_j|$ for a non-decreasing sequence $(x_i)$.

Prove that for any $g \in G$ such that $2g \ge a_k - a_m$ for any $k \le m \le n$,
there exists a non-decreasing sequence $(x_i)$ such that $L(x) \le g$.
-/
/- special open -/ open Finset
variable [LinearOrder α]

/--
The sequence `seqMax f` represents the running maximum of a sequence `f`.
`seqMax f n = max(f 0, f 1, ..., f n)`.
-/
def seqMax (f : Nat → α) : Nat → α
  | 0 => f 0
  | n + 1 => max (seqMax f n) (f n.succ)


theorem imo_sl_2007_A1b_part2 [LinearOrderedAddCommGroup G]
    (a : ℕ → G) (n : ℕ) (g : G)
    (h_g : ∀ k m : ℕ, k ≤ m → m ≤ n → a k - a m ≤ 2 • g) :
    ∃ x : ℕ → G, Monotone x ∧ seqMax (λ i ↦ |x i - a i|) n ≤ g := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 A2

A function $f : \mathbb{N}^+ \to \mathbb{N}^+$ is called *good* if, for any positive integers $m, n$, the following inequality holds:
$$ f(m + n) + 1 \ge f(m) + f(f(n)). $$
For any given $N \in \mathbb{N}^+$, determine all possible values of $k \in \mathbb{N}^+$ for which there exists a good function $f$ such that $f(N) = k$.

The solution states that:
- If $N=1$, the only possible value is $k=1$.
- If $N > 1$, the possible values are all $k$ such that $k \le N+1$.
-/
/- special open -/ open Finset
/--
A function `f : ℕ+ → ℕ+` is "good" if it satisfies the problem's inequality.
Note the inequality is rearranged to use `≤` to align with Lean's conventions.
-/
def goodPNat (f : ℕ+ → ℕ+) := ∀ m n, f m + f (f n) ≤ f (m + n) + 1

theorem imo_sl_2007_A2 {N k : ℕ+} :
  (∃ f : ℕ+ → ℕ+, goodPNat f ∧ f N = k) ↔ if N = 1 then k = 1 else k ≤ N + 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 A3

Let $F$ be a totally ordered field, and let $n$ be a positive integer.
Let $x, y \in F$ be positive elements such that $x^n + y^n = 1$.
Prove that
$$ \left(\sum_{k = 1}^n \frac{1 + x^{2k}}{1 + x^{4k}}\right)
  \left(\sum_{k = 1}^n \frac{1 + y^{2k}}{1 + y^{4k}}\right)
  < \frac{1}{(1 - x)(1 - y)}. $$
-/
/- special open -/ open Finset
theorem imo_sl_2007_A3 [LinearOrderedField F] (n : Nat) {x y : F} (hx : 0 < x) (hy : 0 < y) (h : x ^ n + y ^ n = 1) :
  (range n).sum (λ i ↦ (1 + x ^ (2 * i.succ)) / (1 + x ^ (4 * i.succ)))
    * (range n).sum (λ i ↦ (1 + y ^ (2 * i.succ)) / (1 + y ^ (4 * i.succ)))
    < ((1 - x) * (1 - y))⁻¹ := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 A4

Let $G$ be a totally ordered abelian group and $G_{>0} = \{x \in G : x > 0\}$.
Find all functions $f : G_{>0} \to G_{>0}$ such that for any $x, y \in G_{>0}$,
$$ f(x + f(y)) = f(x + y) + f(y). $$
-/
variable [LinearOrderedAddCommGroup G]

/--
This defines the property of a function `f` satisfying the given functional equation
on the subtype of positive elements `{x : G // 0 < x}`.
-/
def IsGood (f : {x : G // 0 < x} → {x : G // 0 < x}) : Prop :=
  ∀ x y, f (x + f y) = f (x + y) + f y

/--
The solutions to the functional equation are functions that double the input.
-/
theorem imo_sl_2007_A4 (f : {x : G // 0 < x} → {x : G // 0 < x}) :
  IsGood f ↔ f = (fun x ↦ x + x) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 A6

Let $R$ be a totally ordered commutative ring and let $n \ge 5$ be an integer.
Prove that for any sequence $a_1, a_2, \dots, a_n \in R$, the following inequality holds:
$$ \left(3 \sum_{i = 1}^n a_i^2 a_{i + 1}\right)^2 \le 2 \left(\sum_{i = 1}^n a_i^2\right)^3 $$
where the sum is cyclic, i.e., $a_{n+1} = a_1$.
-/
/- special open -/ open Finset
theorem imo_sl_2007_A6 [LinearOrderedCommRing R] (n : ℕ) (hn : 5 ≤ n) (a : Fin n → R) :
  (3 * ∑ i, a i ^ 2 * a (finRotate n i)) ^ 2 ≤ 2 * (∑ i, a i ^ 2) ^ 3 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 C3

Find all finite groups $G$ such that there exists a subset $S \subseteq G$ for which
 the number of triples $(x, y, z) \in S^3 \cup (G \setminus S)^3$ such that $xyz = 1$ is $2007$.
-/
/- special open -/ open Finset
variable [Fintype G] [DecidableEq G] [Group G]

def tripleSet (S : Finset G) : Finset (Fin 3 → G) :=
  (Fintype.piFinset fun _ ↦ S) ∪ (Fintype.piFinset fun _ ↦ Sᶜ)

def filtered_tripleSet (S : Finset G) : Finset (Fin 3 → G) :=
  (tripleSet S).filter fun p ↦ p 0 * p 1 * p 2 = 1

theorem imo_sl_2007_C3 :
  (∃ S : Finset G, (filtered_tripleSet S).card = 2007) ↔
  Fintype.card G = 69 ∨ Fintype.card G = 84 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 N1

Find all pairs $(k, n) \in \mathbb{N}^2$ such that $7^k - 3^n \mid k^4 + n^2$.
-/
def good (k n : ℕ) : Prop := (7 : ℤ) ^ k - 3 ^ n ∣ (k ^ 4 + n ^ 2 : ℕ)

theorem imo_sl_2007_N1 (k n : ℕ) :
  good k n ↔ (k = 0 ∧ n = 0) ∨ (k = 2 ∧ n = 4) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 N2

Fix integers $b > 0$ and $n ≥ 0$.
Suppose that for each $k ∈ ℕ^+$, there exists an integer $a$ such that $k ∣ b - a^n$.
Prove that $b = A^n$ for some integer $A$.
-/
/- special open -/ open Finset
theorem imo_sl_2007_N2 (h : 0 < b) (h0 : ∀ k : ℕ, 0 < k → ∃ c : ℤ, (k : ℤ) ∣ b - c ^ n) :
    ∃ a : ℤ, b = a ^ n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2007 N6 (P5)

Fix $n > 1$, and let $a$ and $b$ be positive integers such that $nab - 1 ∣ (na^2 - 1)^2$.
Prove that $a = b$.
-/
/- special open -/ open Finset
abbrev bad_pair (n : ℤ) (a b : ℕ) := n * a * b - 1 ∣ (n * a ^ 2 - 1) ^ 2

theorem imo_sl_2007_N6 (hn : 1 < n) (ha : 0 < a) (hb : 0 < b) (h : bad_pair n a b) :
    a = b := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A1 (P4)

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 $p, q, r, s > 0$ with $pq = rs$,
$$ (f(p)^2 + f(q)^2) (r^2 + s^2) = (p^2 + q^2) (f(r^2) + f(s^2)). $$
-/
/- special open -/ open Finset
structure weakGood [OrderedSemiring R] (f : R → R) : Prop where
  map_pos_of_pos : ∀ x > 0, f x > 0
  good' : ∀ p > 0, ∀ q > 0, ∀ r > 0, ∀ s > 0, p * q = r * s →
    (f p ^ 2 + f q ^ 2) * (r ^ 2 + s ^ 2) = (p ^ 2 + q ^ 2) * (f (r ^ 2) + f (s ^ 2))

variable [LinearOrderedField R]

def good (f : {x : R // 0 < x} → {x : R // 0 < x}) :=
  ∀ p q r s, p * q = r * s →
    (f p ^ 2 + f q ^ 2) * (r ^ 2 + s ^ 2) = (p ^ 2 + q ^ 2) * (f (r ^ 2) + f (s ^ 2))

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

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A2

1. Let $F$ be an ordered field, and consider $x, y, z \in F \setminus \{1\}$ with $xyz = 1$.
Prove that $$ \frac{x^2}{(x - 1)^2} + \frac{y^2}{(y - 1)^2} + \frac{z^2}{(z - 1)^2} \ge 1. $$

-/
theorem imo_sl_2008_A2a_part1 [LinearOrderedField F]
    {x y z : F} (hx : x ≠ 1) (hy : y ≠ 1) (hz : z ≠ 1) (h : x * y * z = 1) :
    1 ≤ (x / (x - 1)) ^ 2 + (y / (y - 1)) ^ 2 + (z / (z - 1)) ^ 2 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A2

2. Show that there exists infinitely many triplets $(x, y, z) \in (\mathbb{Q} \setminus \{1\})^3$
  with $xyz = 1$ such that the above inequality becomes equality.
-/
structure IsGood (p : Fin 3 → ℚ) : Prop where
  p_ne_one : ∀ i, p i ≠ 1
  p_mul_eq_one : p 0 * p 1 * p 2 = 1
  spec : (p 0 / (p 0 - 1)) ^ 2 + (p 1 / (p 1 - 1)) ^ 2 + (p 2 / (p 2 - 1)) ^ 2 = 1

theorem imo_sl_2008_A2b_part2 : {p : Fin 3 → ℚ | IsGood p}.Infinite := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A3

Let $α$ be a totally ordered type.
A **Spanish couple** on $α$ is a pair of strictly increasing functions $(f, g)$
from $α$ to itself such that for all $x \in α$, $f(g(g(x))) < g(f(x))$.

Determine whether there exists a Spanish couple on:
1. The set of natural numbers $ℕ$.
-/
structure SpanishCouple [Preorder α] (f g : α → α) : Prop where
  f_mono : StrictMono f
  g_mono : StrictMono g
  spec : f ∘ g ∘ g < g ∘ f

theorem imo_sl_2008_A3a_part1 : ¬ ∃ f g : ℕ → ℕ, SpanishCouple f g := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A3

Let $α$ be a totally ordered type.
A **Spanish couple** on $α$ is a pair of strictly increasing functions $(f, g)$
from $α$ to itself such that for all $x \in α$, $f(g(g(x))) < g(f(x))$.

Determine whether there exists a Spanish couple on:
2. The set $ℕ \times ℕ$ with the lexicographical order.
-/
structure SpanishCouple [Preorder α] (f g : α → α) : Prop where
  f_mono : StrictMono f
  g_mono : StrictMono g
  spec : f ∘ g ∘ g < g ∘ f

theorem imo_sl_2008_A3b_part2 : ∃ f g : (ℕ ×ₗ ℕ) → (ℕ ×ₗ ℕ), SpanishCouple f g := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A5

Let $F$ be a totally ordered field and $a_1, a_2, a_3, a_4 \in F$ be positive elements.
Suppose that $a_1 a_2 a_3 a_4 = 1$ and
$$ \sum_{i = 1}^4 \frac{a_i}{a_{i + 1}} < \sum_{i = 1}^4 a_i. $$
Prove that
$$ \sum_{i = 1}^4 a_i < \sum_{i = 1}^4 \frac{a_{i + 1}}{a_i}. $$
-/
theorem imo_sl_2008_A5 [LinearOrderedField F]
    {a b c d : F} (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) (hd : 0 < d)
    (h_prod : a * b * c * d = 1)
    (h_ineq : a / b + b / c + c / d + d / a < a + b + c + d) :
    a + b + c + d < b / a + c / b + d / c + a / d := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 A7

Let $F$ be a totally ordered field.
1. Prove that, for any $a, b, c, d \in F$ positive,
$$ \frac{(a - b)(a - c)}{a + b + c} + \frac{(b - c)(b - d)}{b + c + d} +
  \frac{(c - d)(c - a)}{c + d + a} + \frac{(d - a)(d - b)}{d + a + b} \ge 0. $$
2. Find all cases of equality.
-/
theorem imo_sl_2008_A7 [LinearOrderedField F]
    {a b c d : F} (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) (hd : 0 < d) :
    (0 ≤ (a - b) * (a - c) / (a + b + c) + (b - c) * (b - d) / (b + c + d) +
      (c - d) * (c - a) / (c + d + a) + (d - a) * (d - b) / (d + a + b)) ∧
    ((a - b) * (a - c) / (a + b + c) + (b - c) * (b - d) / (b + c + d) +
      (c - d) * (c - a) / (c + d + a) + (d - a) * (d - b) / (d + a + b) = 0 ↔
      a = c ∧ b = d) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2008 C4

Let $n$ and $d$ be positive integers. Consider $2n$ lamps labelled with a pair $(b, m)$ where
$b \in \{0, 1\}$ and $m \in \{0, 1, \ldots, n - 1\}$. Initially, all the lamps are off.

Consider sequences of $k = 2d + n$ steps, where at each step, one of the lamps is switched
(off to on and vice versa).

Let $S_N$ be the set of $k$-step sequences ending in a state where the lamps labelled $(b, m)$
are on if and only if $b = 0$.

Let $S_M \subseteq S_N$ consist of the sequences that do not touch the lamps labelled
$(0, m)$ at all.

Find the ratio $|S_N|/|S_M|$.
-/
/- special open -/ open Finset
variable (I Λ : Type) [Fintype I] [Fintype Λ]

def IsNSequence [DecidableEq I] [DecidableEq Λ] (f : I → Fin 2 × Λ) : Prop :=
  ∀ p : Fin 2 × Λ, (univ.filter (f · = p)).card % 2 = p.1.val

noncomputable instance IsNSequence.instDecidablePred [DecidableEq I] [DecidableEq Λ] :
  DecidablePred (IsNSequence I Λ) := by
  unfold IsNSequence; infer_instance

def IsMSequence [DecidableEq I] [DecidableEq Λ] (f : I → Λ) : Prop :=
  ∀ l : Λ, (univ.filter (f · = l)).card % 2 = 1

noncomputable instance IsMSequence.instDecidablePred [DecidableEq I] [DecidableEq Λ] :
  DecidablePred (IsMSequence I Λ) := by
  unfold IsMSequence; infer_instance

theorem imo_sl_2008_C4 [DecidableEq I] [DecidableEq Λ] :
    Fintype.card { f : I → Fin 2 × Λ // IsNSequence I Λ f } =
      2 ^ (Fintype.card I - Fintype.card Λ) * Fintype.card { f : I → Λ // IsMSequence I Λ f } := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 A2

Let $F$ be a totally ordered field, and let $a, b, c \in F$ be positive elements.
Prove that
$$ \frac{1}{(2a + b + c)^2} + \frac{1}{(2b + c + a)^2} + \frac{1}{(2c + a + b)^2}
  \le \frac{3}{16(ab+bc+ca)}. $$
-/
theorem imo_sl_2009_A2 [LinearOrderedField F]
    {a b c : F} (ha : 0 < a) (hb : 0 < b) (hc : 0 < c)
    (h_norm : a⁻¹ + b⁻¹ + c⁻¹ = a + b + c) :
    ((2 * a + b + c) ^ 2)⁻¹ + ((2 * b + c + a) ^ 2)⁻¹ + ((2 * c + a + b) ^ 2)⁻¹ ≤ 3 / 16 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 A3

Find all functions $f : \mathbb{N} \to \mathbb{N}$ such that for any $x, y \in \mathbb{N}$, the numbers
$x$, $f(y)$, and $f(y + f(x))$ form the sides of a possibly degenerate triangle.

-/
structure IsNatTriangle (x y z : ℕ) : Prop where
  side_x : x ≤ y + z
  side_y : y ≤ z + x
  side_z : z ≤ x + y

def IsGoodNat (f : ℕ → ℕ) : Prop :=
  ∀ x y, IsNatTriangle x (f y) (f (y + f x))

theorem imo_sl_2009_A3a_nat (f : ℕ → ℕ) : IsGoodNat f ↔ f = id := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 A3

The original problem statement. Find all functions $f : \mathbb{N}^+ \to \mathbb{N}^+$ such that for any
$x, y \in \mathbb{N}^+$, the numbers $x$, $f(y)$, and $f(y + f(x) - 1)$ form the sides of a
non-degenerate triangle.
-/
structure IsPNatTriangle (x y z : ℕ+) : Prop where
  side_x : x < y + z
  side_y : y < z + x
  side_z : z < x + y

def IsGoodPNat (f : ℕ+ → ℕ+) : Prop :=
  ∀ x y, IsPNatTriangle x (f y) (f (y + f x - 1))

theorem imo_sl_2009_A3b_pnat (f : ℕ+ → ℕ+) : IsGoodPNat f ↔ f = id := by sorry