前置知识:杜教筛
给定 n n n,求
∑ i = 1 n ∑ j = 1 n ∑ k = 1 n ϕ ( gcd ( i , j , k ) ) \sum\limits_{i=1}^n\sum\limits_{j=1}^n\sum\limits_{k=1}^n\phi(\gcd(i,j,k)) i=1∑nj=1∑nk=1∑nϕ(gcd(i,j,k))
输出其结果模 20230923 20230923 20230923后的值。
1 ≤ n ≤ 1 0 9 1\leq n\leq 10^9 1≤n≤109
令 d = gcd ( i , j , k ) d=\gcd(i,j,k) d=gcd(i,j,k),则
∑ i = 1 n ∑ j = 1 n ∑ k = 1 n ϕ ( gcd ( i , j , k ) ) = ∑ d = 1 n ϕ ( d ) ∑ i = 1 n ∑ j = 1 n ∑ k = 1 n [ gcd ( i , j , k ) = = d ] = ∑ d = 1 n ϕ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ ∑ k = 1 ⌊ n d ⌋ [ gcd ( i , j , k ) = = 1 ] = ∑ d = 1 n ϕ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ ∑ k = 1 ⌊ n d ⌋ ∑ t ∣ i , t ∣ j , t ∣ k μ ( t ) = ∑ d = 1 n ϕ ( d ) ∑ t = 1 ⌊ n d ⌋ μ ( t ) × ⌊ n d ⌋ 3 \begin{aligned} \sum\limits_{i=1}^n\sum\limits_{j=1}^n\sum\limits_{k=1}^n\phi(\gcd(i,j,k))&=\sum\limits_{d=1}^n\phi(d)\sum\limits_{i=1}^n\sum\limits_{j=1}^n\sum\limits_{k=1}^n[\gcd(i,j,k)==d] \\ \\ &=\sum\limits_{d=1}^n\phi(d)\sum\limits_{i=1}^{\lfloor\frac nd\rfloor}\sum\limits_{j=1}^{\lfloor\frac nd\rfloor}\sum\limits_{k=1}^{\lfloor\frac nd\rfloor}[\gcd(i,j,k)==1] \\ \\ &=\sum\limits_{d=1}^n\phi(d)\sum\limits_{i=1}^{\lfloor\frac nd\rfloor}\sum\limits_{j=1}^{\lfloor\frac nd\rfloor}\sum\limits_{k=1}^{\lfloor\frac nd\rfloor}\sum\limits_{t|i,t|j,t|k}\mu(t) \\ \\ &=\sum\limits_{d=1}^n\phi(d)\sum\limits_{t=1}^{\lfloor\frac nd\rfloor}\mu(t)\times \lfloor\frac nd\rfloor^3 \end{aligned} i=1∑nj=1∑nk=1∑nϕ(gcd(i,j,k))=d=1∑nϕ(d)i=1∑nj=1∑nk=1∑n[gcd(i,j,k)==d]=d=1∑nϕ(d)i=1∑⌊dn⌋j=1∑⌊dn⌋k=1∑⌊dn⌋[gcd(i,j,k)==1]=d=1∑nϕ(d)i=1∑⌊dn⌋j=1∑⌊dn⌋k=1∑⌊dn⌋t∣i,t∣j,t∣k∑μ(t)=d=1∑nϕ(d)t=1∑⌊dn⌋μ(t)×⌊dn⌋3
其中 ∑ t = 1 ⌊ n d ⌋ μ ( t ) × ⌊ n d ⌋ 3 \sum\limits_{t=1}^{\lfloor\frac nd\rfloor}\mu(t)\times \lfloor\dfrac nd\rfloor^3 t=1∑⌊dn⌋μ(t)×⌊dn⌋3可以用杜教筛来求。设 g ( t ) = ∑ t = 1 ⌊ n d ⌋ μ ( t ) × ⌊ n d ⌋ 3 g(t)=\sum\limits_{t=1}^{\lfloor\frac nd\rfloor}\mu(t)\times \lfloor\dfrac nd\rfloor^3 g(t)=t=1∑⌊dn⌋μ(t)×⌊dn⌋3,则整个式子为 ∑ d = 1 n ϕ ( d ) × g ( ⌊ n d ⌋ ) \sum\limits_{d=1}^n\phi(d)\times g(\lfloor \dfrac nd\rfloor) d=1∑nϕ(d)×g(⌊dn⌋),也是可以用杜教筛来求。
求 ϕ ( d ) \phi(d) ϕ(d)和 μ ( t ) \mu(t) μ(t)前缀和的杜教筛都是 O ( n 2 3 ) O(n^{\frac 23}) O(n32)的(求 μ ( t ) \mu(t) μ(t)的杜教筛总体来说可以看做一次数论分块),再来看看枚举的时间复杂度。
枚举的次数可以分为在数论分块值 ≤ n \leq \sqrt n ≤n和数论分块值 > n >\sqrt n >n两个部分。
对于第一部分,第 i i i次外部的数论分块的范围为 n i \dfrac ni in,内部的枚举次数为 n i \sqrt{\dfrac ni} in,枚举次数是 ∑ i = 1 n n i \sum\limits_{i=1}^{\sqrt n}\sqrt{\dfrac ni} i=1∑nin
∑ i = 1 n n i = n ( ∑ i = 1 n 1 i ) ≈ n ∫ 0 n x − 1 2 d x = n × 2 x 1 2 ∣ 0 n = n × 2 x 1 4 = 2 n 3 4 \sum\limits_{i=1}^{\sqrt n}\sqrt{\dfrac ni}=\sqrt n(\sum\limits_{i=1}^{\sqrt n}\sqrt \dfrac 1i)\approx \sqrt n\int_0^{\sqrt n}x^{-\frac 12}dx=\sqrt n\times 2x^{\frac 12}\bigg\vert_0^{\sqrt n}=\sqrt n\times 2x^{\frac 14}=2n^\frac 34 i=1∑nin=n(i=1∑ni1)≈n∫0nx−21dx=n×2x21 0n=n×2x41=2n43
对于第二部分,总共有 n \sqrt n n个数论分块值,则枚举次数为 ∑ i = 1 n i \sum\limits_{i=1}^{\sqrt n}\sqrt i i=1∑ni
∑ i = 1 n i ≈ ∫ 0 n x 1 2 d x = 2 3 x 3 2 ∣ 0 n = 2 3 n 3 4 \sum\limits_{i=1}^{\sqrt n}\sqrt i\approx \int_0^{\sqrt n}x^{\frac 12}dx=\dfrac 23x^{\frac 32}\bigg\vert_0^{\sqrt n}=\dfrac 23n^{\frac 34} i=1∑ni≈∫0nx21dx=32x23 0n=32n43
所以,总时间复杂度为 O ( n 3 4 ) O(n^{\frac 34}) O(n43)。
#include
using namespace std;
const int N=10000000;
const long long mod=20230923;
int z[N+5],p[N+5];
long long ans=0,ph[N+5],sph[N+5],mu[N+5],smu[N+5];
map<long long,long long>mph,mmu;
void init(){
z[1]=ph[1]=mu[1]=1;
for(int i=2;i<=N;i++){
if(!z[i]){
p[++p[0]]=i;
ph[i]=i-1;
mu[i]=-1;
}
for(int j=1;j<=p[0]&&i*p[j]<=N;j++){
z[i*p[j]]=1;
if(i%p[j]==0){
ph[i*p[j]]=ph[i]*p[j];
break;
}
ph[i*p[j]]=ph[i]*ph[p[j]];
mu[i*p[j]]=-mu[i];
}
}
for(int i=1;i<=N;i++){
sph[i]=(sph[i-1]+ph[i])%mod;
smu[i]=(smu[i-1]+mu[i])%mod;
}
}
long long djs1(long long t){
if(t<=N) return sph[t];
if(mph.count(t)) return mph[t];
long long re=0;
for(long long l=2,r;l<=t;l=r+1){
r=t/(t/l);
re=(re+(r-l+1)*djs1(t/l)+mod)%mod;
}
re=(t*(t+1)/2-re+mod)%mod;
mph[t]=re;
return re;
}
long long djs2(long long t){
if(t<=N) return smu[t];
if(mmu.count(t)) return mmu[t];
long long re=0;
for(long long l=2,r;l<=t;l=r+1){
r=t/(t/l);
re=(re+(r-l+1)*djs2(t/l)+mod)%mod;
}
re=(1-re+mod)%mod;
mmu[t]=re;
return re;
}
long long gt(long long t){
long long re=0;
for(long long l=1,r;l<=t;l=r+1){
r=t/(t/l);
re=(re+(djs2(r)-djs2(l-1)+mod)%mod*(t/l)%mod*(t/l)%mod*(t/l)%mod)%mod;
}
return re;
}
int main()
{
long long n;
scanf("%lld",&n);
init();
for(long long l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans=(ans+(djs1(r)-djs1(l-1)+mod)%mod*gt(n/l)%mod)%mod;
}
printf("%lld",ans);
return 0;
}