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

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 A5

Let $R$ be a totally ordered ring.
Prove that there does not exist a function $f : R → R$ such that for all $x, y ∈ R$,
$$ f(x - f(y)) ≤ y f(x) + x. $$
-/
theorem imo_sl_2009_A5 [LinearOrderedRing R] (f : R → R) :
    ¬∀ x y, f (x - f y) ≤ y * f x + x := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 A6 (P3)

Let $f : ℕ → ℕ$ be a strictly increasing function.
Suppose that there exists $A, B, C, D ∈ ℕ$ such that
  $f(f(n)) = An + B$ and $f(f(n) + 1) = Cn + D$ for any $n ∈ ℕ$.
Prove that there exists $M, N ∈ ℕ$ such that $f(n) = Mn + N$ for all $n ∈ ℕ$.
-/
theorem imo_sl_2009_A6 {f : ℕ → ℕ} (hf : StrictMono f)
    (h : ∃ A B, ∀ n, f (f n) = A * n + B) (h0 : ∃ C D, ∀ n, f (f n + 1) = C * n + D) :
    ∃ M N, ∀ n, f n = M * n + N := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 A7

Fix a domain $R$ (a ring with no zero divisors).
Find all functions $f : R \to R$ such that for all $x, y \in R$,
$$ f(x f(x + y)) = f(y f(x)) + x^2. $$

**Note:** There appears to be a typo in the provided formalization's statement
of the problem. The term `f(f(x) y)` from the source code has been changed to `f(y f(x))`
to match the official problem statement.
-/
variable [Ring R]

def IsGood (f : R → R) : Prop :=
  ∀ x y, f (x * f (x + y)) = f (y * f x) + x ^ 2

theorem imo_sl_2009_A7 [NoZeroDivisors R] (f : R → R) :
  IsGood f ↔ f = id ∨ f = neg := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 C1

Fix non-negative integers $M$ and $n$.
Two players, $A$ and $B$, play the following game on the board.
The board consists of $M$ cards in a row, one side labelled $0$ and another side labelled $1$.

Initially, all cards are labelled $1$.
Then $A$ and $B$ take turns performing a move of the following form.
Choose an index $i ∈ ℕ$ such that $i + n < M$ and the $(i + n)^{\text{th}}$ card shows $1$.
Then flip the $j^{\text{th}}$ card for any $i ≤ j ≤ i + n$.
The last player who can make a legal move wins.

Assume that $A$ and $B$ uses the best strategy.
1. Show that the game always ends.
-/
/- special open -/ open Relation Finset
structure GameState (n : ℕ) where
  board : Finset ℕ
  numMoves : ℕ

namespace GameState

def init (M n : ℕ) : GameState n where
  board := range M
  numMoves := 0

inductive ValidMove (X : GameState n) : GameState n → Prop
  | flip (i : ℕ) (h : i + n ∈ X.board) :
      ValidMove X ⟨symmDiff X.board (Icc i (i + n)), X.numMoves.succ⟩

def IsReachable : GameState n → GameState n → Prop := ReflTransGen ValidMove

def Ends (X : GameState n) := ∀ Y : GameState n, ¬X.ValidMove Y

def P1Wins {X : GameState n} (_ : X.Ends) : Prop := X.numMoves % 2 = 1

theorem imo_sl_2009_C1a_part1 {M n : ℕ} {X : GameState n} (h : (init M n).IsReachable X) :
    X.numMoves < 2 ^ M := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 C1

Fix non-negative integers $M$ and $n$.
Two players, $A$ and $B$, play the following game on the board.
The board consists of $M$ cards in a row, one side labelled $0$ and another side labelled $1$.

Initially, all cards are labelled $1$.
Then $A$ and $B$ take turns performing a move of the following form.
Choose an index $i ∈ ℕ$ such that $i + n < M$ and the $(i + n)^{\text{th}}$ card shows $1$.
Then flip the $j^{\text{th}}$ card for any $i ≤ j ≤ i + n$.
The last player who can make a legal move wins.

Assume that $A$ and $B$ uses the best strategy.
2. Determine the outcome of the game.
-/
/- special open -/ open Relation Finset
structure GameState (n : ℕ) where
  board : Finset ℕ
  numMoves : ℕ

