【洛谷 P3768】简单的数学题(莫比乌斯反演+杜教筛)
首先简单莫比乌斯反演可以得到
考虑后面是
考虑杜教筛求的前缀和
卷上得
这个可以构造得到关于的式子求得
然后差不多就做完了
#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;
}