题目大意
很久很久以前,有一只神犇叫yzy;
很久很久之后,有一只蒟蒻叫lty;
输入一个整数nnn,1≤n≤1091\leq n\leq 10^91≤n≤109
请你输出一个整数A=∑i=1nμ(i2)A=\sum\limits_{i=1}^n\mu(i^2)A=i=1∑nμ(i2)
请你输出一个整数B=∑i=1nϕ(i2)B=\sum\limits_{i=1}^n\phi(i^2)B=i=1∑nϕ(i2)
A,BA,BA,B模1e9+71e9+71e9+7
题解
前置知识:杜教筛
对于∑i=1nμ(i2)\sum\limits_{i=1}^n\mu(i^2)i=1∑nμ(i2),我们知道当i>1i>1i>1时μ(i2)=0\mu(i^2)=0μ(i2)=0,所以AAA一定为111。
对于∑i=1nϕ(i2)\sum\limits_{i=1}^n\phi(i^2)i=1∑nϕ(i2),我们可以注意到ϕ(i2)=iϕ(i)\phi(i^2)=i\phi(i)ϕ(i2)=iϕ(i),为什么呢?
我们可以把iii分成若干个质数的乘积,i=p1a1p2a2⋯pkaki=p_1^{a_1}p_2^{a_2}\cdots p_k^{a_k}i=p1a1p2a2⋯pkak,因为ϕ(i)\phi(i)ϕ(i)是积性函数,所以
ϕ(i)=ϕ(p1a1)×ϕ(p2a2)×⋯×ϕ(pkak)=∏i=1k(pi−1)×pia1−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=1∏k(pi−1)×pia1−1
ϕ(i2)=ϕ(p12a1)×ϕ(p22a2)×⋯×ϕ(pk2ak)=∏i=1k(pi−1)×pi2a1−1=(∏i=1kpiai)×(∏i=1k(pi−1)×pia1−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=1∏k(pi−1)×pi2a1−1=(i=1∏kpiai)×(i=1∏k(pi−1)×pia1−1)=i×ϕ(i)
由此可得ϕ(i2)=i×ϕ(i)\phi(i^2)=i\times \phi(i)ϕ(i2)=i×ϕ(i)
由狄利克雷卷积可得,ϕ∗I=Id\phi*I=Idϕ∗I=Id
所以n=∑d∣nϕ(d)n=\sum\limits_{d|n}\phi(d)n=d∣n∑ϕ(d)
n2=n∑d∣nϕ(d)=∑d∣nϕ(d)×d×nd=nϕ(n)+∑d∣n,d<nϕ(d)×d×ndn^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,d<n}\phi(d)\times d\times \dfrac ndn2=nd∣n∑ϕ(d)=d∣n∑ϕ(d)×d×dn=nϕ(n)+d∣n,d<n∑ϕ(d)×d×dn
nϕ(n)=n2−∑d∣n,d<nϕ(d)×d×ndn\phi(n)=n^2-\sum\limits_{d|n,d<n}\phi(d)\times d\times \dfrac ndnϕ(n)=n2−d∣n,d<n∑ϕ(d)×d×dn
设f(d)=dϕ(d)f(d)=d\phi(d)f(d)=dϕ(d)
原式变为
f(n)=n2−∑d∣n,d<nf(d)×ndf(n)=n^2-\sum\limits_{d|n,d<n}f(d)\times \dfrac ndf(n)=n2−d∣n,d<n∑f(d)×dn
分别求前缀和得
∑i=1nf(n)=∑i=1ni2−∑i=1n∑d∣i,d<if(d)×id\sum\limits_{i=1}^nf(n)=\sum\limits_{i=1}^ni^2-\sum\limits_{i=1}^n\sum\limits_{d|i,d<i}f(d)\times \dfrac idi=1∑nf(n)=i=1∑ni2−i=1∑nd∣i,d<i∑f(d)×di
∑i=1nf(n)=∑i=1ni2−∑d=2nd∑i=1⌊nd⌋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=1∑nf(n)=i=1∑ni2−d=2∑ndi=1∑⌊dn⌋f(i)
设F(n)=∑i=1f(i)F(n)=\sum\limits_{i=1}f(i)F(n)=i=1∑f(i),则
F(n)=n(n+1)(2n+1)6−∑d=2ndF(⌊nd⌋)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=2∑ndF(⌊dn⌋)
预处理出前n23n^{\frac 23}n32个iϕ(i)i\phi(i)iϕ(i),用杜教筛解决,时间复杂度为O(n23)O(n^{\frac 23})O(n32)。
code
#include<bits/stdc++.h>
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;
}