前置知识:杜教筛
求 ∑ i = 1 n ϕ ( i ) \sum\limits_{i=1}^n\phi(i) i=1∑nϕ(i)和 ∑ i = 1 n μ ( i ) \sum\limits_{i=1}^n \mu(i) i=1∑nμ(i),其中 1 ≤ n ≤ 1 0 9 1\leq n\leq 10^9 1≤n≤109。
先求 ∑ i = 1 n ϕ ( i ) \sum\limits_{i=1}^n\phi(i) i=1∑nϕ(i),我们知道 ϕ ∗ I = I d \phi*I=Id ϕ∗I=Id
I d ( n ) = ∑ d ∣ n ϕ ( d ) × I ( n d ) = ϕ ( n ) + ∑ d ∣ n , d > 1 I ( d ) × ϕ ( n d ) Id(n)=\sum\limits_{d|n}\phi(d)\times I(\dfrac nd)=\phi(n)+\sum\limits_{d|n,d>1}I(d)\times \phi(\dfrac nd) Id(n)=d∣n∑ϕ(d)×I(dn)=ϕ(n)+d∣n,d>1∑I(d)×ϕ(dn)
分别求前缀和,得
n ( n + 1 ) 2 = ∑ i = 1 n ϕ ( i ) + ∑ i = 1 n ∑ d ∣ i , d > 1 I ( d ) × ϕ ( i d ) \dfrac{n(n+1)}{2}=\sum\limits_{i=1}^n\phi(i)+\sum\limits_{i=1}^n\sum\limits_{d|i,d>1}I(d)\times \phi(\dfrac id) 2n(n+1)=i=1∑nϕ(i)+i=1∑nd∣i,d>1∑I(d)×ϕ(di)
∑ i = 1 n ϕ ( i ) = n ( n + 1 ) 2 − ∑ d = 2 n I ( d ) ∑ i = 1 ⌊ n d ⌋ ϕ ( i ) \sum\limits_{i=1}^n\phi(i)=\dfrac{n(n+1)}{2}-\sum\limits_{d=2}^nI(d)\sum\limits_{i=1}^{\lfloor \frac nd\rfloor}\phi(i) i=1∑nϕ(i)=2n(n+1)−d=2∑nI(d)i=1∑⌊dn⌋ϕ(i)
设 f ( n ) = ∑ i = 1 n ϕ ( i ) f(n)=\sum\limits_{i=1}^n\phi(i) f(n)=i=1∑nϕ(i),则上式变为
f ( n ) = n ( n + 1 ) 2 − ∑ i = 2 n I ( d ) × f ( ⌊ n d ⌋ ) f(n)=\dfrac{n(n+1)}{2}-\sum\limits_{i=2}^nI(d)\times f(\lfloor \dfrac nd\rfloor) f(n)=2n(n+1)−i=2∑nI(d)×f(⌊dn⌋)
预处理出前 n 2 3 n^{\frac 23} n32个 f f f值,则时间复杂度为 O ( n 2 3 ) O(n^{\frac 23}) O(n32)。
再来求 ∑ i = 1 n μ ( i ) \sum\limits_{i=1}^n\mu(i) i=1∑nμ(i),我们知道 μ ∗ I = ϵ \mu*I=\epsilon μ∗I=ϵ
ϵ ( n ) = ∑ d ∣ n μ ( d ) × I ( n d ) = μ ( n ) × I ( 1 ) + ∑ d ∣ n , d < n μ ( d ) × I ( n d ) \epsilon(n)=\sum\limits_{d|n}\mu(d)\times I(\dfrac nd)=\mu(n)\times I(1)+\sum\limits_{d|n,d<n}\mu(d)\times I(\dfrac nd) ϵ(n)=d∣n∑μ(d)×I(dn)=μ(n)×I(1)+d∣n,d<n∑μ(d)×I(dn)
分别求前缀和,得
1 = ∑ i = 1 n μ ( i ) + ∑ i = 1 n ∑ d ∣ i , d > 1 I ( d ) × μ ( i d ) = f ( n ) + ∑ d = 2 n I ( d ) ∑ i = 1 ⌊ n d ⌋ μ ( i ) 1=\sum\limits_{i=1}^n\mu(i)+\sum\limits_{i=1}^n \sum\limits_{d|i,d>1}I(d)\times \mu(\dfrac id)=f(n)+\sum\limits_{d=2}^nI(d)\sum\limits_{i=1}^{\lfloor \frac nd\rfloor}\mu(i) 1=i=1∑nμ(i)+i=1∑nd∣i,d>1∑I(d)×μ(di)=f(n)+d=2∑nI(d)i=1∑⌊dn⌋μ(i)
设 f ( n ) = ∑ i = 1 n μ ( i ) f(n)=\sum\limits_{i=1}^n\mu(i) f(n)=i=1∑nμ(i),则
1 = f ( n ) + ∑ d = 2 n I ( d ) × f ( ⌊ n d ⌋ ) 1=f(n)+\sum\limits_{d=2}^nI(d)\times f(\lfloor \dfrac nd\rfloor) 1=f(n)+d=2∑nI(d)×f(⌊dn⌋)
f ( n ) = 1 − ∑ d = 2 n I ( d ) × f ( ⌊ n d ⌋ ) f(n)=1-\sum\limits_{d=2}^nI(d)\times f(\lfloor \dfrac nd\rfloor) f(n)=1−d=2∑nI(d)×f(⌊dn⌋)
同样可以用 O ( n 2 3 ) O(n^{\frac 23}) O(n32)的时间复杂度解决。
code
#include<bits/stdc++.h>
using namespace std;
const int N=5000000;
int z[N+5],p[N+5],mu[N+5];
long long ph[N+5],sph[N+5],smu[N+5];
map<long long,long long>mph,mmu;
void init(){
mu[1]=ph[1]=1;
for(int i=2;i<=N;i++){
if(!z[i]){
p[++p[0]]=i;mu[i]=-1;ph[i]=i-1;
}
for(int j=1;j<=p[0]&&i*p[j]<=N;j++){
z[i*p[j]]=1;
if(i%p[j]==0){
mu[i*p[j]]=0;
ph[i*p[j]]=ph[i]*p[j];
break;
}
mu[i*p[j]]=-mu[i];
ph[i*p[j]]=ph[i]*(p[j]-1);
}
}
for(int i=1;i<=N;i++){
sph[i]=sph[i-1]+ph[i];
smu[i]=smu[i-1]+mu[i];
}
}
long long gt1(int t){
if(t<=N) return sph[t];
if(mph.count(t)) return mph[t];
long long re=0;
for(int l=2,r;l<=t;l=r+1){
if(t/l==1) r=t;
else r=t/(t/l);
re+=gt1(t/l)*(r-l+1);
if(r==t) break;
}
return mph[t]=t*(t+1ll)/2-re;
}
long long gt2(int t){
if(t<=N) return smu[t];
if(mmu.count(t)) return mmu[t];
long long re=0;
for(int l=2,r;l<=t;l=r+1){
if(t/l==1) r=t;
else r=t/(t/l);
re+=gt2(t/l)*(r-l+1);
if(r==t) break;
}
return mmu[t]=1-re;
}
int main()
{
int t,n;
init();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%lld %lld\n",gt1(n),gt2(n));
}
return 0;
}