[Codeforces 809E] Surprise me! 莫比乌斯反演+虚树

题目链接: http://codeforces.com/contest/809/problem/E

E. Surprise me! Tired of boring dates, Leha and Noora decided to play a game.
Leha found a tree with n vertices numbered from1 ton. We remind you that tree is an undirected graph without cycles. Each vertexv of a tree has a numberav written on it. Quite by accident it turned out that all values written on vertices are distinct and are natural numbers between1 andn.
The game goes in the following way. Noora chooses some vertexu of a tree uniformly at random and passes a move to Leha. Leha, in his turn, chooses (also uniformly at random) some vertexv from remaining vertices of a tree(v?≠?u). As you could guess there aren(n?-?1) variants of choosing vertices by players. After that players calculate the value of a functionf(u,?v)?=?φ(au·avd(u,?v) of the chosen vertices whereφ(x) is Euler's totient function andd(x,?y) is the shortest distance between verticesx andy in a tree.
Soon the game became boring for Noora, so Leha decided to defuse the situation and calculate expected value of functionf over all variants of choosing verticesu 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 numbersa1,?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 numbersx andy(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 modulo109?+?7.
Examples Input

3 1 2 3 1 2 2 3

Output
333333338

Input
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 suchi that1?≤?i?≤?n,andgcd(i,?n)?=?1, wheregcd(x,?y) is the greatest common divisor of numbersx andy.
There are 6 variants of choosing vertices by Leha and Noora in the first testcase:
  • u?=?1, v?=?2,f(1,?2)?=?φ(aa2)·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)?=?φ(aa3)·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)?=?φ(aa3)·d(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 is7·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! 莫比乌斯反演+虚树
文章图片
.




题意:给一棵树,N<=2*10^5,每个点有点权ai,且ai构成一个1-N的排列,
求值:1/(n*(n-1))*sigma(i=1...n,j=1...n,phi(ai*aj)*dis(i,j)) mod 10^9+7.
【[Codeforces 809E] Surprise me! 莫比乌斯反演+虚树】题解:前面的东西可以不管,算完之后乘一下逆元就可以了,
然后,暴力是O(n^2logn)的,虽然时限8s,相信我,你是过不掉的!
首先,那个phi(ai*aj)如果干不掉,就没有办法进行任何优化,
考虑phi的定义式:phi(i)=i*(p1-1)/p1*(p2-1)/p2*...*(pn-1)/pn.
对于phi(ai*aj),phi(ai)=ai*(p1-1)/p1*(p2-1)/p2*...*(pn-1)/pn,
phi(aj)=aj*(p1-1)/p1*.........*(pn-1)/pn.
两者相乘后,我们发现,gcd(ai,aj)部分的所有的(pi-1)/pi被乘了2次,
所以再把多算的算回来就行了,所以有公式:phi(ai*aj)=phi(ai)*phi(aj)*d/phi(d),d=gcd(ai,aj).
然后就变成了一个gcd相关的奇怪的东西,
于是老套路---->gcd提出来,然后莫比乌斯一下,
所求式子变成了sigma(d, d/phi(d)*sigma(i=1...n,j=1....n, phi(ai)*phi(aj)*dis(i,j)*[gcd(ai,aj)==d]))
然后g(d)=sigma(i=1...n,j=1...n,phi(ai)*phi(aj)*dis(i,j)*[gcd(i,j)==d]),
f(d)=sigma(i=1...n,j=1...n,phi(ai)*phi(aj)*dis(i,j)*[d|gcd(i,j)]),
则有g(d)=sigma(i=1..n/d,miu(i)*f(i*d)).
如果可以快速得到f,就可以O(nlogn)得到g数组,从而O(n)算出答案.
考虑f数组的计算:先O(n)对整棵树进行dfs得到dep[i]后,
对于f(i)的求解,只把满足i|ap的点p提出来,建立出虚树进行O(n)树形dp即可。
建立虚树:在计算f(1)...f(n)过程中,总共的有效点数量为O(nlogn),建虚树时再带一个log,复杂度是O(nlognlogn)的。
树形dp:对于每个点,统计其作为lca时的总贡献,
对于其不同子树内的两个点,贡献为dep[a[i]]+dep[a[j]]-2*dep[lca].
遍历子树,记录dep的和以及树的个数,从而可以O(虚树点数)计算出f(i)的值.
总复杂度:O(nlog^2n),或者更准确的说是O(nlnnlogn).
顺便说下为什么O(n^2logn)想都别想过---------->标算做法,如果常数过大,可能连pretest都过不了,更不要说暴力了 :)


Code:





    推荐阅读