P3327 [SDOI2015]约数个数和 解题笔记
原文创建时间:2021-02-13 23:07:38
题目链接
解题笔记—搬运
首先,有结论
$$\displaystyle d(a,b) = \sum_{i|a}\sum_{j|b}[i \perp j]$$
故原式可化为
$$\displaystyle \sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}[x \perp y]$$
下面作变换:
$\displaystyle = \sum_{i=1}^N\sum_{j=1}^M[i \perp j]\left\lfloor\frac{N}{i}\right\rfloor\left\lfloor\frac{M}{j}\right\rfloor$
$\displaystyle = \sum_{i=1}^N\sum_{j=1}^M\varepsilon(\gcd(i,j))\left\lfloor\frac{N}{i}\right\rfloor\left\lfloor\frac{M}{j}\right\rfloor$
$\displaystyle = \sum_{d=1}^{\min(N,M)}\mu(d)\sum_{i=1}^{\left\lfloor\frac{N}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{d}\right\rfloor}\left\lfloor\frac{N}{di}\right\rfloor\left\lfloor\frac{M}{dj}\right\rfloor$
接下来,反用结论得到
$$\displaystyle \sum_{d=1}^{\min(N,M)}\mu(d)\sum_{i=1}^{\left\lfloor\frac{N}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{d}\right\rfloor}\left\lfloor\frac{\left\lfloor\frac{N}{d}\right\rfloor}{i}\right\rfloor\left\lfloor\frac{\left\lfloor\frac{M}{d}\right\rfloor}{j}\right\rfloor$$
不妨记
$$\displaystyle f(n) = \sum_{i=1}^n\left\lfloor\frac{n}{i}\right\rfloor$$
则答案最终为
$$\displaystyle \sum_{d=1}^{\min(N,M)}\mu(d)f\left(\left\lfloor\frac{N}{d}\right\rfloor\right)f\left(\left\lfloor\frac{M}{d}\right\rfloor\right)$$
考虑
$$\displaystyle f(n) = \sum_{i=1}^n\left\lfloor\frac{n}{i}\right\rfloor = \sum_{k=1}^nd(k)$$
则用线性筛求出 $\mu$ 和 $d$ 的前缀和即可。
如何用线性筛求 $d$?
根据 $d$ 的定义和性质,以下为通过线性筛求 $d$ 的代码:
1 |
|
本题代码
1 |
|