P2257 YY的GCD

题意

求:

\[\sum_{i = 1} ^ n \sum_{j = 1} ^ m [\gcd(i, j) \in prime] \]

Sol

考虑先枚举 \(gcd\)

\[\sum_{k = 1} ^ {\min(n, m)} \sum_{i = 1} ^ n \sum_{j = 1} ^ m [\gcd(i, j) = k] (k \in prime) \]

整个式子同时除以 \(k\)

\[\sum_{k = 1} ^ {\min(n, m)} \sum_{i = 1} ^ {\lfloor \frac{n}{k} \rfloor} \sum_{j = 1} ^ {\lfloor \frac{m}{k} \rfloor} [\gcd(i, j) = 1] (k \in prime) \]

反演一下

\[\sum_{k = 1} ^ {\min(n, m)} \sum_{i = 1} ^ {\lfloor \frac{n}{k} \rfloor} \sum_{j = 1} ^ {\lfloor \frac{m}{k} \rfloor} \sum_{d | \gcd(i, j)} \mu(d) (k \in prime) \]

去掉 \(\sum\)

\[\sum_{k = 1} ^ {\min(n, m)} \sum_{d = 1} ^ {\min(n, m)} \mu(d) \lfloor \frac{n}{dk} \rfloor \lfloor \frac{m}{dk} \rfloor (k \in prime) \]

\(g = d \times k\),考虑枚举 \(g\)

\[\sum_{g = 1} ^ {min(n, m)} \lfloor \frac{n}{g} \rfloor \lfloor \frac{m}{g} \rfloor \sum_{k = 1} ^ {\min(n, m)} \mu(\frac{g}{k}) (k \in prime) \]

前面整除分块,后面预处理前缀和,做完了。

复杂度 \(O(n + T \times \sqrt n)\)

posted @ 2024-01-25 11:32  cxqghzj  阅读(3)  评论(0编辑  收藏  举报