P4449 于神之怒加强版 题解
原文创建时间:2021-02-28 23:53:39
我来水一篇。
题目链接
题意
给定 $k$,询问 $T$ 次,每次询问给出 $n,m$,求
$$\displaystyle \sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^k$$
答案对 $10^9+7$ 取模。
解
大力推式子(认为 $n \leq m$):
$\displaystyle \ \ \ \ \sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^k$
$\displaystyle = \sum_{d=1}^nd^k\sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)=d]$
$\displaystyle = \sum_{d=1}^nd^k\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}[i \perp j]$
$\displaystyle = \sum_{d=1}^nd^k\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\sum_{q|\gcd(i,j)}\mu(q)$
$\displaystyle = \sum_{d=1}^nd^k\sum_{q=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(q)\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}[i|q]\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}[j|q]$
$\displaystyle = \sum_{d=1}^nd^k\sum_{q=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(q)\left\lfloor\frac{n}{dq}\right\rfloor\left\lfloor\frac{m}{dq}\right\rfloor$
$\displaystyle = \sum_{t=1}^n\sum_{d|t}d^k\mu\left(\frac{t}{d}\right)\left\lfloor\frac{n}{t}\right\rfloor\left\lfloor\frac{m}{t}\right\rfloor$
$\displaystyle = \sum_{t=1}^n\left\lfloor\frac{n}{t}\right\rfloor\left\lfloor\frac{m}{t}\right\rfloor\sum_{d|t}d^k\mu\left(\frac{t}{d}\right)$
记 $f_k(n) = n^k$,则原式
$\displaystyle = \sum_{t=1}^n\left\lfloor\frac{n}{t}\right\rfloor\left\lfloor\frac{m}{t}\right\rfloor(\mu \ast f_k)(t)$
考虑如何筛出 $g_k = \mu \ast f_k$:
对于质数 $p$,
$$\displaylines{\begin{cases}
g_k(1) = 1 \\
g_k( p ) = p^k - 1 \\
g_k(p^\alpha) = p^{(\alpha-1)k}(p^k - 1)
\end{cases}}$$
从而线性筛解决。
质数的 $k$ 次幂,在线性筛时求出即可。
时间复杂度:$\displaystyle \mathrm{O} \left(N\log k + T\sqrt{n}\right)$
代码
1 |
|