前置知识

狄利克雷卷积

形如 f ( n ) = <munder> d n </munder> g ( d ) h ( <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> ) f(n) = \sum\limits_{d|n} g(d)*h(\dfrac{n}{d}) f(n)=dng(d)h(dn),记作 f f f g g g h h h的狄利克雷卷积,记作 f = g h f = g*h f=gh
e g . eg. eg. f ( n ) = <munder> d n </munder> g ( d ) f = g 1 f(n)=\sum\limits_{d|n}g(d) \Rightarrow f = g*1 f(n)=dng(d)f=g1
f = f e f = f * e f=fe e e e 为单位函数

狄利克雷卷积的性质

狄利克雷卷积满***换律和结合律,即
f = g h k = g k h = g ( h k ) f = g*h*k=g*k*h=g*(h*k) f=ghk=gkh=g(hk)


进入正题


莫比乌斯函数

定义

定义可以参考其他 b l o g blog blog (TAT,分段函数实在太难打了)

性质

<munder> d n </munder> μ ( d ) = [ n = 1 ] \sum\limits_{d|n} \mu(d)=[n=1] dnμ(d)=[n=1],即是 μ 1 = e ( n ) \mu * 1 = e(n) μ1=e(n) e e e 为单位函数。
这是莫比乌斯函数很重要的性质,反演基本基于此。

证明如下:
考虑到每个 u ( d ) u(d) u(d) a n s ans ans 贡献只是 1 1 1 1 -1 1 0 0 0 不用管)
n = i = 1 m p i a i n = \prod\limits_{i=1}^{m}p_i^{a_i} n=i=1mpiai
那么 n > 1 n > 1 n>1 a n s = k = 0 m ( 1 ) k C m k = ( 1 1 ) m = 0 ans = \sum\limits_{k=0}^{m}(-1)^kC_m^k=(1-1)^m=0 ans=k=0m(1)kCmk=(11)m=0
n = 1 n = 1 n=1时, a n s = 1 ans = 1 ans=1
证毕。

莫比乌斯反演定理

若有 f ( n ) = <munder> d n </munder> g ( d ) f(n) = \sum\limits_{d|n}g(d) f(n)=dng(d),则有 g ( n ) = <munder> d n </munder> f ( d ) μ ( <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> ) g(n) = \sum\limits_{d|n}f(d)*\mu(\dfrac{n}{d}) g(n)=dnf(d)μ(dn)

证明如下:
现有 f = g 1 f = g*1 f=g1,则有 f μ = g ( μ 1 ) = g e = g f*\mu=g*(\mu*1)=g*e=g fμ=g(μ1)=ge=g(这是狄利克雷卷积的证法,当然还有其他证法,可参考其他 b l o g blog blog


莫比乌斯函数的应用

①:让我们先来考虑下面一道最最最经典的题:

a n s = i = 1 n j = 1 m [ g c d ( i , j ) = = 1 ] ( n < m ) ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)==1](n < m) ans=i=1nj=1m[gcd(i,j)==1](n<m)
解:考虑到 [ g c d ( i , j ) = = 1 ] [gcd(i,j) == 1] [gcd(i,j)==1] 是单位函数, 而 e = μ 1 e = \mu*1 e=μ1,于是
a n s = i = 1 n j = 1 m <munder> d g c d ( i , j ) </munder> μ ( d ) = i = 1 n j = 1 m <munder> d i , d j </munder> μ ( d ) ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\sum\limits_{d|gcd(i,j)}\mu(d)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\sum\limits_{d|i,d|j}\mu(d) ans=i=1nj=1mdgcd(i,j)μ(d)=i=1nj=1mdi,djμ(d)
考虑每个 μ ( d ) \mu(d) μ(d) 出现了多少次,则有
a n s = d = 1 n μ ( d ) <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> <mstyle displaystyle="true" scriptlevel="0"> m d </mstyle> ans = \sum\limits_{d=1}^{n}\mu(d)*\left\lfloor\dfrac{n}{d}\right\rfloor*\left\lfloor\dfrac{m}{d}\right\rfloor ans=d=1nμ(d)dndm,然后用除法分块,可以使复杂度达到 O ( n ) O(\sqrt{n}) O(n )

②:进阶版

