牛客 contest 43058E 炫酷反演魔术

题目描述

https://ac.nowcoder.com/acm/contest/43058/E

简要题意:给定一个长度为 $n$ 的序列 $a_i$,求 $\sum_{i=1}^n\sum_{j=1}^n\varphi(gcd(a_i,a_j^3))$

$n\le 3\times 10^5,a_i\in[1,n]$

Solution

我们直接化简

注意到我们只需要预处理 $\sum_{d|a_i}$ 和 $\sum_{d|a_i^3}$ 即可,后面那个东西本质和前面的一样,时间复杂度 $O(n\log n)$

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
51
52
53
54
#include <iostream>
#include <algorithm>
#include <queue>
#define maxn 300010
#define ll long long
#define INF 1000000000
using namespace std;

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

int n, cnt[maxn], c1[maxn], c2[maxn], t[maxn];

vector<int> A[maxn], B[maxn];

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

cin >> n; init_isp(n);
for (int i = 1, x; i <= n; ++i) cin >> x, ++cnt[x];
for (int i = 1; i <= n; ++i) {
int x = i, y = 1;
while (x != 1) {
int p = a[x], t = 0;
while (x % p == 0) ++t, x /= p;
for (int i = 1; i <= (t + 2) / 3; ++i) y *= p;
} t[i] = y;
}
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; j += i) c1[i] += cnt[j];
for (int i = 1; i <= n; ++i) c2[i] = c1[t[i]];
ll ans = 0;
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; j += i) ans += 1ll * phi[i] * mu[j / i] * c1[j] * c2[j];
cout << ans << "\n";
return 0;
}