[Codeforces809E] Surprise me!

做法

点分治一下,转化为以下问题:

有两个长为 \(n\) 的数组 \(a_1\)\(d_1\),两个长为 \(m\) 的数组 \(a_2\)\(d_2\),求:

\[ \sum_{i=1}^n \sum_{j=1}^m \phi(a_1[i]a_2[j])(d_1[i]+d_2[j]) \]

枚举一下 \(i\),关键就是如何求形如 \(\sum_j \phi(a_1[i]a_2[j]) d_2[j]\) 的东西。

注意到 \(\phi(ab) = \gcd(a,b) \frac{\phi(a)\phi(b)}{\phi(\gcd(a,b))}\)

\(s_i = \sum_{i|k} d_2[k]\phi(a_2[k])\)

\[ \sum_j \phi(a_1[i]a_2[j]) d_2[j] \\=\phi(a_1[i])\sum_{d\mid a_1[i]} [\gcd(a_1[i],a_2[j])=d]\frac{dd_2[j]\phi(a_2[j])}{\phi(d)} \\=\phi(a_1[i])\sum_{d\mid a_1[i]} \frac d {\phi(d)} \sum_{d \mid k, k \mid a_1[i]} \mu(\frac k d) s_k \]

\(s_i\) 是很容易处理的,只需要对每个 \(1\ldots m\) 中的数 \(i\) 考虑 \(d_2[i]\phi(a_2[i])\) 对每个 \(s\)\(i\) 的约数下标位置的贡献即可。

如果暴力计算上式,复杂度为 \(1 \ldots n\) 中每个数的约数个数的约数个数之和乘以点分治的一个 \(\log\)。前面的部分复杂度是 \(\log^2\) 的(考虑 \((\sum \frac 1 i )^2\)),从而总复杂度为 \(\mathcal O(n\log^3 n)\)

继续对上式变形:

\(t_k = \sum_{d \mid k} \frac d {\phi(d)} \mu(\frac k d)\)\(t_k\) 是一个常数。

\[ \phi(a_1[i])\sum_{d\mid a_1[i]} \frac d {\phi(d)} \sum_{d \mid k, k \mid a_1[i]} \mu(\frac k d) s_k \\=\phi(a_1[i])\sum_{k \mid a_1[i]} s_k \sum_{d \mid k} \frac d {\phi(d)} \mu(\frac k d) \\=\phi(a_1[i])\sum_{k \mid a_1[i]} s_k t_k \]

考虑对 \(t\) 进行预处理。\(t\) 是一个积性函数(因为它是两个积性函数的狄利克雷卷积),我们可以直接把它筛出来。

但是因为我懒,我选择直接暴力预处理 \(t\),时间复杂度为 \(\mathcal O(n \log n)\)

这个算法的复杂度为 \(\mathcal O(n \log^2n)\)

代码

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

const int maxn = 200010;
const int mod = 1e9+7;

vector<int> vd[maxn];
int n, a[maxn], l[maxn], K[maxn], e, ans;
int phi[maxn], iphi[maxn], mu[maxn], isnp[maxn], prm[maxn], pcnt;
int sum1[maxn], sum2[maxn], dep[maxn];

int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL * ret * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ret;
}

struct Edge {
int v, x;
} E[maxn<<1];

inline void addEdge(int u, int v) {
E[e].v = v, E[e].x = l[u], l[u] = e++;
}

int vis[maxn], sz[maxn], mx[maxn], sum[maxn];

void dfs1(int u, int f, vector<int> &vl) {
sz[u] = 1, mx[u] = 0;
vl.push_back(u);
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v] && v != f) {
dfs1(v, u, vl);
sz[u] += sz[v];
mx[u] = max(mx[u], sz[v]);
}
}
}

void dfs2(int u, int f) {
sz[u] = 1;
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v] && v != f) {
dep[v] = dep[u] + 1;
dfs2(v, u);
sz[u] += sz[v];
}
}
}

