前置知识
狄利克雷卷积
形如 f(n)=∑d∣ng(d)∗h(nd)f(n) = \sum\limits_{d|n} g(d)*h(\dfrac{n}{d})f(n)=d∣n∑g(d)∗h(dn),记作 fff是 ggg 和 hhh的狄利克雷卷积,记作 f=g∗hf = g*hf=g∗h。
eg.eg.eg.f(n)=∑d∣ng(d)⇒f=g∗1f(n)=\sum\limits_{d|n}g(d) \Rightarrow f = g*1f(n)=d∣n∑g(d)⇒f=g∗1
f=f∗ef = f * ef=f∗e,eee 为单位函数
狄利克雷卷积的性质
狄利克雷卷积满足交换律和结合律,即
f=g∗h∗k=g∗k∗h=g∗(h∗k)f = g*h*k=g*k*h=g*(h*k)f=g∗h∗k=g∗k∗h=g∗(h∗k)
进入正题
莫比乌斯函数
定义
定义可以参考其他 blogblogblog (TAT,分段函数实在太难打了)
性质
∑d∣nμ(d)=[n=1]\sum\limits_{d|n} \mu(d)=[n=1]d∣n∑μ(d)=[n=1],即是 μ∗1=e(n)\mu * 1 = e(n)μ∗1=e(n),eee 为单位函数。
这是莫比乌斯函数很重要的性质,反演基本基于此。
证明如下:
考虑到每个 u(d)u(d)u(d) 对 ansansans 贡献只是 111 或 −1-1−1( 000 不用管)
设 n=∏i=1mpiain = \prod\limits_{i=1}^{m}p_i^{a_i}n=i=1∏mpiai
那么 n>1n > 1n>1 时ans=∑k=0m(−1)kCmk=(1−1)m=0ans = \sum\limits_{k=0}^{m}(-1)^kC_m^k=(1-1)^m=0ans=k=0∑m(−1)kCmk=(1−1)m=0
n=1n = 1n=1时, ans=1ans = 1ans=1。
证毕。
莫比乌斯反演定理
若有 f(n)=∑d∣ng(d)f(n) = \sum\limits_{d|n}g(d)f(n)=d∣n∑g(d),则有 g(n)=∑d∣nf(d)∗μ(nd)g(n) = \sum\limits_{d|n}f(d)*\mu(\dfrac{n}{d})g(n)=d∣n∑f(d)∗μ(dn)
证明如下:
现有 f=g∗1f = g*1f=g∗1,则有 f∗μ=g∗(μ∗1)=g∗e=gf*\mu=g*(\mu*1)=g*e=gf∗μ=g∗(μ∗1)=g∗e=g(这是狄利克雷卷积的证法,当然还有其他证法,可参考其他 blogblogblog)
莫比乌斯函数的应用
①:让我们先来考虑下面一道最最最经典的题:
求 ans=∑i=1n∑j=1m[gcd(i,j)==1](n<m)ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)==1](n < m)ans=i=1∑nj=1∑m[gcd(i,j)==1](n<m)
解:考虑到 [gcd(i,j)==1][gcd(i,j) == 1][gcd(i,j)==1] 是单位函数, 而 e=μ∗1e = \mu*1e=μ∗1,于是
ans=∑i=1n∑j=1m∑d∣gcd(i,j)μ(d)=∑i=1n∑j=1m∑d∣i,d∣jμ(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=1∑nj=1∑md∣gcd(i,j)∑μ(d)=i=1∑nj=1∑md∣i,d∣j∑μ(d)
考虑每个 μ(d)\mu(d)μ(d) 出现了多少次,则有
ans=∑d=1nμ(d)∗⌊nd⌋∗⌊md⌋ans = \sum\limits_{d=1}^{n}\mu(d)*\left\lfloor\dfrac{n}{d}\right\rfloor*\left\lfloor\dfrac{m}{d}\right\rfloorans=d=1∑nμ(d)∗⌊dn⌋∗⌊dm⌋,然后用除法分块,可以使复杂度达到 O(n)O(\sqrt{n})O(n)
②:进阶版
求 ans=∑i=1n∑j=1mgcd(i,j)(n<m)ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}gcd(i,j)(n < m)ans=i=1∑nj=1∑mgcd(i,j)(n<m)
枚举 d=gcd(i,j)d = gcd(i,j)d=gcd(i,j),有
ans=∑d=1nd∑i=1n∑j=1m[gcd(i/d,j/d)==1]=∑d=1nd∑i=1⌊nd⌋∑j=1⌊md⌋[gcd(i,j)==1]=∑d=1nd∑i=1⌊nd⌋∑j=1⌊md⌋∑k∣i,k∣jμ(k)=∑d=1nd∑k=1⌊nd⌋μ(k)⌊nkd⌋⌊mkd⌋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\rfloorans=d=1∑ndi=1∑nj=1∑m[gcd(i/d,j/d)==1]=d=1∑ndi=1∑⌊dn⌋j=1∑⌊dm⌋[gcd(i,j)==1]=d=1∑ndi=1∑⌊dn⌋j=1∑⌊dm⌋k∣i,k∣j∑μ(k)=d=1∑ndk=1∑⌊dn⌋μ(k)⌊kdn⌋⌊kdm⌋,然后 ⌊nd⌋\left\lfloor\dfrac{n}{d}\right\rfloor⌊dn⌋分块,⌊nkd⌋\left\lfloor\dfrac{n}{kd}\right\rfloor⌊kdn⌋分块。
复杂度是O(n∗n=n)O(\sqrt{n}*\sqrt{n}=n)O(n∗n=n)
③:再进阶一点点
这题是 bzoj 2154bzoj~2154bzoj 2154 Crash的数字表格 / JZPTAB
求 ans=∑i=1n∑j=1mlcm(i,j)ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}lcm(i,j)ans=i=1∑nj=1∑mlcm(i,j)
众所周知,lcm(i,j)=i∗jgcd(i,j)lcm(i,j) = \dfrac{i*j}{gcd(i,j)}lcm(i,j)=gcd(i,j)i∗j,于是
ans=∑i=1n∑j=1mi∗jgcd(i,j)=∑d=1n∑i=1n∑j=1mi∗jd∗[gcd(i,j)==d]=∑d=1n∑i=1⌊nd⌋∑j=1⌊md⌋ijd[gcd(i,j)==1]=∑d=1nd∑i=1⌊nd⌋∑j=1⌊md⌋ij∑k∣i,k∣jμ(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=1∑nj=1∑mgcd(i,j)i∗j=d=1∑ni=1∑nj=1∑mdi∗j∗[gcd(i,j)==d]=d=1∑ni=1∑⌊dn⌋j=1∑⌊dm⌋ijd[gcd(i,j)==1]=d=1∑ndi=1∑⌊dn⌋j=1∑⌊dm⌋ijk∣i,k∣j∑μ(k),然后考虑枚举 kkk,有
ans=∑d=1nd∑k=1⌊nd⌋μ(k)∗k2∑i=1⌊nkd⌋i∑j=1⌊mkd⌋jans = \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}}jans=d=1∑ndk=1∑⌊dn⌋μ(k)∗k2i=1∑⌊kdn⌋ij=1∑⌊kdm⌋j,然后两个整除分块即可,复杂度 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;
}
④ 再进阶一点点
求 ans=∑i=1n∑j=1m[gcd(i,j)∈prime](n<m)ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)\in prime](n < m)ans=i=1∑nj=1∑m[gcd(i,j)∈prime](n<m)
考虑枚举 nnn 以内的素数,于是有
ans=∑p∑i=1n∑j=1m[gcd(i,j)==p]=∑p∑i=1⌊np⌋∑j=1⌊mp⌋[gcd(i,j)==1]=∑p∑i=1⌊np⌋∑j=1⌊mp⌋∑k∣i,k∣jμ(k)=∑p∑k=1⌊np⌋μ(k)⌊nkp⌋⌊mkp⌋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=p∑i=1∑nj=1∑m[gcd(i,j)==p]=p∑i=1∑⌊pn⌋j=1∑⌊pm⌋[gcd(i,j)==1]=p∑i=1∑⌊pn⌋j=1∑⌊pm⌋k∣i,k∣j∑μ(k)=p∑k=1∑⌊pn⌋μ(k)⌊kpn⌋⌊kpm⌋
操作来了!!
我们考虑枚举 T=kpT = kpT=kp,于是有
ans=∑T=1n⌊nT⌋⌊mT⌋∑p∣Tμ(Tp)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=1∑n⌊Tn⌋⌊Tm⌋p∣T∑μ(pT)
对于后面那部分,我们可以预处理,将每个素数的倍数 iii 加上 μ(i)\mu(i)μ(i),复杂度是 O(wln(w))O(wln(w))O(wln(w)),www 为素数个数,而 www 大约是 nln(n)\dfrac{n}{ln(n)}ln(n)n,所以总的复杂度是 O(n)O(n)O(n),单次询问的复杂度为 O(n)O(\sqrt{n})O(n)
1381

被折叠的 条评论
为什么被折叠?



