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

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2012 N1

Let $R$ be a commutative ring.
A set $A ⊆ R$ is called *admissible* if $x^2 + rxy + y^2 ∈ A$ for any $x, y ∈ A$ and $r ∈ R$.
Determine all pairs $(x, y) ∈ R^2$ such that the only
  admissible set containing $x$ and $y$ is $R$.
-/
def admissible [Semiring R] (A : Set R) :=
  ∀ x y : R, x ∈ A → y ∈ A → ∀ r : R, x ^ 2 + r * x * y + y ^ 2 ∈ A

theorem imo_sl_2012_N1 [CommRing R] (x y : R) :
    (∀ A : Set R, admissible A → x ∈ A → y ∈ A → ∀ z : R, z ∈ A) ↔ IsCoprime x y := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2012 N3

Determine all integers $m > 1$ such that $n ∣ \binom{n}{m - 2n}$ for every $n ≤ m/2$.
-/
theorem imo_sl_2012_N3 (h : 1 < m) :
    (∀ n : ℕ, 2 * n ≤ m → n ∣ n.choose (m - 2 * n)) ↔ m.Prime := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2012 N4

An integer $a$ is called *friendly* if there exist positive integers $m, n$ such that
$$ (m^2 + n)(n^2 + m) = a(m - n)^3. $$

1. Prove that $\{1, 2, \dots, 2012\}$ contains at least $500$ friendly integers.
2. Is $2$ friendly?
-/
/- special open -/ open Finset Classical
/-- An integer `a` is friendly if it satisfies the given condition for some positive integers `m` and `n`. -/
def IsFriendly (a : ℤ) : Prop :=
  ∃ m > 0, ∃ n > 0, (m ^ 2 + n) * (n ^ 2 + m) = a * (m - n) ^ 3

theorem imo_sl_2012_N4 :
  500 ≤ ((Icc 1 2012).filter (IsFriendly)).card ∧
  ¬ IsFriendly 2 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2013 A1

Let $R$ be a commutative ring.
Given a list of elements $a_0, \dots, a_{n-1} \in R$, we define a sequence $(u_k)$ by
$u_0 = u_1 = 1$, and $u_{k + 2} = u_{k + 1} + a_k u_k$ for each $0 \le k < n$.
We then define the function $f(a_0, \dots, a_{n-1}) = u_{n + 1}$.

Prove that $f(a_0, \dots, a_{n-1}) = f(a_{n-1}, \dots, a_0)$.
-/
variable {R : Type*} [CommRing R]

/--
A helper function to compute the pair `(u_{k+1}, u_k)` recursively.
`f_aux [a₀, a₁, ..., a_{k-1}]` returns `(u_{k+1}, u_k)`.
-/
def f_aux : List R → R × R
  | [] => (1, 1)
  | r :: l => let (a, b) := f_aux l; (a + r * b, a)

def f (l : List R) : R := (f_aux l).1

theorem imo_sl_2013_A1 (l : List R) : f l.reverse = f l := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2013 A5

Find all functions $f : \mathbb{N} \to \mathbb{N}$ such that for any $n \in \mathbb{N}$,
$$ f(f(f(n))) = f(n + 1) + 1. $$
-/
def IsGood (f : ℕ → ℕ) : Prop :=
  ∀ n, f^[3] n = f (n + 1) + 1

def answer2 : ℕ → ℕ
  | 0 => 1
  | 1 => 6
  | 2 => 3
  | 3 => 0
  | n + 4 => answer2 n + 4

theorem imo_sl_2013_A5 (f : ℕ → ℕ) :
  IsGood f ↔ f = Nat.succ ∨ f = answer2 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2013 N1

Find all functions $f : ℕ^+ → ℕ^+$ such that, for any $m, n : ℕ^+$,
$$ m^2 + f(n) ∣ m f(m) + n. $$
-/
theorem imo_sl_2013_N1 {f : ℕ+ → ℕ+} :
    (∀ m n : ℕ+, m * m + f n ∣ m * f m + n) ↔ f = id := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2013 N2

