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

传送门

首先简单莫比乌斯反演可以得到
∑ i = 1 n ∑ j = 1 n i j g c d ( i j ) \sum_{i=1}^{n}\sum_{j=1}^nijgcd(ij) i=1nj=1nijgcd(ij)
= ∑ d = 1 n d 3 ∑ k = 1 n d μ ( k ) k 2 S ( n k d ) 2 , S ( n ) = ∑ i = 1 n i =\sum_{d=1}^nd^3\sum_{k=1}^{\frac nd }\mu(k)k^2S(\frac n {kd})^2,S(n)=\sum_{i=1}^ni =d=1nd3k=1dnμ(k)k2S(kdn)2,S(n)=i=1ni
= ∑ T = 1 n S ( n T ) 2 T 2 ∑ d ∣ T d μ ( T d ) =\sum_{T=1}^nS(\frac nT)^2T^2\sum_{d|T}d\mu(\frac T d) =T=1nS(Tn)2T2dTdμ(dT)
考虑后面是 μ ∗ I d = μ ∗ I ∗ ϕ = ϕ \mu*Id=\mu*I*\phi=\phi μId=μIϕ=ϕ
= ∑ T = 1 n S ( n T ) 2 T 2 ϕ ( T ) =\sum_{T=1}^nS(\frac nT)^2T^2\phi(T) =T=1nS(Tn)2T2ϕ(T)
考虑杜教筛求 I d 2 ϕ Id^2\phi Id2ϕ的前缀和
卷上 I d 2 Id^2 Id2 ∑ d ∣ n d 2 ( n d ) 2 ϕ ( d ) = n 3 \sum_{d|n}d^2(\frac n d)^2\phi(d)=n^3 dnd2(dn)2ϕ(d)=n3
∑ i = 1 n i 3 = ( n ∗ ( n + 1 ) 2 ) 2 \sum_{i=1}^ni^3=(\frac {n*(n+1)}{2})^2 i=1ni3=(2n(n+1))2
这个可以构造 ∑ n ( ( n + 1 ) 4 − n 4 ) \sum_n((n+1)^4-n^4) n((n+1)4n4)得到关于 n 3 n^3 n3的式子求得

然后差不多就做完了

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值