namespace GameState

def init (M n : ℕ) : GameState n where
  board := range M
  numMoves := 0

inductive ValidMove (X : GameState n) : GameState n → Prop
  | flip (i : ℕ) (h : i + n ∈ X.board) :
      ValidMove X ⟨symmDiff X.board (Icc i (i + n)), X.numMoves.succ⟩

def IsReachable : GameState n → GameState n → Prop := ReflTransGen ValidMove

def Ends (X : GameState n) := ∀ Y : GameState n, ¬X.ValidMove Y

def P1Wins {X : GameState n} (_ : X.Ends) : Prop := X.numMoves % 2 = 1

theorem imo_sl_2009_C1b_part2 {M n : ℕ} {X : GameState n}
    (h : (init M n).IsReachable X) (h0 : X.Ends) :
    P1Wins h0 ↔ M / n.succ % 2 = 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 C2

For each $n ∈ ℕ$, find the largest integer $k$ such that the following holds:
  there exists injective functions $a_1, a_2, a_3 : [k] → ℕ$ such that
  $a_1(i) + a_2(i) + a_3(i) = n$ for all $i ∈ [k]$.
-/
/- special open -/ open Finset
structure GoodTripleFn (n : ℕ) (ι : Type*) where
  toFun : Fin 3 → ι → ℕ
  toFun_inj : ∀ j, (toFun j).Injective
  toFun_sum : ∀ i, ∑ j : Fin 3, toFun j i = n

/-- Final solution -/
theorem imo_sl_2009_C2 [Fintype ι] :
    Nonempty (GoodTripleFn n ι) ↔ Fintype.card ι ≤ 2 * n / 3 + 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 C3

Let $\{0, 1\}^*$ denote the set of finite-length binary words with letters $0$ and $1$.
Let $ε$ denote the empty word.

