推式子,不妨设 $n \le m$ :
$$
\begin {aligned}
& \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m d(i) d(j) d((i, j)) \\
= & \sum _ {x = 1} ^ {n} d(x) \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m d(i) d(j) [(i, j) = x] \\
= & \sum _ {x = 1} ^ {n} d(x) \sum _ {i = 1} ^ {\left \lfloor \frac n x \right \rfloor} \sum _ {j = 1} ^ {\left \lfloor \frac m x \right \rfloor} d(ix) d(jx) [(i, j) = 1] \\
= & \sum _ {x = 1} ^ {n} d(x) \sum _ {i = 1} ^ {\left \lfloor \frac n x \right \rfloor} \sum _ {j = 1} ^ {\left \lfloor \frac m x \right \rfloor} d(ix) d(jx) \sum _ {y | i \wedge y | j} \mu(y) \\
= & \sum _ {x = 1} ^ {n} d(x) \sum _ {y = 1} ^ {\left \lfloor \frac n x \right \rfloor} \mu(y) \sum _ {i = 1} ^ {\left \lfloor \frac n {xy} \right \rfloor} \sum _ {j = 1} ^ {\left \lfloor \frac m {xy} \right \rfloor} d(ixy) d(jxy)
\end {aligned}
$$
令 $T = xy$ ,则:
$$
\begin {aligned}
& \sum _ {T = 1} ^ n \sum _ {x | T} d(x) \mu(\frac T x) \sum _ {i = 1} ^ {\left \lfloor \frac n T \right \rfloor} d(iT) \sum _ {j = 1} ^ {\left \lfloor \frac m T \right \rfloor} d(jT) \\
= & \sum _ {T = 1} ^ n d * \mu(T) \sum _ {i = 1} ^ {\left \lfloor \frac n T \right \rfloor} d(iT) \sum _ {j = 1} ^ {\left \lfloor \frac m T \right \rfloor} d(jT) \\
= & \sum _ {T = 1} ^ n \sum _ {i = 1} ^ {\left \lfloor \frac n T \right \rfloor} d(iT) \sum _ {j = 1} ^ {\left \lfloor \frac m T \right \rfloor} d(jT) \\
\end {aligned}
$$
注意到 $\displaystyle \sum _ {i = 1} ^ {\left \lfloor \frac n T \right \rfloor} d(iT)$ 与 $\displaystyle \sum _ {j = 1} ^ {\left \lfloor \frac m T \right \rfloor} d(jT)$ 是互相独立的,因此只有两层循环,复杂度为 $O(n \log n)$ 。
查看代码
1 |
|