题目链接
题目大意
∑ i = x n ∑ j = y m [ gcd ( i , j ) = k ] ( 1 ≤ T , x , y , n , m , k ≤ 1 0 5 ) \sum_{i=x}^n\sum_{j=y}^m[\gcd(i,j)=k]\quad(1\le T,x,y,n,m,k\le 10^5) i=x∑nj=y∑m[gcd(i,j)=k](1≤T,x,y,n,m,k≤105)
推柿子
考虑前缀和计算,令 f ( n , m ) = ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = k ] f(n,m)=\sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)=k] f(n,m)=∑i=1n∑j=1m[gcd(i,j)=k], ∑ i = x n ∑ j = y m [ gcd ( i , j ) = k ] = f ( n , m ) − f ( x − 1 , m ) − f ( n , y − 1 ) + f ( x − 1 , y − 1 ) \sum_{i=x}^n\sum_{j=y}^m[\gcd(i,j)=k]=f(n,m)-f(x-1,m)-f(n,y-1)+f(x-1,y-1) ∑i=xn∑j=ym[gcd(i,j)=k]=f(n,m)−f(x−1,m)−f(n,y−1)+f(x−1,y−1)。
考虑计算
f
(
n
,
m
)
(
n
≤
m
)
f(n,m)\quad(n\le m)
f(n,m)(n≤m)。
f
(
n
,
m
)
=
∑
i
=
1
n
∑
j
=
1
m
[
gcd
(
i
,
j
)
=
k
]
=
∑
i
=
1
⌊
n
k
⌋
∑
j
=
1
⌊
m
k
⌋
[
gcd
(
i
,
j
)
=
1
]
=
∑
i
=
1
⌊
n
k
⌋
∑
j
=
1
⌊
m
k
⌋
∑
d
∣
gcd
(
i
,
j
)
μ
(
d
)
(
∑
d
∣
n
μ
(
d
)
=
[
n
=
1
]
)
=
∑
d
=
1
⌊
n
k
⌋
μ
(
d
)
∑
i
=
1
⌊
n
k
⌋
d
∣
i
∑
j
=
1
⌊
m
k
⌋
d
∣
j
=
∑
d
=
1
⌊
n
k
⌋
μ
(
d
)
⌊
n
k
d
⌋
⌊
m
k
d
⌋
\begin{aligned}f(n,m)&=\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=k]\\&=\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{k}\rfloor}[\gcd(i,j)=1]\\&=\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{k}\rfloor}\sum_{d|\gcd(i,j)}\mu(d)(\sum_{d|n}\mu(d)=[n=1])\\&=\sum_{d=1}^{\lfloor\frac{n}{k}\rfloor}\mu(d)\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}d|i\sum_{j=1}^{\lfloor\frac{m}{k}\rfloor}d|j\\&=\sum_{d=1}^{\lfloor\frac{n}{k}\rfloor}\mu(d)\lfloor\frac{n}{kd}\rfloor\lfloor\frac{m}{kd}\rfloor\\\end{aligned}
f(n,m)=i=1∑nj=1∑m[gcd(i,j)=k]=i=1∑⌊kn⌋j=1∑⌊km⌋[gcd(i,j)=1]=i=1∑⌊kn⌋j=1∑⌊km⌋d∣gcd(i,j)∑μ(d)(d∣n∑μ(d)=[n=1])=d=1∑⌊kn⌋μ(d)i=1∑⌊kn⌋d∣ij=1∑⌊km⌋d∣j=d=1∑⌊kn⌋μ(d)⌊kdn⌋⌊kdm⌋
O
(
n
)
O(n)
O(n) 线性筛计算
μ
\mu
μ,整除分块计算
∑
d
=
1
⌊
n
k
⌋
μ
(
d
)
⌊
n
k
d
⌋
⌊
m
k
d
⌋
\sum_{d=1}^{\lfloor\frac{n}{k}\rfloor}\mu(d)\lfloor\frac{n}{kd}\rfloor\lfloor\frac{m}{kd}\rfloor
∑d=1⌊kn⌋μ(d)⌊kdn⌋⌊kdm⌋。时间复杂度
O
(
n
+
T
n
)
O(n+T\sqrt{n})
O(n+Tn)。