题目描述
有$T$组询问,每次给定整数$a,b,d$,求有多少对正整数$x,y$,满足$x \le a \wedge y \le b \wedge (x,y)=d$
$1 \le a,b,d,T \le 50000$
题解
$$
\begin{aligned}
a \le b \\
\sum_{i=1}^{a}\sum_{j=1}^{b}[(i,j)=d]
&=\sum_{i=1}^{\lfloor \frac{a}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{b}{d} \rfloor}[(i,j)=1] \\
&=\sum_{i=1}^{\lfloor \frac{a}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{b}{d} \rfloor}\sum_{d’ \mid (i,j)}\mu(d’) \\
&=\sum_{i=1}^{\lfloor \frac{a}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{b}{d} \rfloor}\sum_{d’ \mid i \wedge d’ \mid j}\mu(d’) \\
&=\sum_{d’=1}^{\lfloor \frac{a}{d} \rfloor}\mu(d’)\sum_{d’ \mid i}^{\lfloor \frac{a}{d} \rfloor}\sum_{d’ \mid j}^{\lfloor \frac{b}{d} \rfloor}1 \\
&=\sum_{d’=1}^{\lfloor \frac{a}{d} \rfloor}\mu(d’) \lfloor \frac{a}{dd’} \rfloor \lfloor \frac{b}{dd’} \rfloor
\end{aligned}
$$
$\lfloor \frac{a}{dd’} \rfloor \lfloor \frac{b}{dd’} \rfloor$的取值只有$O(\sqrt a + \sqrt b)$种,预处理$\mu$的前缀和,单次询问可以$O(\sqrt n)$计算
代码
1 |
|