神仙 东方 莫反题
以下所有除法/分数默认下取整,为方便记模数为 $P$
一眼望过去分讨题,然后发现每种都好难……
$$
\begin{aligned}
Ans
= & \prod_{i = 1}^{A} \prod_{j = 1}^{B} \prod_{k = 1}^{C} (\frac{lcm(i, j)}{\gcd(i, k)})^{f(type)} \\
= & \prod_{i = 1}^{A} \prod_{j = 1}^{B} \prod_{k = 1}^{C} (\frac{ij}{\gcd(i, k) \gcd(i, j)})^{f(type)} \\
\end{aligned}
$$
设:
$$
\begin{aligned}
g(a, b, c) = \prod_{i = 1}^{a} \prod_{j = 1}^{b} \prod_{k = 1}^{c} i^{f(type)} \\
h(a, b, c) = \prod_{i = 1}^{a} \prod_{j = 1}^{b} \prod_{k = 1}^{c} \gcd(i, j)^{f(type)}
\end{aligned}
$$
原式为
$$
Ans = \frac{g(A, B, C)g(B, A, C)}{h(A, B, C) h(A, C, B)}
$$
然后分讨
part 1
$$
\begin{aligned}
g(a, b, c)
= & \prod_{i = 1}^{a} \prod_{j = 1}^{b} \prod_{k = 1}^{c} i \\
= & \prod_{i = 1}^A i^{bc} \\
= & (a!)^{bc}
\end{aligned}
$$
$O(n)$ 预处理阶乘,快速幂可以单次 $O(\log P)$ 求出 $g$
$$
\begin{aligned}
h(a, b, c)
= & \prod_{i = 1}^{a} \prod_{j = 1}^{b} \prod_{k = 1}^{c} \gcd(i, j) \\
= & \prod_{i = 1}^{a} \prod_{j = 1}^{b} \gcd(i, j)^{c} \\
\end{aligned}
$$
看到连乘尝试取对数得:
$$
\begin{aligned}
\ln(h(a, b, c))
= & c \sum_{i = 1}^a \sum_{j = 1}^b \ln(\gcd(i, j)) \\
= & c \sum_{d = 1}^{t = \min(a, b)} \ln(d) \sum_{i = 1}^{\frac{a}{d}} \sum_{j = 1}^\frac{b}{d} [\gcd(i, j) == 1] \\
= & c \sum_{d = 1}^{t} \ln(d) \sum_{i = 1}^{\frac{a}{d}} \sum_{j = 1}^\frac{b}{d} \sum_{x | i \wedge x|j} \mu(x) \\
= & c \sum_{d = 1}^{t} \ln(d) \sum_{x}^{\frac{t}{d}} \sum_{i = 1}^{\frac{a}{dx}} \sum_{j = 1}^{\frac{b}{dx}} \mu(x) \\
= & c \sum_{d = 1}^{t} \ln(d) \sum_{x}^{\frac{t}{d}} \mu(x) \frac{a}{dx} \frac{b}{dx}\\
= & c \sum_{d = 1}^{t} \sum_{x}^{\frac{t}{d}} \ln(d) \mu(x) \frac{a}{dx} \frac{b}{dx}\\
= & c \sum_{p = 1}^{t} \sum_{d | p} \ln(d) \mu(\frac{p}{d}) \frac{a}{p} \frac{b}{p}\\
= & c \sum_{p = 1}^{t} \frac{a}{p} \frac{b}{p} \sum_{d | p} \ln(d) \mu(\frac{p}{d}) \\
\end{aligned}
$$
再取 $\exp$ :
$$
\begin{aligned}
h(a, b, c)
= & (\prod_{p = 1}^{t} (\prod_{d | p} d^{\mu(\frac{p}{d})})^{\frac{a}{p} \frac{b}{p}})^c
\end{aligned}
$$
这个跟 shit 一样的东西竟然是可以算的,考虑到 $\mu$ 只有三个取值,只要 $O(n(\log P + \log n))$ 预处理中间的东西,再整除分块可以做到 $O(\sqrt{n} \log P)$
part 2
记 $S(x) = \sum_{i = 1}^x i = \frac{x(x + 1)}{2}$
$$
\begin{aligned}
g(a, b, c)
= & \prod_{i = 1}^{a} \prod_{j = 1}^{b} \prod_{k = 1}^{c} i^{ijk} \\
= & \prod_{i = 1}^{a} (i^i)^{S(b) S(c)} \\
= & (\prod_{i = 1}^{a} i^i)^{S(b) S(c)} \\
\end{aligned}
$$
可以 $O(n \log P)$ 预处理, $O(\log P)$ 求
$$
\begin{aligned}
h(a, b, c)
= & \prod_{i = 1}^{a} \prod_{j = 1}^{b} \prod_{k = 1}^{c} \gcd(i, j)^{ijk} \\
= & \prod_{i = 1}^{a} \prod_{j = 1}^{b} (\gcd(i, j)^{ij})^{S(c)} \\
\end{aligned}
$$
还是取对数:
$$
\begin{aligned}
\ln(h(a, b, c))
= & S(c) \sum_{i = 1}^a \sum_{j = 1}^b ij \ln(\gcd(i, j)) \\
= & S(c) \sum_{d = 1}^{t = \min(a, b)} \sum_{i = 1}^{\frac{a}{d}} \sum_{j = 1}^{\frac{b}{d}} [\gcd(i, j) == 1] i j d^2 \ln(d) \\
= & S(c) \sum_{d = 1}^{t} d^2 \ln(d) \sum_{i = 1}^{\frac{a}{d}} \sum_{j = 1}^{\frac{b}{d}} i j \sum_{x | i \wedge x | j} \mu(x) \\
= & S(c) \sum_{d = 1}^{t} d^2 \ln(d) \sum_{x = 1}^{\frac{t}{d}} \mu(x) \sum_{i = 1}^{\frac{a}{dx}} \sum_{j = 1}^{\frac{b}{dx}} i j x^2\\
= & S(c) \sum_{d = 1}^{t} d^2 \ln(d) \sum_{x = 1}^{\frac{t}{d}} x^2 \mu(x) \sum_{i = 1}^{\frac{a}{dx}} \sum_{j = 1}^{\frac{b}{dx}} i j \\
= & S(c) \sum_{d = 1}^{t} \sum_{x = 1}^{\frac{t}{d}} d^2 \ln(d) x^2 \mu(x) S(\frac{a}{dx}) S(\frac{b}{dx}) \\
= & S(c) \sum_{d = 1}^{t} \sum_{x = 1}^{\frac{t}{d}} (dx)^2 \ln(d) \mu(x) S(\frac{a}{dx}) S(\frac{b}{dx}) \\
= & S(c) \sum_{p = 1}^{t} \sum_{d | p} p^2 \ln(d) \mu(\frac{p}{d}) S(\frac{a}{p}) S(\frac{b}{p}) \\
= & S(c) \sum_{p = 1}^{t} S(\frac{a}{p}) S(\frac{b}{p}) \sum_{d | p} \ln(d) \mu(\frac{p}{d}) p^2 \\
\end{aligned}
$$
然后 $\exp$
$$
\begin{aligned}
h(a, b, c)
= & (\prod_{p = 1}^t (\prod_{d | p} d^{\mu(\frac{p}{d})p^2} )^{S(\frac{a}{p}) S(\frac{b}{p})})^{S(c)}
\end{aligned}
$$
最里面的部分和 part 1 类似,但由于有 $p^2$ 导致复杂度变为 $O(n \log n \log P)$ ,每次询问还是整除分块 $O(\sqrt{n} \log P)$
part 3
此时 $g, h$ 都不太好算,我们直接计算原式:
$$
\begin{aligned}
& \prod_{i = 1}^{A} \prod_{j = 1}^{B} \prod_{k = 1}^{C} (\frac{lcm(i, j)}{\gcd(i, k)})^{\gcd(i, j, k)} \\
\end{aligned}
$$
取对数:
$$
\begin{aligned}
& \sum_{i = 1}^{A} \sum_{j = 1}^{B} \sum_{k = 1}^{C} \gcd(i, j, k) \ln(\frac{lcm(i, j)}{\gcd(i, k)}) \\
= & \sum_{d = 1}^{t = \min(a, b, c)} \sum_{i = 1}^{\frac{A}{d}} \sum_{j = 1}^{\frac{B}{d}} \sum_{k = 1}^{\frac{C}{d}} [\gcd(i, j, k) == 1] d \ln(\frac{lcm(id, jd)}{\gcd(id, kd)}) \\
= & \sum_{d = 1}^{t} \sum_{i = 1}^{\frac{A}{d}} \sum_{j = 1}^{\frac{B}{d}} \sum_{k = 1}^{\frac{C}{d}} d \ln(\frac{lcm(i, j)}{\gcd(i, k)}) \sum_{x | i \wedge x | j \wedge x | k} \mu(x) \\
= & \sum_{d = 1}^{t} \sum_{x = 1}^{\frac{t}{d}} \sum_{i = 1}^{\frac{A}{dx}} \sum_{j = 1}^{\frac{B}{dx}} \sum_{k = 1}^{\frac{C}{dx}} d \mu(x) \ln(\frac{lcm(i, j)}{\gcd(i, k)}) \\
= & \sum_{p = 1}^{t} \sum_{d | p} \sum_{i = 1}^{\frac{A}{p}} \sum_{j = 1}^{\frac{B}{p}} \sum_{k = 1}^{\frac{C}{p}} d \mu(\frac{p}{d}) \ln(\frac{lcm(i, j)}{\gcd(i, k)}) \\
= & \sum_{p = 1}^{t} \sum_{d | p} d \mu(\frac{p}{d}) \sum_{i = 1}^{\frac{A}{p}} \sum_{j = 1}^{\frac{B}{p}} \sum_{k = 1}^{\frac{C}{p}} \ln(\frac{lcm(i, j)}{\gcd(i, k)}) \\
= & \sum_{p = 1}^{t} \varphi(p) \sum_{i = 1}^{\frac{A}{p}} \sum_{j = 1}^{\frac{B}{p}} \sum_{k = 1}^{\frac{C}{p}} \ln(\frac{lcm(i, j)}{\gcd(i, k)}) \\
\end{aligned}
$$
然后 $exp$
$$
\prod_{p = 1}^t (\prod_{i = 1}^{\frac{A}{p}} \prod_{j = 1}^{\frac{B}{p}} \prod_{k = 1}^{\frac{C}{p}} \frac{lcm(i, j)}{\gcd(i, k)})^{\varphi(p)}
$$
发现里面的东西就是 part 1,于是预处理出 $\varphi$ 的前缀和,整除分块是 $O(\sqrt{n} \log P)$ 的,里面又是个 $O(\sqrt{n} \log P)$ ,总复杂度 $O(n \log^2 P)$
代码
注意指数要模的是 $P - 1$ ,筛 $\mu$ 的时候 $-1$ 不要存为 $P - 1$ ,因为 $\mu$ 有可能在指数上,还要预处理的 $\varphi$ 也是模 $P - 1$
1 |
|