Luogu P6055 [RC-02] GCD

题目描述

https://www.luogu.com.cn/problem/P6055

简要题意:求 $\sum_{i=1}^n\sum_{j=1}^n\sum_{p=1}^{\lfloor\frac{n}{j}\rfloor}\sum_{q=1}^{\lfloor\frac{n}{j}\rfloor}[(i,j)=1][(p,q)=1]$

求 $n\le 2\times 10^9$

Solution

注意到后面的式子像是在枚举 $gcd$ 为 $j$ 的二元组 $(p,q),我们按照这个思路化简式子

$\sum_{i=1}^n\sum_{j=1}^n\sum_{p=1}^n\sum_{q=1}^n[(i,j)=1][(p,q)=j]\rightarrow\sum_{i=1}^n\sum_{p=1}^n\sum_{q=1}^n[(i,p,q)=1]$​​​,这个式子我们就很熟了

容易得到 $\sum_{d=1}^n\mu(d)\lfloor\frac{n}{d}\rfloor^3$,这个东西可以直接数论分块加杜教筛,复杂度仍然是 $O(n^\frac{2}{3})$

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
#include <iostream>
#include <unordered_map>
#define maxn 1000010
#define ll long long
using namespace std;

const int p = 998244353;

int pri[maxn], mu[maxn], cnt; bool isp[maxn];
void init_isp(int n) {
mu[1] = 1;
for (int i = 2; i <= n; ++i) {
if (!isp[i]) pri[++cnt] = i, mu[i] = -1;
for (int j = 1; j <= cnt && i * pri[j] <= n; ++j) {
isp[i * pri[j]] = 1;
if (i % pri[j] == 0) break;
mu[i * pri[j]] = -mu[i];
}
}
for (int i = 1; i <= n; ++i) mu[i] += mu[i - 1];
}

unordered_map<int, int> _mu; const int N = 1000000;
int calc_mu(int n) {
if (n <= N) return mu[n];
if (_mu.find(n) != _mu.end()) return _mu[n];
int ans = 1;
for (ll l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
ans -= (r - l + 1) * calc_mu(n / l);
}
return _mu[n] = ans;
}


int n, m;


int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n; init_isp(N); ll ans = 0;
for (int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l); ll v = n / l;
ans = (ans + (calc_mu(r) - calc_mu(l - 1)) * v % p * v % p * v) % p;
} cout << (ans + p) % p << "\n";
return 0;
}