You are given a tree consisting of nn vertices. A number is written on each vertex; the number on vertex ii is equal to aiai.

Let's denote the function g(x,y)g(x,y) as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex xx to vertex yy(including these two vertices).

For every integer from 11 to 2⋅1052⋅105 you have to count the number of pairs (x,y)(x,y) (1≤x≤y≤n)(1≤x≤y≤n) such that g(x,y)g(x,y) is equal to this number.

Input

The first line contains one integer nn — the number of vertices (1≤n≤2⋅105)(1≤n≤2⋅105).

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤2⋅105)(1≤ai≤2⋅105) — the numbers written on vertices.

Then n−1n−1 lines follow, each containing two integers xx and yy (1≤x,y≤n,x≠y)(1≤x,y≤n,x≠y)denoting an edge connecting vertex xx with vertex yy. It is guaranteed that these edges form a tree.

Output

For every integer ii from 11 to 2⋅1052⋅105 do the following: if there is no pair (x,y)(x,y) such that x≤yx≤y and g(x,y)=ig(x,y)=i, don't output anything. Otherwise output two integers: iiand the number of aforementioned pairs. You have to consider the values of ii in ascending order.

See the examples for better understanding.

Examples

Input
3
1 2 3
1 2
2 3
Output
1 4
2 1
3 1
Input
6
1 2 4 8 16 32
1 6
6 3
3 4
4 2
6 5
Output
1 6
2 5
4 6
8 1
16 2
32 1
Input
4
9 16 144 6
1 3
2 3
4 3
Output
1 1
2 1
3 1
6 2
9 2
16 2
144 1

题意:求所有简单路径的GCD,统计数量。

思路:不难想到是分治,问题转化为多个小问题:统计经过某点的路径的GCD,由于GCD具有收敛性,不同GCD的数量级是log级别的,虽然有多个链,但感觉gcd是数量就算不会太多,2333,我猜复杂度不超过O(N*logN*logN*logN)级别吧。所以对于当前子树,每次访问一条链的时候统计这条链和之前所有GCD的gcd。。。。说不清楚,反正一想就会相通的东西。

(具有收敛性的有:GCD,或,且...)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
const int inf=0x7FFFFFFF;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cnt,N,sn;
int a[maxn],sz[maxn],son[maxn],vis[maxn],root; ll ans[maxn];
map<int,int>mp,tp;
map<int,int>::iterator it1,it2;
inline void read(int &x) {
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c<=''&&c>='') x=(x<<)+(x<<)+c-'',c=getchar();
}
void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
void getroot(int u,int fa) //找重心
{
sz[u]=; son[u]=;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=fa&&!vis[To[i]]){
getroot(To[i],u);
sz[u]+=sz[To[i]];
son[u]=max(son[u],sz[To[i]]);
}
}
son[u]=max(son[u],sn-son[u]);
if(root==||son[root]>son[u]) root=u;
}
void getans(int u,int fa,int num) //对于当前链产生的新GCD
{
tp[num]++;
for(int i=Laxt[u];i;i=Next[i]){
if(!vis[To[i]]&&To[i]!=fa){
getans(To[i],u,__gcd(num,a[To[i]]));
}
}
}
void solve(int u) //解决以u为根的子问题
{
mp.clear(); mp[a[u]]++; ans[a[u]]++;
for(int i=Laxt[u];i;i=Next[i])
if(!vis[To[i]]) {
tp.clear(); getans(To[i],u,__gcd(a[u],a[To[i]]));
for(it1=mp.begin();it1!=mp.end();it1++)
for(it2=tp.begin();it2!=tp.end();it2++){
int g=__gcd((*it1).first,(*it2).first);
ans[g]+=(ll)(*it1).second*(*it2).second;
}
for(it2=tp.begin();it2!=tp.end();it2++)
mp[(*it2).first]+=(*it2).second;
}
}
void dfs(int u) //分治
{
vis[u]=; solve(u);
for(int i=Laxt[u];i;i=Next[i]){
if(vis[To[i]]) continue;
root=; sn=sz[To[i]];
getroot(To[i],); dfs(root);
}
}
int main()
{
read(N); int u,v,Max=;
for(int i=;i<=N;i++) read(a[i]),Max=max(Max,a[i]);
for(int i=;i<N;i++) {
read(u);read(v);
add(u,v); add(v,u);
}
root=; sn=N; getroot(,); dfs(root);
for(int i=;i<=Max;i++) if(ans[i]) printf("%d %I64d\n",i,ans[i]);
return ;
}

