Sum of Totient Function¶
- Title:
Sum of Totient Function - Judge / source:
Library Checker - Original URL: https://judge.yosupo.jp/problem/sum_of_totient_function
- Secondary topics:
Du Jiao sieve,Quotient set,Dirichlet inverse recurrence - Difficulty:
hard - Subtype:
Prefix phi via phi * 1 = id on Q_n - Status:
solved - Solution file: sumoftotientfunction.cpp
Why Practice This¶
This is the cleanest in-repo anchor for the first exact Min_25 / Du Jiao lane.
The statement is just:
compute sum_{k<=n} phi(k)
but the reusable lesson is:
- direct sieving to
nis no longer the right scale - the answer is recovered from a Dirichlet relation
- the real state space is the quotient set
Q_n
It is the first point in this repo where the prefix sum itself becomes the unknown object solved by recurrence.
Recognition Cue¶
Reach for this lane when:
- the target is one huge prefix sum of
phiormu - direct
O(n)sieving is too large - the repeated subproblems are values of
floor(n / i) - the statement is about one large arithmetic-function prefix query, not an array of bounded values
The strongest smell here is:
I know a Dirichlet identity for the function,
but I need the prefix sum itself, not the point values.
Problem-Specific Transformation¶
Start from:
Summing over all m <= n gives:
where:
Now isolate the k = 1 term:
Then group equal quotients:
so the whole block contributes:
Core Idea¶
Solve the recurrence on the quotient set:
not on every integer from 1..n.
That works because:
So once the values of Phi(q) for smaller quotient states are known, the current Phi(x) is computed by grouped subtraction from:
The implementation in this repo builds Q_n, processes it in ascending order, and stores:
prefix_phi(x)prefix_mu(x)
for every x in the set.
Complexity¶
- state count:
|Q_n| = O(sqrt(n)) - each state uses grouped floor-division blocks
- overall: sublinear in
n, good enough for the intended large single-query route
This repo starter is the clean introductory implementation, not the last word on asymptotics.
Pitfalls / Judge Notes¶
- The problem asks for the answer modulo
998244353. - Do not try to sieve
phiall the way ton. - The grouped recurrence uses block lengths
(r-l+1), not the quotientqitself as the coefficient. - If you treat this as the same lane as
sum sigma, you will miss thatPhiis now the unknown. - Full Min_25 machinery is outside the first starter even though the lane title includes it.
Reusable Pattern¶
- Topic page: Min_25 / Du Jiao
- Practice ladder: Min_25 / Du Jiao ladder
- Starter template: du-jiao-prefix-phi-mu.cpp
- Notebook refresher: Min_25 / Du Jiao hot sheet
- Compare points:
- Sum of Divisors
- Dirichlet Convolution / Prefix Sums Of Number-Theoretic Functions
- This note adds: the first in-repo route where one arithmetic-function prefix sum is recovered implicitly on
Q_ninstead of opened directly by one divisor-side formula.
Solutions¶
- Code: sumoftotientfunction.cpp