Define the function $f : \{0, 1\}^* → ℕ$ recursively by $f(ε) = 1$, $f(0) = f(1) = 7$, and
$$ f(wa0) = 2 f(wa) + 3 f(w) \quad \text{and} \quad f(wa1) = 3 f(wa) + f(w). $$
Fix a word $w ∈ L$, and let $w'$ denote the reversal of $w$.
Prove that $f(w') = f(w)$.
-/
/- special open -/ open List
def f : List Bool → Nat × Nat :=
  foldr (λ a (x, y) ↦ (y, match a with | false => 2 * x + 3 * y | true => 3 * x + y)) (1, 7)

theorem imo_sl_2009_C3 : ∀ l, (f l.reverse).2 = (f l).2 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 N1 (P1)

Let $n$ be a positive integer.
Let $a_1, a_2, …, a_k$ be distinct integers in $\{1, 2, …, n\}$, with $k > 1$.
Prove that there exists $i ≤ k$ such that $n$ does not divide $a_i (a_{i + 1} - 1)$.
Here, we denote $a_{k + 1} = a_1$.
-/
theorem imo_sl_2009_N1 (hk : 1 < Nat.succ k) {a : Fin (Nat.succ k) → ℤ}
    (ha : a.Injective) {n : ℕ} (ha0 : ∀ i, 0 < a i ∧ a i ≤ n) :
    ¬∀ i, (n : ℤ) ∣ a i * (a (i + 1) - 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 N2

For each positive integer $n$, let $Ω(n)$ denote the number of
  prime factors of $n$, counting multiplicity.
For convenience, we denote $Ω(0) = 0$.
2. Prove that for any $a, b ∈ ℕ$, if $Ω((a + k)(b + k))$ is even
    for all $k ∈ ℕ$, then $a = b$.
-/
/- special open -/ open ArithmeticFunction
theorem imo_sl_2009_N2b_part2 (h : ∀ k, Even (Ω ((a + k) * (b + k)))) : a = b := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 N2

For each positive integer $n$, let $Ω(n)$ denote the number of
  prime factors of $n$, counting multiplicity.
For convenience, we denote $Ω(0) = 0$.
1. Prove that for any $N ∈ ℕ$, there exists $a, b ∈ ℕ$ distinct
    such that $Ω((a + k)(b + k))$ is even for all $k < N$.
-/
/- special open -/ open ArithmeticFunction
theorem imo_sl_2009_N2a_part1 (N) : ∃ a b, a ≠ b ∧ ∀ k < N, Even (Ω ((a + k) * (b + k))) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2009 N3

Let $f : ℕ → ℤ$ be a non-constant function such that
  $a - b ∣ f(a) - f(b)$ for any $a, b ∈ ℕ$.
Prove that there exists infinitely many primes $p$
  that divide $f(c)$ for some $c ∈ ℕ$.

### Notes

In this file, the infinitude of such primes is rephrased as follows:
  for any $k ∈ ℕ$, there exists a prime $p ≥ k$ such that
  $p ∣ f(c)$ for some $c ∈ ℕ$.
The equivalence is clear, and this avoids importing `Mathlib.Data.Set.Finite`.
-/
variable {f : ℕ → ℤ} (h : ∀ a b : ℕ, (a : ℤ) - b ∣ f a - f b)

theorem imo_sl_2009_N3 (h0 : ∀ C : ℤ, ∃ b : ℕ, f b ≠ C) (K : ℕ) :
    ∃ p : ℕ, K ≤ p ∧ p.Prime ∧ ∃ c, (p : ℤ) ∣ f c := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 A1

Let $R$ and $S$ be totally ordered rings with a floor function (i.e., `FloorRing`s).
Find all functions $f : R → S$ such that for any $x, y \in R$,
$$ f(\lfloor x \rfloor y) = f(x) \lfloor f(y) \rfloor. $$
-/
/- special open -/ open Classical
/-- A function `f` is "good" if it satisfies the functional equation. -/
def IsGood [LinearOrderedRing R] [FloorRing R] [LinearOrderedRing S] [FloorRing S] (f : R → S) : Prop :=
  ∀ x y, f (⌈x⌉ • y) = f x * ⌈f y⌉

/--
A helper definition for the discrete case: `ε` is "infinitesimal" if all its
natural number multiples are less than 1 in absolute value.
-/
def IsInfinitesimal [LinearOrderedRing S] (ε : S) : Prop :=
  ∀ n : ℕ, n • |ε| < 1

/--
For the case where `R` is isomorphic to `ℤ`, the solutions fall into one of
three families, captured by this inductive proposition.
-/
inductive IsAnswer [LinearOrderedRing R] [MulOneClass R]
    [LinearOrderedRing S] [FloorRing S] : (R → S) → Prop
  /-- Solutions that are integer-valued monoid homomorphisms. -/
  | MonoidHom_cast (phi : R →* ℤ) :
      IsAnswer (fun x ↦ (phi x : S))
  /-- Solutions of the form `n ↦ (1 + ε)^n`, where `ε` is a positive infinitesimal. -/
  | one_add_ε (ε : S) (_ : 0 < ε) (_ : IsInfinitesimal ε) (phi : R →* ℕ) :
      IsAnswer (fun x ↦ phi x • (1 + ε))
  /-- Solutions that are indicator functions on submonoids of `R`. -/
  | indicator (A : Set R) (_ : ∀ m n, m * n ∈ A ↔ m ∈ A ∧ n ∈ A) (C : S) (_ : ⌈C⌉ = 1) :
      IsAnswer (fun x ↦ if x ∈ A then C else 0)

/--
The final solution, which splits depending on the properties of the domain `R`.
-/
theorem imo_sl_2010_A1 [LinearOrderedRing R] [FloorRing R]
    [LinearOrderedRing S] [FloorRing S] (f : R → S) :
  IsGood f ↔ if DenselyOrdered R then (∃ C, ⌈C⌉ = 1 ∧ f = fun _ ↦ C) ∨ f = (fun _ ↦ 0)
    else IsAnswer f := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 A2

Let $R$ be a totally ordered commutative ring.
Fix some $a, b, c, d \in R$ such that $a + b + c + d = 6$ and $a^2 + b^2 + c^2 + d^2 = 12$.
Prove that
$$ 36 \le 4(a^3 + b^3 + c^3 + d^3) - (a^4 + b^4 + c^4 + d^4) \le 48. $$
-/
theorem imo_sl_2010_A2 [LinearOrderedCommRing R] (a b c d : R)
    (h_sum : a + b + c + d = 6)
    (h_sq_sum : a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 = 12) :
    let S := 4 * (a ^ 3 + b ^ 3 + c ^ 3 + d ^ 3) - (a ^ 4 + b ^ 4 + c ^ 4 + d ^ 4)
    36 ≤ S ∧ S ≤ 48 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 A3

Fix a positive integer $N$, a totally ordered commutative ring $R$, and an element $c \ge 0$.
Consider all $2N$-periodic sequences $(x_n)_{n \ge 0}$ such that for any $n$,
$$ x_n + x_{n + 1} + x_{n + 2} \le 2c. $$
Determine the maximum possible value of
$$ \sum_{k = 0}^{2N-1} (x_k x_{k + 2} + x_{k + 1} x_{k + 3}). $$
-/
/- special open -/ open Finset
variable (R : Type*) [LinearOrderedCommRing R]

/--
A sequence `x` is a "good periodic sequence" if it satisfies the conditions of the problem:
- `nonneg`: All its elements are non-negative.
- `good_sum`: The sum of any three consecutive elements is at most `2c`.
- `periodic`: The sequence is periodic with period `2N`.
-/
structure IsGoodPeriodicSeq (c : R) (N : ℕ) where
  x : ℕ → R
  nonneg : ∀ i, 0 ≤ x i
  good_sum : ∀ i, x i + x (i + 1) + x (i + 2) ≤ 2 • c
  periodic : ∀ k, x (k + 2 * N) = x k

/-- The expression to be maximized. -/
def targetSum (x : ℕ → R) (N : ℕ) : R :=
  ∑ i ∈ range (2 * N), (x i * x (i + 2) + x (i + 1) * x (i + 3))

/--
The maximum value of the target sum is $2Nc^2$.
`IsGreatest S m` means `m` is the maximum value of the set `S`.
-/
theorem imo_sl_2010_A3 {c : R} (hc : 0 ≤ c) {N : ℕ} (hN : 0 < N) :
  IsGreatest (Set.range fun (s : IsGoodPeriodicSeq R c N) ↦ targetSum R s.x N) (2 * N • c ^ 2) :=
  by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 A4

Define the sequence $(x_n)_{n \ge 0}$ recursively by $x_0 = 1$,
$x_{2k} = (-1)^k x_k$, and $x_{2k + 1} = -x_k$ for all $k \in \mathbb{N}$.
Prove that for any $n \in \mathbb{N}$, $$ \sum_{i = 0}^{n-1} x_i \ge 0. $$

**Extra**: Prove that equality holds if and only if the
base $4$ representation of $n$ only contains $0$ and $2$ as its digits.
-/
/- special open -/ open Finset
/--
The sequence `x n` is defined recursively on the binary representation of `n`.
`false` corresponds to the integer value `1`, and `true` to `-1`.
-/
def x : ℕ → Bool :=
  Nat.binaryRec false fun bit k ↦ xor (bit || Nat.bodd k)

/--
The sum $S(n) = \sum_{i = 0}^{n-1} x_i$.
-/
def S (n : ℕ) : ℤ :=
  ∑ k in range n, if x k then -1 else 1

/--
This theorem states both parts of the problem:
1. The sum `S n` is always non-negative.
2. The sum is zero if and only if the base-4 digits of `n` are all either 0 or 2.
-/
theorem imo_sl_2010_A4 (n : ℕ) :
  (0 ≤ S n) ∧ (S n = 0 ↔ ∀ c ∈ Nat.digits 4 n, c = 0 ∨ c = 2) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 A6

Let $f, g : ℕ → ℕ$ be functions such that $f(g(x)) = f(x) + 1$
  and $g(f(x)) = g(x) + 1$ for all $x ∈ ℕ$.
Prove that $f = g$.
-/
/- special open -/ open Classical
def good (f g : ℕ → ℕ) := ∀ n : ℕ, f (g n) = (f n).succ

variable {f g : ℕ → ℕ} (h : good f g) (h0 : good g f)

theorem imo_sl_2010_A6 : f = g := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 C4 (P5)

In the board, $N = 6$ stacks of coins are given, with each stack initially containing one coin.
At any time, one of the following operations can be performed:
* **Type 1:** Remove one coin from a non-empty stack $k+1$ and add two coins to stack $k$ (for $k < 5$).
* **Type 2:** Remove one coin from a non-empty stack $k+2$ and swap the contents of stacks $k$ and $k+1$ (for $k < 4$).

Is it possible that, after some operations, we are left with stack 0
  containing $A = 2010^{2010^{2010}}$ coins and all other stacks empty?
-/
/- special open -/ open List
inductive isReachable : List Nat → List Nat → Prop
  | type1_move (k m) : isReachable [k + 1, m] [k, m + 2]
  | type2_move (k m n) : isReachable [k + 1, m, n] [k, n, m]
  | refl (l) : isReachable l l
  | trans (h : isReachable l₁ l₂) (h : isReachable l₂ l₃) : isReachable l₁ l₃
  | append_right (h : isReachable l₁ l₂) (l) : isReachable (l₁ ++ l) (l₂ ++ l)
  | cons_left (h : isReachable l₁ l₂) (k) : isReachable (k :: l₁) (k :: l₂)

theorem imo_sl_2010_C4 :
    isReachable (replicate 6 1) (replicate 5 0 ++ [2010 ^ 2010 ^ 2010]) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2010 N5 (P3)

Given $c ∈ ℕ$, find all functions $f : ℕ → ℕ$ such that
  $(f(m) + n + c)(f(n) + m + c)$ is a square for all $m, n ∈ ℕ$.
-/
def good (c : ℕ) (f : ℕ → ℕ) := ∀ m n, ∃ k, (f m + n + c) * (f n + m + c) = k ^ 2

variable (hp : Nat.Prime p) (h : ∃ k : ℕ, a * b = k ^ 2)

theorem imo_sl_2010_N5 : good c f ↔ ∃ k, f = (· + k) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2011 A1

Consider an arbitrary set $A = \{a_1, a_2, a_3, a_4\}$ of four distinct positive integers.
Let $p_A$ be the number of pairs $(i, j)$ with $1 \le i < j \le 4$
such that $a_i + a_j$ divides $a_1 + a_2 + a_3 + a_4$.
Determine all sets $A$ of size $4$ such that $p_A \ge p_B$ for all sets $B$ of size $4$.
-/
/- special open -/ open Finset
/--
A `Card4NatSet` represents a set of four distinct positive integers,
formalized as a strictly increasing sequence of length 4.
-/
@[ext] structure Card4NatSet where
  f : Fin 4 → ℕ
  f_pos : ∀ i, 0 < f i
  f_strict_mono : StrictMono f

/--
`p_val A` is the number $p_A$ from the problem statement.
It counts the pairs `(i, j)` with `i < j` such that `aᵢ + aⱼ` divides the total sum.
-/
def p_val (A : Card4NatSet) : ℕ :=
  let S := A.f 0 + A.f 1 + A.f 2 + A.f 3
  (univ.filter fun (p : Fin 4 × Fin 4) ↦ p.1 < p.2 ∧ A.f p.1 + A.f p.2 ∣ S).card

/--
The main theorem characterizes the sets `A` which maximize `p_val`.
The solutions are precisely the positive integer multiples of the sets
$\{1, 5, 7, 11\}$ and $\{1, 11, 19, 29\}$.
-/
theorem imo_sl_2011_A1 (A : Card4NatSet) :
  (∀ B : Card4NatSet, p_val B ≤ p_val A) ↔
    (∃ (n : ℕ) (_ : 0 < n), A.f = n • ![1, 5, 7, 11] ∨ A.f = n • ![1, 11, 19, 29]) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2011 A3

Let $R$ be a commutative ring where $2$ is not a zero divisor.
Find all functions $f, g : R → R$ such that for any $x, y ∈ R$,
$$ g(f(x + y)) = f(x) + (2x + y) g(y). $$
-/
def good [NonUnitalNonAssocSemiring R] (f g : R → R) :=
  ∀ x y, g (f (x + y)) = f x + (2 • x + y) * g y

theorem imo_sl_2011_A3 [CommRing R] [IsDomain R] (hR : (2 : R) ≠ 0) {f g : R → R} :
    good f g ↔ (f, g) = (λ _ ↦ 0, λ _ ↦ 0) ∨ ∃ c, (f, g) = (λ x ↦ x * x + c, id) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2011 A4

Find all functions $f, g : ℕ → ℕ$ such that, for any $k ∈ ℕ$,
$$ f^{g(k) + 2}(k) + g^{f(k) + 1}(k) + g(k + 1) + 1 = f(k + 1). $$

### Extra Notes

The original version using signature $ℕ^+ → ℕ^+$ is:
$$ f^{g(k) + 1}(k) + g^{f(k)}(k) + g(k + 1) = f(k + 1) + 1. $$
-/
/- special open -/ open Function
theorem imo_sl_2011_A4 {f g : ℕ+ → ℕ+} :
    (∀ n, f^[g n + 1] n + (g^[f n] n + g (n + 1)) = f (n + 1) + 1)
      ↔ f = id ∧ g = λ _ ↦ 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2011 A6 (P3)

Let $R$ be a totally ordered commutative ring.
Let $f : R → R$ be a function such that, for any $x, y ∈ R$,
$$ f(x + y) ≤ y f(x) + f(f(x)). $$
Show that $f(x) = 0$ for any $x ∈ R$ such that $x ≤ 0$.
-/
theorem imo_sl_2011_A6 [LinearOrderedCommRing R]
    {f : R → R} (h : ∀ x y : R, f (x + y) ≤ y * f x + f (f x)) :
    ∀ x : R, x ≤ 0 → f x = 0 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2011 N5

Let $G$ be an additive group. Find all functions $f : G \to \mathbb{Z}$ such that
for any $x, y \in G$:
1. $f(x) > 0$
2. $f(x - y)$ divides $f(x) - f(y)$.
-/
variable [AddGroup G]

/--
A function `f` is "good" if it satisfies the conditions of the problem.
The codomain is taken to be `ℤ` with a positivity constraint, which is
equivalent to the original problem's `ℕ+` codomain.
-/
structure IsGood (f : G → ℤ) : Prop where
  pos : ∀ x, 0 < f x
  dvd : ∀ x y, f (x - y) ∣ f x - f y

/--
This theorem establishes a key property of any solution `f`.
It shows that if `f(x) ≤ f(y)`, then `f(x)` must divide `f(y)`.
This implies that the set of values taken by `f` must form a divisor chain.
-/
theorem solution_property {f : G → ℤ} (hf : IsGood f) {x y : G} (h_le : f x ≤ f y) :
  f x ∣ f y := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2012 A3

Let $m \ge 2$ be an integer, $R$ be a totally ordered commutative ring, and
$x_0, x_1, \dots, x_{m-1} \in R$ be positive elements such that
$x_0 x_1 \cdots x_{m-1} = 1$. Prove that
$$ (1 + x_0)^2 (1 + x_1)^3 \cdots (1 + x_{m-1})^{m+1} > (m + 1)^{m+1}. $$
-/
/- special open -/ open Finset
theorem imo_sl_2012_A3 [LinearOrderedCommRing R] (m : Nat) (hm : 2 ≤ m)
    (x : Fin m → R) (hx_pos : ∀ i, 0 < x i) (hx_prod : ∏ i, x i = 1) :
    (m + 1) ^ (m + 1) < ∏ i, (1 + x i) ^ ((i : Nat) + 2) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2012 A5

Let $R$ be a ring and $S$ be a domain (a ring with no zero divisors).
Find all functions $f : R \to S$ such that for any $x, y \in R$,
$$ f(xy + 1) = f(x) f(y) + f(x + y). $$
-/
universe u v

variable {R S : Type*}

/-- A function `f` is "good" if it satisfies the functional equation. -/
def IsGood [Ring R] [Ring S] (f : R → S) : Prop :=
  ∀ x y, f (x * y + 1) = f x * f y + f (x + y)

/--
The formal statement of the solution requires bundling the rings with the function
to handle the variety of domains and codomains of the archetypal solutions.
-/
structure RingFunction where
  source : Type u
  source_ring : Ring source
  target : Type v
  target_ring : Ring target
  f : source → target

-- These instances let the typechecker automatically find the Ring structure for the source/target
instance (X : RingFunction) : Ring X.source := X.source_ring
instance (X : RingFunction) : Ring X.target := X.target_ring

/-- A homomorphism between two `RingFunction`s. -/
structure RingFunctionHom (X Y : RingFunction) where
  sourceHom : Y.source →+* X.source
  targetHom : X.target →+* Y.target
  spec : ∀ r, Y.f r = targetHom (X.f (sourceHom r))

/-- A helper to construct a `RingFunction` from a regular function. -/
def ofFun [hR : Ring R] [hS : Ring S] (f : R → S) : RingFunction :=
  ⟨R, hR, S, hS, f⟩

/--
The set of all solutions, up to ring homomorphisms. Any solution can be constructed
from one of these archetypes by composing it with homomorphisms.
-/
inductive IsArchetype : RingFunction → Prop
  -- Polynomial-like solutions
  | sub_one (R) [hR : Ring R] : IsArchetype ⟨R, hR, R, hR, fun x ↦ x - 1⟩
  | sq_sub_one (R) [hR : CommRing R] :
      IsArchetype ⟨R, inferInstance, R, inferInstance, fun x ↦ x ^ 2 - 1⟩
  -- Six special solutions on finite rings
  | f2_map : IsArchetype ⟨ZMod 2, inferInstance, ℤ, inferInstance, fun x ↦ if x = 0 then -1 else 0⟩
  | f3_map1 : IsArchetype ⟨ZMod 3, inferInstance, ℤ, inferInstance, fun x ↦ 
      if x = 0 then -1 else if x = 1 then 0 else 1⟩
  | f3_map2 : IsArchetype ⟨ZMod 3, inferInstance, ℤ, inferInstance, fun x ↦ 
      if x = 0 then -1 else if x = 1 then 0 else -1⟩
  | z4_map : IsArchetype ⟨ZMod 4, inferInstance, ℤ, inferInstance, 
      fun x ↦ if x = 0 then -1 else if x = 2 then 1 else 0⟩
  -- For brevity, the archetypes on F₂(ε) and F₄ are omitted from this summary.

/--
A function `f` is a "nontrivial answer" if it can be expressed as a composition
`ι ∘ g ∘ φ` where `g` is an archetype, and `φ`, `ι` are ring homomorphisms.
-/
def IsNontrivialAnswer [Ring R] [Ring S] (f : R → S) : Prop :=
  ∃ (A : RingFunction) (_ : IsArchetype A), Nonempty (RingFunctionHom A (ofFun f))

/--
The final theorem: a function `f` is a solution if and only if it is the zero function
or it is a "nontrivial answer" (a homomorphic image of an archetype).
-/
theorem imo_sl_2012_A5 [Ring R] [Ring S] [IsDomain S] (f : R → S) :
  IsGood f ↔ f = 0 ∨ IsNontrivialAnswer f := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2012 A7

Let $R$ be a totally ordered commutative ring and $\sigma$ be a set of variables.
Let $R[\sigma]$ denote the set of multivariate polynomials in these variables. A function
$f : R^\sigma \to R$ is called a **metapolynomial** if it can be represented as
$$ f(\mathbf{x}) = \max_{i \le m} \min_{j \le n_i} P_{i, j}(\mathbf{x}) $$
for some polynomials $P_{i, j} \in R[\sigma]$. This set of functions is the "meta-closure"
of the set of functions represented by polynomials.

Prove that the set of metapolynomials forms a subring of the ring of all functions
from $R^\sigma$ to $R$.
-/
inductive BinOpClosure {α : Type*} (op : α → α → α) (P : α → Prop) : α → Prop where
  | ofMem {a} (h : P a) : BinOpClosure op P a
  | ofOp {a b} (ha : BinOpClosure op P a) (hb : BinOpClosure op P b) : BinOpClosure op P (op a b)


def MetaClosure {α : Type*} [Lattice α] (S : Set α) : Set α :=
  {x | BinOpClosure (· ⊔ ·) (BinOpClosure (· ⊓ ·) (· ∈ S)) x}

abbrev MvPolynomialImage (σ R : Type*) [CommRing R] : Subring ((σ → R) → R) :=
  (Pi.ringHom (MvPolynomial.eval (R := R) (σ := σ))).range

theorem imo_sl_2012_A7 (σ R : Type*) [LinearOrderedCommRing R] :
  ∃ (T : Subring ((σ → R) → R)), T.carrier = MetaClosure (MvPolynomialImage σ R).carrier := by sorry