【BZOJ4916】神犇与蒟蒻

题目大意

很久很久以前,有一只神犇叫yzy;
很久很久之后,有一只蒟蒻叫lty;

输入一个整数 n n n 1 ≤ n ≤ 1 0 9 1\leq n\leq 10^9 1n109
请你输出一个整数 A = ∑ i = 1 n μ ( i 2 ) A=\sum\limits_{i=1}^n\mu(i^2) A=i=1nμ(i2)
请你输出一个整数 B = ∑ i = 1 n ϕ ( i 2 ) B=\sum\limits_{i=1}^n\phi(i^2) B=i=1nϕ(i2)
A , B A,B A,B 1 e 9 + 7 1e9+7 1e9+7


题解

前置知识:杜教筛

对于 ∑ i = 1 n μ ( i 2 ) \sum\limits_{i=1}^n\mu(i^2) i=1nμ(i2),我们知道当 i > 1 i>1 i>1 μ ( i 2 ) = 0 \mu(i^2)=0 μ(i2)=0,所以 A A A一定为 1 1 1

对于 ∑ i = 1 n ϕ ( i 2 ) \sum\limits_{i=1}^n\phi(i^2) i=1nϕ(i2),我们可以注意到 ϕ ( i 2 ) = i ϕ ( i ) \phi(i^2)=i\phi(i) ϕ(i2)=iϕ(i),为什么呢?

我们可以把 i i i分成若干个质数的乘积, i = p 1 a 1 p 2 a 2 ⋯ p k a k i=p_1^{a_1}p_2^{a_2}\cdots p_k^{a_k} i=p1a1p2a2pkak,因为 ϕ ( i ) \phi(i) ϕ(i)是积性函数,所以

ϕ ( i ) = ϕ ( p 1 a 1 ) × ϕ ( p 2 a 2 ) × ⋯ × ϕ ( p k a k ) = ∏ i = 1 k ( p i − 1 ) × p i a 1 − 1 \phi(i)=\phi(p_1^{a_1})\times \phi(p_2^{a_2})\times \cdots\times \phi(p_k^{a_k})=\prod\limits_{i=1}^k(p_i-1)\times p_i^{a_1-1} ϕ(i)=ϕ(p1a1)×ϕ(p2a2)××ϕ(pkak)=i=1k(pi1)×pia11

ϕ ( i 2 ) = ϕ ( p 1 2 a 1 ) × ϕ ( p 2 2 a 2 ) × ⋯ × ϕ ( p k 2 a k ) = ∏ i = 1 k ( p i − 1 ) × p i 2 a 1 − 1 = ( ∏ i = 1 k p i a i ) × ( ∏ i = 1 k ( p i − 1 ) × p i a 1 − 1 ) = i × ϕ ( i ) \phi(i^2)=\phi(p_1^{2a_1})\times \phi(p_2^{2a_2})\times \cdots\times \phi(p_k^{2a_k})=\prod\limits_{i=1}^k(p_i-1)\times p_i^{2a_1-1}=(\prod\limits_{i=1}^kp_i^{a_i})\times (\prod\limits_{i=1}^k(p_i-1)\times p_i^{a_1-1})=i\times \phi(i) ϕ(i2)=ϕ(p12a1)×ϕ(p22a2)××ϕ(pk2ak)=i=1k(pi1)×pi2a11=(i=1kpiai)×(i=1k(pi1)×pia11)=i×ϕ(i)

由此可得 ϕ ( i 2 ) = i × ϕ ( i ) \phi(i^2)=i\times \phi(i) ϕ(i2)=i×ϕ(i)

由狄利克雷卷积可得, ϕ ∗ I = I d \phi*I=Id ϕI=Id

所以 n = ∑ d ∣ n ϕ ( d ) n=\sum\limits_{d|n}\phi(d) n=dnϕ(d)

