Totient Sum Optimization
Find sum_(k=1)^N varphi(k) (mod 10^9+7) for N = 10^7.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A full $k$-ary tree is a tree with a single root node, such that every node is either a leaf or has exactly $k$ ordered children. The height of a $k$-ary tree is the number of edges in the longest path from the root to a leaf.
For instance, there is one full 3-ary tree of height 0, one full 3-ary tree of height 1, and seven full 3-ary trees of height 2. These seven are shown below.

For integers $n$ and $k$ with $n\ge 0$ and $k \ge 2$, define $t_k(n)$ to be the number of full $k$-ary trees of height $n$ or less.
Thus, $t_3(0) = 1$, $t_3(1) = 2$, and $t_3(2) = 9$. Also, $t_2(0) = 1$, $t_2(1) = 2$, and $t_2(2) = 5$.
Define $S_k$ to be the set of positive integers $m$ such that $m$ divides $t_k(n)$ for some integer $n\ge 0$. For instance, the above values show that 1, 2, and 5 are in $S_2$ and 1, 2, 3, and 9 are in $S_3$.
Let $S = \bigcap_p S_p$ where the intersection is taken over all primes $p$. Finally, define $R(N)$ to be the sum of all elements of $S$ not exceeding $N$. You are given that $R(20) = 18$ and $R(1000) = 2089$.
Find $R(10^7)$.
Problem 927: Totient Sum Optimization
Mathematical Foundation
Theorem 1 (Euler’s Product Formula). For :
Proof. By inclusion-exclusion on the set , subtracting multiples of each prime dividing :
Alternatively, since is multiplicative and (there are integers in coprime to ), the product formula follows from multiplicativity.
Theorem 2 (Divisor Sum Identity). For all : .
Proof. Partition by the value of . For each divisor , the set has cardinality (since iff , and ranges over ). Summing: . Reindexing gives .
Theorem 3 (Sub-linear Recursion for ). Let . Then:
Proof. From Theorem 2, summing over :
The left side is . Grouping by :
Equivalently, using hyperbola-method notation: (by Dirichlet’s hyperbola method applied to ). Isolating :
Lemma 1 (Complexity of the Recursion). Using memoization and grouping identical values of , the recursion computes in time and space.
Proof. The set has distinct values. With a sieve precomputing for , the remaining values are handled by the recursion, each requiring work. Total: .
Theorem 4 (Asymptotic Formula). .
Proof. Since (Mobius inversion of Theorem 2), the Dirichlet series . By standard Tauberian arguments (or Perron’s formula), , using .
Editorial
Compute the sum of Euler’s totient function phi(k) for k = 1..N, modulo 10^9+7. Uses a sieve-based approach to compute all phi values efficiently. Key ideas:.
Pseudocode
Sieve phi(n) for n = 1..N, then sum
phi[1..N]
For n from 1 to N:
phi[n] = n
For p from 2 to N:
if phi[p] == p: // p is prime
For m from p to N step p:
phi[m] = phi[m] / p * (p - 1)
S = 0
For n from 1 to N:
S = (S + phi[n]) mod MOD
Return S
Complexity Analysis
- Time: for the Euler sieve. The inner loop over primes processes multiples, and by Mertens’ theorem.
- Space: for the array .
For the sub-linear approach: time and space (see Lemma 1).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/*
* Problem 927: Totient Sum Optimization
*
* Find sum_{k=1}^{10^7} phi(k) mod 10^9+7.
*
* Euler sieve: phi(n) = n * prod_{p|n} (1 - 1/p).
* Initialize phi[n] = n, then for each prime p:
* phi[m] -= phi[m]/p for all m divisible by p.
*
* Key identity: sum_{d|n} phi(d) = n.
* Asymptotic: Phi(N) = 3N^2/pi^2 + O(N log N).
*
* Complexity: O(N log log N) sieve, O(N) summation.
*/
int main() {
const int N = 10000000;
const long long MOD = 1000000007;
vector<int> phi(N + 1);
iota(phi.begin(), phi.end(), 0);
for (int p = 2; p <= N; p++) {
if (phi[p] == p) { // p is prime
for (int m = p; m <= N; m += p) {
phi[m] -= phi[m] / p;
}
}
}
long long ans = 0;
for (int k = 1; k <= N; k++) {
ans = (ans + phi[k]) % MOD;
}
cout << ans << endl;
// Verify: phi(1)=1, phi(2)=1, phi(6)=2, phi(12)=4
assert(phi[1] == 1);
assert(phi[2] == 1);
assert(phi[6] == 2);
assert(phi[12] == 4);
return 0;
}
"""
Problem 927: Totient Sum Optimization
Compute the sum of Euler's totient function phi(k) for k = 1..N, modulo 10^9+7.
Uses a sieve-based approach to compute all phi values efficiently.
Key ideas:
- phi(n) = n * product(1 - 1/p) for each prime p dividing n.
- Sieve of Eratosthenes variant computes phi for all n in O(N log log N).
- Summatory totient: Phi(N) = sum_{k=1}^{N} phi(k) ~ 3N^2 / pi^2.
- phi(n)/n ratio distribution reflects multiplicative structure.
Methods:
1. Euler sieve for phi(1..N)
2. Direct phi computation via factorization (verification)
3. Ratio phi(n)/n distribution analysis
4. Summatory function vs asymptotic approximation
"""
from math import gcd
def euler_sieve(N):
"""Compute phi(0..N) using multiplicative sieve."""
phi = list(range(N + 1))
for p in range(2, N + 1):
if phi[p] == p: # p is prime
for m in range(p, N + 1, p):
phi[m] -= phi[m] // p
return phi
def solve(N=10**7):
MOD = 10**9 + 7
phi = euler_sieve(N)
return sum(phi[1:]) % MOD
def phi_direct(n):
"""Compute phi(n) by counting coprime integers."""
if n <= 1:
return n
return sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)
def phi_factor(n):
"""Compute phi(n) from prime factorization."""
result = n
p = 2
temp = n
while p * p <= temp:
if temp % p == 0:
while temp % p == 0:
temp //= p
result -= result // p
p += 1
if temp > 1:
result -= result // temp
return result
def summatory_totient(phi_list):
"""Compute cumulative sum of phi values."""
cumsum = [0]
for k in range(1, len(phi_list)):
cumsum.append(cumsum[-1] + phi_list[k])
return cumsum
# Solve and verify
answer = solve()
# Verify sieve against direct computation for small values
phi_small = euler_sieve(100)
for n in range(1, 101):
assert phi_small[n] == phi_direct(n), f"Mismatch at n={n}"
assert phi_small[n] == phi_factor(n), f"Factor mismatch at n={n}"
# Known values
assert phi_small[1] == 1
assert phi_small[2] == 1
assert phi_small[6] == 2
assert phi_small[12] == 4
assert phi_small[97] == 96 # prime
print(answer)