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!
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.
In a single line print a number equal to P·Q - 1 modulo 109 + 7.
3
1 2 3
1 2
2 3
333333338
5
5 4 3 1 2
3 5
1 2
4 3
2 5
8
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·a2)·d(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·a3)·d(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·a3)·d(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$