牛客挑战赛51 E NIT的gcd(欧拉反演,建图优化,三元环计数)
Posted 繁凡さん
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了牛客挑战赛51 E NIT的gcd(欧拉反演,建图优化,三元环计数)相关的知识,希望对你有一定的参考价值。
整理的算法模板合集: ACM模板
实际上是一个全新的精炼模板整合计划
Problem
给你一个正整数 n n n。
请你输出 ∑ i = 1 n ∑ j = 1 n ∑ k = 1 n gcd ( i , j ) gcd ( i , k ) gcd ( j , k ) \\sum\\limits_{i=1}^n\\sum\\limits_{j=1}^n\\sum\\limits_{k=1}^n \\gcd(i,j)\\gcd(i,k)\\gcd(j,k) i=1∑nj=1∑nk=1∑ngcd(i,j)gcd(i,k)gcd(j,k) 的值,对 998244353 998244353 998244353 取模。
Solution
首先进行经典欧拉反演:
φ ∗ 1 = i d ⇒ n = ∑ d ∣ n φ ( d ) \\begin{aligned}\\varphi*1&=id\\Rightarrow n=\\sum_{d|n}\\varphi(d)\\end{aligned} φ∗1=id⇒n=d∣n∑φ(d)
gcd ( i , j ) = ∑ d ∣ g c d ( i , j ) φ ( d ) = ∑ d ∣ i ∑ d ∣ j φ ( d ) \\begin{aligned}\\gcd(i,j)&=\\sum_{d|gcd(i,j)}\\varphi(d)\\\\&=\\sum_{d|i}\\sum_{d|j}\\varphi(d)\\end{aligned} gcd(i,j)=d∣gcd(i,j)∑φ(d)=d∣i∑d∣j∑φ(d)
a
n
s
=
∑
i
=
1
n
∑
j
=
1
n
∑
k
=
1
n
gcd
(
i
,
j
)
gcd
(
i
,
k
)
gcd
(
j
,
k
)
=
∑
i
=
1
n
∑
j
=
1
n
∑
k
=
1
n
∑
T
1
∣
i
,
T
1
∣
j
φ
(
T
1
)
∑
T
2
∣
i
,
T
2
∣
k
φ
(
T
2
)
∑
T
3
∣
j
,
T
3
∣
k
φ
(
T
3
)
=
∑
T
1
=
1
n
φ
(
T
1
)
∑
T
2
=
1
n
φ
(
T
2
)
∑
T
3
=
1
n
φ
(
T
3
)
∑
T
1
∣
i
,
T
2
∣
i
n
1
∑
T
1
∣
j
,
T
3
∣
j
n
1
∑
T
2
∣
k
,
T
3
∣
k
n
1
=
∑
T
1
=
1
n
φ
(
T
1
)
∑
T
2
=
1
n
φ
(
T
2
)
∑
T
3
=
1
n
φ
(
T
3
)
⌊
n
lcm
(
T
1
,
T
2
)
⌋
⌊
n
lcm
(
T
1
,
T
3
)
⌋
⌊
n
lcm
(
T
2
,
T
3
)
⌋
\\begin{aligned}ans&=\\sum\\limits_{i=1}^n\\sum\\limits_{j=1}^n\\sum\\limits_{k=1}^n \\gcd(i,j)\\gcd(i,k)\\gcd(j,k)&\\\\&=\\sum\\limits_{i=1}^n\\sum\\limits_{j=1}^n\\sum\\limits_{k=1}^n\\sum\\limits_{T_1|i,T_1|j}\\varphi(T_1)\\sum\\limits_{T_2|i,T_2|k}\\varphi(T_2)\\sum\\limits_{T_3|j,T_3|k}\\varphi (T_3)&\\\\& =\\sum\\limits_{T_1=1}^n\\varphi(T_1)\\sum\\limits_{T_2=1}^n\\varphi(T_2)\\sum\\limits_{T_3=1}^n\\varphi(T_3)\\sum\\limits_{T_1|i,T_2|i}^n1\\sum\\limits_{T_1|j,T_3|j}^n1\\sum\\limits_{T_2|k,T_3|k}^n1&\\\\&=\\sum\\limits_{T_1=1}^n\\varphi(T_1)\\sum\\limits_{T_2=1}^n\\varphi(T_2)\\sum\\limits_{T_3=1}^n\\varphi(T_3)\\lfloor\\frac{n}{\\text{lcm}(T_1,T_2)}\\rfloor\\lfloor\\frac{n}{\\text{lcm}(T_1,T_3)}\\rfloor\\lfloor\\frac{n}{\\text{lcm}(T_2,T_3)}\\rfloor\\end{aligned}
ans=i=1∑nj=1∑nk=1∑ngcd(i,j)gcd(i,k)gcd(j,k)=i=1∑nj=1∑nk=1∑nT1∣i,T1∣j∑φ(T1)T2∣i,T2∣k∑φ(T2)T3∣j,T3∣k∑φ(T3)=T1=1∑nφ(T1)T2=1∑nφ(T2)T3=1∑nφ(T3)T1∣i,T2∣i∑n1T1∣j,T3∣j∑n1T
[luogu P2586] GCD 解题报告 (莫比乌斯反演|欧拉函数)