a n s = i = 1 n j = 1 m g c d ( i , j ) ( n < m ) ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}gcd(i,j)(n < m) ans=i=1nj=1mgcd(i,j)(n<m)
枚举 d = g c d ( i , j ) d = gcd(i,j) d=gcd(i,j),有
a n s = d = 1 n d i = 1 n j = 1 m [ g c d ( i / d , j / d ) = = 1 ] = d = 1 n d i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> </mstyle> j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m d </mstyle> </mstyle> [ g c d ( i , j ) = = 1 ] = d = 1 n d i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> </mstyle> j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m d </mstyle> </mstyle> <munder> k i , k j </munder> μ ( k ) = d = 1 n d k = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> </mstyle> μ ( k ) <mstyle displaystyle="true" scriptlevel="0"> n k d </mstyle> <mstyle displaystyle="true" scriptlevel="0"> m k d </mstyle> ans = \sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i/d,j/d)==1] = \sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{m}{d}\right\rfloor}}[gcd(i,j)==1]=\sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{m}{d}\right\rfloor}}\sum\limits_{k|i,k|j}\mu(k)=\sum\limits_{d=1}^{n}d\sum\limits_{k=1}^{{\tiny\left\lfloor\dfrac{n}{d}\right\rfloor}}\mu(k)\left\lfloor\dfrac{n}{kd}\right\rfloor\left\lfloor\dfrac{m}{kd}\right\rfloor ans=d=1ndi=1nj=1m[gcd(i/d,j/d)==1]=d=1ndi=1dnj=1dm[gcd(i,j)==1]=d=1ndi=1dnj=1dmki,kjμ(k)=d=1ndk=1dnμ(k)kdnkdm,然后 <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> \left\lfloor\dfrac{n}{d}\right\rfloor dn分块, <mstyle displaystyle="true" scriptlevel="0"> n k d </mstyle> \left\lfloor\dfrac{n}{kd}\right\rfloor kdn分块。
复杂度是 O ( n n = n ) O(\sqrt{n}*\sqrt{n}=n) O(n n =n)

③:再进阶一点点

这题是 b z o j <mtext>   </mtext> 2154 bzoj~2154 bzoj 2154 Crash的数字表格 / JZPTAB
a n s = i = 1 n j = 1 m l c m ( i , j ) ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}lcm(i,j) ans=i=1nj=1mlcm(i,j)
众所周知, l c m ( i , j ) = <mstyle displaystyle="true" scriptlevel="0"> i j g c d ( i , j ) </mstyle> lcm(i,j) = \dfrac{i*j}{gcd(i,j)} lcm(i,j)=gcd(i,j)ij,于是
a n s = i = 1 n j = 1 m <mstyle displaystyle="true" scriptlevel="0"> i j g c d ( i , j ) </mstyle> = d = 1 n i = 1 n j = 1 m <mstyle displaystyle="true" scriptlevel="0"> i j d </mstyle> [ g c d ( i , j ) = = d ] = d = 1 n i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> </mstyle> j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m d </mstyle> </mstyle> i j d [ g c d ( i , j ) = = 1 ] = d = 1 n d i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> </mstyle> j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m d </mstyle> </mstyle> i j <munder> k i , k j </munder> μ ( k ) ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\dfrac{i*j}{gcd(i,j)}=\sum\limits_{d=1}^{n}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\dfrac{i*j}{d}*[gcd(i,j)==d]=\sum\limits_{d=1}^{n}\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{d}\right\rfloor}}ijd[gcd(i,j)==1]=\sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{d}\right\rfloor}}ij\sum\limits_{k|i,k|j}\mu(k) ans=i=1nj=1mgcd(i,j)ij=d=1ni=1nj=1mdij[gcd(i,j)==d]=d=1ni=1dnj=1dmijd[gcd(i,j)==1]=d=1ndi=1dnj=1dmijki,kjμ(k),然后考虑枚举 k k k,有
a n s = d = 1 n d k = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n d </mstyle> </mstyle> μ ( k ) k 2 i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n k d </mstyle> </mstyle> i j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m k d </mstyle> </mstyle> j ans = \sum\limits_{d=1}^{n}d\sum\limits_{k=1}^{{\tiny \left\lfloor\dfrac{n}{d}\right\rfloor}}\mu(k)*k^2\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{kd}\right\rfloor}}i\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{kd}\right\rfloor}}j ans=d=1ndk=1dnμ(k)k2i=1kdnij=1kdmj,然后两个整除分块即可,复杂度 O ( n ) O(n) O(n)

