CF1101D GCD Counting
题目地址:CF1101D GCD Counting
zz的我比赛时以为是树剖或者点分治然后果断放弃了
这道题不能顺着做,而应该从答案入手反着想
由于一个数的质因子实在太少了,因此首先找到每个点的点权的所有质因子
进行一次树形dp,每次更新暴力枚举所有质因子即可
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 200006;
int n, ans = 1;
vector<int> p[N], c[N], e[N];
bool flag = 1, v[N];
inline void update(int x, int y) {
for (unsigned int i = 0; i < p[x].size(); i++)
for (unsigned int j = 0; j < p[y].size(); j++)
if (p[x][i] == p[y][j]) {
ans = max(ans, c[x][i] + c[y][j]);
c[x][i] = max(c[x][i], c[y][j] + 1);
}
}
void dp(int x) {
v[x] = 1;
for (unsigned int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if (v[y]) continue;
dp(y);
update(x, y);
}
}
inline void divide(int x, int t) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0) {
p[t].push_back(i);
c[t].push_back(1);
while (x % i == 0) x /= i;
}
if (x > 1) {
p[t].push_back(x);
c[t].push_back(1);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
divide(x, i);
if (x != 1) flag = 0;
}
if (flag) {
puts("0");
return 0;
}
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dp(1);
cout << ans << endl;
return 0;
}
CF1101D GCD Counting的更多相关文章
- CF1101D GCD Counting(数学,树的直径)
几个月的坑终于补了…… 题目链接:CF原网 洛谷 题目大意:一棵 $n$ 个点的树,每个点有点权 $a_i$.一条路径的长度定义为该路径经过的点数.一条路径的权值定义为该路径经过所有点的点权的 GC ...
- CF1101D GCD Counting 点分治+质因数分解
题意:求最长的树上路径点值的 $gcd$ 不为 $1$ 的长度. 由于只要求 $gcd$ 不为一,所以只要 $gcd$ 是一个大于等于 $2$ 的质数的倍数就可以了. 而我们发现 $2\times 1 ...
- Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting
G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE..... 需要每次清空一下子树的map... #inc ...
- CF EDU 1101D GCD Counting 树形DP + 质因子分解
CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...
- CodeForces - 1101D:GCD Counting (树分治)
You are given a tree consisting of n vertices. A number is written on each vertex; the number on ver ...
- D. GCD Counting(树上dp)
题目链接:http://codeforces.com/contest/1101/problem/D 题目大意:给你n个点,每个点都有权值,然后给你边的关系,问你树上的最大距离.(这里的最大距离指的是这 ...
- CodeForces990G:GCD Counting(树分治+GCD)
You are given a tree consisting of nn vertices. A number is written on each vertex; the number on ve ...
- GCD Counting Codeforces - 990G
https://www.luogu.org/problemnew/show/CF990G 耶,又一道好题被我浪费掉了,不会做.. 显然可以反演,在这之前只需对于每个i,统计出有多少(x,y),满足x到 ...
- codeforces1101D GCD Counting 【树形DP】
题目分析: 蛮简单的一道题,对于每个数拆质因子,对于每个质因子找出最长链,在每个地方枚举一下拼接 代码: #include<bits/stdc++.h> using namespace s ...
随机推荐
- 应用实战:从Redis到Aerospike,我们踩了这些坑
个推专注为开发者们提供消息推送服务多年.通过个推SDK,手机终端与服务器建立长连接,维持在线状态.然而在网络异常等情况下,消息无法实时送达到终端用户,因而推送服务器建立了一份离线消息列表,以待用户重新 ...
- DB2常用命令整理
1.基本命令 查看命令选项list command options信息帮助SQL statement例子:statement =30081SQL30081启动当前的DB2数据库管理实例db2start ...
- 我们数学中常用的自然常数e代表什么?看完长知识了!
我们在学习期间都接触过自然常数e,也知道e ≍ 2.718,学过极限的同学应该也知道 那么大家知道e的含义是什么吗?为啥叫“自然常数”? e的含义可以用一个计算利息的例子来解释. 假如你有1块钱,银行 ...
- SpringCloud之注册中心Eureka搭建
POM: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- Win7下mysql的安装
一.简述 mysql与oracle相比小,便宜,装机量大,下载地址:https://www.mysql.com/downloads/,去找Community Edition,然后根据自己的Window ...
- Linux安装npm并打包前端代码
查看node版本$ node -v查看npm版本$ npm -v如果没有安装node及npm,需要先安装node及npm#yum install node# yum install npm安装cnpm ...
- img标签的onerror事件
#情景分析: 有时,img标签中的src图片加载失败,原来的图片位置会出现一个碎片图标,这样让人很不爽,如何变得美观些呢? #解决方案: 可以借用img标签的onerror事件,img标签支持oner ...
- linux chmod命令使用
chmod:更改文件9个属性 Linux文件属性有两种设置方法,一种是数字,一种是符号. Linux文件的基本权限就有九个,分别是owner/group/others三种身份各有自己的read/wri ...
- Quartus II 中 Verilog 常见警告/错误汇总
Verilog 常见错误汇总 1.Found clock-sensitive change during active clock edge at time <time> on regis ...
- golang cronexpr定时任务包使用
包获取 go get -u github.com/gorhill/cronexpr 创建一个定时任务 expr, err = cron.Parse("* * * * *"); 获得 ...