n 2 = n ∑ d ∣ n ϕ ( d ) = ∑ d ∣ n ϕ ( d ) × d × n d = n ϕ ( n ) + ∑ d ∣ n , d < n ϕ ( d ) × d × n d n^2=n\sum\limits_{d|n}\phi(d)=\sum\limits_{d|n}\phi(d)\times d\times \dfrac nd=n\phi(n)+\sum\limits_{d|n,dn2=ndnϕ(d)=dnϕ(d)×d×dn=nϕ(n)+dn,d<nϕ(d)×d×dn

n ϕ ( n ) = n 2 − ∑ d ∣ n , d < n ϕ ( d ) × d × n d n\phi(n)=n^2-\sum\limits_{d|n,dnϕ(n)=n2dn,d<nϕ(d)×d×dn

f ( d ) = d ϕ ( d ) f(d)=d\phi(d) f(d)=dϕ(d)

原式变为

f ( n ) = n 2 − ∑ d ∣ n , d < n f ( d ) × n d f(n)=n^2-\sum\limits_{d|n,df(n)=n2dn,d<nf(d)×dn

分别求前缀和得

∑ i = 1 n f ( n ) = ∑ i = 1 n i 2 − ∑ i = 1 n ∑ d ∣ i , d < i f ( d ) × i d \sum\limits_{i=1}^nf(n)=\sum\limits_{i=1}^ni^2-\sum\limits_{i=1}^n\sum\limits_{d|i,di=1nf(n)=i=1ni2i=1ndi,d<if(d)×di

∑ i = 1 n f ( n ) = ∑ i = 1 n i 2 − ∑ d = 2 n d ∑ i = 1 ⌊ n d ⌋ f ( i ) \sum\limits_{i=1}^nf(n)=\sum\limits_{i=1}^ni^2-\sum\limits_{d=2}^nd\sum\limits_{i=1}^{\lfloor \frac nd\rfloor}f(i) i=1nf(n)=i=1ni2d=2ndi=1dnf(i)

F ( n ) = ∑ i = 1 f ( i ) F(n)=\sum\limits_{i=1}f(i) F(n)=i=1f(i),则

F ( n ) = n ( n + 1 ) ( 2 n + 1 ) 6 − ∑ d = 2 n d F ( ⌊ n d ⌋ ) F(n)=\dfrac{n(n+1)(2n+1)}{6}-\sum\limits_{d=2}^ndF(\lfloor \dfrac nd\rfloor) F(n)=6n(n+1)(2n+1)d=2ndF(⌊dn⌋)

预处理出前 n 2 3 n^{\frac 23} n32 i ϕ ( i ) i\phi(i) iϕ(i),用杜教筛解决,时间复杂度为 O ( n 2 3 ) O(n^{\frac 23}) O(n32)

code

#include
using namespace std;
const int N=2000000;
const long long mod=1000000007;
int z[N+5],p[N+5];
long long ny,ph[N+5],s[N+5];
map<long long,long long>mp;
long long mi(long long t,long long v){
	if(v==0) return 1;
	long long re=mi(t,v/2);
	re=re*re%mod;
	if(v&1) re=re*t%mod;
	return re;
}
void init(){
	ph[1]=1;
	for(int i=2;i<=N;i++){
		if(!z[i]){
			p[++p[0]]=i;
			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){
				ph[i*p[j]]=ph[i]*p[j]%mod;
				break;
			}
			ph[i*p[j]]=ph[i]*(p[j]-1)%mod;
		}
	}
	for(int i=1;i<=N;i++){
		s[i]=(s[i-1]+ph[i]*i%mod)%mod;
	}
	ny=mi(6ll,mod-2);
}
long long djs(int t){
	if(t<=N) return s[t];
	if(mp.count(t)) return mp[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)*(r+l)/2%mod*djs(t/l)%mod)%mod;
	}
	re=(1ll*t*(t+1)%mod*(2*t+1)%mod*ny%mod-re+mod)%mod;
	return mp[t]=re;
}
int main()
{
	int n;
	init();
	scanf("%d",&n);
	printf("1\n%lld",djs(n));
	return 0;
}

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