codeforces 809E Surprise me!

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 . To further surprise Noora, he wants to name her the value .

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 . The value Leha wants to name Noora is 7·3 - 1 = 7·333333336 = 333333338 .

In the second testcase expected value equals to , so Leha will have to surprise Hoora by number 8·1 - 1 = 8 .

$\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$

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 using namespace std;
  7 typedef long long lol;
  8 struct Node
  9 {
 10   int next,to;
 11 }edge[400001],edge2[400001];
 12 int num,head[200001],head2[200001],mu[200001],phi[200001],vis[200001],inv[200001],Mod=1e9+7;
 13 int n,tot,prime[200001],h[200001],dep[200001],fa[200001][21],dfn[200001],cnt,size[200001],st[400001];
 14 int bin[21],ed[200001],flag[200001],ans,a[200001],sum,f[200001],l[400001],b[400001],top,g[200001];
 15 int id[200001];
 16 bool cmp(int a,int b)
 17 {
 18   return dfn[a]<dfn[b];
 19 }
 20 void add(int u,int v)
 21 {
 22   num++;
 23   edge[num].next=head[u];
 24   head[u]=num;
 25   edge[num].to=v;
 26 }
 27 void add2(int u,int v)
 28 {
 29   num++;
 30   edge2[num].next=head2[u];
 31   head2[u]=num;
 32   edge2[num].to=v;
 33 }
 34 int qpow(int x,int y)
 35 {
 36   int res=1;
 37   while (y)
 38     {
 39       if (y&1) res=1ll*res*x%Mod;
 40       x=1ll*x*x%Mod;
 41       y>>=1;
 42     }
 43   return res;
 44 }
 45 void prework()
 46 {int i,j;
 47   mu[1]=phi[1]=1;
 48   inv[1]=1;
 49   for (i=2;i<=n;i++)
 50     inv[i]=1ll*(Mod-Mod/i)*inv[Mod%i]%Mod;
 51   for (i=2;i<=n;i++)
 52     {
 53       if (vis[i]==0)
 54     {
 55       ++tot;
 56       prime[tot]=i;
 57       mu[i]=-1;
 58       phi[i]=i-1;
 59     }
 60       for (j=1;j<=tot;j++)
 61     {
 62       if (1ll*i*prime[j]>n) break;
 63       vis[i*prime[j]]=1;
 64       if (i%prime[j]==0)
 65         {
 66           phi[i*prime[j]]=1ll*phi[i]*prime[j];
 67           break;
 68         }
 69       else
 70         {
 71           phi[i*prime[j]]=1ll*phi[i]*(prime[j]-1);
 72           mu[i*prime[j]]=-mu[i];
 73         }
 74     }
 75     }
 76   for (i=1;i<=n;i++)
 77     {
 78       for (j=1;j<=n&&1ll*i*j<=n;j++)
 79     {
 80       h[i*j]+=1ll*mu[j]*i%Mod*inv[phi[i]]%Mod;
 81       h[i*j]%=Mod;
 82     }
 83     }
 84 }
 85 int lca(int x,int y)
 86 {int i;
 87   if (dep[x]<dep[y]) swap(x,y);
 88   for (i=20;i>=0;i--)
 89     if (dep[fa[x][i]]>=dep[y])
 90       x=fa[x][i];
 91   if (x==y) return x;
 92   for (i=20;i>=0;i--)
 93     {
 94       if (fa[x][i]!=fa[y][i])
 95     {
 96       x=fa[x][i];
 97       y=fa[y][i];
 98     }
 99     }
100   return fa[x][0];
101 }
102 int get_dis(int x,int y)
103 {
104   return dep[x]+dep[y]-2*dep[lca(x,y)];
105 }
106 void dfs(int x,int pa)
107 {int i;
108   dep[x]=dep[pa]+1;
109   dfn[x]=++cnt;
110   size[x]=1;
111   for (i=1;bin[i]<=dep[x];i++)
112     fa[x][i]=fa[fa[x][i-1]][i-1];
113   for (i=head[x];i;i=edge[i].next)
114     {
115       int v=edge[i].to;
116       if (v==pa) continue;
117       fa[v][0]=x;
118       dfs(v,x);
119       size[x]+=size[v];
120     }
121   ed[x]=cnt;
122 }
123 int DP(int x)
124 {
125   int s1=0,s2=0;
126   if (flag[x])
127     {
128       ans+=2ll*phi[a[x]]%Mod*sum%Mod*dep[x]%Mod;
129       ans%=Mod;
130       f[x]=phi[a[x]];
131       s1=1ll*f[x]*f[x]%Mod*dep[x]%Mod;
132     }
133   else f[x]=0;
134   for (int i=head2[x];i;i=edge2[i].next)
135     {
136       int v=edge2[i].to;
137       DP(v);
138       s2=(s2+1ll*f[x]*f[v]%Mod*dep[x]%Mod)%Mod;
139       f[x]=(f[x]+f[v])%Mod;
140     }
141   ans=((ans-4ll*s2%Mod)%Mod-2ll*s1%Mod)%Mod;
142   ans=(ans+Mod)%Mod;
143 }
144 void solve(int x)
145 {int i,Lca;
146   int tot=0;sum=0;
147   for (i=x;i<=n;i+=x)
148     flag[l[++tot]=id[i]]=1,b[tot]=l[tot],sum=(sum+phi[i])%Mod;
149   sort(l+1,l+tot+1,cmp);
150   Lca=l[1];
151   for (i=2;i<=tot;i++)
152     if (ed[l[i-1]]<dfn[l[i]]) l[++tot]=lca(l[i],l[i-1]),Lca=lca(Lca,l[i]);
153   l[++tot]=Lca;
154   sort(l+1,l+tot+1,cmp);
155   tot=unique(l+1,l+tot+1)-l-1;
156   top=0;num=0;ans=0;
157   st[++top]=Lca;
158   for (i=2;i<=tot;i++)
159     {
160       while (top&&ed[st[top]]<dfn[l[i]]) top--;
161       add2(st[top],l[i]);
162       //cout<<x<<' '<<st[top]<<' '<<l[i]<<endl;
163       st[++top]=l[i];
164     }
165   DP(Lca);
166   g[x]=ans%Mod;
167   for (i=1;i<=tot;i++) flag[l[i]]=0,head2[l[i]]=0;
168 }
169 int main()
170 {int i,j,u,v;
171   cin>>n;
172   bin[0]=1;
173   for (i=1;i<=20;i++)
174     bin[i]=bin[i-1]*2;
175   prework();
176   for (i=1;i<=n;i++)
177     {
178       scanf("%d",&a[i]);
179       id[a[i]]=i;
180     }
181   for (i=1;i<=n-1;i++)
182     {
183       scanf("%d%d",&u,&v);
184       add(u,v);add(v,u);
185     }
186   dfs(1,0);
187   for (i=1;i<=n;i++)
188     solve(i);
189   ans=0;
190   for (i=1;i<=n;i++)
191     ans=(ans+1ll*g[i]*h[i]%Mod)%Mod;
192   ans=1ll*ans*qpow(n-1,Mod-2)%Mod*qpow(n,Mod-2)%Mod;
193   cout<<(ans+Mod)%Mod;
194 }

 

$\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$

转载于:https://www.cnblogs.com/Y-E-T-I/p/8669838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值