CF_#415(Div. 1)_E

参考了一下大神的题解、、
题解传送门:[https://blog.sengxian.com/solutions/cf-809e]

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
//created by missever
#include<bits/stdc++.h>
#define MAX 1000000007
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 5;
int n,rt;
vector<int> g[maxn],g2[maxn],fac[maxn],vec[maxn];
int cnt = 0,bn = 0,b[maxn * 2],a[maxn];
int f[maxn * 2],dfn[maxn],dis[maxn],st[maxn];
bool flag[maxn];
int phi[maxn],pri[maxn],mu[maxn],inv[maxn],cc[maxn],xc[maxn];
int tot = 0;
void add(int &x,int y) {
x += y;
while(x >= MAX) x -= MAX;
while(x < 0) x += MAX;
}
int mod(int x,int y) {
LL z = 1LL * x * y;
return z - z / MAX * MAX;
}
void dfs(int u,int fa) {
int tmp = ++cnt;
b[++bn] = tmp;
f[tmp] = u;
dfn[u] = bn;
for(auto v:fac[a[u]]) vec[v].push_back(u);
for (auto v:g[u]) {
if (v == fa) continue;
dis[v] = dis[u] + 1;
dfs(v,u);
b[++bn] = tmp;
}
}
int dp[maxn * 2][20];
void rmq_init() {
int i,j;
for (i = 1; i <= bn; i++) dp[i][0] = b[i];
int m = floor(log(bn*1.0)/log(2.0));
for (j = 1; j <= m; j++)
for (i=1; i <= bn-(1<<j)+1 ; i++)
dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq(int l,int r) {
int k = floor(log((r-l+1)*1.0)/log(2.0));
return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int lca(int x,int y) {
if(x == y) return x;
if (dfn[x] > dfn[y]) swap(x,y);
int k = rmq(dfn[x],dfn[y]);
return f[k];
}
bool cmp(const int &i, const int &j) {
return dfn[i] < dfn[j];
}
void build_tree(vector<int> &vc) {
int sz = 0,k = vc.size();
sort(vc.begin(),vc.end(),cmp);
st[sz] = 0;
g2[0].clear();
for (int i = 0; i < k; ++i) {
int u = vc[i], ll = lca(u, st[sz]);
g2[u].clear();
if (ll == st[sz]) st[++sz] = u;
else {
while (sz >= 1 && dis[st[sz - 1]] >= dis[ll]) {
g2[st[sz - 1]].push_back(st[sz]);
sz--;
}
if (st[sz] != ll) {
g2[ll].clear();
g2[ll].push_back(st[sz--]);
st[++sz] = ll;
}
st[++sz] = u;
}
}
for (int i = 0; i < sz; ++i) g2[st[i]].push_back(st[i + 1]);
}
int dfs2(int u,int ff){
int s1,s2;
s1 = s2 = 0;
xc[u] = (a[u] % ff == 0) ? phi[a[u]] : 0;
for(auto v:g2[u]){
add(s1,dfs2(v,ff));
add(s2,mod(xc[u],xc[v]));
add(xc[u],xc[v]);
}
add(s1,mod(s2,2 * dis[u]));
if(a[u] % ff == 0) add(s1,mod(dis[u],mod(phi[a[u]],phi[a[u]])));
return s1;
}
void Get_phi() {
mu[1] = phi[1] = 1;
for(int i = 2; i <= n; i++) {
if(!flag[i]) {
pri[tot++] = i;
phi[i] = i - 1;
mu[i] = -1;
}
for(int j = 0; j < tot; j++) {
if(i * pri[j] > n) break;
flag[i * pri[j]] = true;
if(i % pri[j] == 0) {
mu[i * pri[j]] = 0;
phi[i * pri[j]] = pri[j] * phi[i];
break;
} else {
phi[i * pri[j]] = (pri[j] - 1) * phi[i];
mu[i * pri[j]] = -mu[i];
}
}
}
}
int calc(int u) {
int s1,s2;
s1 = s2 = 0;
for(auto v:vec[u]) {
add(s1,phi[a[v]]);
add(s2,mod(phi[a[v]],dis[v]));
}
s1 = mod(2,mod(s1,s2));
build_tree(vec[u]);
add(s1,-2 * dfs2(0,u));
return s1;
}
void solve() {
Get_phi();
for(int i = 1; i <= n; i++) {
for(int j = i; j <= n; j += i) fac[j].push_back(i);
}
inv[1] = 1;
for(int i = 2; i <= n; i++) add(inv[i],MAX - mod(MAX / i,inv[MAX % i]));
dis[1] = 1;
dfs(1,0);
rmq_init();
for(int i = 1; i <= n; i++) cc[i] = calc(i);
int ans = 0;
for(int i = 1; i <= n; i++) {
int s = 0;
for(int j = 1; i * j <= n; j++) add(s,mu[j] * cc[i * j]);
add(ans,mod(mod(s,i),inv[phi[i]]));
}
ans = mod(ans,mod(inv[n],inv[n - 1]));
printf("%d\n",ans);
}
int main() {
int i,u,v;
scanf("%d",&n);
for(i = 1; i <= n; i++) scanf("%d",&a[i]);
for(i = 1; i < n; i++) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
solve();
return 0;
}