整除分块.
可以把 $i=j$ 的贡献算上,后面再减掉.
假设 $n\le m$ .
$$
\begin{aligned}
ans&=\sum_{i=1}^n \sum_{j=1}^m (n\bmod i)(m\bmod j)-\sum_{i=1}^n (n\bmod i)(m\bmod i) \\
&=\sum_{i=1}^n \sum_{j=1}^m (n-\lfloor\frac n i \rfloor\cdot i)(m-\lfloor\frac m j \rfloor\cdot j)-\sum_{i=1}^n (n-\lfloor\frac n i \rfloor\cdot i)(m-\lfloor\frac m i \rfloor\cdot i) \\
&=\sum_{i=1}^n (n-\lfloor\frac n i \rfloor\cdot i)\sum_{i=1}^m (m-\lfloor\frac m i \rfloor\cdot i) -\sum_{i=1}^n nm+i^2\cdot \lfloor\frac n i \rfloor\lfloor\frac m i \rfloor-n\cdot \lfloor\frac m i \rfloor\cdot i-m\cdot \lfloor\frac n i \rfloor \cdot i
\end{aligned}
$$
整除分块计算即可.
1 |
|