End power_injective.
Fact divides_power p a b : a <= b -> divides (power a p) (power b p).
Fact divides_msum k n f : (forall i, i < n -> divides k (f i)) -> divides k (∑ n f).
Fact inc_seq_split_lt n f k :
(forall i j, i < j < n -> f i < f j)
-> { p | p <= n /\ (forall i, i < p -> f i < k) /\ forall i, p <= i < n -> k <= f i }.
Fact inc_seq_split_le n f h : (forall i j, i < j < n -> f i < f j)
-> { q | q <= n
/\ (forall i, i < q      -> f i <= h)
/\ (forall i, q <= i < n -> h < f i) }.
Fact divides_lt p q : q < p -> divides p q -> q = 0.
Fact sum_powers_inc_lt_last n f r :
2 <= r
-> (forall i j, i < j <= n -> f i < f j)
-> ∑ (S n) (fun i => power (f i) r) < power (S (f n)) r.
Fact sum_powers_inc_lt n f p r :
2 <= r
-> (forall i, i < n -> f i < p)
-> (forall i j, i < j < n -> f i < f j)
-> ∑ n (fun i => power (f i) r) < power p r.
(* the value r^f1 + ... + f^fn uniquely determines n and f1 < ... < fn  *)
Fact sum_powers_injective r n f m g :
2 <= r
-> (forall i j, i < j < n -> f i < f j)
-> (forall i j, i < j < m -> g i < g j)
-> ∑ n (fun i => power (f i) r) = ∑ m (fun i => power (g i) r)
-> n = m /\ forall i, i < n -> f i = g i.
Fact power_divides_sum_power r p n f :
2 <= r
-> 0 < n
-> (forall i j, i < j < n -> f i < f j)
-> divides (power p r) (∑ n (fun i => power (f i) r)) <-> p <= f 0.
Fact smono_upto_injective n f :
(forall i j, i < j < n -> f i < f j)
-> (forall i j, i < n -> j < n -> f i = f j -> i = j).
Fact product_sums n f g : (∑ n f)*(∑ n g)
= ∑ n (fun i => f i*g i)
+ ∑ n (fun i => ∑ i (fun j => f i*g j + f j*g i)).
Section sums.
Fact square_sum n f : (∑ n f)*(∑ n f) = ∑ n (fun i => f i*f i) + 2*∑ n (fun i => ∑ i (fun j => f i*f j)).
Fact sum_regroup r k n f :
(forall i, i < n -> f i < k)
-> (forall i j, i < j < n -> f i < f j)
-> { g | ∑ n (fun i => power (f i) r)
= ∑ k (fun i => g i * power i r)
/\ (forall i, i < k  -> g i <= 1)
/\ (forall i, k <= i -> g i = 0) }.
Section sum_sum_regroup.
Variable (r n k : nat) (f : nat -> nat)
(Hf1 : forall i, i < n -> f i <= k)
(Hf2 : forall i j, i < j < n -> f i < f j).

Theorem sum_sum_regroup : { g | ∑ n (fun i => ∑ i (fun j => power (f i + f j) r))
= ∑ (2*k) (fun i => g i * power i r)
/\ forall i, g i <= n }.