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$
#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$
codeforces 809E Surprise me!的更多相关文章
- Codeforces 809E Surprise me! [莫比乌斯反演]
洛谷 Codeforces 非常套路的一道题,很适合我在陷入低谷时提升信心-- 思路 显然我们需要大力推式子. 设\(p_{a_i}=i\),则有 \[ \begin{align*} n(n-1)an ...
- Codeforces 809E - Surprise me!(虚树+莫比乌斯反演)
Codeforces 题目传送门 & 洛谷题目传送门 1A,就 nm 爽( 首先此题一个很棘手的地方在于贡献的计算式中涉及 \(\varphi(a_ia_j)\),而这东西与 \(i,j\) ...
- Codeforces.809E.Surprise me!(莫比乌斯反演 虚树)
题目链接 \(Description\) 给定一棵树,求\[\frac{1}{n(n-1)/2}\times\sum_{i\in[1,n],j\in[1,n],i\neq j}\varphi(a_i\ ...
- 【Codeforces 809E】Surprise me!(莫比乌斯反演 & 虚树)
Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 的权值为 \(a_i\).求: \[\frac{1}{n(n-1)}\sum_{i=1}^n\sum_{j=1}^n\var ...
- Codeforces Round #415 (Div. 1) (CDE)
1. CF 809C Find a car 大意: 给定一个$1e9\times 1e9$的矩阵$a$, $a_{i,j}$为它正上方和正左方未出现过的最小数, 每个询问求一个矩形内的和. 可以发现$ ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- Codeforces Beta Round #51 B. Smallest number dfs
B. Smallest number Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/pro ...
- 【CF809E】Surprise me!(动态规划,虚树,莫比乌斯反演)
[CF809E]Surprise me!(动态规划,虚树,莫比乌斯反演) 题面 洛谷 CodeForces 翻译: 给定一棵\(n\)个节点的树,每个点有一个权值\(a[i]\),保证\(a[i]\) ...
- Codeforces 915 E Physical Education Lessons
题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...
随机推荐
- 【RabbitMQ系列】 Spring mvc整合RabbitMQ
一.linux下安装rabbitmq 1.安装erlang环境 wget http://erlang.org/download/otp_src_18.2.1.tar.gz tar xvfz otp_s ...
- 掌握SQLServer锁的相关概念
一.为什么要引入锁 当多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: ◆丢失更新 A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 ◆脏读 ...
- 201621123031 《Java程序设计》第3周学习总结
Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系 ...
- 使用XIB实现嵌套自定义视图
在进行iOS开发的过程中,对于一些复杂的界面,我们可以通过Interface Builder这个Xcode集成的可视化界面编辑工具在完成,这回节省大部分时间以及代码量.它的使用方法这里不做介绍了,这次 ...
- "一不小心就火了"团队采访
团队采访 一. 采访团队 团队:一不小心就火了 采访形式:线上问答 二.采访内容 你们是怎么合理地具体分配组员里的工作的?有些团队会出现个别组员代码任务很重,个别组员无所事事的情况,你们有什么有效的方 ...
- prop attr 到底哪里不一样?
好吧 首先承认错误 说好的每天进行一次只是总结 但是我没坚持住 准确的来说 我并没有每天会学到了东西 但是 我一直在持续努力着 以后应该不会每天都写 但是自己觉得有用的 或者想加强记忆的 可 ...
- 用nodejs 开发的智能提示
用nodejs 开发的智能提示 时间:2014-07-01 03:50:18 类别:搜索引擎 访问: 2576 次 感谢:http://lutaf.com/223.htm 智能提示对于搜索非常重要,相 ...
- ZendStudio的使用技巧
为了使得ZendStudio支持volt模版可以在首选项中的ContentType加上.volt就行 在ZendStudio中的->help中有一个installNewssoftWare,然后会 ...
- babel基本用法
babel-cli babel-cli是本地使用编译js文件 1.安装: cnpm i babel-cli babel-preset-env -D 2.配置packjson: "script ...
- (转载) ASP.NET(C#) Web Api 通过文件流下载文件到本地实例
下载文件到本地是很多项目开发中需要实现的一个很简单的功能.说简单,是从具体的代码实现上来说的,.NET的文件下载方式有很多种,本示例给大家介绍的是ASP.NET Web Api方式返回HttpResp ...