这题其实是个六合一(
不仅代码写得精神污染,这篇题解写得更加精神污染……
以下分析复杂度时设 \(A,B,C\) 同阶,并以 \(n\) 代替。
以及 CYJ 题解中对于 \(\mathrm{type}=2\) 的枚举 \(\gcd(i,j)\) 的推导好像有点问题,不过也许是我太菜了(
所以这里直接枚举 \(\gcd(i,j,k)\)。
第一步: \[ \newcommand{\lcm}{\operatorname{lcm}} \newcommand{\type}{\text{type}} \newcommand{\ffrac}[2]{\left\lfloor\frac{ #1 }{ #2 }\right\rfloor} \prod_{i=1}^{A}\prod_{j=1}^{B}\prod_{k=1}^{C}\left(\frac{\lcm(i,j)}{\gcd(i,k)}\right)^{f(\type)} = \prod_{i=1}^{A}\prod_{j=1}^{B}\prod_{k=1}^{C}\left(\frac{ij}{\gcd(i,j)\gcd(i,k)}\right)^{f(\type)} \]
所以?
所以分别算 \[\prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C i^{f(\type)},\prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C {\gcd(i,j)}^{f(\type)}\] 即可。
于是这就成了个六合一(
type = 0
\[\prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C i = (A!)^{BC}\]
\[ \begin{align*} \prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C \gcd(i,j) &= \prod\limits_{i=1}^A\prod\limits_{j=1}^B \text{gcd}^C(i,j) \\ &= \prod\limits_{d=1}^{\min(A,B)}\prod\limits_{i=1}^{\ffrac Ad} \prod\limits_{j=1}^{\ffrac Bd} d^{[\gcd(i,j)=1]C} \\ &= \prod\limits_{d=1}^{\min(A,B)} d^{C\sum\limits_{i=1}^{\ffrac Ad}\sum\limits_{j=1}^{\ffrac Bd}[\gcd(i,j)=1]} \\ &= \prod\limits_{d=1}^{\min(A,B)} d^{C\sum\limits_{k=1}^{\min\left(\ffrac Ad,\ffrac Bd\right)}\mu(k)\ffrac A{dk}\ffrac B{dk}} \\ &= \prod\limits_{T=1}^{\min(A,B)} \prod\limits_{d|T} d^{\mu\left(\frac Td\right)\ffrac AT\ffrac BTC} \\ &= \prod\limits_{T=1}^{\min(A,B)} \left(\prod\limits_{d|T} d^{\mu\left(\frac Td\right)}\right)^{\ffrac AT\ffrac BTC} \end{align*} \]
\(O(n \log n)\) 预处理 \(\prod\limits_{d|T} d^{\mu\left(\frac Td\right)}\) 的前缀积和前缀积的逆,然后单次询问 \(O(\sqrt n \log n)\)。
type = 1
\[\prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C i^{ijk} = \left(\prod\limits_{i=1}^A i^i\right)^{BC(B+1)(C+1)/4}\]
\[ \begin{align*} \prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C \text{gcd}^{ijk}(i,j) &= \left(\prod\limits_{i=1}^A\prod\limits_{j=1}^B \text{gcd}^{ij}(i,j)\right)^{C(C+1)/2} \\ &= \left(\prod\limits_{d=1}^{\min(A,B)}\prod\limits_{i=1}^{\ffrac Ad}\prod\limits_{j=1}^{\ffrac Bd} {d}^{[\gcd(i,j)=1]d^2ij}\right)^{C(C+1)/2} \\ &= \left(\prod\limits_{d=1}^{\min(A,B)} d^{d^2\sum\limits_{i=1}^{\ffrac Ad} \sum\limits_{j=1}^{\ffrac Bd} [\gcd(i,j)=1]ij}\right)^{C(C+1)/2} \\ &= \left(\prod\limits_{d=1}^{\min(A,B)} d^{d^2\sum\limits_{k=1}^{\min\left(\ffrac Ad,\ffrac Bd\right)}\mu(k)k^2\ffrac A{dk}\ffrac B{dk}\left(\ffrac A{dk}+1\right)\left(\ffrac B{dk}+1\right)/4}\right)^{C(C+1)/2} \\ &= \left(\prod\limits_{T=1}^{\min(A,B)} \left(\prod\limits_{d|T} d^{\mu\left(\frac Td\right)}\right)^{T^2\ffrac AT\ffrac BT\left(\ffrac AT + 1\right)\left(\ffrac BT + 1\right) / 4}\right)^{C(C+1)/2} \end{align*} \]
\(O(n \log n)\) 预处理 \(\left(\prod\limits_{d|T} d^{\mu\left(\frac Td\right)}\right)^{T^2}\) 的前缀积和前缀积的逆,然后单词询问 \(O(\sqrt n \log n)\)。
type = 2
\[ \begin{align*} \prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C i^{\gcd(i,j,k)} &= \prod\limits_{d=1}^{\min(A,B,C)}\prod\limits_{i=1}^{\ffrac Ad} (id)^{d\sum\limits_{j=1}^{\ffrac Bd}\sum\limits_{k=1}^{\ffrac Bd}[\gcd(i,j,k)=1]} \\ &= \prod\limits_{d=1}^{\min(A,B,C)}\prod\limits_{t=1}^{\ffrac Ad}\prod\limits_{i=1}^{\ffrac A{td}} (itd)^{\mu(t)d\ffrac B{td}\ffrac C{td}} \\ &= \prod\limits_{T=1}^{\min(A,B,C)} \prod\limits_{d|T} \left(\ffrac AT!T^{\ffrac AT}\right)^{\mu\left(\frac Td\right)d \ffrac BT\ffrac CT} \\ &= \prod\limits_{T=1}^{\min(A,B,C)} \left(\ffrac AT!T^{\ffrac AT}\right)^{\varphi(T) \ffrac BT\ffrac CT} \end{align*} \]
预处理一下 \(T^{\varphi(T)}\) 的前缀积即可。
\[ \begin{align*} \prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C {\gcd(i,j)}^{\gcd(i,j,k)} &= \prod\limits_{d=1}^{\min(A,B,C)} \prod\limits_{i=1}^{\ffrac Ad} \prod\limits_{j=1}^{\ffrac Bd} (d\gcd(i,j))^{d\sum\limits_{k=1}^{\ffrac Cd}[\gcd(i,j,k)=1]} \\ &= \prod\limits_{d=1}^{\min(A,B,C)} \prod\limits_{t=1}^{\min\left(\ffrac Ad,\ffrac Bd,\ffrac Cd\right)} \prod\limits_{i=1}^{\ffrac A{td}} \prod\limits_{j=1}^{\ffrac B{td}}(td\gcd(i,j))^{\mu(t)d\ffrac C{td}} \end{align*} \]
把底数拆出来,看 \(td\) 的部分:
\[ \begin{align*} & \prod\limits_{d=1}^{\min(A,B,C)} \prod\limits_{t=1}^{\min\left(\ffrac Ad,\ffrac Bd,\ffrac Cd\right)} \prod\limits_{i=1}^{\ffrac A{td}} \prod\limits_{j=1}^{\ffrac B{td}}(td)^{\mu(t)d\ffrac C{td}} \\ =& \prod\limits_{d=1}^{\min(A,B,C)} \prod\limits_{t=1}^{\min\left(\ffrac Ad,\ffrac Bd,\ffrac Cd\right)}(td)^{\mu(t)d\ffrac A{td}\ffrac B{td}\ffrac C{td}} \\ =& \prod\limits_{T=1}^{\min(A,B,C)} T^{\ffrac AT\ffrac BT\ffrac CT\varphi(T)} \end{align*} \]
是否有一种似曾相识的感觉?
看看上面那个 \(i^{\gcd(i,j,k)}\) 的式子,发现可以约分掉这部分。
于是乎那个式子只要求 \[
\prod\limits_{T=1}^{\min(A,B,C)} \left(\ffrac AT!\right)^{\varphi(T) \ffrac BT\ffrac CT}
\]
而这个式子要求 \[ \begin{align*} & \prod\limits_{d=1}^{\min(A,B,C)} \prod\limits_{t=1}^{\min\left(\ffrac Ad,\ffrac Bd,\ffrac Cd\right)} \prod\limits_{i=1}^{\ffrac A{td}} \prod\limits_{j=1}^{\ffrac B{td}}\text{gcd}^{\mu(t)d\ffrac C{td}}(i,j) \\ =& \prod\limits_{d=1}^{\min(A,B,C)} \prod\limits_{t=1}^{\min\left(\ffrac Ad,\ffrac Bd,\ffrac Cd\right)} \prod\limits_{k=1}^{\min\left(\ffrac A{td},\ffrac B{td},\ffrac C{td}\right)} \prod\limits_{l=1}^{\min\left(\ffrac A{tdk},\ffrac B{tdk},\ffrac C{tdk}\right)} (kl)^{\mu(t)\mu(l)d\ffrac C{td}\ffrac A{tdkl}\ffrac B{tdkl}} \\ =& \prod\limits_{T=1}^{\min(A,B,C)} \prod\limits_{K=1}^{\min\left(\ffrac AT,\ffrac BT,\ffrac CT\right)}\prod\limits_{d|K} d^{\mu\left(\frac Kd\right)\ffrac A{TK}\ffrac B{TK}\varphi(T)\ffrac CT} \end{align*} \]
然后两只数论分块加上 \(O(n \log n)\) 预处理出前缀积和前缀积的逆就可以单次询问 \(O(n^{3/4} \log n)\) 了。
(在洛谷要开 O2 才能过,不过常数确实还有优化空间)
代码: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using namespace std;
const int N = 1e5;
int T,mod;
int fac[N + 5],ifac[N + 5],inv[N + 5];
int es[N + 5],s[N + 5];
int vis[N + 5],cnt,prime[N + 5];
int mu[N + 5],emu[N + 5],pemu[N + 5],iemu[N + 5],esmu[N + 5],pesmu[N + 5],iesmu[N + 5];
int phi[N + 5],sphi[N + 5],ephi[N + 5];
int A,B,C;
int ans1 = 1,ans2 = 1;
inline int fpow(int a,int b)
{
int ret = 1;
for(;b;b >>= 1)
(b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
return ret;
}
inline int f(int A,int B)
{
int ret = 1;
for(register int l = 1,r;l <= min(A,B);l = r + 1)
{
r = min(A / (A / l),B / (B / l));
ret = (long long)ret * fpow((long long)pemu[r] * iemu[l - 1] % mod,(long long)(A / l) * (B / l) % (mod - 1)) % mod;
}
return ret;
}
int main()
{
scanf("%d%d",&T,&mod),fac[0] = inv[0] = mu[1] = phi[1] = pemu[0] = es[0] = pesmu[0] = 1;
for(register int i = 2;i <= N;++i)
{
if(!vis[i])
mu[prime[++cnt] = i] = mod - 2,phi[i] = i - 1;
for(register int j = 1;j <= cnt && i * prime[j] <= N;++j)
{
vis[i * prime[j]] = 1;
if(!(i % prime[j]))
{
phi[i * prime[j]] = (long long)phi[i] * prime[j] % mod;
break;
}
mu[i * prime[j]] = (mod - 1 - mu[i]) % (mod - 1),phi[i * prime[j]] = (long long)phi[i] * (prime[j] - 1) % mod;
}
}
for(register int i = 0;i <= N;++i)
emu[i] = 1,s[i] = (long long)i * (i + 1) / 2 % (mod - 1);
for(register int i = 1;i <= N;++i)
for(register int j = 1;i * j <= N;++j)
emu[i * j] = (long long)emu[i * j] * fpow(i,mu[j]) % mod;
for(register int i = 1;i <= N;++i)
esmu[i] = fpow(emu[i],(long long)i * i % (mod - 1)),
sphi[i] = (sphi[i - 1] + phi[i]) % (mod - 1);
for(register int i = 1;i <= N;++i)
pemu[i] = (long long)pemu[i - 1] * emu[i] % mod,
fac[i] = (long long)fac[i - 1] * i % mod,
es[i] = (long long)es[i - 1] * fpow(i,i) % mod,
pesmu[i] = (long long)pesmu[i - 1] * esmu[i] % mod;
iemu[N] = fpow(pemu[N],mod - 2),
ifac[N] = fpow(fac[N],mod - 2),
iesmu[N] = fpow(pesmu[N],mod - 2);
for(register int i = N;i;--i)
iemu[i - 1] = (long long)iemu[i] * emu[i] % mod,
ifac[i - 1] = (long long)ifac[i] * i % mod,
inv[i] = (long long)ifac[i] * fac[i - 1] % mod,
iesmu[i - 1] = (long long)iesmu[i] * esmu[i] % mod;
for(;T;--T)
{
scanf("%d%d%d",&A,&B,&C);
ans1 = (long long)fpow(fac[A],(long long)B * C % (mod - 1)) * fpow(fac[B],(long long)A * C % (mod - 1)) % mod;
for(register int l = 1,r;l <= min(A,B);l = r + 1)
{
r = min(A / (A / l),B / (B / l));
ans2 = (long long)ans2 * fpow((long long)pemu[r] * iemu[l - 1] % mod,(long long)(A / l) * (B / l) % (mod - 1) * C % (mod - 1)) % mod;
}
for(register int l = 1,r;l <= min(A,C);l = r + 1)
{
r = min(A / (A / l),C / (C / l));
ans2 = (long long)ans2 * fpow((long long)pemu[r] * iemu[l - 1] % mod,(long long)(A / l) * (C / l) % (mod - 1) * B % (mod - 1)) % mod;
}
printf("%d ",(int)((long long)ans1 * fpow(ans2,mod - 2) % mod)),ans1 = ans2 = 1;
ans1 = (long long)fpow(es[A],(long long)s[B] * s[C] % (mod - 1)) * fpow(es[B],(long long)s[A] * s[C] % (mod - 1)) % mod;
for(register int l = 1,r;l <= min(A,B);l = r + 1)
{
r = min(A / (A / l),B / (B / l));
ans2 = (long long)ans2 * fpow((long long)pesmu[r] * iesmu[l - 1] % mod,(long long)s[A / l] * s[B / l] % (mod - 1) * s[C] % (mod - 1)) % mod;
}
for(register int l = 1,r;l <= min(A,C);l = r + 1)
{
r = min(A / (A / l),C / (C / l));
ans2 = (long long)ans2 * fpow((long long)pesmu[r] * iesmu[l - 1] % mod,(long long)s[A / l] * s[C / l] % (mod - 1) * s[B] % (mod - 1)) % mod;
}
printf("%d ",(int)((long long)ans1 * fpow(ans2,mod - 2) % mod)),ans1 = ans2 = 1;
for(register int l = 1,r;l <= min(A,min(B,C));l = r + 1)
{
r = min(A / (A / l),min(B / (B / l),C / (C / l)));
ans1 = (long long)ans1 * fpow(fac[A / l],(long long)(sphi[r] - sphi[l - 1] + mod - 1) * (B / l) % (mod - 1) * (C / l) % (mod - 1)) % mod;
}
for(register int l = 1,r;l <= min(A,min(B,C));l = r + 1)
{
r = min(A / (A / l),min(B / (B / l),C / (C / l)));
ans1 = (long long)ans1 * fpow(fac[B / l],(long long)(sphi[r] - sphi[l - 1] + mod - 1) * (A / l) % (mod - 1) * (C / l) % (mod - 1)) % mod;
}
for(register int l = 1,r;l <= min(A,min(B,C));l = r + 1)
{
r = min(A / (A / l),min(B / (B / l),C / (C / l)));
ans2 = (long long)ans2 * fpow(f(A / l,B / l),(long long)(sphi[r] - sphi[l - 1] + mod - 1) * (C / l) % (mod - 1)) % mod;
}
for(register int l = 1,r;l <= min(A,min(B,C));l = r + 1)
{
r = min(A / (A / l),min(B / (B / l),C / (C / l)));
ans2 = (long long)ans2 * fpow(f(A / l,C / l),(long long)(sphi[r] - sphi[l - 1] + mod - 1) * (B / l) % (mod - 1)) % mod;
}
printf("%d\n",(int)((long long)ans1 * fpow(ans2,mod - 2) % mod)),ans1 = ans2 = 1;
}
}