$$
\begin {aligned}
\sum _ {i = 1} ^ n \sum _ {j = 1} ^ n (i, j) ^ {i + j} & = \sum _ {d = 1} ^ n \sum _ {i = 1} ^ n \sum _ {j = 1} ^ n [(i, j) = d] d ^ {i + j} \\
& = \sum _ {d = 1} ^ n \sum _ {i = 1} ^ {\lfloor \frac n d \rfloor } \sum _ {j = 1} ^ {\lfloor \frac n d \rfloor } [(i, j) = 1] d ^ {id + jd} \\
& = \sum _ {d = 1} ^ n \sum _ {i = 1} ^ {\lfloor \frac n d \rfloor } \sum _ {j = 1} ^ {\lfloor \frac n d \rfloor } \sum _ {k | (i, j)}\mu(k) d ^ {id + jd} \\
& = \sum _ {d = 1} ^ n \sum _ {k = 1} ^ n \mu(k) \sum _ {i = 1} ^ {\lfloor \frac n {kd} \rfloor } \sum _ {j = 1} ^ {\lfloor \frac n {kd} \rfloor } d ^ {id + jd} \\
& = \sum _ {d = 1} ^ n \sum _ {k = 1} ^ {\lfloor \frac n d \rfloor } \mu(k) (\sum _ {i = 1} ^ {\lfloor \frac n {kd} \rfloor } d ^ {id}) ^ 2 \\
\end {aligned}
$$
最后部分可以用等比数列算。于是可以做到 $O(n \ln n \log p)$ 。
查看代码
1 |
|