Prove that for any positive integers $k, n$, there exist positive integers $m_1, m_2, \dots, m_k$
such that
$$ 1 + \frac{2^k - 1}{n} = \prod_{i = 1}^k \left(1 + \frac{1}{m_i}\right). $$
-/
/- special open -/ open Finset
theorem imo_sl_2013_N2 (k n : ℕ+) :
  ∃ (m : Fin k → ℕ+), (1 : ℚ) + ((2 : ℚ) ^ (k : ℕ) - 1) / (n : ℚ) =
    ∏ i : Fin k, (1 + 1 / (m i : ℚ)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2013 N3

For each positive integer $n$, let $P(n)$ denote the largest prime divisor of $n$.
Prove that there exists infinitely many $n ∈ ℕ$ such that
$$ P(n^4 + n^2 + 1) = P((n + 1)^4 + (n + 1)^2 + 1). $$
-/
noncomputable def lpf (n : ℕ) : ℕ :=
  ((Nat.primeFactors n).toList.maximum?).getD 1

theorem imo_sl_2013_N3 :
    ∀ (C : ℕ), ∃ n ≥ C, lpf (n ^ 4 + n ^ 2 + 1)
      = lpf ((n + 1) ^ 4 + (n + 1) ^ 2 + 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2013 N6

Determine all functions $f : ℚ → ℤ$ such that for any $x ∈ ℚ$, $a ∈ ℤ$, and $b ∈ ℕ^+$,
$$ f\left(\frac{f(x) + a}{b}\right) = f\left(\frac{x + a}{b}\right). $$
-/
def good (f : ℚ → ℤ) :=
    ∀ (x : ℚ) (a : ℤ) (b : ℕ), 0 < b → f ((f x + a) / b) = f ((x + a) / b)


theorem imo_sl_2013_N6 : good f ↔ (∃ C, f = λ _ ↦ C) ∨ f = Int.floor ∨ f = Int.ceil := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2014 A1

Let $(z_n)_{n \ge 0}$ be an infinite sequence of positive integers.
1. Prove that there exists a unique non-negative integer $N$ such that
   $$ N z_N < \sum_{j = 0}^N z_j \le (N + 1) z_{N + 1}. $$
   (Note: The sum is often written as $\sum_{j=0}^{N-1} z_j \le N z_N < \sum_{j=0}^N z_j$. The version
   above is what is used in the formalization).
2. Prove that $N$ is positive.
3. Show that $\binom{N}{2} < z_0$.
-/
/- special open -/ open Finset
variable {z : ℕ → ℤ}

/-- `IsTheN z N` is the property that `N` satisfies the double inequality from the problem. -/
def IsTheN (z : ℕ → ℤ) (N : ℕ) : Prop :=
  (N : ℤ) * z N < (∑ i in range (N + 1), z i) ∧
  (∑ i in range (N + 1), z i) ≤ (N + 1) * z (N + 1)

theorem imo_sl_2014_A1 (hz_pos : ∀ n, 0 < z n) (hz_mono : StrictMono z) :
  (∃! N, IsTheN z N) ∧
  (∀ N, IsTheN z N → 0 < N ∧ N.choose 2 < (z 0).natAbs) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2014 A4

Let $b$ and $c$ be integers with $|b| > 1$ and $c ≠ 0$.
Find all functions $f : ℤ → ℤ$ such that, for any $x, y ∈ ℤ$,
$$ f(y + f(x)) - f(y) = f(bx) - f(x) + c. $$
-/
/- special open -/ open Finset
def good (b c : ℤ) (f : ℤ → ℤ) := ∀ x y : ℤ, f (y + f x) - f y = f (b * x) - f x + c

theorem imo_sl_2014_A4 {b c : ℤ} (h : 1 < b.natAbs) (h0 : c ≠ 0) :
    good b c f ↔ b - 1 ∣ c ∧ f = ((b - 1) * · + c / (b - 1)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2014 C4

Consider 4 types of skew-tetrominoes in $ℕ^2$, classified by its orientation.
Let $S ⊆ ℕ^2$ be a multiset, and suppose that it can be partitioned into skew-tetrominos.
Prove that the parity of the number of skew-tetrominoes used for
  each type in the partition does not depend on the partition.
-/
/- special open -/ open Multiset
/-- Base skew-tetrominoes, representing the four orientations. -/
def BaseSkewT4 : Fin 4 → Multiset (ℕ × ℕ)
  | 1 => {(0, 0), (1, 0), (1, 1), (2, 1)}
  | 2 => {(1, 0), (1, 1), (0, 1), (0, 2)}
  | 3 => {(0, 1), (1, 1), (1, 0), (2, 0)}
  | 4 => {(0, 0), (0, 1), (1, 1), (1, 2)}

/-- A specific skew-tetromino piece, defined by its type and position. -/
def SkewT4 (q : Fin 4 × ℕ × ℕ) : Multiset (ℕ × ℕ) :=
  (BaseSkewT4 q.1).map λ p ↦ q.2 + p

/--
Let `P` and `Q` be two different partitions of the same shape `S` into skew-tetrominoes.
This is formally stated as `(map SkewT4 P).sum = (map SkewT4 Q).sum`.
The theorem asserts that for any type `i`, the number of tetrominoes of that type
has the same parity in both partitions.
-/
theorem imo_sl_2014_C4 {P Q : Multiset (Fin 4 × ℕ × ℕ)}
    (h : (map SkewT4 P).sum = (map SkewT4 Q).sum) (i : Fin 4) :
    (P.map Prod.fst).count i % 2 = (Q.map Prod.fst).count i % 2 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-
# IMO 2014 N2

Determine all pairs $(x, y)$ of integers such that
$$ 7x^2 - 13xy + y^2 = (|x - y| + 1)^3. $$
-/
def good (x y : ℤ) := 7 * x ^ 2 - 13 * x * y + 7 * y ^ 2 = (|x - y| + 1) ^ 3

theorem imo_sl_2014_N2 :
    good x y ↔ (∃ m, (x, y) = (m ^ 3 + 2 * m ^ 2 - m - 1, m ^ 3 + m ^ 2 - 2 * m - 1)) ∨
      (∃ m, (x, y) = (m ^ 3 + m ^ 2 - 2 * m - 1, m ^ 3 + 2 * m ^ 2 - m - 1)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2014 N3

Consider a collection $C$ of coins, where each coin has a value of $1/n$ for some positive
integer $n$. A partition of $C$ into $N$ groups is called an *$N$-Cape Town* partition
if the total value of coins in each group is at most $1$.

Prove that if the total value of all coins in $C$ is at most $N + 1/2$,
then $C$ has an $(N + 1)$-Cape Town partition.
-/
/- special open -/ open Multiset
variable (N : ℕ) (C : Multiset ℕ)

/--
A `CapeTownPartition N C` is a partition of the multiset of coins `C` into `N + 1` groups,
where the sum of the values of the coins in each group is at most 1.
-/
structure CapeTownPartition where
  /-- The list of groups in the partition. -/
  part : Multiset (Multiset ℕ)
  /-- The number of groups is `N + 1`. -/
  card_part : card part = N + 1
  /-- The groups form a partition of `C`. -/
  sum_part : part.sum = C
  /-- The total value of coins in each group is at most 1. -/
  total_bound : ∀ G ∈ part, (G.map (fun x ↦ (x : ℚ)⁻¹)).sum ≤ 1

theorem imo_sl_2014_N3 (h_total_value : (C.map (fun x ↦ (x : ℚ)⁻¹)).sum ≤ (N : ℚ) + 1/2) :
  Nonempty (CapeTownPartition N C) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2014 N4

Prove that, for any positive integer $n > 1$, there exists infinitely many
  positive integers $k$ such that $⌊n^k/k⌋$ is odd.
-/
theorem imo_sl_2014_N4 (hn : 1 < n) (N) : ∃ k > N, Odd (n ^ k / k) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2015 A1

Let $F$ be a totally ordered field.
Let $(a_n)_{n ≥ 0}$ be a sequence of positive elements of $F$ such that
  $a_{k + 1} ≥ \dfrac{(k + 1) a_k}{a_k^2 + k}$ for all $k ∈ ℕ$.
Prove that, for every $n ≥ 2$,
$$ a_0 + a_1 + … + a_{n - 1} ≥ n. $$

### Further directions

Generalize to totally ordered semirings `R` with `ExistsAddOfLE R`.

If impossible, we can alternatively generalize the above sequence to
  two sequences $(a_n)_{n ≥ 0}$, $(b_n)_{n ≥ 0}$ satisfying
  $b_{k + 1} ≤ a_k + b_k$ and $a_k b_k ≥ k$ for all $k ∈ ℕ$.
-/
/- special open -/ open Finset
theorem imo_sl_2015_A1 [LinearOrderedField F]
    {a : ℕ → F} (h : ∀ k : ℕ, 0 < a k)
    (h0 : ∀ k : ℕ, ((k.succ : F) * a k) / (a k ^ 2 + (k : F)) ≤ a k.succ) :
    ∀ n : ℕ, 2 ≤ n → (n : F) ≤ (range n).sum (fun i => a i) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2015 A2

Find all functions $f : ℤ → ℤ$ such that, for any $x, y ∈ ℤ$,
$$ f(x - f(y)) = f(f(x)) - f(y) - 1. $$
-/
/- special open -/ open Finset
theorem imo_sl_2015_A2 {f : Int → Int} :
    (∀ x y, f (x - f y) = f (f x) - f y - 1) ↔ (f = λ _ ↦ -1) ∨ f = (· + 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2015 N1

Define the function $f : ℤ → ℤ$ by $f(n) = n ⌊n/2⌋$.
Find all integers $M$ such that $f^k(M)$ is even for some $k ∈ ℕ$.

### Notes

The original formulation is slightly different.
Instead of $f : ℤ → ℤ$, we define $f : ℚ → ℚ$ by $f(q) = q ⌊q⌋$.
Then the problem asks for which $M ∈ ℕ^+$ does there exists
  $k ∈ ℕ$ such that $f^k(M + 1/2)$ is an integer.
-/
/- special open -/ open Finset
abbrev f (n : ℤ) := n * (n / 2)

theorem imo_sl_2015_N1 : (∃ k : ℕ, 2 ∣ f^[k] M) ↔ M ≠ 3 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2015 N2

Let $a, b ∈ ℕ$ such that $a! + b! ∣ a! b!$.
Prove that $3a ≥ 2b + 2$, and find all the equality cases.
-/
/- special open -/ open Finset
def good (c d : ℕ) := c + d ∣ c * d

theorem imo_sl_2015_N2 (h : good a.factorial b.factorial) :
    2 * b + 2 ≤ 3 * a ∧ (2 * b + 2 = 3 * a ↔ a = 2 ∧ b = 2 ∨ a = 4 ∧ b = 5) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2015 N3

Let $m$ and $n > 1$ be positive integers such that $k ∣ m$ whenever $n ≤ k < 2n$.
Prove that $L - 1$ is not a power of $2$, where
$$ L = \prod_{k = n}^{2n - 1} \left(\frac{m}{k} + 1\right). $$
-/
/- special open -/ open Finset
theorem imo_sl_2015_N3 (hm : 0 < m) (hn : 1 < n) (h : ∀ k ∈ Ico n (2 * n), k ∣ m) :
    ∀ N, ∏ k ∈ Ico n (2 * n), (m / k + 1) ≠ 2 ^ N + 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 A1

Let $R$ be a totally ordered commutative ring.
Let $a_1, a_2, \dots, a_n, c \in R$ be non-negative elements such that $a_i a_j \ge c$
whenever $i \ne j$.
Let $r \in R$ be an element such that $n r \ge a_1 + a_2 + \dots + a_n$.
Prove that
$$ \prod_{i = 1}^n (a_i^2 + c) \le (r^2 + c)^n. $$
-/
/- special open -/ open Finset
theorem imo_sl_2016_A1 [LinearOrderedCommRing R] (n : ℕ) (a : Fin n → R) (c r : R)
    (ha : ∀ i, 0 ≤ a i)
    (hc : 0 ≤ c)
    (h_prod_ge : ∀ i j, i ≠ j → c ≤ a i * a j)
    (hr_ge_avg : ∑ i, a i ≤ n • r) :
    ∏ i, (a i ^ 2 + c) ≤ (r ^ 2 + c) ^ n := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 A4

Let $M$ be an integral multiplicative monoid with a cancellative, distributive addition.
Find all functions $f : M → M$ such that, for all $x, y ∈ M$,
$$ x f(x^2) f(f(y)) + f(y f(x)) = f(xy) \left(f(f(y^2)) + f(f(x^2))\right). $$
-/
def good [Mul M] [Add M] (f : M → M) :=
  ∀ x y, x * f (x * x) * f (f y) + f (y * f x) = f (x * y) * (f (f (y * y)) + f (f (x * x)))

class CancelCommDistribMonoid (M) extends CancelCommMonoid M, Distrib M

variable [CancelCommDistribMonoid M]

theorem imo_sl_2016_A4 [IsCancelAdd M] {f : M → M} : good f ↔ ∀ x, x * f x = 1 := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 A5

1. Prove that, for every $n ∈ ℕ$, there exists some $a, b ∈ ℕ$
  such that $0 < b ≤ \sqrt{n} + 1$ and $b^2 n ≤ a^2 ≤ b^2 (n + 1)$.
-/
theorem imo_sl_2016_A5a_part1 (n) :
    ∃ a b, 0 < b ∧ b ≤ n.sqrt + 1 ∧ b ^ 2 * n ≤ a ^ 2 ∧ a ^ 2 ≤ b ^ 2 * (n + 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 A5

2. Prove that, for infinitely many $n ∈ ℕ$, there does not exist $a, b ∈ ℕ$
  such that $0 < b ≤ \sqrt{n}$ and $b^2 n ≤ a^2 ≤ b^2 (n + 1)$.
-/
theorem imo_sl_2016_A5b_part2 (N) :
    ∃ n > N, ¬∃ a b, 0 < b ∧ b ≤ n.sqrt ∧ b ^ 2 * n ≤ a ^ 2 ∧ a ^ 2 ≤ b ^ 2 * (n + 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 A7

Let $R$ be a ring and $S$ be a totally ordered commutative ring.
Find all functions $f : R \to S$ such that for any $x, y \in R$,
$$ f(x + y)^2 = 2 f(x) f(y) + \max\{f(x^2) + f(y^2), f(x^2 + y^2)\}. $$
-/
variable {R : Type*} [Ring R] {S : Type*} [LinearOrderedCommRing S]

/-- A function `f` is a solution if it satisfies the functional equation. -/
def IsSolution (f : R → S) : Prop :=
  ∀ x y : R, f (x + y) ^ 2 = 2 * f x * f y + max (f (x ^ 2) + f (y ^ 2)) (f (x ^ 2 + y ^ 2))

theorem imo_sl_2016_A7 (f : R → S) :
  IsSolution f ↔
    (f = (fun _ ↦ (0 : S)) ∨ ∃ (phi : RingHom R S), f = phi) ∨
    (f = (fun _ ↦ (-1 : S)) ∨ ∃ (phi : RingHom R S), f = (fun x ↦ phi x - 1)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 N4

Consider some $k, ℓ, m, n ∈ ℕ^+$ with $n > 1$ such that
$$ n^k + mn^ℓ + 1 ∣ n^{k + ℓ} - 1. $$
Prove that one of the following holds:
* $m = 1$ and $ℓ = 2k$; or
* $k = (t + 1)ℓ$ and $m(n^ℓ - 1) = n^{t ℓ} - 1$ for some $t > 0$.
-/
theorem imo_sl_2016_N4 (hk : 0 < k) (hl : 0 < l) (hm : 0 < m) (hn : 1 < n)
    (h : n ^ k + m * n ^ l + 1 ∣ n ^ (k + l) - 1) :
    (m = 1 ∧ l = 2 * k) ∨ (∃ t > 0, k = (t + 1) * l ∧ m * (n ^ l - 1) = n ^ (l * t) - 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2016 N5

Fix some $k, a ∈ ℤ$ with $k ≥ 0$ and $a > 0$.
A pair $(x, y) ∈ ℤ^2$ is called *nice* if $(k + 1) y^2 - k x^2 = a$.
Prove that the following two statements are equivalent:
* There exists a nice pair $(x, y)$ with $x ≥ 0$ and $x^2 > a$;
* There exists a nice pair $(x, y)$ with $x ≥ 0$ and $x^2 ≤ a$.
-/
def nice (k a x y : ℤ) := (k + 1) * y ^ 2 - k * x ^ 2 = a

theorem imo_sl_2016_N5 :
    (∃ x y, 0 ≤ x ∧ a < x ^ 2 ∧ nice k a x y)
      ↔ (∃ x y, 0 ≤ x ∧ x ^ 2 ≤ a ∧ nice k a x y) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 A1

Let $a_1, a_2, \dots, a_n$ and $k$ be positive integers such that the sum of the reciprocals
of the $a_i$ is equal to $k$, i.e., $\sum_{i=1}^n \frac{1}{a_i} = k$.
Suppose that at least one of the $a_i$ is greater than $1$.

Prove that for any totally ordered commutative semiring $R$ and any positive element $x \in R$,
$$ \left(\prod_{i=1}^n a_i\right) (x + 1)^k < \prod_{i=1}^n (x + a_i). $$
-/
/- special open -/ open Finset
theorem imo_sl_2017_A1 [LinearOrderedField R] (n : ℕ) (a : Fin n → ℕ) (k : ℕ) (x : R)
    (ha_pos : ∀ i, 0 < a i)
    (ha_gt_one : ∃ i, 1 < a i)
    (hk_sum : (∑ i, (a i : ℚ)⁻¹) = k)
    (hx : 0 < x) :
    ((∏ i, a i) : R) * (x + 1) ^ k < ∏ i, (x + (a i : R)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 A3

Let $S$ be a finite set, and fix some $f : S → S$.
Suppose that, for any $g : S → S$, $$f ∘ g ∘ f = g ∘ f ∘ g \implies g = f. $$
Prove that $f^2(S) = f(S)$.
-/
theorem imo_sl_2017_A3 (h : ∀ g : S → S, f ∘ g ∘ f = g ∘ f ∘ g → g = f) :
    Set.range f^[2] = Set.range f := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 A4

Let $G$ be a totally ordered abelian group and let $D$ be a natural number.
A sequence $(a_n)_{n \ge 0}$ of elements of $G$ satisfies the following properties:
* for any $i, j \in \mathbb{N}$ with $i + j \ge D$, we have $a_{i + j + 1} \le -a_i - a_j$,
* for any $n \ge D$, there exists $i, j \in \mathbb{N}$ such that
  $i + j = n$ and $a_{n + 1} = -a_i - a_j$.

Prove that $(a_n)_{n \ge 0}$ is bounded. Explicitly, prove that for all $n$,
$|a_n| \le 2 \max\{B, C - B\}$, where
$B = \max_{k \le D} a_k$ and $C = \max_{k \le D} (-a_k)$.
-/
variable {G : Type*} [LinearOrderedAddCommGroup G]

def seqMax (a : ℕ → G) (n : ℕ) : G :=
  if h : n = 0 then
    a 0
  else
    max (seqMax a (n-1)) (a n)

def IsGood1 (D : ℕ) (a : ℕ → G) : Prop :=
  ∀ i j : ℕ, D ≤ i + j → a (i + j + 1) ≤ -(a i) - (a j)

def IsGood2 (D : ℕ) (a : ℕ → G) : Prop :=
  ∀ n ≥ D, ∃ i j : ℕ, i + j = n ∧ a (n + 1) = -(a i) - (a j)

theorem imo_sl_2017_A4 (D : ℕ) (a : ℕ → G) (h1 : IsGood1 D a) (h2 : IsGood2 D a) (n : ℕ) :
  |a n| ≤ max ((2 : ℕ) • seqMax a D) ((2 : ℕ) • (seqMax (fun i => -a i) D - seqMax a D)) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 A6

Let $R$ be a ring, $G$ be an abelian (additive) group, and $\iota : G \to R$ be a group
homomorphism. Find all functions $f : R \to G$ such that for any $x, y \in R$,
$$ f(\iota(f(x)) \iota(f(y))) + f(x + y) = f(xy). $$
-/
variable {R G : Type*}

def IsGoodFun [Ring R] [AddCommGroup G] (ι : G →+ R) (f : R → G) : Prop :=
  ∀ x y : R, f (ι (f x) * ι (f y)) + f (x + y) = f (x * y)

@[ext] structure CentralInvolutive (R : Type*) [Ring R] where
  val : R
  val_mul_self_eq_one : val * val = 1
  val_mul_comm (x : R) : x * val = val * x

theorem imo_sl_2017_A6 [Ring R] [AddCommGroup G]
    (hG2 : ∀ x y : G, 2 • x = 2 • y → x = y)
    (hG3 : ∀ x y : G, 3 • x = 3 • y → x = y)
    (ι : G →+ R) (f : R → G) :
    IsGoodFun ι f ↔
      ∃ (rc : RingCon R) (a : CentralInvolutive (rc.Quotient))
        (phi : {ψ : rc.Quotient →+ G // ∀ x, rc.toQuotient (ι (ψ x)) = x}),
        f = fun x ↦ phi.1 (a.val * (rc.toQuotient (1 - x))) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 A7

Let $(b_n)_{n \ge 0}$ be a sequence of positive integers.
Let $(a_n)_{n \ge 0}$ be a sequence of integers defined by $a_0 = 0$, $a_1 = 1$, and
for $n \ge 0$:
- $a_{n + 2} = a_{n + 1} b_{n + 1} + a_n$ if $b_n = 1$;
- $a_{n + 2} = a_{n + 1} b_{n + 1} - a_n$ if $b_n > 1$.

Prove that $\max\{a_n, a_{n + 1}\} \ge n$ for any $n \ge 0$.
-/
def a (b : ℕ → ℤ) : ℕ → ℤ
  | 0 => 0
  | 1 => 1
  | n + 2 => a b (n + 1) * b (n + 1) + a b n * if b n = 1 then 1 else -1

theorem imo_sl_2017_A7 (b : ℕ → ℤ) (b_pos : ∀ n, 0 < b n) (n : ℕ) :
  (n : ℤ) ≤ a b n ∨ (n : ℤ) ≤ a b (n + 1) := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 A8

Let $G$ be a totally ordered abelian group. We are interested in functions $f : G \to G$
that satisfy the following property: for any $x, y \in G$,
$$ \text{if } f(x) + y < f(y) + x, \text{ then } f(x) + y \le 0 \le f(y) + x. $$

The problem is to show that if $G$ is densely ordered, then every function $f$ with the
above property also satisfies:
$$ f(y) + x \le f(x) + y \quad \text{for all } x, y \in G \text{ with } x \le y. $$

-/
def IsGood {G : Type*} [AddCommGroup G] [LinearOrder G] (f : G → G) : Prop :=
  ∀ x y : G, f x + y < f y + x → f x + y ≤ 0 ∧ 0 ≤ f y + x

theorem imo_sl_2017_A8 {G : Type*} [AddCommGroup G] [LinearOrder G] :
  (∀ (f : G → G), IsGood f → (∀ x y, x ≤ y → f y + x ≤ f x + y)) ↔ DenselyOrdered G := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 C1

A rectangle in $ℕ^2$ is a subset of form $\{a, a + 1, …, a + w - 1\}
  × \{b, b + 1, …, b + h - 1\}$ for some $a, b, w, h ∈ ℕ$.
Given such rectangle, the quantity $w$ and $h$ are called the
  *width* and *height* of the rectangle, respectively.

A rectangle $R$ in $ℕ^2$ with odd width and height is
  partitioned into small rectangles.
Prove that there exists a small rectangle $R'$ with the following property:
  the distances from the sides of $R'$ to the respective sides
    of $R$ all have the same parity.
-/
/- special open -/ open Finset
def latticeRect (q : (ℕ × ℕ) × ℕ × ℕ) : Finset (ℕ × ℕ) :=
  Ico q.1.1 (q.1.1 + q.2.1) ×ˢ Ico q.1.2 (q.1.2 + q.2.2)

theorem imo_sl_2017_C1 {I : Finset ι}
    (h : (I : Set ι).PairwiseDisjoint (latticeRect ∘ Q))
    (h0 : m.bodd = true ∧ n.bodd = true)
    (h1 : latticeRect ⟨⟨0, 0⟩, ⟨m, n⟩⟩ = I.disjiUnion (latticeRect ∘ Q) h) :
    ∃ i ∈ I, ((Q i).2.1.bodd = true ∧ (Q i).2.2.bodd = true)
      ∧ ((Q i).1.1 + (Q i).1.2).bodd = false := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 N1 (P1)

For each $n ∈ ℕ$, define $f(n)$ by $\sqrt{n}$ if $n$ is a square and $n + 3$ otherwise.
Find all $N ∈ ℕ$ such that $\{n : f^n(N) = a\}$ is infinite for some $a ∈ ℕ$.

-/
/- special open -/ open Finset
def f (p k : ℕ) : ℕ := if k.sqrt ^ 2 = k then k.sqrt else k + p

def good (p N : ℕ) := ∃ a, ∀ m, ∃ n ≥ m, (f p)^[n] N = a

theorem imo_sl_2017_N1 : good 3 N ↔ N = 1 ∨ 3 ∣ N := by sorry
import Mathlib
import Aesop

set_option maxHeartbeats 0

open BigOperators Real Nat Topology Rat

/-!
# IMO 2017 N3

Let $n > 1$ be an integer.
A *special $n$-tuple* is an $n$-tuple $\mathbf{a} = (a_0, a_1, …, a_{n - 1})$ of integers
  such that there exists an indexing function $f : [n] → [n]$ such that for all $i$,
$$ n ∣ a_i + a_{i + 1} + … + a_{i + f(i)}. $$
Determine all $n > 1$ such that any special $n$-tuple $\mathbf{a}$ satisfies
$$ n ∣ a_0 + a_1 + … + a_{n-1}. $$
-/
/- special open -/ open Finset Function
structure SpecialTuple (n : ℕ) where
  toFun : Fin n.pred.succ → ℤ
  jump_shift : Fin n.pred.succ → Fin n.pred.succ
  jump_shift_spec : ∀ i, (n : ℤ) ∣ ∑ j ∈ Ico i.1 (i.1 + ((jump_shift i).1 + 1)), toFun j

def sum (X : SpecialTuple n) : ℤ := ∑ i, X.toFun i

theorem imo_sl_2017_N3 (hn : 1 < n) : (∀ X : SpecialTuple n, (n : ℤ) ∣ sum X) ↔ n.Prime := by sorry