代码如下
#include <bits/stdc++.h>
#define N 10000005
#define LL long long
#define mod 20101009
using namespace std;
int p[N], cnt, mu[N], s[N], g[N]; 
bool x[N];
LL z = 1, t;
int main(){
	int i, j, n, m, l1, l2, r1, r2, sum, ans, a, b;
	scanf("%d%d", &n, &m);
	if(n > m) swap(n, m);
	mu[1] = 1;
	for(i = 2; i <= m; i++){
		if(!x[i]) x[i] = 1, p[++cnt] = i, mu[i] = -1;
		for(j = 1; j <= cnt; j++){
			t = z * i * p[j];
			if(t > m) break;
			x[t] = 1;
			if(i % p[j] == 0) break;
			mu[t] = -mu[i];
		}
	}
	for(i = 1; i <= m; i++) g[i] = (g[i - 1] + i) % mod, s[i] = (s[i - 1] + z * mu[i] * i * i % mod + mod) % mod;
	ans = sum = 0;
	for(l1 = 1; l1 <= n; l1 = r1 + 1){
		j = n / l1; r1 = min(n / (n / l1), m / (m / l1));
		a = n / l1, b = m / l1;
		sum = 0;
		for(l2 = 1; l2 <= j; l2 = r2 + 1){
			r2 = min(a / (a / l2), b / (b / l2));
			sum = (sum + z * (s[r2] - s[l2 - 1]) * g[a / l2] % mod * g[b / l2] % mod) % mod;
		}
		ans = (ans + z * sum * (g[r1] - g[l1 - 1]) % mod) % mod;
	}
	printf("%d", (ans + mod) % mod);
	return 0;
}

④ 再进阶一点点

a n s = i = 1 n j = 1 m [ g c d ( i , j ) p r i m e ] ( n < m ) ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)\in prime](n < m) ans=i=1nj=1m[gcd(i,j)prime](n<m)
考虑枚举 n n n 以内的素数,于是有
a n s = <munder> p </munder> i = 1 n j = 1 m [ g c d ( i , j ) = = p ] = <munder> p </munder> i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n p </mstyle> </mstyle> j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m p </mstyle> </mstyle> [ g c d ( i , j ) = = 1 ] = <munder> p </munder> i = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n p </mstyle> </mstyle> j = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> m p </mstyle> </mstyle> <munder> k i , k j </munder> μ ( k ) = <munder> p </munder> k = 1 <mstyle mathsize="0&#46;5em"> <mstyle displaystyle="true" scriptlevel="0"> n p </mstyle> </mstyle> μ ( k ) <mstyle displaystyle="true" scriptlevel="0"> n k p </mstyle> <mstyle displaystyle="true" scriptlevel="0"> m k p </mstyle> ans = \sum\limits_{p}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)== p]=\sum\limits_{p}\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{p}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{p}\right\rfloor}}[gcd(i,j)==1]=\sum\limits_{p}\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{p}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{p}\right\rfloor}}\sum\limits_{k|i,k|j}\mu(k)=\sum\limits_{p}\sum\limits_{k=1}^{{\tiny \left\lfloor\dfrac{n}{p}\right\rfloor}}\mu(k){\left\lfloor\dfrac{n}{kp}\right\rfloor}{\left\lfloor\dfrac{m}{kp}\right\rfloor} ans=pi=1nj=1m[gcd(i,j)==p]=pi=1pnj=1pm[gcd(i,j)==1]=pi=1pnj=1pmki,kjμ(k)=pk=1pnμ(k)kpnkpm

操作来了!!

我们考虑枚举 T = k p T = kp T=kp,于是有
a n s = T = 1 n <mstyle displaystyle="true" scriptlevel="0"> n T </mstyle> <mstyle displaystyle="true" scriptlevel="0"> m T </mstyle> <munder> p T </munder> μ ( <mstyle displaystyle="true" scriptlevel="0"> T p </mstyle> ) ans = \sum\limits_{T=1}^{n}{\left\lfloor\dfrac{n}{T}\right\rfloor}{ \left\lfloor\dfrac{m}{T}\right\rfloor}\sum\limits_{p|T}\mu({\dfrac{T}{p}}) ans=T=1nTnTmpTμ(pT)
对于后面那部分,我们可以预处理,将每个素数的倍数 i i i 加上 μ ( i ) \mu(i) μ(i),复杂度是 O ( w l n ( w ) ) O(wln(w)) O(wln(w)) w w w 为素数个数,而 w w w 大约是 <mstyle displaystyle="true" scriptlevel="0"> n l n ( n ) </mstyle> \dfrac{n}{ln(n)} ln(n)n,所以总的复杂度是 O ( n ) O(n) O(n),单次询问的复杂度为 O ( n ) O(\sqrt{n}) O(n )

暂时就写这么多吧