【洛谷 P3768】简单的数学题(莫比乌斯反演+杜教筛)

传送门

首先简单莫比乌斯反演可以得到
i=1nj=1nijgcd(ij)\sum_{i=1}^{n}\sum_{j=1}^nijgcd(ij)
=d=1nd3k=1ndμ(k)k2S(nkd)2,S(n)=i=1ni=\sum_{d=1}^nd^3\sum_{k=1}^{\frac nd }\mu(k)k^2S(\frac n {kd})^2,S(n)=\sum_{i=1}^ni
=T=1nS(nT)2T2dTdμ(Td)=\sum_{T=1}^nS(\frac nT)^2T^2\sum_{d|T}d\mu(\frac T d)
考虑后面是μId=μIϕ=ϕ\mu*Id=\mu*I*\phi=\phi
=T=1nS(nT)2T2ϕ(T)=\sum_{T=1}^nS(\frac nT)^2T^2\phi(T)
考虑杜教筛求Id2ϕId^2\phi的前缀和
卷上Id2Id^2dnd2(nd)2ϕ(d)=n3\sum_{d|n}d^2(\frac n d)^2\phi(d)=n^3
i=1ni3=(n(n+1)2)2\sum_{i=1}^ni^3=(\frac {n*(n+1)}{2})^2
这个可以构造n((n+1)4n4)\sum_n((n+1)^4-n^4)得到关于n3n^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;
}
inline ll readl(){
    char ch=gc();
    ll 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;}
int mod;
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);}
cs int N=10000006;
int pr[N],tot,phi[N],f[N],iv3;
bitset<N>vis;
inline int P(int x){return mul(x,x);}
inline int S1(int x){return 1ll*x*(x+1)/2%mod;}
inline int S2(int x){return mul(mul(S1(x),2*x+1),iv3);}
inline int S3(int x){return P(1ll*x*(x+1)/2%mod);}
inline void init(cs int len=N-6){
	phi[1]=1;
	for(int i=2;i<=len;i++){
		if(!vis[i])pr[++tot]=i,phi[i]=i-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){phi[p]=phi[i]*pr[j];break;}
			phi[p]=phi[i]*phi[pr[j]];
		}
	}
	
	for(int i=1;i<=len;i++)f[i]=add(f[i-1],mul(phi[i],P(i)));
}
ll n;
struct Map{
	static cs int mod=192733;
	int val[mod];ll key[mod];
	Map(){memset(key,-1,sizeof(key));}
	cs int &operator [](cs ll &k)cs{
		int p=k%mod;
		while((~key[p])&&(key[p]!=k))p=(p==mod-1)?0:(p+1);
		return val[p];
	}
	int &operator [](cs ll &k){
		int p=k%mod;
		while((~key[p])&&(key[p]!=k))p=(p==mod-1)?0:(p+1);
		if(key[p]!=k)key[p]=k;
		return val[p];		
	}
	inline bool count(ll k){
		int p=k%mod;
		while((~key[p])&&(key[p]!=k))p=(p==mod-1)?0:(p+1);
		return key[p]==k;
	}
};
Map mp;
inline int S(ll n){
	if(n<=N-6)return f[n];
	if(mp.count(n))return mp[n];
	int ret=S3(n%mod);
	for(ll i=2,j;i<=n;i=j+1){
		j=n/(n/i);
		Dec(ret,mul(dec(S2(j%mod),S2((i-1)%mod)),S(n/i)));
	}
	return mp[n]=ret;
}
signed main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	mod=read(),n=readl();
	init(),iv3=Inv(3);
	int ret=0;
	for(ll i=1,j;i<=n;i=j+1){
		j=n/(n/i);
		Add(ret,mul(P(S1(n/i%mod)),dec(S(j),S(i-1))));
	}
	cout<<ret<<'\n';return 0;
}
posted @ 2020-01-22 12:14  Stargazer_cykoi  阅读(154)  评论(0编辑  收藏  举报