题目地址: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的更多相关文章

  1. CF1101D GCD Counting(数学,树的直径)

    几个月的坑终于补了…… 题目链接:CF原网  洛谷 题目大意:一棵 $n$ 个点的树,每个点有点权 $a_i$.一条路径的长度定义为该路径经过的点数.一条路径的权值定义为该路径经过所有点的点权的 GC ...

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

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

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

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

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

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

  5. CodeForces - 1101D:GCD Counting (树分治)

    You are given a tree consisting of n vertices. A number is written on each vertex; the number on ver ...

  6. D. GCD Counting(树上dp)

    题目链接:http://codeforces.com/contest/1101/problem/D 题目大意:给你n个点,每个点都有权值,然后给你边的关系,问你树上的最大距离.(这里的最大距离指的是这 ...

  7. CodeForces990G:GCD Counting(树分治+GCD)

    You are given a tree consisting of nn vertices. A number is written on each vertex; the number on ve ...

  8. GCD Counting Codeforces - 990G

    https://www.luogu.org/problemnew/show/CF990G 耶,又一道好题被我浪费掉了,不会做.. 显然可以反演,在这之前只需对于每个i,统计出有多少(x,y),满足x到 ...

  9. codeforces1101D GCD Counting 【树形DP】

    题目分析: 蛮简单的一道题,对于每个数拆质因子,对于每个质因子找出最长链,在每个地方枚举一下拼接 代码: #include<bits/stdc++.h> using namespace s ...

随机推荐

  1. mysql全备份脚本速查

    mysql全备份脚本 # 快捷备份方式[root@nb scripts]# cat db.backup.sh #!/bin/bashmysqldump -ubackup -pbackuppwd -P3 ...

  2. Go多组Raft库

    Go多组Raft库 https://github.com/lni/dragonboat/blob/master/README.CHS.md 使用用例 https://github.com/lni/dr ...

  3. nGrinder TestRunner XFF / X-Forwarded-For

    s 我们在压测请求报文里面带了这个"x-forward-for":"10.24.51.132"这个字段,所以我们所有的压测请求穿透到应用系统的时候,应用系统上采 ...

  4. JAVA核心技术I---JAVA基础知识(数据结构基础)

    一:数组 (一)基本内容是与C一致的 (二)数组定义和初始化 (1)声明 int a[]; //a没有new操作,没有被分配内存,为null int[] b; //b没有new操作,没有被分配内存,为 ...

  5. winddow10下 virtualBox Ubuntu网络设置

    1. virtualbox 全局设置 2. 虚拟机网络设置 3.虚拟机中设置 4. 宿主机 设置 5. 最后 在虚拟机中 ping www.baidu.com

  6. HDU - 6313 Hack It(构造)

    http://acm.hdu.edu.cn/showproblem.php?pid=6313 题意 让你构造一个矩阵使得里面不存在四个顶点都为1的矩形,并且矩阵里面1的个数要>=85000 分析 ...

  7. window.open post

    前端代码 expExcel(){ window.open(PreURL+'company_list_exp?keyword='+this.keyword+'&area_code='+this. ...

  8. PC机Win10声音问题两例处理办法

    1.PC电脑接HDMI显示器后无声的解决方案点击声音->播放,下面有一个是显示器,一个扬声器,选择扬声器即可.2.低音太重解决办法扬声器属性,增强,禁用所有声音效果.

  9. 嫁给程序员的好处,你get到了吗?

    首先,我们要知道,什么是程序员?程序员是做什么的? "程序员(英文Programmer)是从事程序开发.维护的专业人员.一般将程序员分为程序设计人员和程序编码人员,但两者的界限并不非常清楚, ...

  10. 关于HTTP的笔记

    网上看了一篇关于HTTP的博客,觉得还不错,这里就记下来了. 参考:https://www.cnblogs.com/guguli/p/4758937.html 一.主要特点 1.支持客户/服务器模式2 ...