# 推导

$\sum\limits_{i=1}^n\sum\limits_{d|i}g(\frac{i}{d})f(d)$

$\sum\limits_{i=1}^n\sum\limits_{d\neq 1,d|i}g(d)f(\frac{i}{d})$

$=\sum\limits_{d=2}^n g(d)\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}f(i)$

$=\sum\limits_{d=2}^n g(d)S(\lfloor\frac{n}{d}\rfloor)$

# 例子

$S(n)=\sum\limits_{i=1}^n\sum\limits_{d|i}\varphi(d)-\sum\limits_{i=1}^n\sum\limits_{d|i,d\neq i}\varphi(d)\\ = \sum\limits_{i=1}^ni-\sum\limits_{i=1}^n\sum\limits_{d|i,d\neq 1}\varphi(\frac{n}{d})\\ =\frac{n(n+1)}{2}-\sum\limits_{d=2}^n\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\varphi(i)\\ = \frac{n(n+1}{2}-\sum\limits_{d=2}^nS(\lfloor\frac{n}{d}\rfloor)$

$S(n)=\sum\limits_{i=1}^n\sum\limits_{d|i}\mu(d)-\sum\limits_{i=1}^n\sum\limits_{d|i,d\neq i}\mu(d)\\ =1-\sum\limits_{d=2}^n\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\mu(i)\\ = 1-\sum\limits_{d=2}^nS(\lfloor\frac{n}{d}\rfloor)$

# 代码

void pre() {
phi[1] = 1;
for(int i = 2;i < N;++i) {
if(!vis[i]) {
prime[++tot] = i;
phi[i] = i - 1;
}
for(int j = 1;j <= tot && prime[j] * i < N;++j) {
vis[prime[j] * i] = 1;
if(i % prime[j]) {
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
else {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
}
}
for(int i = 2;i < N;++i) phi[i] = (phi[i] + phi[i - 1]) % mod;
}
ll MAX;

ll PHI(ll n) {
if(n < N) return phi[n];

if(vis[MAX / n]) return maphi[MAX / n];
vis[MAX / n] = 1;
ll ret = (n % mod) * ((n + 1) % mod) % mod * inv % mod;

for(ll l = 2,r;l <= n;l = r + 1) {
r = n / (n / l);
ret -= ((r - l + 1) % mod) * PHI(n / l) % mod;
ret %= mod;
}
return maphi[MAX / n] = ret;
}


posted @ 2020-04-25 08:26  wxyww  阅读(...)  评论(...编辑  收藏