int mo(int x) {
if (x >= mod) return x - mod;
if (x < 0) return x + mod;
return x;
}

void dfs3_inc(int u, int f) {
int w = a[u];
for (int i = 0; i < vd[w].size(); i++) {
int d = vd[w][i];
sum1[d] = mo(sum1[d] + phi[w]);
sum2[d] = mo(sum2[d] + 1LL * phi[w] % mod * dep[u] % mod);
}
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v] && v != f) {
dfs3_inc(v, u);
}
}
}

void dfs3_dec(int u, int f) {
int w = a[u];
for (int i = 0; i < vd[w].size(); i++) {
int d = vd[w][i];
sum1[d] = mo(sum1[d] - phi[w]);
sum2[d] = mo(sum2[d] - 1LL * phi[w] % mod * dep[u] % mod);
}
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v] && v != f) {
dfs3_dec(v, u);
}
}
}

void dfs4(int u, int f) {
int w = a[u];
int s1 = 0, s2 = 0, cur_sum1 = 0, cur_sum2 = 0;
for (int i = 0; i < vd[w].size(); i++) {
int d = vd[w][i];
cur_sum1 = mo(cur_sum1 + 1LL * K[d] * sum1[d] % mod);
cur_sum2 = mo(cur_sum2 + 1LL * K[d] * sum2[d] % mod);
}
ans = mo(ans + 1LL * cur_sum1 * dep[u] % mod * phi[w] % mod);
ans = mo(ans + 1LL * cur_sum2 * phi[w] % mod);
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v] && v != f) {
dfs4(v, u);
}
}
}

void solve(int u, int s) {
vector<int> vl;
dfs1(u, 0, vl);
int c = 0;
for (int i = 0; i < vl.size(); i++) {
int u = vl[i];
mx[u] = max(mx[u], s - sz[u]);
if (!c || mx[u] < mx[c]) c = u;
}
dep[c] = 0;
dfs2(c, 0);
vector<int> nv, ns;
for (int p = l[c]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v]) {
nv.push_back(v), ns.push_back(sz[v]);
}
}
dfs3_inc(c, 0);
for (int i = 0; i < nv.size(); i++) {
int v = nv[i];
dfs3_dec(v, c);
dfs4(v, c);
dfs3_inc(v, c);
}
{
int w = a[c];
for (int i = 0; i < vd[w].size(); i++) {
int d = vd[w][i];
ans = (ans + 1LL * K[d] * sum2[d] % mod * phi[w] % mod) % mod;
}
}
dfs3_dec(c, 0);
vis[c] = 1;
for (int i = 0; i < nv.size(); i++) solve(nv[i], ns[i]);
}

int main() {
memset(l, -1, sizeof(l));
scanf("%d", &n);
phi[1] = 1, mu[1] = 1;
for (int i = 2; i <= n; i++) {
if (!isnp[i]) {
phi[i] = i-1;
mu[i] = mod - 1;
prm[++ pcnt] = i;
}
for (int j = 1; j <= pcnt && prm[j] * i <= n; j++) {
isnp[prm[j] * i] = 1;
if (i % prm[j] == 0) {
mu[i * prm[j]] = 0;
phi[i * prm[j]] = phi[i] * prm[j];
break;。,
} else {
mu[i * prm[j]] = (mod - mu[i]) % mod;
phi[i * prm[j]] = phi[i] * (prm[j] - 1);
}
}
}
for (int i = 1; i <= n; i++) iphi[i] = qpow(phi[i], mod-2);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j * i <= n; j++) {
vd[j*i].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < vd[i].size(); j++) {
int d = vd[i][j];
K[i] = (K[i] + 1LL * mu[i/d] * d % mod * iphi[d] % mod) % mod;
}
}
for (int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
addEdge(u, v), addEdge(v, u);
}
solve(1, n);
ans = 1LL * ans * qpow(1LL * n * (n-1) % mod, mod-2) % mod;
printf("%d\n", ans);
return 0;
}