正文
增加 210 行删除 91 行
7 行未改动8910111213141516171819202122232425262736 行未改动646566676869707172737475764 行未改动8182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212 行未改动13513613713813914014114214314414514614714814915015115210 行未改动1631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422433 行未改动24724824925025125225325425525625725825926026126226326426526619 行未改动28628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633716 行未改动35435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942037 行未改动7 行未改动## 基础的函数定义- 数论函数:定义在正整数集上的函数。- 积性函数:若数论函数 $f$ 满足$$\gcd(a,b)=1 \Rightarrow f(ab)=f(a)f(b),$$则称 $f$ 为积性函数。- 完全积性函数:若数论函数 $f$ 对任意正整数 $a,b$ 都满足$$f(ab)=f(a)f(b),$$则称 $f$ 为完全积性函数。这些术语不必死记,后面会用到的核心是积性函数、完全积性函数和狄利克雷卷积。## 一些比较常见的函数- 单位元函数:36 行未改动$$\mu(n)=\begin{cases}1, & n=1,\\(-1)^k, & n=p_1p_2\cdots p_k,\ p_i\in\mathbb{P},\ p_i\text{ are pairwise distinct},\\0, & \exists p\in\mathbb{P},\ p^2\mid n.\end{cases}$$以上这些函数都比较常见。## 狄利克雷卷积4 行未改动=\sum_{d\mid n}f(d)\,g\!\left(\frac{n}{d}\right)=\sum_{xy=n}f(x)g(y).$$#### 狄利克雷卷积的应用:通过狄利克雷卷积,我们可以得到莫比乌斯反演最基本的一个转换:$$\mu * I=\varepsilon,$$即$$\sum_{d\mid n}\mu(d)=[n=1].$$由此可得:若$$F(n)=\sum_{d\mid n}f(d),$$则$$f(n)=\sum_{d\mid n}\mu(d)F\!\left(\frac nd\right)=\sum_{d\mid n}\mu\!\left(\frac nd\right)F(d).$$这里也就有了做题需要的最重要的一个结论:$$[\gcd(i,j)=1]=\sum_{d \mid \gcd(i,j)} \mu(d).$$还有一些其他的可以自己手动证一下:- $\operatorname{Id}_k * I = \sigma_k \Rightarrow \operatorname{Id}_k = \sigma_k * \mu$。- $\varphi * I = \operatorname{Id} \Rightarrow \varphi = \operatorname{Id} * \mu$。接下来是莫比乌斯函数的求法::::success[直接算]```cppint mu(int x){12 行未改动:::success[线性筛]```cppvoid init(int M){np[0]=np[1]=1;//不是质数mu[1]=1;//莫比乌斯函数的值for(int i=2;i<=M;i++){if(!np[i]){p[++tot]=i;mu[i]=-1;}for(int j=1;j<=tot && i*p[j]<=M;j++){np[i*p[j]]=1;if(i%p[j]==0)break;mu[i*p[j]]=-mu[i];}}return;10 行未改动先从简单的做起,其实都挺套路的。### [P2158 [SDOI2008] 仪仗队](https://www.luogu.com.cn/problem/P2158)考虑到如果一个节点会遮住另一个节点当且仅当它们在同一条直线 $y=kx+b$ 上,也就是它们所在过原点直线的斜率 $k$ 相等,C 君的位置为 $(0,0)$,则 $b$ 为 $0$,对于两个点 $(x,y)$ 和 $(u,v)$,有一个点会被遮挡当且仅当 $\frac{y}{x}=\frac{v}{u}$,所以问题转换为了求:这里要注意 $n=1$ 时答案为 $0$。当 $n>1$ 时:$$2+\sum_{i=1}^{n-1} \sum_{j=1}^{n-1} [\gcd(i,j)=1].$$考虑对其反演一下。即:$$2+\sum_{i=1}^{n-1} \sum_{j=1}^{n-1}\sum_{d\mid \gcd(i,j)}\mu(d).$$把 $d$ 给提到最前面:$$2+\sum_{d=1}^{n-1} \mu(d) \sum_{i=1}^{n-1} [d \mid i] \sum_{j=1}^{n-1} [d\mid j]=2+\sum_{d=1}^{n-1} \mu(d) \cdot \left \lfloor \frac{n-1}{d} \right \rfloor \cdot \left \lfloor \frac{n-1}{d} \right \rfloor.$$然后就可以 $O(n)$ 的做了。预处理 $\mu$ 的前缀和后,也可以用整除分块做到 $O(\sqrt{n})$。如果你不会整除分块,[戳这里](https://oi-wiki.org/math/number-theory/sqrt-decomposition/)。### [P3455 [POI 2007] ZAP-Queries](https://www.luogu.com.cn/problem/P3455)题目很显然了,让我们求:$$\sum_{i=1}^{a} \sum_{j=1}^{b} [\gcd(i,j)=d].$$令 $n=\left \lfloor \frac{a}{d} \right \rfloor$,$m=\left \lfloor \frac{b}{d} \right \rfloor$。这里我们考虑用 $i=i'd,j=j'd$ 来表示,则有:$$\sum_{i'=1}^{n}\sum_{j'=1}^{m}[\gcd(i'd,j'd)=d].$$重新改成 $i,j$ 有:$$\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=1].$$还是和上一道题一样转换:$$\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{t\mid \gcd(i,j)}\mu(t)\\\sum_{t=1}^{\min(n,m)}\mu(t)\sum_{i=1}^{n}[t\mid i]\sum_{j=1}^{m}[t\mid j]\\\sum_{t=1}^{\min(n,m)}\mu(t)\left \lfloor \frac{n}{t} \right \rfloor \cdot \left \lfloor \frac{m}{t} \right \rfloor.$$预处理 $\mu$ 的前缀和后,用整除分块,单次可以做到 $O(\sqrt{\min(n,m)})$。### [P2522 [HAOI2011] Problem b](https://www.luogu.com.cn/problem/P2522)这题还是比较简单的,我们令 $\operatorname{get}(n,m)$ 表示$$\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=1].$$这个的求法同上一题,然后你发现答案就可以通过二维前缀和转换为:$$\begin{aligned}&\operatorname{get}(\left \lfloor \frac{b}{k} \right \rfloor,\left \lfloor \frac{d}{k} \right \rfloor)-\operatorname{get}(\left \lfloor \frac{a-1}{k} \right \rfloor,\left \lfloor \frac{d}{k} \right \rfloor)\\&\quad -\operatorname{get}(\left \lfloor \frac{b}{k} \right \rfloor,\left \lfloor \frac{c-1}{k} \right \rfloor)+\operatorname{get}(\left \lfloor \frac{a-1}{k} \right \rfloor,\left \lfloor \frac{c-1}{k} \right \rfloor).\end{aligned}$$预处理 $\mu$ 的前缀和后整除分块直接做就可以了。### [P2257 CYY的GCD](https://www.luogu.com.cn/problem/P2257)默认 $n<m$。发现这道题目实际让我们求:$$\sum_{i=1}^{n} \sum_{j=1}^{m}[\gcd(i,j) \in \mathbb{P}],$$其中 $\mathbb{P}$ 表示质数集。3 行未改动\sum_{p\in\mathbb{P},\ p\le n}\sum_{i=1}^{n} \sum_{j=1}^{m}[\gcd(i,j) = p].$$老套路:$$\sum_{p\in\mathbb{P},\ p\le n}\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{p} \right \rfloor}[\gcd(i,j) = 1]\\\sum_{p\in\mathbb{P},\ p\le n}\sum_{d=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\mu(d) \times \left \lfloor \frac{n}{pd} \right \rfloor \times \left \lfloor \frac{m}{pd} \right \rfloor.$$然后令 $T=pd$,则:$$\sum_{T=1}^{n} \left \lfloor \frac{n}{T} \right \rfloor \cdot \left \lfloor \frac{m}{T} \right \rfloor \cdot \sum_{\substack{p\mid T\\ p\in\mathbb P}}\mu(T/p).$$发现最后一个和式是一个积性函数。可以 $O(n\log \log n)$ 预处理出来,然后就可以 $O(\sqrt{n})$ 的做。这里给出预处理的代码::::success[code]```cpp19 行未改动:::### [P2398 GCD SUM](https://www.luogu.com.cn/problem/P2398)还是考虑枚举 $\gcd$。$$\sum_{d=1}^{n}d\sum_{i=1}^{n}\sum_{j=1}^{n}[\gcd(i,j)=d]\\\sum_{d=1}^{n}d\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\sum_{j=1}^{\left \lfloor \frac{n}{d} \right \rfloor}[\gcd(i,j)=1]\\\sum_{d=1}^{n}d \sum_{x=1}^{\left\lfloor n/d\right\rfloor}\mu(x) \left \lfloor \frac{n}{dx} \right \rfloor ^2.$$令 $T=dx$,有:$$\sum_{T=1}^{n}\left \lfloor \frac{n}{T} \right \rfloor ^2\sum_{d\mid T}d\cdot \mu\left(\frac{T}{d}\right)\\\sum_{T=1}^{n}\varphi(T)\left \lfloor \frac{n}{T} \right \rfloor ^2.$$预处理 $\varphi$ 的前缀和后整除分块即可,时间 $O(\sqrt n)$。### [P1829 [集训队互测 2010] Crash的数字表格 / JZPTAB](https://www.luogu.com.cn/problem/P1829)还是默认 $n<m$。转换一下:$$\sum_{i=1}^{n}\sum_{j=1}^{m}\operatorname{lcm}(i,j).$$这里考虑将 $\operatorname{lcm}(i,j)$ 转换成 $\frac{i \times j}{\gcd(i,j)}$,再枚举这个 $\gcd$,变成:$$\sum_{d=1}^{n}\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{i\times j}{d}[\gcd(i,j)=d]\\\sum_{d=1}^{n}\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}i \cdot j \cdot d[\gcd(i,j)=1].$$把 $d$ 提出来:$$\sum_{d=1}^{n}d\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}i \cdot j[\gcd(i,j)=1]\\\sum_{d=1}^{n}d\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}i \cdot j \sum_{k \mid \gcd(i,j)} \mu(k).$$然后令 $f(x)=\frac{x(x+1)}{2}$,则:$$\sum_{d=1}^{n}d\sum_{k=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\mu(k)k^2\cdot f(\left \lfloor \frac{n}{dk} \right \rfloor) \cdot f(\left \lfloor \frac{m}{dk} \right \rfloor)\\\sum_{T=1}^{n}f(\left \lfloor \frac{n}{T} \right \rfloor)\cdot f(\left \lfloor \frac{m}{T} \right \rfloor) T\sum_{k\mid T}\mu(k)k.$$后面这坨还是一个积性函数,同样用线性筛处理即可。:::success[Code]```cppvoid init(int M){np[0]=np[1]=1;16 行未改动:::### [P3327 [SDOI2015] 约数个数和](https://www.luogu.com.cn/problem/P3327)首先考虑一个转换 $d(ij)=\sum_{x\mid i}\sum_{y\mid j}[\gcd(x,y)=1]$。给出一个证明,通过质因数分解,设 $p$ 在 $i$ 中出现 $a$ 次,在 $j$ 中出现 $b$ 次,则总共出现 $a+b$ 次,还有 $0$,总共是 $a+b+1$ 种不同的选择。右边,对于一个质数 $p$:- $x\mid i$,则 $x$ 中 $p$ 可以取 $[0,a]$ 次。- $y \mid j$,则 $y$ 中 $p$ 可以取 $[0,b]$ 次。- 因为 $\gcd(x,y)=1$,所以 $p$ 不能同时出现在左右两边。则 $(a+1)(b+1)-ab=a+b+1$ 和左边相等,可以证明出来。接下来考虑推式子了。还是 $n<m$。$$\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x\mid i}\sum_{y\mid j}[\gcd(x,y)=1].$$交换一下:$$\sum_{x=1}^{n}\sum_{y=1}^{m}\left \lfloor \frac{n}{x} \right \rfloor \left \lfloor \frac{m}{y} \right \rfloor [\gcd(x,y)=1]\\\sum_{x=1}^{n}\sum_{y=1}^{m}\left \lfloor \frac{n}{x} \right \rfloor \left \lfloor \frac{m}{y} \right \rfloor \sum_{d \mid \gcd(x,y)}\mu (d)\\\sum_{d=1}^{n}\mu(d) \sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{n}{dx} \right \rfloor \sum_{y=1}^{\left \lfloor \frac{m}{d} \right \rfloor}\left \lfloor \frac{m}{dx} \right \rfloor.$$因为$$\sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{n}{dx} \right \rfloor=\sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{\left \lfloor \frac{n}{d} \right \rfloor}{x} \right \rfloor.$$那么令$$f(k)=\sum_{i=1}^{k}\left \lfloor \frac{k}{i} \right \rfloor=\sum_{i=1}^{k}d(i),$$即 $f(k)$ 是约数个数函数 $d$ 的前缀和。那么原式就变成了:$$\sum_{d=1}^{n}\mu(d)\,f\left(\left \lfloor \frac{n}{d} \right \rfloor\right)f\left(\left \lfloor \frac{m}{d} \right \rfloor\right).$$于是就可以有 $O(\sqrt{\min(n,m)})$ 的做法了。### [P4449 于神之怒加强版](https://www.luogu.com.cn/problem/P4449)还是先枚举:$$\sum_{d=1}^{n}d^k\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=d]\\\sum_{d=1}^{n}d^k\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}[\gcd(i,j)=1]\\\sum_{d=1}^{n}d^k\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}\sum_{t\mid \gcd(i,j)} \mu(t)\\\sum_{d=1}^{n}d^k\sum_{t=1}^{\left \lfloor \frac{n}{d} \right \rfloor } \mu(t)\left \lfloor \frac{n}{dt} \right \rfloor \left \lfloor \frac{m}{dt} \right \rfloor.$$最后令 $T=dt$。$$\sum_{T=1}^{n}\left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor \sum_{d\mid T}d^k\cdot \mu\!\left(\frac{T}{d}\right).$$后面这个依然是一个积性函数,预处理出来::::success[Code]```cppvoid init(int M){37 行未改动7 行未改动8910111213141516171819202122232425262736 行未改动6465666768697071727374757677784 行未改动838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915012 行未改动16316416516616716816917017117217317417517617717817918018118218310 行未改动1941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993 行未改动30330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533619 行未改动35635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043116 行未改动44844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853937 行未改动7 行未改动## 基础的函数定义- 数论函数:定义在正整数集上的函数。- 积性函数:若数论函数 $f$ 满足 $f(1)=1$,且对任意满足 $\gcd(a,b)=1$ 的正整数 $a,b$,都有$$f(ab)=f(a)f(b),$$则称 $f$ 为积性函数。- 完全积性函数:若数论函数 $f$ 满足 $f(1)=1$,且对任意正整数 $a,b$,都有$$f(ab)=f(a)f(b),$$则称 $f$ 为完全积性函数。后面主要用到积性函数和狄利克雷卷积。## 一些比较常见的函数- 单位元函数:36 行未改动$$\mu(n)=\begin{cases}1, & n=1,\\(-1)^k, & n=p_1p_2\cdots p_k,\ p_1<p_2<\cdots<p_k,\ p_i\in\mathbb{P},\\0, & \exists p\in\mathbb{P},\ p^2\mid n.\end{cases}$$其中 $\mathbb{P}$ 表示全体质数构成的集合。以上这些函数都比较常见。## 狄利克雷卷积4 行未改动=\sum_{d\mid n}f(d)\,g\!\left(\frac{n}{d}\right)=\sum_{xy=n}f(x)g(y).$$### 狄利克雷卷积的应用首先证明恒等式$$\mu * I=\varepsilon.$$当 $n=1$ 时,$$(\mu*I)(1)=\mu(1)I(1)=1=\varepsilon(1).$$当 $n>1$ 时,设 $n$ 的互不相同的质因子为 $p_1,p_2,\dots,p_k$。只有不含平方因子的约数 $d$ 才满足 $\mu(d)\ne 0$,于是$$(\mu*I)(n)=\sum_{d\mid n}\mu(d)=\sum_{S\subseteq\{1,2,\dots,k\}}(-1)^{|S|}=(1-1)^k=0=\varepsilon(n).$$因此 $\mu*I=\varepsilon$,即$$\sum_{d\mid n}\mu(d)=[n=1].$$下面用它推导莫比乌斯反演。若$$F(n)=\sum_{d\mid n}f(d),$$也就是 $F=f*I$,两边同时卷积 $\mu$,由卷积的结合律与交换律得$$F*\mu=(f*I)*\mu=f*(I*\mu)=f*\varepsilon=f.$$因此$$f(n)=\sum_{d\mid n}\mu(d)F\!\left(\frac nd\right)=\sum_{d\mid n}\mu\!\left(\frac nd\right)F(d).$$特别地,取 $n=\gcd(i,j)$,由$\varepsilon(n)=[n=1]$ 可得后面最常用的结论:$$[\gcd(i,j)=1]=\sum_{d \mid \gcd(i,j)} \mu(d).$$还有一些其他的卷积恒等式,可以自己手动证一下:- $\operatorname{Id}_k * I = \sigma_k \Rightarrow \operatorname{Id}_k = \sigma_k * \mu$。- $\varphi * I = \operatorname{Id} \Rightarrow \varphi = \operatorname{Id} * \mu$。接下来是莫比乌斯函数的求法。:::success[直接算]```cppint mu(int x){12 行未改动:::success[线性筛]```cppvoid init(int M){np[0]=np[1]=1;mu[1]=1;for(int i=2;i<=M;i++){if(!np[i]){p[++tot]=i;mu[i]=-1;}for(int j=1;j<=tot && i*p[j]<=M;j++){np[i*p[j]]=1;if(i%p[j]==0){mu[i*p[j]]=0;break;}mu[i*p[j]]=-mu[i];}}return;10 行未改动先从简单的做起,其实都挺套路的。### [P2158 [SDOI2008] 仪仗队](https://www.luogu.com.cn/problem/P2158)第一象限内的点 $(x,y)$ 满足 $x,y\ge 1$。两个第一象限内的点 $(x,y)$ 与 $(u,v)$ 在过原点的同一条直线上,当且仅当$$\frac{y}{x}=\frac{v}{u},$$即 $yu=xv$。因此点 $(x,y)$ 可见当且仅当 $\gcd(x,y)=1$。坐标轴上的点 $(1,0)$ 与 $(0,1)$ 也可见。当 $n=1$ 时只有原点,答案为 $0$。当 $n>1$ 时,答案为$$2+\sum_{i=1}^{n-1} \sum_{j=1}^{n-1} [\gcd(i,j)=1].$$考虑反演,即$$2+\sum_{i=1}^{n-1} \sum_{j=1}^{n-1}\sum_{d\mid \gcd(i,j)}\mu(d).$$交换求和次序,将 $d$ 放到最外层:$$2+\sum_{d=1}^{n-1} \mu(d) \sum_{i=1}^{n-1} [d \mid i] \sum_{j=1}^{n-1} [d\mid j]=2+\sum_{d=1}^{n-1} \mu(d) \left \lfloor \frac{n-1}{d} \right \rfloor^2.$$直接枚举 $d$ 可以做到 $O(n)$。若预处理 $\mu$ 的前缀和,则单次询问可以用整除分块做到 $O(\sqrt n)$。如果你不会整除分块,[戳这里](https://oi-wiki.org/math/number-theory/sqrt-decomposition/)。### [P3455 [POI 2007] ZAP-Queries](https://www.luogu.com.cn/problem/P3455)题目要求$$\sum_{i=1}^{a} \sum_{j=1}^{b} [\gcd(i,j)=d].$$若 $d>\min(a,b)$,则答案为 $0$。否则令$$n=\left \lfloor \frac{a}{d} \right \rfloor,\qquadm=\left \lfloor \frac{b}{d} \right \rfloor.$$令 $i=di',\ j=dj'$,则$$\sum_{i=1}^{a}\sum_{j=1}^{b}[\gcd(i,j)=d]=\sum_{i'=1}^{n}\sum_{j'=1}^{m}[\gcd(i',j')=1].$$重新记成 $i,j$,即$$\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=1].$$还是和上一道题一样转换:$$\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{t\mid \gcd(i,j)}\mu(t)=\sum_{t=1}^{\min(n,m)}\mu(t)\sum_{i=1}^{n}[t\mid i]\sum_{j=1}^{m}[t\mid j]=\sum_{t=1}^{\min(n,m)}\mu(t)\left \lfloor \frac{n}{t} \right \rfloor \left \lfloor \frac{m}{t} \right \rfloor.$$预处理 $\mu$ 的前缀和后,用整除分块,单次询问可以做到 $O(\sqrt n+\sqrt m)$。### [P2522 [HAOI2011] Problem b](https://www.luogu.com.cn/problem/P2522)这题还是比较简单的。令 $\operatorname{get}(n,m)$ 表示$$\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=1],$$并规定若 $n\le 0$ 或 $m\le 0$,则 $\operatorname{get}(n,m)=0$。这个的求法同上一题。利用二维前缀和,答案转换为$$\begin{aligned}&\operatorname{get}\!\left(\left \lfloor \frac{b}{k} \right \rfloor,\left \lfloor \frac{d}{k} \right \rfloor\right)-\operatorname{get}\!\left(\left \lfloor \frac{a-1}{k} \right \rfloor,\left \lfloor \frac{d}{k} \right \rfloor\right)\\&\quad -\operatorname{get}\!\left(\left \lfloor \frac{b}{k} \right \rfloor,\left \lfloor \frac{c-1}{k} \right \rfloor\right)+\operatorname{get}\!\left(\left \lfloor \frac{a-1}{k} \right \rfloor,\left \lfloor \frac{c-1}{k} \right \rfloor\right).\end{aligned}$$预处理 $\mu$ 的前缀和后整除分块直接做就可以了。### [P2257 CYY的GCD](https://www.luogu.com.cn/problem/P2257)不妨设 $n\le m$。题目要求$$\sum_{i=1}^{n} \sum_{j=1}^{m}[\gcd(i,j) \in \mathbb{P}],$$其中 $\mathbb{P}$ 表示质数集。3 行未改动\sum_{p\in\mathbb{P},\ p\le n}\sum_{i=1}^{n} \sum_{j=1}^{m}[\gcd(i,j) = p].$$老套路:$$\sum_{p\in\mathbb{P},\ p\le n}\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{p} \right \rfloor}[\gcd(i,j) = 1]=\sum_{p\in\mathbb{P},\ p\le n}\sum_{t=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\mu(t) \left \lfloor \frac{n}{pt} \right \rfloor \left \lfloor \frac{m}{pt} \right \rfloor.$$然后令 $T=pt$,则$$\sum_{T=1}^{n} \left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor \sum_{\substack{p\mid T\\ p\in\mathbb P}}\mu(T/p).$$记$$g(T)=\sum_{\substack{p\mid T\\ p\in\mathbb P}}\mu(T/p).$$注意 $g(T)$ 不是积性函数,例如 $g(2)=1,\ g(3)=1$,但 $g(6)=-2\ne g(2)g(3)$。不过可以枚举每个质数 $p$,将它对倍数的贡献累加到 $g$ 中,复杂度为$$\sum_{p\le n}\left\lfloor\frac np\right\rfloor=O(n\log\log n).$$预处理 $g$ 的前缀和后,单次询问可以用整除分块做到 $O(\sqrt n+\sqrt m)$。这里给出预处理的代码::::success[code]```cpp19 行未改动:::### [P2398 GCD SUM](https://www.luogu.com.cn/problem/P2398)还是考虑枚举 $d=\gcd(i,j)$:$$\sum_{d=1}^{n}d\sum_{i=1}^{n}\sum_{j=1}^{n}[\gcd(i,j)=d]=\sum_{d=1}^{n}d\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\sum_{j=1}^{\left \lfloor \frac{n}{d} \right \rfloor}[\gcd(i,j)=1].$$再用莫比乌斯反演:$$\sum_{d=1}^{n}d \sum_{x=1}^{\left\lfloor n/d\right\rfloor}\mu(x) \left \lfloor \frac{n}{dx} \right \rfloor ^2.$$令 $T=dx$,有$$\sum_{T=1}^{n}\left \lfloor \frac{n}{T} \right \rfloor ^2\sum_{d\mid T}d\cdot \mu\!\left(\frac{T}{d}\right)=\sum_{T=1}^{n}\varphi(T)\left \lfloor \frac{n}{T} \right \rfloor ^2.$$其中最后一步用了$\varphi=\operatorname{Id}*\mu$。预处理 $\varphi$ 的前缀和后整除分块即可,单次询问 $O(\sqrt n)$,预处理 $O(n)$。### [P1829 [集训队互测 2010] Crash的数字表格 / JZPTAB](https://www.luogu.com.cn/problem/P1829)不妨设 $n\le m$。转换一下:$$\sum_{i=1}^{n}\sum_{j=1}^{m}\operatorname{lcm}(i,j).$$将 $\operatorname{lcm}(i,j)$ 转换成 $\frac{i \times j}{\gcd(i,j)}$,再枚举 $d=\gcd(i,j)$,变成$$\sum_{d=1}^{n}\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{i\times j}{d}[\gcd(i,j)=d]=\sum_{d=1}^{n}\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}i \cdot j \cdot d[\gcd(i,j)=1].$$令 $i=di',\ j=dj'$,则 $\frac{i\times j}{d}=d i'j'$,于是 $d$ 作为因子出现。化简得$$\sum_{d=1}^{n}d\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}i \cdot j[\gcd(i,j)=1]=\sum_{d=1}^{n}d\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}i \cdot j \sum_{k \mid \gcd(i,j)} \mu(k).$$然后令 $f(x)=\frac{x(x+1)}{2}$,则$$\sum_{d=1}^{n}d\sum_{k=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\mu(k)k^2\cdot f\!\left(\left \lfloor \frac{n}{dk} \right \rfloor\right) f\!\left(\left \lfloor \frac{m}{dk} \right \rfloor\right)=\sum_{T=1}^{n}f\!\left(\left \lfloor \frac{n}{T} \right \rfloor\right) f\!\left(\left \lfloor \frac{m}{T} \right \rfloor\right) T\sum_{k\mid T}\mu(k)k.$$记$$H(T)=\sum_{k\mid T}\mu(k)k,\qquad F(T)=T H(T).$$$H(T)$ 是积性函数,且对质数 $p$,有 $H(p)=1-p$;对 $a\ge 2$,有 $H(p^a)=H(p^{a-1})=1-p$。因此 $F(T)=T H(T)$ 也是积性函数,并且对质数 $p$ 有 $F(p)=p(1-p)$,对 $a\ge 2$ 有$$F(p^a)=p^a(1-p)=p\,F(p^{a-1}).$$下面的代码在筛法中先预处理 $H(i)$,最后乘 $i$ 并求前缀和得到 $F(i)$ 的前缀和。单次询问用整除分块,复杂度为 $O(\sqrt n+\sqrt m)$。:::success[Code]```cppvoid init(int M){np[0]=np[1]=1;16 行未改动:::### [P3327 [SDOI2015] 约数个数和](https://www.luogu.com.cn/problem/P3327)首先考虑转换$$d(ij)=\sum_{x\mid i}\sum_{y\mid j}[\gcd(x,y)=1].$$给出一个证明。通过质因数分解,设 $p$ 在 $i$ 中出现 $a$ 次,在 $j$ 中出现 $b$ 次,则在 $ij$ 中出现 $a+b$ 次,指数共有 $0,1,\dots,a+b$ 共 $a+b+1$ 种选择。右边,对于一个质数 $p$:- $x\mid i$,则 $x$ 中 $p$ 的指数可以取 $0\sim a$。- $y \mid j$,则 $y$ 中 $p$ 的指数可以取 $0\sim b$。- 因为 $\gcd(x,y)=1$,所以 $p$ 不能同时出现在 $x$ 和 $y$ 中。合法选择数为$$(a+1)(b+1)-ab=a+b+1,$$与左边相等,因此恒等式成立。接下来考虑推式子。不妨设 $n\le m$。$$\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x\mid i}\sum_{y\mid j}[\gcd(x,y)=1].$$交换求和次序:$$\sum_{x=1}^{n}\sum_{y=1}^{m}\left \lfloor \frac{n}{x} \right \rfloor \left \lfloor \frac{m}{y} \right \rfloor [\gcd(x,y)=1]=\sum_{x=1}^{n}\sum_{y=1}^{m}\left \lfloor \frac{n}{x} \right \rfloor \left \lfloor \frac{m}{y} \right \rfloor \sum_{d \mid \gcd(x,y)}\mu (d)=\sum_{d=1}^{n}\mu(d) \sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{n}{dx} \right \rfloor \sum_{y=1}^{\left \lfloor \frac{m}{d} \right \rfloor}\left \lfloor \frac{m}{dy} \right \rfloor.$$因为$$\sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{n}{dx} \right \rfloor=\sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{\left \lfloor \frac{n}{d} \right \rfloor}{x} \right \rfloor.$$令$$f(k)=\sum_{i=1}^{k}\left \lfloor \frac{k}{i} \right \rfloor=\sum_{i=1}^{k}d(i),$$即 $f(k)$ 是约数个数函数 $d$ 的前缀和。预处理 $d(i)$ 的复杂度为 $O(N\log N)$(也可以线性筛做到 $O(N)$),预处理 $f(k)$ 的复杂度为 $O(N)$。那么原式就变成了$$\sum_{d=1}^{n}\mu(d)\,f\!\left(\left \lfloor \frac{n}{d} \right \rfloor\right)f\!\left(\left \lfloor \frac{m}{d} \right \rfloor\right).$$预处理 $\mu$ 的前缀和后,单次询问用整除分块,复杂度为 $O(\sqrt n+\sqrt m)$。### [P4449 于神之怒加强版](https://www.luogu.com.cn/problem/P4449)不妨设 $n\le m$。还是先枚举 $d=\gcd(i,j)$:$$\sum_{d=1}^{n}d^k\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=d]=\sum_{d=1}^{n}d^k\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}[\gcd(i,j)=1].$$再用莫比乌斯反演:$$\sum_{d=1}^{n}d^k\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{d} \right \rfloor}\sum_{t\mid \gcd(i,j)} \mu(t)=\sum_{d=1}^{n}d^k\sum_{t=1}^{\left \lfloor \frac{n}{d} \right \rfloor } \mu(t)\left \lfloor \frac{n}{dt} \right \rfloor \left \lfloor \frac{m}{dt} \right \rfloor.$$最后令 $T=dt$:$$\sum_{T=1}^{n}\left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor \sum_{d\mid T}d^k\cdot \mu\!\left(\frac{T}{d}\right).$$记$$H(T)=\sum_{d\mid T}d^k\mu\!\left(\frac{T}{d}\right)=(\operatorname{Id}_k*\mu)(T).$$$H$ 是积性函数。下面的代码用枚举倍数的方式预处理 $H$,复杂度为 $O(M\log M)$,其中 $M$ 为最大 $n$。预处理 $H$ 的前缀和后,单次询问整除分块做到 $O(\sqrt n+\sqrt m)$。:::success[Code]```cppvoid init(int M){37 行未改动