洛谷P1390 公约数的和
标签
- 欧拉函数
- 线性筛
- 容斥
前言
- 我的csdn和博客园是同步的,欢迎来访danzh-博客园~
欢迎关注~ - 被自己以前的博客坑了…
简明题意
- 给定
n
(
n
<
=
2
e
6
)
n(n <= 2e6)
n(n<=2e6),需要你求
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) \sum_{i=1}^n\sum_{j=i+1}^ngcd(i,j) i=1∑nj=i+1∑ngcd(i,j)
思路
- 首先我们把原式改成枚举gcd,然后用gcd的值去乘以出现的次数,于是原式等价于:
∑ d = 1 n ( d ∗ ∑ i = 1 n ∑ j = i + 1 n [ g c d ( i , j ) = = d ] ) \sum_{d=1}^n\left(d*\sum_{i=1}^n\sum_{j=i+1}^n[gcd(i,j)==d]\right) d=1∑n(d∗i=1∑nj=i+1∑n[gcd(i,j)==d]) - 看到 [ g c d ( i , j ) = = d ] [gcd(i,j)==d] [gcd(i,j)==d]是不是很激动?(不激动的同学应该是刚刚入门,基础还不牢固,那就建议看看我关于这类题的其他入门级别的博客)
这里我再啰嗦一下,看到 [ g c d ( i , j ) = = d ] [gcd(i,j)==d] [gcd(i,j)==d]后,首要的工作是通过改变枚举上限,把它写成 [ g c d ( i , j ) = = 1 ] [gcd(i,j)==1] [gcd(i,j)==1]的形式,为什么要这样做?因为换成 [ g c d ( i , j ) = = 1 ] [gcd(i,j)==1] [gcd(i,j)==1]这种形式,既可以用欧拉函数快速求出,也可以用莫比乌斯函数性质替换从而降低复杂度。而通常,对于 ∑ i ∑ j [ g c d ( i , j ) = = 1 ] \sum_i\sum_j[gcd(i,j)==1] ∑i∑j[gcd(i,j)==1],如果 i , j i,j i,j的上限相同,则用直接欧拉函数求,如果 i , j i,j i,j的上限不相同,则应该用莫比乌斯函数性质替换 ∑ d ∣ n μ ( d ) = [ n = = 1 ] \sum\limits_{d|n}\mu(d)=[n==1] d∣n∑μ(d)=[n==1]
- 这里
i
,
j
i,j
i,j的范围是一样的,因此我们考虑用欧拉函数求。但是
∑
i
=
1
n
∑
j
=
i
+
1
n
[
g
c
d
(
i
,
j
)
=
=
d
]
\sum_{i=1}^n\sum_{j=i+1}^n[gcd(i,j)==d]
i=1∑nj=i+1∑n[gcd(i,j)==d]我们只知道
∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = = d ] \sum_{i=1}^n\sum_{j=1}^n[gcd(i,j)==d] i=1∑nj=1∑n[gcd(i,j)==d]
的求法,如果j不从1开始枚举,那就不知道怎么求了。这时候,我们用容斥。我们回到最原始的式子:
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) \sum_{i=1}^n\sum_{j=i+1}^ngcd(i,j) i=1∑nj=i+1∑ngcd(i,j) - 很容易发现,实际上有
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) = ∑ i = 1 n ∑ j = 1 j g c d ( i , j ) \sum_{i=1}^n\sum_{j=i+1}^ngcd(i,j)=\sum_{i=1}^n\sum_{j=1}^jgcd(i,j) i=1∑nj=i+1∑ngcd(i,j)=i=1∑nj=1∑jgcd(i,j)
因为这两半是对称的,而又有
∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = ∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) + ∑ i = 1 n ∑ j = 1 j g c d ( i , j ) + 对 角 线 的 g c d 之 和 \sum_{i=1}^n\sum_{j=1}^ngcd(i,j)=\sum_{i=1}^n\sum_{j=i+1}^ngcd(i,j)+\sum_{i=1}^n\sum_{j=1}^jgcd(i,j)+对角线的gcd之和 i=1∑nj=1∑ngcd(i,j)=i=1∑nj=i+1∑ngcd(i,j)+i=1∑nj=1∑jgcd(i,j)+对角线的gcd之和 - 对角线的ij是相等的,而且gcd(i,i)就等于i,与就是,对角线gcd之和=
∑
i
n
i
=
(
1
+
n
)
∗
n
2
\sum_i^ni=\frac {(1+n)*n}2
∑ini=2(1+n)∗n,所以,
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) = ∑ i = 1 n ∑ j = 1 n g c d ( i , j ) − ( 1 + n ) ∗ n 2 2 \sum_{i=1}^n\sum_{j=i+1}^ngcd(i,j)= \frac {\sum\limits_{i=1}^n\sum\limits_{j=1}^ngcd(i,j)-\frac {(1+n)*n}2}{2} i=1∑nj=i+1∑ngcd(i,j)=2i=1∑nj=1∑ngcd(i,j)−2(1+n)∗n - 现在问题就转换成求
∑ i = 1 n ∑ j = 1 n g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^ngcd(i,j) i=1∑nj=1∑ngcd(i,j) - 改为枚举
g
c
d
(
i
,
j
)
gcd(i,j)
gcd(i,j)
∑ d = 1 n ( d ∗ ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = = d ] ) \sum_{d=1}^n\left(d*\sum_{i=1}^n\sum_{j=1}^n[gcd(i,j)==d]\right) d=1∑n(d∗i=1∑nj=1∑n[gcd(i,j)==d]) - 换
g
c
d
(
i
,
j
)
=
=
1
gcd(i,j)==1
gcd(i,j)==1
∑ d = 1 n ( d ∗ ∑ i = 1 [ n d ] ∑ j = 1 [ n d ] [ g c d ( i , j ) = = 1 ] ) \sum_{d=1}^n\left(d*\sum_{i=1}^{[\frac nd]}\sum_{j=1}^{[\frac nd]}[gcd(i,j)==1]\right) d=1∑n⎝⎛d∗i=1∑[dn]j=1∑[dn][gcd(i,j)==1]⎠⎞ - 其中
∑ i = 1 [ n d ] ∑ j = 1 [ n d ] [ g c d ( i , j ) = = 1 ] = 2 ∗ p r e ϕ ( [ p d ] ) − 1 \sum_{i=1}^{[\frac nd]}\sum_{j=1}^{[\frac nd]}[gcd(i,j)==1]=2*pre\phi([\frac pd])-1 i=1∑[dn]j=1∑[dn][gcd(i,j)==1]=2∗preϕ([dp])−1 - 原式:
∑ d = 1 n ( d ∗ ( 2 ∗ p r e ϕ ( [ p d ] ) − 1 ) ) \sum_{d=1}^n\left(d* \left(2*pre\phi([\frac pd])-1\right)\right) d=1∑n(d∗(2∗preϕ([dp])−1)) - 接下来先把 ϕ \phi ϕ线性筛出来,然后前缀和一下,最后的复杂度就是 O ( n ) O(n) O(n)了
注意事项
- 注意是
∑ i = 1 [ n d ] ∑ j = 1 [ n d ] [ g c d ( i , j ) = = 1 ] = 2 ∗ p r e ϕ ( [ p d ] ) − 1 \sum_{i=1}^{[\frac nd]}\sum_{j=1}^{[\frac nd]}[gcd(i,j)==1]=2*pre\phi([\frac pd])-1 i=1∑[dn]j=1∑[dn][gcd(i,j)==1]=2∗preϕ([dp])−1
而不是
∑ i = 1 [ n d ] ∑ j = 1 [ n d ] [ g c d ( i , j ) = = 1 ] = 2 ∗ ϕ ( [ p d ] ) − 1 \sum_{i=1}^{[\frac nd]}\sum_{j=1}^{[\frac nd]}[gcd(i,j)==1]=2*\phi([\frac pd])-1 i=1∑[dn]j=1∑[dn][gcd(i,j)==1]=2∗ϕ([dp])−1
不要把那个前缀和搞成 ϕ ( [ p d ] ) \phi([\frac pd]) ϕ([dp])
总结
- 不要把那个前缀和搞成 ϕ ( [ p d ] ) \phi([\frac pd]) ϕ([dp])…
AC代码
#include<cstdio>
const int maxn = 2e6 + 10;
bool no_prime[maxn];
int prime[maxn], phi[maxn];
long long pre[maxn];
int shai(int n)
{
int cnt = 0;
phi[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!no_prime[i])
prime[++cnt] = i, phi[i] = i - 1;
for (int j = 1; j <= cnt && prime[j] * i <= n; j++)
{
no_prime[prime[j] * i] = 1;
phi[prime[j] * i] = (i % prime[j] == 0) ? phi[i] * prime[j] : phi[i] * (prime[j] - 1);
if (i % prime[j] == 0) break;
}
}
for (int i = 1; i <= n; i++)
pre[i] = pre[i - 1] + phi[i];
return cnt;
}
void solve()
{
long long n;
scanf("%lld", &n);
shai(n);
long long ans = 0;
for (int d = 1; d <= n; d++)
ans += (2 * pre[n / d] - 1) * d;
printf("%lld\n", (ans - (1 + n) * n / 2) / 2);
}
int main()
{
solve();
return 0;
}