Luogu P3768 simple math problem-Mobius inversion + Du Jiaosi derivation detailed explanation

Prerequisite skills: basic number theory, Mobius inversion, Dirichlet convolution, Du Jiao sieve

topic

Portal

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 ans=\sum_{i=1}^n\sum_{j=1}^nijgcd(i,j), 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:

ans=\sum_{i=1}^n\sum_{j=1}^nijgcd(i,j)=\sum_{d=1}^nd^3\sum_{i=1}^{\left\lfloor \frac{n}{d} \right\rfloor}\sum_{j=1}^{\left\lfloor \frac{n}{d} \right\rfloor}ij[gcd(i,j)=1]

The last two \sum_parts are very familiar. We found that we can use Mobius inversion. Let mf(x)=\sum_{i=1}^{n}\sum_{j=1}^{n}ij[gcd(i,j)=x],   mF(x)=\sum_{x|d}mf(x)(using m-size f to represent the function derived by Mobius inversion is to distinguish it from the latter),

Perceptual analysis is available, mF(x)=\sum_{x|d}mf(x)=x^2(\sum_{i=1}^{\left\lfloor \frac{n}{x} \right\rfloor}i)^2=x^2sum(\left\lfloor \frac{n}{x} \right\rfloor)^2(definition  sum(x)=\sum_{i=1}^xi=\frac{x(x+1)}{2} ) ,

So there is mf(x)=\sum_{x|d}\mu(\frac{d}{x})mF(d)=\sum_{x|d}\mu(\frac{d}{x})d^2sum(\left\lfloor \frac{n}{d} \right\rfloor)^2,

Will be mf(1)brought into the original ans=\sum_{d=1}^nd^3\sum_{i=1}^{\left\lfloor \frac{n}{d} \right\rfloor}\mu(i)i^2sum(\left\lfloor \frac{n}{di} \right\rfloor)^2equation: ,

Then we found that there is one in the loop. It is impossible to sum directly with the \ mucurrent 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 T = di, i = \ frac {T} {d}, there is ans=\sum_{T=1}^nsum(\left\lfloor \frac{n}{T} \right\rfloor)^2\sum_{d|T}d^3\frac{T^2}{d^2}\mu(\frac{T}{d})=\sum_{T=1}^nsum(\left\lfloor \frac{n}{T} \right\rfloor)^2T^2\sum_{d|T}d\mu(\frac{T}{d});

\sum_{d|T}d\mu(\frac{T}{d})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 \left\{\begin{matrix} \phi*I=id\\ \mu*I=\epsilon \end{matrix}\right. \Rightarrow \phi=\mu*idis to say \phi(x)=\sum_{d|x}d\mu(\frac{x}{d}),

It's nice, because we have been able to bring \ mua bunch replaced Euler function: ans=\sum_{T=1}^nsum(\left\lfloor \frac{n}{T} \right\rfloor)^2T^2\sum_{d|T}d\mu(\frac{T}{d})=\sum_{T=1}^nsum(\left\lfloor \frac{n}{T} \right\rfloor)^2T^2\phi(T),

Set f(x)=x^2\phi(x), then ans=\sum_{T=1}^nsum(\left\lfloor \frac{n}{T} \right\rfloor)^2f(T);

Obviously, \left\lfloor \frac{n}{T} \right\rfloorthere are only 2\sqrt{n}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 S(n)=\sum_{i=1}^nf(i), h(i)=(f*g)(i)=\sum_{d|i}f(d)g(\frac{i}{d}),

S(n)=(\sum_{i=1}^nh(i)-\sum_{i=2}^ng(i)S(\left\lfloor \frac{n}{i} \right\rfloor))/g(1)

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 g(x)=id^2(x)=x^2, then h(i)=\sum_{d|i}\phi(d)d^2\frac{i^2}{d^2}=i^2\sum_{d|i}\phi(d),

By \phi*I=id, that is \sum_{d|i}\phi(d)=id(i)=i, can be obtained  h(i)=i^3;

Thus we get a quick way to find the prefix and the function h, it is a prefix and a cubic equation: \ sum_ {i = 1} ^ ni ^ 3 = (\ sum_ {i = 1} ^ ni) ^ 2 = sum (n) ^ 2,

Then sets Du teach sieve: S(n)=\sum_{i=1}^ni^3-\sum_{i=1}^ni^2S(\left\lfloor \frac{n}{i} \right\rfloor)=sum(n)^2-\sum_{i=1}^ni^2S(\left\lfloor \frac{n}{i} \right\rfloor), (prefix and square formula: \ sum_ {i = 1} ^ ni ^ 2 = \ frac {n (n + 1) (2n + 1)} {6})

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 O (n), yes O(n^{\frac{2}{3}}).

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;
}

 

Guess you like

Origin blog.csdn.net/weixin_43960287/article/details/113483123