洛谷P6055 [RC-02] GCD

洛谷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=1Nj=1Np=1jNq=1jN[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=1Nj=1N[gcd(i,j)=1]p=1jNq=1jN[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=1Nj=1Ndi,djμ(d)p=1jNq=1jN[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=1NdNμ(d)k=1dNp=1kdNq=1kdN[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=1ni=1knj=1kn[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=1NdNμ(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=1ni=1knj=1kn[gcd(i,j)=1]=k=1ni=1knj=1kndi,djμ(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=1nd=1knμ(d)×kdn2

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=1nTn2dTμ(d)

我们知道只有当 T = 1 T=1 T=1时, ∑ d ∣ T μ ( d ) \sum\limits_{d|T}\mu(d) dTμ(d)的值为 1 1 1,所以只有 T = 1 T=1 T=1 ⌊ n T ⌋ 2 \lfloor\dfrac nT\rfloor^2 Tn2才会对 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=1NdNμ(d)×f(⌊dN⌋)=d=1Nμ(d)×dN3

用杜教筛求 μ ( d ) \mu(d) μ(d)的前缀和,再用数论分块来求答案,时间复杂度为 O ( n 2 3 ) O(n^{\frac 23}) O(n32)

code

#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;
}

你可能感兴趣的:(题解,c++)