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
| #include <cstdio> #include <vector> #include <algorithm> #define pb push_back using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') flag |= c == '-'; x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...Rest> void read (Type &x, Rest &...y) { read(x); read(y...); } template <class Type> void write (Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar('0' + x % 10); } typedef long long LL; const int N = 2e5 + 10, M = 20, mod = 1e9 + 7; bool vis[N]; int top, stk[N]; int n, d[N], A[N], dfn[N], w[N], r[N], s[N]; int cnt, p[N], mu[N], phi[N], f[N]; vector <int> h, e[N]; struct Edge { int v, w; }; vector <Edge> g[N]; int binpow (int b, int k = mod - 2) { int res = 1; for (; k; k >>= 1, b = (LL)b * b % mod) if (k & 1) res = (LL)res * b % mod; return res; } namespace LCA { int stmp, lg[N << 1], st[N << 1][M]; int dmin (int a, int b) { return d[a] < d[b] ? a : b; } void dfs (int u = 1) { st[dfn[u] = ++stmp][0] = u; for (int v : e[u]) if (!dfn[v]) { d[v] = d[u] + 1; dfs(v); st[++stmp][0] = u; } } int lca (int a, int b) { if ((a = dfn[a]) > (b = dfn[b])) swap(a, b); int k = lg[b - a + 1]; return dmin(st[a][k], st[b - (1 << k) + 1][k]); } void init () { dfs(); for (int i = 2; i <= stmp; ++i) lg[i] = lg[i >> 1] + 1; for (int k = 1; 1 << k <= stmp; ++k) for (int i = 1; i + (1 << k) - 1 <= stmp; ++i) st[i][k] = dmin(st[i][k - 1], st[i + (1 << k - 1)][k - 1]); } } void init () { vis[1] = true; mu[1] = 1; phi[1] = 1; for (int i = 2; i <= n; ++i) { if (!vis[i]) { p[++cnt] = i; mu[i] = -1; phi[i] = i - 1; } for (int j = 1; j <= cnt && i * p[j] <= n; ++j) { vis[i * p[j]] = true; if (i % p[j] == 0) { mu[i * p[j]] = 0; phi[i * p[j]] = phi[i] * p[j]; break; } mu[i * p[j]] = mu[i] * mu[p[j]]; phi[i * p[j]] = phi[i] * phi[p[j]]; } } for (int i = 1; i <= n; ++i) for (int j = 1; i * j <= n; ++j) (f[i * j] += (LL)i * mu[j] * binpow(phi[i]) % mod) %= mod; } void dfs1 (int u = 1) { r[u] = w[u], s[u] = 0; for (Edge i : g[u]) { dfs1(i.v); (r[u] += r[i.v]) %= mod; (s[u] += ((LL)r[i.v] * i.w + s[i.v]) % mod) %= mod; } } void dfs2 (int u = 1) { for (Edge i : g[u]) { s[i.v] = (s[u] + (r[1] - 2ll * r[i.v]) * i.w) % mod; dfs2(i.v); } } int calc () { sort(h.begin(), h.end(), [&](int a, int b) { return dfn[a] < dfn[b]; }); g[stk[top = 1] = 1].clear(); w[1] = h[0] == 1 ? phi[1] : 0; auto add = [&](int a, int b) { g[a].pb((Edge){b, d[b] - d[a]}); }; for (int i : h) if (i ^ 1) { int t = LCA::lca(i, stk[top]); if (t ^ stk[top]) { for (; top > 1 && dfn[t] < dfn[stk[top - 1]]; --top) add(stk[top - 1], stk[top]); if (dfn[t] > dfn[stk[top - 1]]) { g[t].clear(); w[t] = 0; add(t, stk[top]); stk[top] = t; } else add(stk[top - 1], stk[top]), --top; } g[stk[++top] = i].clear(); w[i] = phi[i]; } for (; top > 1; --top) add(stk[top - 1], stk[top]); dfs1(), dfs2(); int res = 0; for (int i : h) (res += (LL)phi[i] * s[i] % mod) %= mod; return res; } int main () { read(n); init(); for (int i = 1; i <= n; ++i) read(A[i]); for (int i = 1, a, b; i < n; ++i) { read(a, b), a = A[a], b = A[b]; e[a].pb(b), e[b].pb(a); } LCA::init(); int res = 0; for (int d = 1; d <= n; ++d) { h.clear(); for (int i = d; i <= n; i += d) h.pb(i); (res += (LL)calc() * f[d] % mod) %= mod; } write(((LL)res * binpow((LL)n * (n - 1) % mod) % mod + mod) % mod); return 0; }
|