CodeForces990G:GCD Counting(树分治+GCD)的更多相关文章

  1. CF1101D GCD Counting 点分治+质因数分解

    题意:求最长的树上路径点值的 $gcd$ 不为 $1$ 的长度. 由于只要求 $gcd$ 不为一,所以只要 $gcd$ 是一个大于等于 $2$ 的质数的倍数就可以了. 而我们发现 $2\times 1 ...

  2. CF990G GCD Counting 点分治+容斥+暴力

    只想出来 $O(nlogn\times 160)$ 的复杂度,没想到还能过~ Code: #include <cstdio> #include <vector> #includ ...

  3. CF1101D GCD Counting

    题目地址:CF1101D GCD Counting zz的我比赛时以为是树剖或者点分治然后果断放弃了 这道题不能顺着做,而应该从答案入手反着想 由于一个数的质因子实在太少了,因此首先找到每个点的点权的 ...

  4. CF EDU 1101D GCD Counting 树形DP + 质因子分解

    CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...

  5. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  6. Ultimate Weirdness of an Array CodeForces - 671C (gcd,线段树)

    大意: 定义一个数列的特征值为两个数gcd的最大值, $f(l,r)$表示数列删除区间$[l,r]$的元素后剩余元素的特征值, 求$\sum_{i=1}^n\sum_{j=i}^n{f(i,j)}$ ...

  7. Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting

    G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE.....  需要每次清空一下子树的map... #inc ...

  8. HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)

    树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/6 ...

  9. D - 小Z的加油店 线段树+差分+GCD

    D - 小Z的加油店 HYSBZ - 5028   这个题目是一个线段树+差分+GCD 推荐一个差分的博客:https://www.cnblogs.com/cjoierljl/p/8728110.ht ...

随机推荐

  1. 【android】getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()区别详解

    在自定义控件中使用自定义属性时,经常需要使用java代码获取在xml中定义的尺寸,相关有以下三个函数 getDimension() getDimensionPixelOffset() getDimen ...

  2. linux删除空行操作:awk、grep、tr、sed

    如下:如何删除空行 shen\nshen\\n sen seh sehe she she 真正删除空行,可以使用vim: 通过命令模式删除空行.vim在命令模式下(在vim里输入英文字符:进入命令模式 ...

  3. U-net图像分割

    [Keras]基于SegNet和U-Net的遥感图像语义分割 2014 年,加州大学伯克利分校的 Long 等人提出全卷积网络(FCN),这使得卷积神经网络无需全连接层即可进行密集的像素预测,CNN ...

  4. 使用 sftp 向linux服务器传输文件

    sftp是加密的文件传输. 登陆 sftp name@123.21.331.1 1 2.把本地文件name1传到服务器name2下 put /name1.html /name2/ 1 把服务器name ...

  5. myeclipse2014破解

    去年出现的struts2 bug问题,在过去的项目中一直没做调整,前段时间发现受到影响了.本想这个bug都已经这么长时间了,工具中的包应该也已经被替换了吧,于是下载了最新的myeclipse2014, ...

  6. hadoop集群搭建datenode为0问题的解决

       搭建了一个小的实验集群,一共4台机器,一台namenode,三台datenode.运行start-all,发如今namenode上没有报不论什么错误,可是启动后直接显示datenode数量为0. ...

  7. Fckeditor常见漏洞的挖掘与利用整理汇总

    查看编辑器版本号 FCKeditor/_whatsnew.html ------------------------------------------------------------- 2. V ...

  8. freescale-sdk linux移植一搭建编译环境脚本host-prepare.sh分析

    接下来使用自己的课外歇息时间,对基于PowerPC架构freescale-sdk,进行linux移植和分析.主要參考官方文档freescale linux sdk START_HERE.html,首先 ...

  9. 【机器学习算法-python实现】PCA 主成分分析、降维

    1.背景         PCA(Principal Component Analysis),PAC的作用主要是减少数据集的维度,然后挑选出基本的特征.         PCA的主要思想是移动坐标轴, ...

  10. OcelotAPI 简单使用—服务发现、流控

    我这人比较懒 直接上配置文件的图 其中serviceName是服务名称, LoadBalancer是负载均衡策略. 对于流控我为了做测试写的1s 限制5次请求. 剩下的看名字就OK了. 要使用服务发现 ...