【51nod 1220】约数之和(莫比乌斯反演+杜教筛)

传送门

考虑类似约数个数和的结论
d(ij)=kipj[gcd(k,p)=1]ipkd(ij)=\sum_{k|i}\sum_{p|j}[gcd(k,p)=1]\frac{ip}{k}

于是可以愉快地莫反了

最后搞出来就是
d=1dμ(d)i=1ndkiikj=1ndljl\sum_{d=1}d\mu(d)\sum_{i=1}^{\frac nd }\sum_{k|i}\frac i k\sum_{j=1}^{\frac n d}\sum_{l|j}l
=d=1ndμ(d)(i=1ndkik)2=\sum_{d=1}^nd\mu(d)(\sum_{i=1}^{\frac nd }\sum_{k|i}k)^2
=d=1ndμ(d)f(nd)2=\sum_{d=1}^nd\mu(d)f(\frac nd )^2

前面是IdμId*\mu,卷上IdId就是ee
后面
f(n)=i=1ninif(n)=\sum_{i=1}^ni*\lfloor\frac n i\rfloor
=i=1nσ(i)=\sum_{i=1}^n\sigma(i)

ff可以用类似杜教筛的方法
线筛前n23n^\frac 2 3
具体记录每个数最小质因子的值来做
实现可以参见代码

对于后面的利用第一个式子每次n\sqrt n来求

复杂度O(n23)O(n^\frac 2 3)

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=1000006;
int pr[N],sig[N],lst[N],mu[N],tot;
bitset<N>vis;
inline void init(cs int len=N-6){
	mu[1]=1,sig[1]=1;
	for(int i=2;i<=len;i++){
		if(!vis[i])pr[++tot]=i,sig[i]=i+1,lst[i]=i,mu[i]=mod-1;
		for(int j=1,p;j<=tot&&i*pr[j]<=len;j++){
			p=i*pr[j],vis[p]=1;
			if(i%pr[j]==0){
				lst[p]=lst[i]*pr[j];
				if(lst[p]==p)sig[p]=add(sig[i],p);
				else sig[p]=mul(sig[lst[p]],sig[p/lst[p]]);
				break;
			}
			lst[p]=pr[j];
			sig[p]=mul(sig[i],pr[j]+1),mu[p]=mod-mu[i];
		}
	}
	for(int i=1;i<=len;i++)Add(sig[i],sig[i-1]),mu[i]=add(mul(mu[i],i),mu[i-1]);
}
map<int,int> s;
inline int S(int x){return (1ll*x*(x+1)/2)%mod;}
inline int P(int x){return mul(x,x);}
inline int Ssig(int n){
	if(n<=N-6)return sig[n];
	int ret=0;
	for(int i=1,j;i<=n;i=j+1){
		j=n/(n/i);
		Add(ret,mul(n/i,dec(S(j),S(i-1))));
	}
	return ret;
}
inline int Sf(int n){
	if(n<=N-6)return mu[n];
	if(s.count(n))return s[n];
	int ret=1;
	for(int i=2,j;i<=n;i=j+1){
		j=n/(n/i);
		Dec(ret,mul(dec(S(j),S(i-1)),Sf(n/i)));
	}
	return s[n]=ret;
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	init();
	int n=read();
	int ret=0;
	for(int i=1,j;i<=n;i=j+1){
		j=n/(n/i);
		Add(ret,mul(dec(Sf(j),Sf(i-1)),P(Ssig(n/i))));
	}
	cout<<ret<<'\n';
}
posted @ 2020-02-07 19:22  Stargazer_cykoi  阅读(171)  评论(0编辑  收藏  举报