codeforces 809E Surprise me!

时间:2022-08-13 22:28:43

Tired of boring dates, Leha and Noora decided to play a game.

Leha found a tree with n vertices numbered from 1 to n. We remind you that tree is an undirected graph without cycles. Each vertex v of a tree has a number av written on it. Quite by accident it turned out that all values written on vertices are distinct and are natural numbers between 1 and n.

The game goes in the following way. Noora chooses some vertex u of a tree uniformly at random and passes a move to Leha. Leha, in his turn, chooses (also uniformly at random) some vertex v from remaining vertices of a tree (v ≠ u). As you could guess there are n(n - 1) variants of choosing vertices by players. After that players calculate the value of a function f(u, v) = φ(au·av) · d(u, v) of the chosen vertices where φ(x) is Euler's totient function and d(x, y) is the shortest distance between vertices x and y in a tree.

Soon the game became boring for Noora, so Leha decided to defuse the situation and calculate expected value of function f over all variants of choosing vertices u and v, hoping of at least somehow surprise the girl.

Leha asks for your help in calculating this expected value. Let this value be representable in the form of an irreducible fraction codeforces 809E Surprise me!. To further surprise Noora, he wants to name her the value codeforces 809E Surprise me!.

Help Leha!

Input

The first line of input contains one integer number n (2 ≤ n ≤ 2·105)  — number of vertices in a tree.

The second line contains n different numbers a1, a2, ..., an (1 ≤ ai ≤ n) separated by spaces, denoting the values written on a tree vertices.

Each of the next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n), describing the next edge of a tree. It is guaranteed that this set of edges describes a tree.

Output

In a single line print a number equal to P·Q - 1 modulo 109 + 7.

Examples
Input
Copy
3
1 2 3
1 2
2 3
Output
333333338
Input
Copy
5
5 4 3 1 2
3 5
1 2
4 3
2 5
Output
8
Note

Euler's totient function φ(n) is the number of such i that 1 ≤ i ≤ n,and gcd(i, n) = 1, where gcd(x, y) is the greatest common divisor of numbers x and y.

