[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 vertex u 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·av)·d(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. 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 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) = φ(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 is7·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.



题意:给一棵树,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.

题解:前面的东西可以不管,算完之后乘一下逆元就可以了,

            然后,暴力是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:</>



全部评论

相关推荐

想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务