[MtOI2019]幽灵乐团 / 莫比乌斯反演基础练习题
题目链接:luogu P5518
题目大意
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
(
lcm
(
i
,
j
)
gcd
(
i
,
k
)
)
f
(
t
y
p
e
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}(\dfrac{\text{lcm}(i,j)}{\gcd(i,k)})^{f(type)}
i=1∏Aj=1∏Bk=1∏C(gcd(i,k)lcm(i,j))f(type)
多组数据,要你在
f
(
t
y
p
e
)
f(type)
f(type) 分别是
1
,
i
×
j
×
k
,
gcd
(
i
,
j
,
k
)
1,i\times j\times k,\gcd(i,j,k)
1,i×j×k,gcd(i,j,k) 的时候求式子的值。
思路
主要的思想是对于每个部分你就求两个东西:
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
i
f
(
t
y
p
e
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}i^{f(type)}
i=1∏Aj=1∏Bk=1∏Cif(type)
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
gcd
(
i
,
j
)
f
(
t
y
p
e
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\gcd(i,j)^{f(type)}
i=1∏Aj=1∏Bk=1∏Cgcd(i,j)f(type)
不多说了直接看我推导过程吧!
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
(
lcm
(
i
,
j
)
gcd
(
i
,
k
)
)
f
(
t
y
p
e
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}(\dfrac{\text{lcm}(i,j)}{\gcd(i,k)})^{f(type)}
i=1∏Aj=1∏Bk=1∏C(gcd(i,k)lcm(i,j))f(type)
求
∏
i
=
1
n
∏
j
=
1
m
gcd
(
i
,
j
)
\prod\limits_{i=1}^n\prod\limits_{j=1}^m\gcd(i,j)
i=1∏nj=1∏mgcd(i,j)
∏
d
d
∑
i
=
1
n
∑
j
=
1
m
[
gcd
(
i
,
j
)
=
=
d
]
\prod\limits_{d}d^{\sum\limits_{i=1}^n\sum\limits_{j=1}^m[\gcd(i,j)==d]}
d∏di=1∑nj=1∑m[gcd(i,j)==d]
O ( n log n ) O(n\log n) O(nlogn)
t
y
p
e
=
1
type=1
type=1
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
lcm
(
i
,
j
)
gcd
(
i
,
k
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\dfrac{\text{lcm}(i,j)}{\gcd(i,k)}
i=1∏Aj=1∏Bk=1∏Cgcd(i,k)lcm(i,j)
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
1
gcd
(
i
,
k
)
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
lcm
(
i
,
j
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)}\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\text{lcm}(i,j)
i=1∏Aj=1∏Bk=1∏Cgcd(i,k)1i=1∏Aj=1∏Bk=1∏Clcm(i,j)
(
∏
i
=
1
A
∏
k
=
1
C
1
gcd
(
i
,
k
)
)
B
(
∏
i
=
1
A
∏
j
=
1
B
lcm
(
i
,
j
)
)
C
(\prod\limits_{i=1}^{A}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)})^B(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\text{lcm}(i,j))^C
(i=1∏Ak=1∏Cgcd(i,k)1)B(i=1∏Aj=1∏Blcm(i,j))C
(
∏
i
=
1
A
∏
k
=
1
C
1
gcd
(
i
,
k
)
)
B
(
∏
i
=
1
A
∏
j
=
1
B
i
j
gcd
(
i
,
j
)
)
C
(\prod\limits_{i=1}^{A}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)})^B(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\dfrac{ij}{\gcd(i,j)})^C
(i=1∏Ak=1∏Cgcd(i,k)1)B(i=1∏Aj=1∏Bgcd(i,j)ij)C
(
∏
i
=
1
A
∏
k
=
1
C
1
gcd
(
i
,
k
)
)
B
(
∏
i
=
1
A
∏
j
=
1
B
1
gcd
(
i
,
j
)
)
C
(
∏
i
=
1
A
∏
j
=
1
B
i
j
)
C
(\prod\limits_{i=1}^{A}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)})^B(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\dfrac{1}{\gcd(i,j)})^C(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}ij)^C
(i=1∏Ak=1∏Cgcd(i,k)1)B(i=1∏Aj=1∏Bgcd(i,j)1)C(i=1∏Aj=1∏Bij)C
(
∏
i
=
1
A
∏
k
=
1
C
1
gcd
(
i
,
k
)
)
B
(
∏
i
=
1
A
∏
j
=
1
B
1
gcd
(
i
,
j
)
)
C
(
(
A
!
)
B
(
B
!
)
A
)
C
(\prod\limits_{i=1}^{A}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)})^B(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\dfrac{1}{\gcd(i,j)})^C((A!)^B(B!)^A )^C
(i=1∏Ak=1∏Cgcd(i,k)1)B(i=1∏Aj=1∏Bgcd(i,j)1)C((A!)B(B!)A)C
∏
i
=
1
A
∏
j
=
1
B
gcd
(
i
,
j
)
i
j
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\gcd(i,j)^{ij}
i=1∏Aj=1∏Bgcd(i,j)ij
∏
d
d
∑
i
=
1
A
∑
j
=
1
B
[
gcd
(
i
,
j
)
=
=
d
]
(
i
j
)
\prod\limits_{d}d^{\sum\limits_{i=1}^{A}\sum\limits_{j=1}^{B}[\gcd(i,j)==d](ij)}
d∏di=1∑Aj=1∑B[gcd(i,j)==d](ij)
∏
d
d
∑
i
=
1
⌊
A
d
⌋
∑
j
=
1
⌊
B
d
⌋
[
gcd
(
i
,
j
)
=
=
1
]
(
i
d
j
d
)
\prod\limits_{d}d^{\sum\limits_{i=1}^{\left\lfloor\frac{A}{d}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{B}{d}\right\rfloor}[\gcd(i,j)==1](idjd)}
d∏di=1∑⌊dA⌋j=1∑⌊dB⌋[gcd(i,j)==1](idjd)
∏
d
d
d
2
d
′
2
∑
d
′
μ
(
d
′
)
∑
i
=
1
⌊
A
d
d
′
⌋
∑
j
=
1
⌊
B
d
d
′
⌋
(
i
j
)
\prod\limits_{d}d^{d^2d'^2\sum\limits_{d'}\mu(d')\sum\limits_{i=1}^{\left\lfloor\frac{A}{dd'}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{B}{dd'}\right\rfloor}(ij)}
d∏dd2d′2d′∑μ(d′)i=1∑⌊dd′A⌋j=1∑⌊dd′B⌋(ij)
∏
d
d
d
2
d
′
2
∑
d
′
μ
(
d
′
)
∑
i
=
1
⌊
A
d
d
′
⌋
i
∑
j
=
1
⌊
B
d
d
′
⌋
j
\prod\limits_{d}d^{d^2d'^2\sum\limits_{d'}\mu(d')\sum\limits_{i=1}^{\left\lfloor\frac{A}{dd'}\right\rfloor}i\sum\limits_{j=1}^{\left\lfloor\frac{B}{dd'}\right\rfloor}j}
d∏dd2d′2d′∑μ(d′)i=1∑⌊dd′A⌋ij=1∑⌊dd′B⌋j
∏
D
(
∏
d
∣
D
d
μ
(
⌊
D
d
⌋
)
)
D
2
∑
i
=
1
⌊
A
D
⌋
i
∑
j
=
1
⌊
B
D
⌋
j
\prod\limits_{D}(\prod\limits_{d|D}d^{\mu(\left\lfloor\frac{D}{d}\right\rfloor)})^{D^2\sum\limits_{i=1}^{\left\lfloor\frac{A}{D}\right\rfloor}i\sum\limits_{j=1}^{\left\lfloor\frac{B}{D}\right\rfloor}j}
D∏(d∣D∏dμ(⌊dD⌋))D2i=1∑⌊DA⌋ij=1∑⌊DB⌋j
<—>
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
gcd
(
i
,
j
)
i
j
k
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\gcd(i,j)^{ijk}
i=1∏Aj=1∏Bk=1∏Cgcd(i,j)ijk
∏
k
=
1
C
(
∏
i
=
1
A
∏
j
=
1
B
gcd
(
i
,
j
)
i
j
)
k
\prod\limits_{k=1}^{C}(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\gcd(i,j)^{ij})^k
k=1∏C(i=1∏Aj=1∏Bgcd(i,j)ij)k
(
∏
i
=
1
A
∏
j
=
1
B
gcd
(
i
,
j
)
i
j
)
∑
k
=
1
C
k
(\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\gcd(i,j)^{ij})^{\sum\limits_{k=1}^{C}k}
(i=1∏Aj=1∏Bgcd(i,j)ij)k=1∑Ck
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
i
i
j
k
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}i^{ijk}
i=1∏Aj=1∏Bk=1∏Ciijk
∏
i
=
1
A
i
i
∑
j
=
1
B
∑
k
=
1
C
j
k
\prod\limits_{i=1}^{A}i^{i\sum\limits_{j=1}^B\sum\limits_{k=1}^Cjk}
i=1∏Aiij=1∑Bk=1∑Cjk
∏
i
=
1
A
i
i
(
∑
j
=
1
B
j
)
(
∑
k
=
1
C
k
)
\prod\limits_{i=1}^{A}i^{i(\sum\limits_{j=1}^Bj)(\sum\limits_{k=1}^Ck)}
i=1∏Aii(j=1∑Bj)(k=1∑Ck)
t
y
p
e
=
1
type=1
type=1
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
(
lcm
(
i
,
j
)
gcd
(
i
,
k
)
)
i
j
k
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}(\dfrac{\text{lcm}(i,j)}{\gcd(i,k)})^{ijk}
i=1∏Aj=1∏Bk=1∏C(gcd(i,k)lcm(i,j))ijk
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
1
gcd
(
i
,
k
)
i
j
k
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
lcm
(
i
,
j
)
i
j
k
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)^{ijk}}\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\text{lcm}(i,j)^{ijk}
i=1∏Aj=1∏Bk=1∏Cgcd(i,k)ijk1i=1∏Aj=1∏Bk=1∏Clcm(i,j)ijk
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
1
gcd
(
i
,
k
)
i
j
k
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
(
i
j
gcd
(
i
,
j
)
)
i
j
k
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\dfrac{1}{\gcd(i,k)^{ijk}}\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}(\dfrac{ij}{\gcd(i,j)})^{ijk}
i=1∏Aj=1∏Bk=1∏Cgcd(i,k)ijk1i=1∏Aj=1∏Bk=1∏C(gcd(i,j)ij)ijk
t y p e = 2 type=2 type=2
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
i
gcd
(
i
,
j
,
k
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}i^{\gcd(i,j,k)}
i=1∏Aj=1∏Bk=1∏Cigcd(i,j,k)
∏
d
∏
i
=
1
A
i
d
∑
j
=
1
B
∑
k
=
1
C
[
gcd
(
i
,
j
,
k
)
=
=
d
]
\prod\limits_{d}\prod\limits_{i=1}^{A}i^{d\sum\limits_{j=1}^{B}\sum\limits_{k=1}^{C}[\gcd(i,j,k)==d]}
d∏i=1∏Aidj=1∑Bk=1∑C[gcd(i,j,k)==d]
∏
d
∏
i
=
1
⌊
A
d
⌋
(
i
d
)
d
∑
j
=
1
⌊
B
d
⌋
∑
k
=
1
⌊
C
d
⌋
[
gcd
(
i
,
j
,
k
)
=
=
1
]
\prod\limits_{d}\prod\limits_{i=1}^{\left\lfloor \frac{A}{d}\right\rfloor}(id)^{d\sum\limits_{j=1}^{\left\lfloor \frac{B}{d}\right\rfloor}\sum\limits_{k=1}^{\left\lfloor \frac{C}{d}\right\rfloor}[\gcd(i,j,k)==1]}
d∏i=1∏⌊dA⌋(id)dj=1∑⌊dB⌋k=1∑⌊dC⌋[gcd(i,j,k)==1]
∏
d
∏
i
=
1
⌊
A
d
⌋
(
i
d
)
d
∑
j
=
1
⌊
B
d
⌋
∑
k
=
1
⌊
C
d
⌋
[
gcd
(
i
,
j
,
k
)
=
=
1
]
\prod\limits_{d}\prod\limits_{i=1}^{\left\lfloor \frac{A}{d}\right\rfloor}(id)^{d\sum\limits_{j=1}^{\left\lfloor \frac{B}{d}\right\rfloor}\sum\limits_{k=1}^{\left\lfloor \frac{C}{d}\right\rfloor}[\gcd(i,j,k)==1]}
d∏i=1∏⌊dA⌋(id)dj=1∑⌊dB⌋k=1∑⌊dC⌋[gcd(i,j,k)==1]
∏
d
∏
d
′
∏
i
=
1
⌊
A
d
d
′
⌋
(
i
d
d
′
)
d
μ
(
d
′
)
⌊
B
d
d
′
⌋
⌊
C
d
d
′
⌋
\prod\limits_{d}\prod\limits_{d'}\prod\limits_{i=1}^{\left\lfloor \frac{A}{dd'}\right\rfloor}(idd')^{d\mu(d')\left\lfloor \frac{B}{dd'}\right\rfloor\left\lfloor \frac{C}{dd'}\right\rfloor}
d∏d′∏i=1∏⌊dd′A⌋(idd′)dμ(d′)⌊dd′B⌋⌊dd′C⌋
∏
D
(
∏
d
∣
D
∏
i
=
1
⌊
A
D
⌋
(
i
D
)
d
μ
(
⌊
D
d
⌋
)
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}(\prod\limits_{d|D}\prod\limits_{i=1}^{\left\lfloor \frac{A}{D}\right\rfloor}(iD)^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)})^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏(d∣D∏i=1∏⌊DA⌋(iD)dμ(⌊dD⌋))⌊DB⌋⌊DC⌋
∏
D
(
∏
d
∣
D
(
∏
i
=
1
⌊
A
D
⌋
(
i
)
d
μ
(
⌊
D
d
⌋
)
)
(
D
d
μ
(
⌊
D
d
⌋
)
⌊
A
D
⌋
)
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}(\prod\limits_{d|D}(\prod\limits_{i=1}^{\left\lfloor \frac{A}{D}\right\rfloor}(i)^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)})(D^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)\left\lfloor\frac{A}{D}\right\rfloor}))^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏(d∣D∏(i=1∏⌊DA⌋(i)dμ(⌊dD⌋))(Ddμ(⌊dD⌋)⌊DA⌋))⌊DB⌋⌊DC⌋
∏
D
(
∏
d
∣
D
(
⌊
A
D
⌋
)
!
d
μ
(
⌊
D
d
⌋
)
D
d
μ
(
⌊
D
d
⌋
)
⌊
A
D
⌋
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{d|D}(\left\lfloor \frac{A}{D}\right\rfloor)!^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)}D^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)\left\lfloor\frac{A}{D}\right\rfloor}\right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏(d∣D∏(⌊DA⌋)!dμ(⌊dD⌋)Ddμ(⌊dD⌋)⌊DA⌋)⌊DB⌋⌊DC⌋
∏
D
(
∏
d
∣
D
(
(
⌊
A
D
⌋
)
!
D
⌊
A
D
⌋
)
d
μ
(
⌊
D
d
⌋
)
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{d|D}\left( (\left\lfloor \frac{A}{D}\right\rfloor)!D^{\left\lfloor\frac{A}{D}\right\rfloor}\right)^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)} \right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏(d∣D∏((⌊DA⌋)!D⌊DA⌋)dμ(⌊dD⌋))⌊DB⌋⌊DC⌋
∏
D
(
(
(
⌊
A
D
⌋
)
!
D
⌊
A
D
⌋
)
∑
d
∣
D
d
μ
(
⌊
D
d
⌋
)
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}\left(\left( (\left\lfloor \frac{A}{D}\right\rfloor)!D^{\left\lfloor\frac{A}{D}\right\rfloor}\right)^{\sum\limits_{d|D}d\mu(\left\lfloor\frac{D}{d}\right\rfloor)} \right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏(((⌊DA⌋)!D⌊DA⌋)d∣D∑dμ(⌊dD⌋))⌊DB⌋⌊DC⌋
∏
D
(
(
(
⌊
A
D
⌋
)
!
D
⌊
A
D
⌋
)
φ
(
D
)
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}\left(\left( (\left\lfloor \frac{A}{D}\right\rfloor)!D^{\left\lfloor\frac{A}{D}\right\rfloor}\right)^{\varphi(D)} \right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏(((⌊DA⌋)!D⌊DA⌋)φ(D))⌊DB⌋⌊DC⌋
∏
D
(
(
⌊
A
D
⌋
)
!
φ
(
D
)
(
D
φ
(
D
)
)
⌊
A
D
⌋
)
⌊
B
D
⌋
⌊
C
D
⌋
\prod\limits_{D}\left((\left\lfloor \frac{A}{D}\right\rfloor)!^{\varphi(D)}\left(D^{\varphi(D)}\right)^{\left\lfloor\frac{A}{D}\right\rfloor} \right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
D∏((⌊DA⌋)!φ(D)(Dφ(D))⌊DA⌋)⌊DB⌋⌊DC⌋
(
∏
D
(
⌊
A
D
⌋
)
!
φ
(
D
)
)
⌊
B
D
⌋
⌊
C
D
⌋
(
∏
D
(
D
φ
(
D
)
)
)
⌊
A
D
⌋
⌊
B
D
⌋
⌊
C
D
⌋
\left(\prod\limits_{D}(\left\lfloor \frac{A}{D}\right\rfloor)!^{\varphi(D)}\right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}\left(\prod\limits_{D}\left(D^{\varphi(D)}\right) \right)^{\left\lfloor\frac{A}{D}\right\rfloor\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
(D∏(⌊DA⌋)!φ(D))⌊DB⌋⌊DC⌋(D∏(Dφ(D)))⌊DA⌋⌊DB⌋⌊DC⌋
这里右边可以预处理来做。(不过后面我们可以消掉这个部分)
t
y
p
e
=
2
type=2
type=2
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
gcd
(
i
,
j
)
gcd
(
i
,
j
,
k
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\gcd(i,j)^{\gcd(i,j,k)}
i=1∏Aj=1∏Bk=1∏Cgcd(i,j)gcd(i,j,k)
∏
d
d
(
∑
k
=
1
C
gcd
(
d
,
k
)
)
(
∑
i
=
1
A
∑
j
=
1
B
[
gcd
(
i
,
j
)
=
=
d
]
)
\prod\limits_{d}d^{(\sum\limits_{k=1}^{C}\gcd(d,k))(\sum\limits_{i=1}^{A}\sum\limits_{j=1}^{B}[\gcd(i,j)==d])}
d∏d(k=1∑Cgcd(d,k))(i=1∑Aj=1∑B[gcd(i,j)==d])
∏
d
d
(
∑
k
=
1
C
gcd
(
d
,
k
)
)
(
∑
i
=
1
⌊
A
d
⌋
∑
j
=
1
⌊
B
d
⌋
[
gcd
(
i
,
j
)
=
=
1
]
)
\prod\limits_{d}d^{(\sum\limits_{k=1}^{C}\gcd(d,k))(\sum\limits_{i=1}^{\left\lfloor \frac{A}{d}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor \frac{B}{d}\right\rfloor}[\gcd(i,j)==1])}
d∏d(k=1∑Cgcd(d,k))(i=1∑⌊dA⌋j=1∑⌊dB⌋[gcd(i,j)==1])
∏
d
d
(
∑
k
=
1
C
gcd
(
d
,
k
)
)
(
∑
d
′
μ
(
d
′
)
⌊
A
d
d
′
⌋
⌊
B
d
d
′
⌋
)
\prod\limits_{d}d^{(\sum\limits_{k=1}^{C}\gcd(d,k))(\sum\limits_{d'}\mu(d')\left\lfloor \frac{A}{dd'}\right\rfloor\left\lfloor \frac{B}{dd'}\right\rfloor)}
d∏d(k=1∑Cgcd(d,k))(d′∑μ(d′)⌊dd′A⌋⌊dd′B⌋)
∏
D
(
∏
d
∣
D
d
μ
(
D
d
)
(
∑
k
=
1
C
gcd
(
d
,
k
)
)
)
⌊
A
D
⌋
⌊
B
D
⌋
\prod\limits_{D}\left(\prod\limits_{d|D}d^{\mu(\frac{D}{d})(\sum\limits_{k=1}^{C}\gcd(d,k))}\right)^{\left\lfloor \frac{A}{D}\right\rfloor\left\lfloor \frac{B}{D}\right\rfloor}
D∏⎝⎛d∣D∏dμ(dD)(k=1∑Cgcd(d,k))⎠⎞⌊DA⌋⌊DB⌋
O
(
T
n
log
n
)
O(Tn\log n)
O(Tnlogn) 不太行常数大。
∏
i
=
1
A
∏
j
=
1
B
∏
k
=
1
C
gcd
(
i
,
j
)
gcd
(
i
,
j
,
k
)
\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\prod\limits_{k=1}^{C}\gcd(i,j)^{\gcd(i,j,k)}
i=1∏Aj=1∏Bk=1∏Cgcd(i,j)gcd(i,j,k)
∏
d
∏
i
=
1
A
∏
j
=
1
B
gcd
(
i
,
j
)
d
(
∑
k
=
1
C
[
gcd
(
i
,
j
,
k
)
=
=
d
]
)
\prod\limits_{d}\prod\limits_{i=1}^{A}\prod\limits_{j=1}^{B}\gcd(i,j)^{d(\sum\limits_{k=1}^{C}[\gcd(i,j,k)==d])}
d∏i=1∏Aj=1∏Bgcd(i,j)d(k=1∑C[gcd(i,j,k)==d])
∏
d
∏
i
=
1
⌊
A
d
⌋
∏
j
=
1
⌊
B
d
⌋
(
d
gcd
(
i
,
j
)
)
d
(
∑
k
=
1
⌊
C
d
⌋
[
gcd
(
i
,
j
,
k
)
=
=
1
]
)
\prod\limits_{d}\prod\limits_{i=1}^{\left\lfloor \frac{A}{d}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{d}\right\rfloor}(d\gcd(i,j))^{d(\sum\limits_{k=1}^{\left\lfloor \frac{C}{d}\right\rfloor}[\gcd(i,j,k)==1])}
d∏i=1∏⌊dA⌋j=1∏⌊dB⌋(dgcd(i,j))d(k=1∑⌊dC⌋[gcd(i,j,k)==1])
∏
d
∏
i
=
1
⌊
A
d
⌋
∏
j
=
1
⌊
B
d
⌋
(
d
gcd
(
i
,
j
)
)
d
(
∑
k
=
1
⌊
C
d
⌋
[
gcd
(
i
,
j
,
k
)
=
=
1
]
)
\prod\limits_{d}\prod\limits_{i=1}^{\left\lfloor \frac{A}{d}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{d}\right\rfloor}(d\gcd(i,j))^{d(\sum\limits_{k=1}^{\left\lfloor \frac{C}{d}\right\rfloor}[\gcd(i,j,k)==1])}
d∏i=1∏⌊dA⌋j=1∏⌊dB⌋(dgcd(i,j))d(k=1∑⌊dC⌋[gcd(i,j,k)==1])
∏
d
∏
d
′
∏
i
=
1
⌊
A
d
d
′
⌋
∏
j
=
1
⌊
B
d
d
′
⌋
(
d
d
′
gcd
(
i
,
j
)
)
d
μ
(
d
′
)
⌊
C
d
d
′
⌋
\prod\limits_{d}\prod\limits_{d'}\prod\limits_{i=1}^{\left\lfloor \frac{A}{dd'}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{dd'}\right\rfloor}(dd'\gcd(i,j))^{d\mu(d')\left\lfloor \frac{C}{dd'}\right\rfloor}
d∏d′∏i=1∏⌊dd′A⌋j=1∏⌊dd′B⌋(dd′gcd(i,j))dμ(d′)⌊dd′C⌋
(
∏
d
∏
d
′
∏
i
=
1
⌊
A
d
d
′
⌋
∏
j
=
1
⌊
B
d
d
′
⌋
gcd
(
i
,
j
)
d
μ
(
d
′
)
⌊
C
d
d
′
⌋
)
(
∏
d
∏
d
′
d
d
′
d
μ
(
d
′
)
⌊
A
d
d
′
⌋
⌊
B
d
d
′
⌋
⌊
C
d
d
′
⌋
)
(\prod\limits_{d}\prod\limits_{d'}\prod\limits_{i=1}^{\left\lfloor \frac{A}{dd'}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{dd'}\right\rfloor}\gcd(i,j)^{d\mu(d')\left\lfloor \frac{C}{dd'}\right\rfloor})(\prod\limits_{d}\prod\limits_{d'}dd'^{d\mu(d')\left\lfloor \frac{A}{dd'}\right\rfloor\left\lfloor \frac{B}{dd'}\right\rfloor\left\lfloor \frac{C}{dd'}\right\rfloor})
(d∏d′∏i=1∏⌊dd′A⌋j=1∏⌊dd′B⌋gcd(i,j)dμ(d′)⌊dd′C⌋)(d∏d′∏dd′dμ(d′)⌊dd′A⌋⌊dd′B⌋⌊dd′C⌋)
(
∏
d
∏
d
′
∏
i
=
1
⌊
A
d
d
′
⌋
∏
j
=
1
⌊
B
d
d
′
⌋
gcd
(
i
,
j
)
d
μ
(
d
′
)
⌊
C
d
d
′
⌋
)
(
∏
D
∏
d
∣
D
D
d
μ
(
⌊
D
d
⌋
)
⌊
A
D
⌋
⌊
B
D
⌋
⌊
C
D
⌋
)
(\prod\limits_{d}\prod\limits_{d'}\prod\limits_{i=1}^{\left\lfloor \frac{A}{dd'}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{dd'}\right\rfloor}\gcd(i,j)^{d\mu(d')\left\lfloor \frac{C}{dd'}\right\rfloor})(\prod\limits_{D}\prod\limits_{d|D}D^{d\mu(\left\lfloor\frac{D}{d}\right\rfloor)\left\lfloor \frac{A}{D}\right\rfloor\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor})
(d∏d′∏i=1∏⌊dd′A⌋j=1∏⌊dd′B⌋gcd(i,j)dμ(d′)⌊dd′C⌋)(D∏d∣D∏Ddμ(⌊dD⌋)⌊DA⌋⌊DB⌋⌊DC⌋)
(
∏
d
∏
d
′
∏
i
=
1
⌊
A
d
d
′
⌋
∏
j
=
1
⌊
B
d
d
′
⌋
gcd
(
i
,
j
)
d
μ
(
d
′
)
⌊
C
d
d
′
⌋
)
(
∏
D
(
D
φ
(
D
)
)
⌊
A
D
⌋
⌊
B
D
⌋
⌊
C
D
⌋
)
(\prod\limits_{d}\prod\limits_{d'}\prod\limits_{i=1}^{\left\lfloor \frac{A}{dd'}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{dd'}\right\rfloor}\gcd(i,j)^{d\mu(d')\left\lfloor \frac{C}{dd'}\right\rfloor})(\prod\limits_{D}(D^{\varphi(D)})^{\left\lfloor \frac{A}{D}\right\rfloor\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor})
(d∏d′∏i=1∏⌊dd′A⌋j=1∏⌊dd′B⌋gcd(i,j)dμ(d′)⌊dd′C⌋)(D∏(Dφ(D))⌊DA⌋⌊DB⌋⌊DC⌋)
这个右边跟上面右边一样,一个是在上面贡献一个是在下面贡献,而且跟谁贡献无关,都是两个贡献,所以可以消掉。
t
y
p
e
=
2
type=2
type=2
(
∏
D
(
⌊
A
D
⌋
)
!
φ
(
D
)
)
⌊
B
D
⌋
⌊
C
D
⌋
\left(\prod\limits_{D}(\left\lfloor \frac{A}{D}\right\rfloor)!^{\varphi(D)}\right)^{\left\lfloor \frac{B}{D}\right\rfloor\left\lfloor \frac{C}{D}\right\rfloor}
(D∏(⌊DA⌋)!φ(D))⌊DB⌋⌊DC⌋
∏
d
∏
d
′
∏
i
=
1
⌊
A
d
d
′
⌋
∏
j
=
1
⌊
B
d
d
′
⌋
gcd
(
i
,
j
)
d
μ
(
d
′
)
⌊
C
d
d
′
⌋
\prod\limits_{d}\prod\limits_{d'}\prod\limits_{i=1}^{\left\lfloor \frac{A}{dd'}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{dd'}\right\rfloor}\gcd(i,j)^{d\mu(d')\left\lfloor \frac{C}{dd'}\right\rfloor}
d∏d′∏i=1∏⌊dd′A⌋j=1∏⌊dd′B⌋gcd(i,j)dμ(d′)⌊dd′C⌋
∏
D
(
∏
d
∣
D
∏
i
=
1
⌊
A
D
⌋
∏
j
=
1
⌊
B
D
⌋
gcd
(
i
,
j
)
d
μ
(
D
d
)
)
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{d|D}\prod\limits_{i=1}^{\left\lfloor \frac{A}{D}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{D}\right\rfloor}\gcd(i,j)^{d\mu(\frac{D}{d})}\right)^{\left\lfloor \frac{C}{D}\right\rfloor}
D∏⎝⎛d∣D∏i=1∏⌊DA⌋j=1∏⌊DB⌋gcd(i,j)dμ(dD)⎠⎞⌊DC⌋
∏
D
(
∏
i
=
1
⌊
A
D
⌋
∏
j
=
1
⌊
B
D
⌋
gcd
(
i
,
j
)
φ
(
D
)
)
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{i=1}^{\left\lfloor \frac{A}{D}\right\rfloor}\prod\limits_{j=1}^{\left\lfloor \frac{B}{D}\right\rfloor}\gcd(i,j)^{\varphi(D)}\right)^{\left\lfloor \frac{C}{D}\right\rfloor}
D∏⎝⎛i=1∏⌊DA⌋j=1∏⌊DB⌋gcd(i,j)φ(D)⎠⎞⌊DC⌋
∏
D
(
∏
t
t
(
∑
i
=
1
⌊
A
D
⌋
∑
j
=
1
⌊
B
D
⌋
[
gcd
(
i
,
j
)
=
=
t
]
)
φ
(
D
)
)
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{t}t^{\left(\sum\limits_{i=1}^{\left\lfloor \frac{A}{D}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor \frac{B}{D}\right\rfloor}[\gcd(i,j)==t]\right)\varphi(D)}\right)^{\left\lfloor \frac{C}{D}\right\rfloor}
D∏⎝⎜⎜⎜⎛t∏t(i=1∑⌊DA⌋j=1∑⌊DB⌋[gcd(i,j)==t])φ(D)⎠⎟⎟⎟⎞⌊DC⌋
∏
D
(
∏
t
t
(
∑
i
=
1
⌊
A
D
t
⌋
∑
j
=
1
⌊
B
D
t
⌋
[
gcd
(
i
,
j
)
=
=
1
]
)
φ
(
D
)
)
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{t}t^{\left(\sum\limits_{i=1}^{\left\lfloor \frac{A}{Dt}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor \frac{B}{Dt}\right\rfloor}[\gcd(i,j)==1]\right)\varphi(D)}\right)^{\left\lfloor \frac{C}{D}\right\rfloor}
D∏⎝⎜⎜⎜⎛t∏t(i=1∑⌊DtA⌋j=1∑⌊DtB⌋[gcd(i,j)==1])φ(D)⎠⎟⎟⎟⎞⌊DC⌋
∏
D
(
∏
t
t
(
∑
t
′
μ
(
t
′
)
⌊
A
D
t
t
′
⌋
⌊
B
D
t
t
′
⌋
)
φ
(
D
)
)
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{t}t^{\left(\sum\limits_{t'}\mu(t')\left\lfloor \frac{A}{Dtt'}\right\rfloor\left\lfloor \frac{B}{Dtt'}\right\rfloor\right)\varphi(D)}\right)^{\left\lfloor \frac{C}{D}\right\rfloor}
D∏⎝⎛t∏t(t′∑μ(t′)⌊Dtt′A⌋⌊Dtt′B⌋)φ(D)⎠⎞⌊DC⌋
∏
D
(
∏
t
∏
t
′
t
μ
(
t
′
)
⌊
A
D
t
t
′
⌋
⌊
B
D
t
t
′
⌋
φ
(
D
)
)
⌊
C
D
⌋
\prod\limits_{D}\left(\prod\limits_{t}\prod\limits_{t'}t^{\mu(t')\left\lfloor \frac{A}{Dtt'}\right\rfloor\left\lfloor \frac{B}{Dtt'}\right\rfloor\varphi(D)}\right)^{\left\lfloor \frac{C}{D}\right\rfloor}
D∏(t∏t′∏tμ(t′)⌊Dtt′A⌋⌊Dtt′B⌋φ(D))⌊DC⌋
∏
D
(
∏
T
(
∏
t
∣
T
t
μ
(
T
t
)
)
⌊
A
D
T
⌋
⌊
B
D
T
⌋
)
⌊
C
D
⌋
φ
(
D
)
\prod\limits_{D}\left(\prod\limits_{T}\left(\prod\limits_{t|T}t^{\mu(\frac{T}{t})}\right)^{\left\lfloor \frac{A}{DT}\right\rfloor\left\lfloor \frac{B}{DT}\right\rfloor}\right)^{\left\lfloor \frac{C}{D}\right\rfloor\varphi(D)}
D∏⎝⎛T∏(t∣T∏tμ(tT))⌊DTA⌋⌊DTB⌋⎠⎞⌊DC⌋φ(D)
预处理最里面的括号里面的值,然后两次整除分块做。
代码
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
int T, mo, A, B, C;
ll ksm(ll x, ll y, ll p) {
ll re = 1;
while (y) {if (y & 1) re = re * x % p; x = x * x % p; y >>= 1;}
return re;
}
int prime[N], mu[N]; bool np[N];
ll jc[N], inv[N], ii2[N], F2[N], f2[N], fv2[N], phi[N], fakphi[N], fakphiv[N];
void Init() {
inv[0] = inv[1] = 1; for (int i = 2; i <= 100000; i++) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
jc[0] = 1; for (int i = 1; i <= 100000; i++) jc[i] = jc[i - 1] * i % mo;
ii2[0] = 1; for (int i = 1; i <= 100000; i++) ii2[i] = ii2[i - 1] * ksm(i, i, mo) % mo;
mu[1] = 1; phi[1] = 1;
for (int i = 2; i <= 100000; i++) {
if (!np[i]) mu[i] = -1, prime[++prime[0]] = i, phi[i] = i - 1;
for (int j = 1; j <= prime[0] && i * prime[j] <= 100000; j++) {
np[i * prime[j]] = 1;
if (i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
mu[i * prime[j]] = -mu[i]; phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
mu[i] += mu[i - 1];
phi[i] += phi[i - 1];
}
for (int i = 0; i <= 100000; i++) F2[i] = 1;
for (int i = 1; i <= 100000; i++) {
f2[i] = i; fv2[i] = ksm(i, mo - 2, mo);
for (int j = i; j <= 100000; j += i) {
if (!(mu[j / i] - mu[j / i - 1])) continue;
F2[j] = F2[j] * (mu[j / i] - mu[j / i - 1] == 1 ? f2[i] : fv2[i]) % mo;
}
F2[i] = ksm(F2[i], 1ll * i * i, mo);
F2[i] = F2[i - 1] * F2[i] % mo;
}
for (int i = 0; i <= 100000; i++) fakphi[i] = 1; fakphiv[0] = 1;
for (int i = 1; i <= 100000; i++) {
for (int j = i; j <= 100000; j += i) {
ll now = 1; if (mu[j / i] - mu[j / i - 1]) now = (mu[j / i] - mu[j / i - 1] == 1) ? i : inv[i];
fakphi[j] = fakphi[j] * now % mo;
}
fakphi[i] = fakphi[i - 1] * fakphi[i] % mo;
fakphiv[i] = ksm(fakphi[i], mo - 2, mo);
}
}
ll sumgcd1(int n, int m, ll p) {
ll re = 0;
for (int L = 1, R; L <= n && L <= m; L = R + 1) {
R = min(n / (n / L), m / (m / L));
(re += 1ll * (mu[R] - mu[L - 1] + p) % p * (n / L) % p * (m / L) % p) %= p;
}
return re;
}
ll prodgcd1(int n, int m) {
ll re = 1;
for (int L = 1, R; L <= n && L <= m; L = R + 1) {
R = min(n / (n / L), m / (m / L));
ll di = 1; for (int i = L; i <= R; i++) di = di * i % mo;
re = re * ksm(di, sumgcd1(n / L, m / L, mo - 1), mo) % mo;
}
return re;
}
void work1() {
ll re = ksm(ksm(prodgcd1(A, C), mo - 2, mo), B, mo);
(re *= ksm(ksm(jc[A], B, mo) * ksm(jc[B], A, mo) % mo, C, mo)) %= mo;
(re *= ksm(ksm(prodgcd1(A, B), mo - 2, mo), C, mo)) %= mo;
printf("%lld ", re);
}
ll sumn(ll x, ll p) {
return 1ll * x * (x + 1) / 2 % p;
}
ll prodgcd2(ll n, ll m) {
ll re = 1;
for (int L = 1, R; L <= n && L <= m; L = R + 1) {
R = min(n / (n / L), m / (m / L));
re = re * ksm(F2[R] * ksm(F2[L - 1], mo - 2, mo) % mo, sumn(n / L, mo - 1) * sumn(m / L, mo - 1) % (mo - 1), mo) % mo;
}
return re;
}
void work2() {
ll re = ksm(prodgcd2(A, C), (mo - 2) * sumn(B, mo - 1) % (mo - 1), mo);
(re *= ksm(ii2[A], sumn(B, mo - 1) * sumn(C, mo - 1) % (mo - 1), mo)) %= mo;
(re *= ksm(ii2[B], sumn(A, mo - 1) * sumn(C, mo - 1) % (mo - 1), mo)) %= mo;
(re *= ksm(prodgcd2(A, B), (mo - 2) * sumn(C, mo - 1) % (mo - 1), mo)) %= mo;
printf("%lld ", re);
}
ll prodi3(ll A, ll B, ll C) {
ll re = 1;
for (int L = 1, R; L <= A && L <= B && L <= C; L = R + 1) {
R = min(A / (A / L), min(B / (B / L), C / (C / L)));
ll now = ksm(jc[A / L], phi[R] - phi[L - 1], mo);
re = re * ksm(now, (B / L) * (C / L) % (mo - 1), mo) % mo;
}
return re;
}
ll prodgcd3s(ll A, ll B) {
ll re = 1;
for (int L = 1, R; L <= A && L <= B; L = R + 1) {
R = min(A / (A / L), B / (B / L));
ll sum = fakphi[R] * fakphiv[L - 1] % mo;
(re *= ksm(sum, (A / L) * (B / L) % (mo - 1), mo)) %= mo;
}
return re;
}
ll prodgcd3(ll A, ll B, ll C) {
ll re = 1;
for (int L = 1, R; L <= A && L <= B && L <= C; L = R + 1) {
R = min(A / (A / L), min(B / (B / L), C / (C / L)));
re = re * ksm(prodgcd3s(A / L, B / L), (C / L) * (phi[R] - phi[L - 1]) % (mo - 1), mo) % mo;
}
return re;
}
void work3() {
ll re = ksm(prodgcd3(A, B, C), mo - 2, mo);
(re *= prodi3(A, B, C)) %= mo;
(re *= prodi3(B, A, C)) %= mo;
(re *= ksm(prodgcd3(A, C, B), mo - 2, mo)) %= mo;
printf("%lld\n", re);
}
int main() {
scanf("%d %d", &T, &mo);
Init();
while (T--) {
scanf("%d %d %d", &A, &B, &C);
work1();
work2();
work3();
}
return 0;
}