求助WA+MLE
查看原帖
求助WA+MLE
160839
Prean楼主2020/5/3 11:30

下载了第一个数据之后发现的对的。。。求助QwQ

Code:

#include<unordered_map>
#include<iostream>
#define ll long long
const int M=4641590;
ll n,inv,mod,ans,phi[M];int top,pri[M],zhi[M];
std::unordered_map<ll,ll>Sphi;
inline ll Get1(ll n){return n*(n+1)%mod*(2*n+1)*inv%mod;}
inline ll Get2(ll n){return (n*(n+1)/2)%mod*(n*(n+1)/2)%mod;}
inline ll pow(ll a,ll b,ll p)
{
	ll ans=1;for(;b;b>>=1,a=a*a%p)if(b&1)ans=ans*a%p;
	return ans;
}
inline ll GetSphi(ll n)
{
	if(n<=M)return phi[n];
	if(Sphi.find(n)!=Sphi.end())return Sphi[n];
	ll ans=Get2(n%mod);
	for(ll L=1,R;L<=n;L=R+1)
	{
		R=n/(n/L);
		ans=(ans+((Get1(R)-Get1(L-1)%mod)+mod)%mod*GetSphi(n/L))%mod;
	}
	return Sphi[n]=ans;
}
signed main()
{
	int i,j,x;zhi[1]=phi[1]=1;
	std::cin>>mod>>n;inv=pow(6,mod-2,mod);
	for(i=2;i<=M;++i)
	{
		if(!zhi[i])pri[++top]=i,phi[i]=i-1;
		for(j=1;j<=top&&(x=i*pri[j])<=M;++j)
		{
			zhi[x]=1;
			if(i%pri[j])phi[x]=phi[i]*(pri[j]-1);
			else{phi[x]=phi[i]*pri[j];break;}
		}
		phi[i]=(phi[i-1]+phi[i]*i%mod*i%mod)%mod;
	}
	for(ll L=1,R;L<=n;L=R+1)
	{
		R=n/(n/L);
		ans=(ans+Get2(n/L)*(GetSphi(R)-GetSphi(L-1)+mod)%mod)%mod;
	}
	std::cout<<ans;
}
2020/5/3 11:30
加载中...