There are 6 variants of choosing vertices by Leha and Noora in the first testcase:

  • u = 1, v = 2, f(1, 2) = φ(a1·a2d(1, 2) = φ(1·2)·1 = φ(2) = 1
  • u = 2, v = 1, f(2, 1) = f(1, 2) = 1
  • u = 1, v = 3, f(1, 3) = φ(a1·a3d(1, 3) = φ(1·3)·2 = 2φ(3) = 4
  • u = 3, v = 1, f(3, 1) = f(1, 3) = 4
  • u = 2, v = 3, f(2, 3) = φ(a2·a3d(2, 3) = φ(2·3)·1 = φ(6) = 2
  • u = 3, v = 2, f(3, 2) = f(2, 3) = 2

Expected value equals to codeforces 809E Surprise me!. The value Leha wants to name Noora is 7·3 - 1 = 7·333333336 = 333333338 codeforces 809E Surprise me!.

In the second testcase expected value equals to codeforces 809E Surprise me!, so Leha will have to surprise Hoora by number 8·1 - 1 = 8 codeforces 809E Surprise me!.

$\varphi(a_i*a_j)=\frac{\varphi(a_i)\varphi(a_j)gcd(a_i,a_j)}{\varphi(gcd(a_i,a_j))}$
$\sum_{d=1}^{n}\frac{d}{\varphi(d)}\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(a_i)\varphi(a_j)dist(i,j)[gcd(a_i,a_j)=d]$
$f(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(a_i)\varphi(a_j)dist(i,j)[gcd(a_i,a_j)=d]$
$g(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(a_i)\varphi(a_j)dist(i,j)[d|gcd(a_i,a_j)]$
$g(d)=\sum_{d|x}^{n}f(x)$
$f(d)=\sum_{d|x}^{n}\mu(\frac{x}{d})g(x)$
$f(d)=\sum_{i=1}^{\frac{n}{d}}\mu(i)g(id)$
$ans=\sum_{d=1}^{n}\frac{d}{\varphi(d)}\sum_{i=1}^{\frac{n}{d}}\mu(i)g(id)$
令$T=id$
$ans=\sum_{T=1}^{n}g(T)\sum_{d|T}\mu(\frac{T}{d})\frac{d}{\varphi(d)}$
令$h(T)=\sum_{d|T}\mu(\frac{T}{d})\frac{d}{\varphi(d)}$
对于g(d)有
$g(d)=\sum_{i=1且d|a_i}^{n}\sum_{j=1 且d|a_j}^{n}\varphi(a_i)\varphi(a_j)(dep_i+dep_j-2*dep_lca)$
$g(d)=\sum_{i=1且d|a_i}^{n}\varphi(a_i)*2*dep_i\sum_{j=1 且d|a_j}^{n}\varphi(a_j)-2*\sum_{i=1且d|a_i}^{n}\sum_{j=1 且d|a_j}^{n}\varphi(a_i)\varphi(a_j)*dep_lca$
$\sum_{i=1且d|a_i}^{n}\varphi(a_i)*2*dep_i\sum_{j=1且d|a_j}^{n}\varphi(a_j)=2*\sum_{i=1且d|a_i}^{n}\varphi(a_i)*dep_i*Sum$

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
struct Node
{
int next,to;
}edge[],edge2[];
int num,head[],head2[],mu[],phi[],vis[],inv[],Mod=1e9+;
int n,tot,prime[],h[],dep[],fa[][],dfn[],cnt,size[],st[];
int bin[],ed[],flag[],ans,a[],sum,f[],l[],b[],top,g[];
int id[];
bool cmp(int a,int b)
{
return dfn[a]<dfn[b];
}
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
void add2(int u,int v)
{
num++;
edge2[num].next=head2[u];
head2[u]=num;
edge2[num].to=v;
}
int qpow(int x,int y)
{
int res=;
while (y)
{
if (y&) res=1ll*res*x%Mod;
x=1ll*x*x%Mod;
y>>=;
}
return res;
}
void prework()
{int i,j;
mu[]=phi[]=;
inv[]=;
for (i=;i<=n;i++)
inv[i]=1ll*(Mod-Mod/i)*inv[Mod%i]%Mod;
for (i=;i<=n;i++)
{
if (vis[i]==)
{
++tot;
prime[tot]=i;
mu[i]=-;
phi[i]=i-;
}
for (j=;j<=tot;j++)
{
if (1ll*i*prime[j]>n) break;
vis[i*prime[j]]=;
if (i%prime[j]==)
{
phi[i*prime[j]]=1ll*phi[i]*prime[j];
break;
}
else
{
phi[i*prime[j]]=1ll*phi[i]*(prime[j]-);
mu[i*prime[j]]=-mu[i];
}
}
}
for (i=;i<=n;i++)
{
for (j=;j<=n&&1ll*i*j<=n;j++)
{
h[i*j]+=1ll*mu[j]*i%Mod*inv[phi[i]]%Mod;
h[i*j]%=Mod;
}
}
}
int lca(int x,int y)
{int i;
if (dep[x]<dep[y]) swap(x,y);
for (i=;i>=;i--)
if (dep[fa[x][i]]>=dep[y])
x=fa[x][i];
if (x==y) return x;
for (i=;i>=;i--)
{
if (fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
}
int get_dis(int x,int y)
{
return dep[x]+dep[y]-*dep[lca(x,y)];
}
void dfs(int x,int pa)
{int i;
dep[x]=dep[pa]+;
dfn[x]=++cnt;
size[x]=;
for (i=;bin[i]<=dep[x];i++)
fa[x][i]=fa[fa[x][i-]][i-];
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
fa[v][]=x;
dfs(v,x);
size[x]+=size[v];
}
ed[x]=cnt;
}
int DP(int x)
{
int s1=,s2=;
if (flag[x])
{
ans+=2ll*phi[a[x]]%Mod*sum%Mod*dep[x]%Mod;
ans%=Mod;
f[x]=phi[a[x]];
s1=1ll*f[x]*f[x]%Mod*dep[x]%Mod;
}
else f[x]=;
for (int i=head2[x];i;i=edge2[i].next)
{
int v=edge2[i].to;
DP(v);
s2=(s2+1ll*f[x]*f[v]%Mod*dep[x]%Mod)%Mod;
f[x]=(f[x]+f[v])%Mod;
}
ans=((ans-4ll*s2%Mod)%Mod-2ll*s1%Mod)%Mod;
ans=(ans+Mod)%Mod;
}
void solve(int x)
{int i,Lca;
int tot=;sum=;
for (i=x;i<=n;i+=x)
flag[l[++tot]=id[i]]=,b[tot]=l[tot],sum=(sum+phi[i])%Mod;
sort(l+,l+tot+,cmp);
Lca=l[];
for (i=;i<=tot;i++)
if (ed[l[i-]]<dfn[l[i]]) l[++tot]=lca(l[i],l[i-]),Lca=lca(Lca,l[i]);
l[++tot]=Lca;
sort(l+,l+tot+,cmp);
tot=unique(l+,l+tot+)-l-;
top=;num=;ans=;
st[++top]=Lca;
for (i=;i<=tot;i++)
{
while (top&&ed[st[top]]<dfn[l[i]]) top--;
add2(st[top],l[i]);
//cout<<x<<' '<<st[top]<<' '<<l[i]<<endl;
st[++top]=l[i];
}
DP(Lca);
g[x]=ans%Mod;
for (i=;i<=tot;i++) flag[l[i]]=,head2[l[i]]=;
}
int main()
{int i,j,u,v;
cin>>n;
bin[]=;
for (i=;i<=;i++)
bin[i]=bin[i-]*;
prework();
for (i=;i<=n;i++)
{
scanf("%d",&a[i]);
id[a[i]]=i;
}
for (i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs(,);
for (i=;i<=n;i++)
solve(i);
ans=;
for (i=;i<=n;i++)
ans=(ans+1ll*g[i]*h[i]%Mod)%Mod;
ans=1ll*ans*qpow(n-,Mod-)%Mod*qpow(n,Mod-)%Mod;
cout<<(ans+Mod)%Mod;
}

$\varphi(a_i*a_j)=\frac{\varphi(a_i)\varphi(a_j)gcd(a_i,a_j)}{\varphi(gcd(a_i,a_j))}$
$\sum_{d=1}^{n}\frac{d}{\varphi(d)}\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(a_i)\varphi(a_j)dist(i,j)[gcd(a_i,a_j)=d]$
$f(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(a_i)\varphi(a_j)dist(i,j)[gcd(a_i,a_j)=d]$
$g(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(a_i)\varphi(a_j)dist(i,j)[d|gcd(a_i,a_j)]$
$g(d)=\sum_{d|x}^{n}f(x)$
$f(d)=\sum_{d|x}^{n}\mu(\frac{x}{d})g(x)$
$f(d)=\sum_{i=1}^{\frac{n}{d}}\mu(i)g(id)$
$ans=\sum_{d=1}^{n}\frac{d}{\varphi(d)}\sum_{i=1}^{\frac{n}{d}}\mu(i)g(id)$
令$T=id$
$ans=\sum_{T=1}^{n}g(T)\sum_{d|T}\mu(\frac{T}{d})\frac{d}{\varphi(d)}$
令$h(T)=\sum_{d|T}\mu(\frac{T}{d})\frac{d}{\varphi(d)}$
对于g(d)有
$g(d)=\sum_{i=1且d|a_i}^{n}\sum_{j=1 且d|a_j}^{n}\varphi(a_i)\varphi(a_j)(dep_i+dep_j-2*dep_lca)$
$g(d)=\sum_{i=1且d|a_i}^{n}\varphi(a_i)*2*dep_i\sum_{j=1 且d|a_j}^{n}\varphi(a_j)-2*\sum_{i=1且d|a_i}^{n}\sum_{j=1 且d|a_j}^{n}\varphi(a_i)\varphi(a_j)*dep_lca$
$\sum_{i=1且d|a_i}^{n}\varphi(a_i)*2*dep_i\sum_{j=1且d|a_j}^{n}\varphi(a_j)=2*\sum_{i=1且d|a_i}^{n}\varphi(a_i)*dep_i*Sum$