【51nod 1220】约数之和(莫比乌斯反演+杜教筛)
考虑类似约数个数和的结论
有
于是可以愉快地莫反了
最后搞出来就是
前面是,卷上就是
后面
可以用类似杜教筛的方法
线筛前
具体记录每个数最小质因子的值来做
实现可以参见代码
对于后面的利用第一个式子每次来求
复杂度
#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';
}