洛谷P6055 [RC-02] GCD
前置知识:杜教筛
题意即求
∑ i = 1 N ∑ j = 1 N ∑ p = 1 ⌊ N j ⌋ ∑ q = 1 ⌊ N j ⌋ [ gcd ( i , j ) = 1 ] [ gcd ( p , q ) = 1 ] \sum_{i=1}^N\sum_{j=1}^N\sum_{p=1}^{\lfloor\frac{N}{j}\rfloor}\sum_{q=1}^{\lfloor\frac{N}{j}\rfloor}[\gcd(i,j)=1][\gcd(p,q)=1] i=1∑Nj=1∑Np=1∑⌊jN⌋q=1∑⌊jN⌋[gcd(i,j)=1][gcd(p,q)=1]
将 [ gcd ( i , j ) = 1 ] [\gcd(i,j)=1] [gcd(i,j)=1]提到前面,得
∑ i = 1 N ∑ j = 1 N [ gcd ( i , j ) = 1 ] ∑ p = 1 ⌊ N j ⌋ ∑ q = 1 ⌊ N j ⌋ [ gcd ( p , q ) = 1 ] \sum_{i=1}^N\sum_{j=1}^N[\gcd(i,j)=1]\sum_{p=1}^{\lfloor\frac{N}{j}\rfloor}\sum_{q=1}^{\lfloor\frac{N}{j}\rfloor}[\gcd(p,q)=1] i=1∑Nj=1∑N[gcd(i,j)=1]p=1∑⌊jN⌋q=1∑⌊jN⌋[gcd(p,q)=1]
利用莫比乌斯函数的性质,得
∑ i = 1 N ∑ j = 1 N ∑ d ∣ i , d ∣ j μ ( d ) ∑ p = 1 ⌊ N j ⌋ ∑ q = 1 ⌊ N j ⌋ [ gcd ( p , q ) = 1 ] \sum_{i=1}^N\sum_{j=1}^N\sum\limits_{d|i,d|j}\mu(d)\sum_{p=1}^{\lfloor\frac{N}{j}\rfloor}\sum_{q=1}^{\lfloor\frac{N}{j}\rfloor}[\gcd(p,q)=1] i=1∑Nj=1∑Nd∣i,d∣j∑μ(d)p=1∑⌊jN⌋q=1∑⌊jN⌋[gcd(p,q)=1]
枚举 d d d,得
∑ d = 1 N ⌊ N d ⌋ μ ( d ) ∑ k = 1 ⌊ N d ⌋ ∑ p = 1 ⌊ N k d ⌋ ∑ q = 1 ⌊ N k d ⌋ [ gcd ( p , q ) = 1 ] \sum\limits_{d=1}^N\lfloor\dfrac Nd\rfloor\mu(d)\sum\limits_{k=1}^{\lfloor\frac Nd\rfloor}\sum_{p=1}^{\lfloor\frac{N}{kd}\rfloor}\sum_{q=1}^{\lfloor\frac{N}{kd}\rfloor}[\gcd(p,q)=1] d=1∑N⌊dN⌋μ(d)k=1∑⌊dN⌋p=1∑⌊kdN⌋q=1∑⌊kdN⌋[gcd(p,q)=1]
设 f ( n ) = ∑ k = 1 n ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ n k ⌋ [ gcd ( i , j ) = 1 ] f(n)=\sum\limits_{k=1}^n\sum\limits_{i=1}^{\lfloor\frac nk\rfloor}\sum\limits_{j=1}^{\lfloor\frac nk\rfloor}[\gcd(i,j)=1] f(n)=k=1∑ni=1∑⌊kn⌋j=1∑⌊kn⌋[gcd(i,j)=1],则原式变为
∑ d = 1 N ⌊ N d ⌋ μ ( d ) × f ( ⌊ N d ⌋ ) \sum\limits_{d=1}^N\lfloor\dfrac Nd\rfloor\mu(d)\times f(\lfloor\frac Nd\rfloor) d=1∑N⌊dN⌋μ(d)×f(⌊dN⌋)
接下来我们来求 f ( n ) f(n) f(n)
f ( n ) = ∑ k = 1 n ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ n k ⌋ [ gcd ( i , j ) = 1 ] = ∑ k = 1 n ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ n k ⌋ ∑ d ∣ i , d ∣ j μ ( d ) f(n)=\sum\limits_{k=1}^n\sum\limits_{i=1}^{\lfloor\frac nk\rfloor}\sum\limits_{j=1}^{\lfloor\frac nk\rfloor}[\gcd(i,j)=1]=\sum\limits_{k=1}^n\sum\limits_{i=1}^{\lfloor\frac nk\rfloor}\sum\limits_{j=1}^{\lfloor\frac nk\rfloor}\sum\limits_{d|i,d|j}\mu(d) f(n)=k=1∑ni=1∑⌊kn⌋j=1∑⌊kn⌋[gcd(i,j)=1]=k=1∑ni=1∑⌊kn⌋j=1∑⌊kn⌋d∣i,d∣j∑μ(d)
枚举 d d d得
f ( n ) = ∑ k = 1 n ∑ d = 1 ⌊ n k ⌋ μ ( d ) × ⌊ n k d ⌋ 2 f(n)=\sum\limits_{k=1}^n\sum\limits_{d=1}^{\lfloor\frac nk\rfloor}\mu(d)\times\lfloor\frac{n}{kd}\rfloor^2 f(n)=k=1∑nd=1∑⌊kn⌋μ(d)×⌊kdn⌋2
令 T = k d T=kd T=kd,枚举 T T T得
f ( n ) = ∑ T = 1 n ⌊ n T ⌋ 2 ∑ d ∣ T μ ( d ) f(n)=\sum\limits_{T=1}^n\lfloor\frac nT\rfloor^2\sum\limits_{d|T}\mu(d) f(n)=T=1∑n⌊Tn⌋2d∣T∑μ(d)
我们知道只有当 T = 1 T=1 T=1时, ∑ d ∣ T μ ( d ) \sum\limits_{d|T}\mu(d) d∣T∑μ(d)的值为 1 1 1,所以只有 T = 1 T=1 T=1时 ⌊ n T ⌋ 2 \lfloor\dfrac nT\rfloor^2 ⌊Tn⌋2才会对 f ( n ) f(n) f(n)有贡献,由此可得
f ( n ) = n 2 f(n)=n^2 f(n)=n2
所以
∑ d = 1 N ⌊ N d ⌋ μ ( d ) × f ( ⌊ N d ⌋ ) = ∑ d = 1 N μ ( d ) × ⌊ N d ⌋ 3 \sum\limits_{d=1}^N\lfloor\dfrac Nd\rfloor\mu(d)\times f(\lfloor\frac Nd\rfloor)=\sum\limits_{d=1}^N\mu(d)\times \lfloor\dfrac Nd\rfloor^3 d=1∑N⌊dN⌋μ(d)×f(⌊dN⌋)=d=1∑Nμ(d)×⌊dN⌋3
用杜教筛求 μ ( d ) \mu(d) μ(d)的前缀和,再用数论分块来求答案,时间复杂度为 O ( n 2 3 ) O(n^{\frac 23}) O(n32)。
#include
using namespace std;
const int N=100000;
const long long mod=998244353;
int z[N+5],p[N+5],mu[N+5];
long long ans=0,smu[N+5];
map<long long,long long>mmu;
void init(){
mu[1]=1;
for(int i=2;i<=N;i++){
if(!z[i]){
p[++p[0]]=i;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) break;
mu[i*p[j]]=-mu[i];
}
}
for(int i=1;i<=N;i++){
smu[i]=(smu[i-1]+mu[i]+mod)%mod;
}
}
long long djs(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){
r=t/(t/l);
re=(re+1ll*(r-l+1)*djs(t/l)%mod)%mod;
}
re=(1-re+mod)%mod;
return mmu[t]=re;
}
int main()
{
int n;
scanf("%d",&n);
init();
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
long long t=n/l;t=t*t%mod*t%mod;
ans=(ans+(djs(r)-djs(l-1)+mod)%mod*t%mod)%mod;
}
printf("%lld",ans);
return 0;
}