P3768 简单的数学题(莫比乌斯反演+杜教筛)
P3768 简单的数学题
设
令
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace FGF
{
ll n,mo,m;
const int N=5e6+5;
unordered_map<ll,ll> res;
int p[N],cnt;
ll phi[N],ans,inv6,inv4;
ll qpow(ll x,ll y)
{
ll s=1;
while(y)
{
if(y&1)s=s*x%mo;
x=x*x%mo;
y>>=1;
}
return s;
}
ll getsum(ll x)
{
return x%mo*x%mo*(x%mo+1)%mo*(x%mo+1)%mo*inv4%mo;
}
ll getsum2(ll x)
{
return x%mo*(x%mo+1)%mo*(2LL*x%mo+1)%mo*inv6%mo;
}
void init()
{
phi[1]=1;
for(int i=2;i<=m;i++)
{
if(!phi[i])phi[i]=i-1,p[++cnt]=i;
for(int j=1;j<=cnt&&p[j]*i<=m;j++)
{
if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*phi[p[j]];
}
}
for(ll i=1;i<=m;i++)
phi[i]=(phi[i-1]+i*i%mo*phi[i]%mo)%mo;
}
ll getf(ll x)
{
if(x<=m)return phi[x];
if(!res[x])
{
ll s=getsum(x);
for(ll l=2,r;l<=x;l=r+1)
r=x/(x/l),s=(s-getf(x/l)*((getsum2(r)-getsum2(l-1)+mo)%mo)%mo+mo)%mo;
res[x]=s;
}
return res[x];
}
void work()
{
scanf("%lld%lld",&mo,&n);
m=pow(n,2.0/3.0),inv6=qpow(6,mo-2),inv4=qpow(4,mo-2);
init();
for(ll l=1,r;l<=n;l=r+1)
r=n/(n/l),ans=(ans+(getf(r)-getf(l-1)+mo)%mo*getsum(n/l)%mo)%mo;
printf("%lld",ans);
}
}
int main()
{
FGF::work();
return 0;
}