LibreOJ 6693.「MtOI2019」幽灵乐团

这题其实是个六合一(
不仅代码写得精神污染,这篇题解写得更加精神污染……
以下分析复杂度时设 A,B,CA,B,C 同阶,并以 nn 代替。

以及 CYJ 题解中对于 type=2\mathrm{type}=2 的枚举 gcd(i,j)\gcd(i,j) 的推导好像有点问题,不过也许是我太菜了(
所以这里直接枚举 gcd(i,j,k)\gcd(i,j,k)

第一步:

所以?
所以分别算

即可。
于是这就成了个六合一(

type = 0

i=1Aj=1Bk=1Ci=(A!)BC\prod\limits_{i=1}^A\prod\limits_{j=1}^B\prod\limits_{k=1}^C i = (A!)^{BC}

O(nlogn)O(n \log n) 预处理 dTdμ(Td)\prod\limits_{d|T} d^{\mu\left(\frac Td\right)} 的前缀积和前缀积的逆,然后单次询问 O(nlogn)O(\sqrt n \log n)

type = 1

i=1Aj=1Bk=1Ciijk=(i=1Aii)BC(B+1)(C+1)/4\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}

O(nlogn)O(n \log n) 预处理 (dTdμ(Td))T2\left(\prod\limits_{d|T} d^{\mu\left(\frac Td\right)}\right)^{T^2} 的前缀积和前缀积的逆,然后单词询问 O(nlogn)O(\sqrt n \log n)

type = 2

预处理一下 Tφ(T)T^{\varphi(T)} 的前缀积即可。

把底数拆出来,看 tdtd 的部分:

是否有一种似曾相识的感觉?
看看上面那个 igcd(i,j,k)i^{\gcd(i,j,k)} 的式子,发现可以约分掉这部分。
于是乎那个式子只要求

而这个式子要求

然后两只数论分块加上 O(nlogn)O(n \log n) 预处理出前缀积和前缀积的逆就可以单次询问 O(n3/4logn)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
#pragma GCC optimize (2)
#include <cstdio>
#include <algorithm>
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;
}
}