MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / sumphi

Function sumphi

code/mathematics/summatory_phi.cpp:4–11  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2ll sp[N];
3unordered_map<ll,ll> mem;
4ll sumphi(ll n) {
5 if (n < N) return sp[n];
6 if (mem.find(n) != mem.end()) return mem[n];
7 ll ans = 0, done = 1;
8 for (ll i = 2; i*i <= n; i++) ans += sumphi(n/i), done = i;
9 for (ll i = 1; i*i <= n; i++)
10 ans += sp[i] * (n/i - max(done, n/(i+1)));
11 return mem[n] = n*(n+1)/2 - ans; }
12void sieve() {
13 for (int i = 1; i < N; i++) sp[i] = i;
14 for (int i = 2; i < N; i++) {

Callers 1

testFunction · 0.85

Calls 1

findMethod · 0.45

Tested by 1

testFunction · 0.68