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
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "bitset" #include "queue" using namespace std; #define MAXN 200006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) #define P 1000000007 typedef long long ll; int n , m; int A[MAXN];
int Pow( int x , int a ) { int ans = 1 , cur = x % P; while( a ) { if( a & 1 ) ans = 1ll * ans * cur % P; cur = 1ll * cur * cur % P , a >>= 1; } return ans; }
int pri[MAXN] , en , phi[MAXN] , mu[MAXN] , poi[MAXN]; void sieve() { phi[1] = mu[1] = 1; for( int i = 2 ; i < MAXN ; ++ i ) { if( !pri[i] ) pri[++ en] = i , phi[i] = i - 1 , mu[i] = -1; for( int j = 1 ; j <= en && i * pri[j] < MAXN ; ++ j ) { pri[i * pri[j]] = 1; 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]; } } }
vector<int> G[MAXN]; int g[MAXN][19] , dep[MAXN] , dfn[MAXN] , R[MAXN] , bac[MAXN] , clo; void dfs( int u , int fa ) { dfn[u] = ++ clo , bac[clo] = u; for( int v : G[u] ) if( v != fa ) { dep[v] = dep[u] + 1; g[v][0] = u; rep( k , 1 , 18 ) if( g[g[v][k-1]][k-1] ) g[v][k] = g[g[v][k-1]][k-1]; else break; dfs( v , u ); } R[u] = clo; } int lca( int u , int v ) { if( dep[u] < dep[v] ) swap( u , v ); if( dep[u] != dep[v] ) per( k , 18 , 0 ) if( dep[g[u][k]] >= dep[v] ) u = g[u][k]; if( u == v ) return u; per( k , 18 , 0 ) if( g[u][k] != g[v][k] ) u = g[u][k] , v = g[v][k]; return g[u][0]; }
vector<int> p;
namespace faketree { bool cmp(int a, int b) { return dfn[a] < dfn[b]; } vector<int> G[MAXN]; int n; int stk[MAXN] , tp , rt; void build() { sort(all(p) , cmp); rep(i, 1, p.size() - 1) p.pb(lca(p[i], p[i - 1])); sort(all(p) , cmp); n = unique( all( p ) ) - p.begin(); tp = 0; stk[0] = 0; rt = p[0]; rep( i , 0 , n - 1 ) { while( tp && R[stk[tp]] < dfn[p[i]] ) -- tp; G[stk[tp]].pb( p[i] ) , G[p[i]].pb( stk[tp] ); stk[++ tp] = p[i]; } } int cur , sz[MAXN] , re , vis[MAXN]; void dfs1( int u , int fa ) { sz[u] = vis[u] * phi[A[u]] , ( cur += ( vis[u] * 1ll * ( dep[u] - dep[rt] ) * phi[A[u]] ) % P ) %= P; for( int v : G[u] ) if( v != fa ) { dfs1( v , u ); ( sz[u] += sz[v] ) %= P; } } void dfs( int u , int fa ) { ( re += ( 1ll * vis[u] * cur * phi[A[u]] ) % P ) %= P; for( int v : G[u] ) if( v != fa ) { int del = 1ll * ( dep[v] - dep[u] ) * ( ( 1ll * sz[rt] - 2 * sz[v] ) % P + P ) % P; if( !u ) del = 0; ( cur += del ) %= P; dfs( v , u ); ( cur += P - del ) %= P; } } int workit( ) { for( int x : p ) vis[x] = 1; build( ); re = cur = 0; dep[0] = -1; dfs1( 0 , 0 ); dfs( 0 , 0 ); for( int x : p ) G[x].clear() , vis[x] = 0; G[0].clear(); return re; } }
vi fk[MAXN]; int f[MAXN];
void solve() { sieve(); cin >> n; rep( i , 1 , n ) scanf("%d",&A[i]) , fk[A[i]].pb( i ); int u , v; rep( i , 2 , n ) scanf("%d%d",&u,&v) , G[u].pb( v ) , G[v].pb( u ); dep[1] = 1; dfs( 1 , 1 ); rep( i , 1 , n ) { p.clear(); for (int j = i; j <= n; j += i) for (int t : fk[j]) p.pb(t); f[i] = faketree::workit(); }
rep( i , 1 , n ) for( int j = i + i ; j <= n ; j += i ) ( f[i] += f[j] * mu[j / i] < 0 ? f[j] * mu[j / i] + P : f[j] * mu[j / i] ) %= P;
rep( i , 1 , n ) poi[i] = 1; rep( i , 1 , en ) if( pri[i] <= n ) for( int j = pri[i] ; j <= n ; j += pri[i] ) poi[j] = 1ll * poi[j] * Pow( pri[i] - 1 , P - 2 ) % P , poi[j] = 1ll * poi[j] * pri[i] % P; int ans = 0; rep( i , 1 , n ) ( ans += 1ll * poi[i] * f[i] % P ) %= P; printf("%lld\n",1ll * ans * Pow( 1ll * n * ( n - 1 ) % P , P - 2 ) % P); }
signed main() {
solve(); }
|