题意:给出一棵点带权的树,求i$\in$[1,200000]所有路径的上点权的gcd==i的个数。

考虑点分治,对于一棵以u为根的子树,如何统计经过u的路径的答案?

显然既然是经过点u的路径,那么所有经过u的路径上的点权的gcd肯定是点u的点权的约数。

暴力算下,2e5以内最多只有160个约数。

然后dfs出u子树里所有点到u路径的gcd,然后用个桶,最多\(u的点权的约数个数^2\)数下数就行了,但是实际应该是远远不满的。

最慢的一个点1404ms,4.5s的时限应该没什么问题。

然而这题的标签里有个dp(滑稽

//by zykykyk
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
#define ll long long
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define cross(i,k) for (register int i=first[k];i;i=last[i])
inline ll read(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
if (ch=='-'){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int N = 2e5+10;
int n,x,y,Size,rt,a[N];
ll ans[N];
int tot,first[N],last[N<<1],to[N<<1];
inline void Add(int x,int y){to[++tot]=y,last[tot]=first[x],first[x]=tot;}
int size[N],Max[N];
bool vis[N];
inline void GetRoot(int u,int fa){
Max[u]=0,size[u]=1;
cross(i,u) if (to[i]!=fa&&!vis[to[i]]) GetRoot(to[i],u),size[u]+=size[to[i]],Max[u]=max(Max[u],size[to[i]]);
Max[u]=max(Max[u],Size-size[u]);
if (Max[rt]>Max[u]) rt=u;
}
int cnt,g[N];
ll b[N];
inline int gcd(int a,int b){return !b?a:gcd(b,a%b);}
inline void dfs(int u,int fa,int Gcd,int x,int rt){
if (x!=1||x==1&&u!=rt) g[++cnt]=Gcd,b[Gcd]++;
cross(i,u) if (to[i]!=fa&&!vis[to[i]]) dfs(to[i],u,gcd(Gcd,a[to[i]]),x,rt);
}
inline void solve(int u){
cnt=0,dfs(u,u,a[u],1,u),vis[u]=1;
sort(g+1,g+1+cnt);
int tot=unique(g+1,g+1+cnt)-g-1;
For(i,1,tot){
ans[g[i]]+=b[g[i]]+b[g[i]]*(b[g[i]]-1)/2;
For(j,i+1,tot) ans[gcd(g[i],g[j])]+=b[g[i]]*b[g[j]];
}
For(i,1,tot) b[g[i]]=0;
cross(k,u)
if (!vis[to[k]]){
cnt=0,dfs(to[k],u,gcd(a[u],a[to[k]]),0,to[k]);
sort(g+1,g+1+cnt);
int tot=unique(g+1,g+1+cnt)-g-1;
For(i,1,tot){
ans[g[i]]-=b[g[i]]*(b[g[i]]-1)/2;
For(j,i+1,tot) ans[gcd(g[i],g[j])]-=b[g[i]]*b[g[j]];
}
For(i,1,tot) b[g[i]]=0;
}
cross(i,u) if (!vis[to[i]]) Size=size[to[i]],rt=0,GetRoot(to[i],u),solve(rt);
}
int main(){
n=read();
For(i,1,n) a[i]=read(),ans[a[i]]++;
For(i,1,n-1) x=read(),y=read(),Add(x,y),Add(y,x);
Size=n,Max[0]=1e9,GetRoot(1,1),solve(rt);
For(i,1,N-10) if (ans[i]) printf("%d %lld\n",i,ans[i]);
}

Codeforces 990G 点分治+暴力的更多相关文章

  1. CodeForces - 990G (点分治+链表计数)

    题目:https://vjudge.net/contest/307753#problem/J 题意:一棵树,每个点都有个权值,现在问你,树上gcd每个不同的数有多少个 思路:点分治,首先范围只有 1e ...

  2. Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

    D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...

  3. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

  4. codeforces 897A Scarborough Fair 暴力签到

    codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...

  5. 【BZOJ 4059】 (分治暴力|扫描线+线段树)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 632  Solved: 22 ...

  6. Codeforces A. Playlist(暴力剪枝)

    题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces 1101D 点分治

    题意:有一颗树,每个点有一个点权,边权都是1,问路径上的所有点的gcd不是1的最长路径是多少? 思路:之前补这道题的时候,用分解质因数 + 树形DP做的,其实用点分治可以更暴力一点.对于这类统计树上路 ...

  8. Codeforces 293E 点分治+cdq

    Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...

  9. codeforces 161D 点分治

    传送门:https://codeforces.com/problemset/problem/161/D 题意: 求树上点对距离恰好为k的点对个数 题解: 与poj1741相似 把点分治的模板改一下即可 ...

随机推荐

  1. Use JPath but not recursively loop a JObject to modify the values.

    I am dealing with a Json file, I parsed it into jObject, I have another list which flattened the pro ...

  2. Redis数据类型之列表(list)

    1. 什么是列表 redis的列表使用双向链表实现,往列表中放元素的时候复杂度是O(1),但是随机访问的时候速度就不行了,因为需要先遍历到指定的位置才可以取到元素. 既然列表是使用链表实现的,那么就说 ...

  3. zepto.js 实现原理解析

    zepto 是移动端常用的 dom 库,代码轻巧,操作方式类同 jquery.那么 zepto 的核心实现原理是什么呢?

  4. B - GuGuFishtion(莫比乌斯 欧拉函数 预处理mu函数的欧拉函数的模板)

    题目链接:https://cn.vjudge.net/contest/270608#problem/B 题目大意:题目中说,就是对欧拉函数的重新定义的一种函数的求和. 证明方法: AC代码: #inc ...

  5. Tinyos 第三版Make系统

    1.make系统安装 cd tools ./Bootstrap ./configure make sudo make install 2.make系统结构 3.第三版Makerules文件部分解析 # ...

  6. bootstrap带图标的按钮与图标做连接

    bootstrap通过引入bootstrap的JS与css文件,给元素添加class属性即可. 使用图标只需要加入一个span,class属性设置为对应的图标属性即可.图标对应的class属性可以参考 ...

  7. Ubuntu16.04安装记

    Ubuntu16.04安装记 基本信息: 华硕笔记本 Windows 10 家庭版 处理器:Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2.71GHz 已安装的内 ...

  8. css 水平、垂直居中

    水平居中 行内元素 行内元素:(img.span.文字等行内元素),通过在父级元素设置 text-align:center 使元素水平居中. 块级元素 块级元素:(div.p.h1...h6.ul.l ...

  9. shell中的变量与eval(转)

    原文链接:http://www.361way.com/shell-eval-variable/4957.html shell 中经常会用到变量的嵌套的情况.比如,单个或多个变量的值作为变量名,再对该变 ...

  10. 制作一棵ztree

    我们在做web项目时,常会用到一些树形菜单.在此,我们利用ztree实现树形菜单的效果.zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTr ...