Prerequisite skills: basic number theory, Mobius inversion, Dirichlet convolution, Du Jiao sieve
topic
answer
(Because the author has a special liking for rich text editors, the following derivation formulas may be shown in pictures that cannot be copied)
The meaning of the question is very simple, ask , the modulus p is a prime number ≥ 5e8, so rest assured to find the inverse element;
Start pushing the formula below:
I found that there is a gcd, and there is no good way to sum up quickly, so I brought up the gcd:
The last two parts are very familiar. We found that we can use Mobius inversion. Let , (using m-size f to represent the function derived by Mobius inversion is to distinguish it from the latter),
Perceptual analysis is available, (definition ) ,
So there is ,
Will be brought into the original equation: ,
Then we found that there is one in the loop. It is impossible to sum directly with the current ability (I am too silly) , unless i is changed into a factor form of a certain number, so we change the enumeration order instead of enumerating d and i, but Give d*i and d;
So , there is ;
Looking at the tail part , we find that we are a bit familiar again, because according to the knowledge of Dirichlet convolution that we just learned,, that is to say ,
It's nice, because we have been able to bring a bunch replaced Euler function: ,
Set , then ;
Obviously, there are only kinds of values at most . If we can get the prefix sum of the f function quickly, we can use number theory to do it in blocks;
So we use a Dujiao Sieve:
Set , ,
;
According to the routine, the g function should be set as the most suitable complete integrative function according to the f function. Here, g is set to , then ,
By , that is , can be obtained ;
Thus we get a quick way to find the prefix and the function h, it is a prefix and a cubic equation: ,
Then sets Du teach sieve: , (prefix and square formula: )
At this time, using a number theory block, the prefix sum of the f function can be obtained faster,
Finally, the two processes are combined. According to the time complexity analysis of Du Jiaosi, the complexity will not be achieved even if all is flattened , yes .
Code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<tr1/unordered_map>
#define ll long long
#define MAXN 10000005
using namespace std;
using namespace tr1;
inline ll read(){
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
return f?x:-x;
}
ll n,p[MAXN],sp[MAXN],MOD=read();
unordered_map<ll,ll>ph;
bool nop[MAXN];
vector<ll>h;
inline ll ksm(ll a,ll b){
ll res=1;
for(;b;b>>=1){
if(b&1)res=res*a%MOD;
a=a*a%MOD;
}
return res;
}
ll NI=ksm(6,MOD-2);//提前求逆元
inline void build(){
nop[0]=nop[1]=1;
for(int i=1;i<MAXN-4;i++)p[i]=i;
for(int a=2;a<MAXN-4;a++){
if(!nop[a])h.push_back(a),p[a]=a-1;
for(int i=0,u;i<h.size()&&h[i]*a<MAXN-4;i++){
u=a*h[i],nop[u]=1;
if(a%h[i]==0)p[u]=p[a]*h[i],i=MAXN;
else p[u]=p[a]*p[h[i]];
}
}
sp[1]=p[1];
for(int i=2;i<MAXN-4;i++)sp[i]=(sp[i-1]+p[i]*i%MOD*i%MOD)%MOD;
}
inline ll sumx(ll n){n%=MOD;//n的范围比模数大就离谱
return (n*(n+1)>>1)%MOD; //一次前缀和(sum)
}
inline ll sumy(ll n){n%=MOD;
return n*(n+1)%MOD*(n*2+1)%MOD*NI%MOD; //平方前缀和
}
inline ll sumphi(ll n){
if(n<MAXN-4)return sp[n];
if(ph.find(n)!=ph.end())return ph[n];
ll res=sumx(n)*sumx(n)%MOD;
for(ll i=2,ls;i<=n;i=ls+1)
ls=n/(n/i),res=(res-(sumy(ls)-sumy(i-1)+MOD)%MOD*sumphi(n/i)%MOD+MOD)%MOD;
return ph[n]=res;
}
inline ll getans(ll n){
ll res=0;
for(ll i=1,ls;i<=n;i=ls+1)
ls=n/(n/i),res=(res+(sumphi(ls)-sumphi(i-1)+MOD)%MOD*sumx(n/i)%MOD*sumx(n/i)%MOD)%MOD;
return res;
}
int main()
{
build();n=read();
printf("%lld\n",getans(n));
return 0;
}