题目地址: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. C++: 可变参数;

    可变参数,即参数的个数是动态变化的, 可多可少. 1. 可变参数: 可变参数一般采用”..."表示,用在宏上表示变参宏, 如: #define WriteLine(format,...) p ...

  2. slave have equal MySQL server UUIDs

    在部署MySQL主从复制架构的时候,碰到了"Last_IO_Error: Fatal error: The slave I/O thread stops because master and ...

  3. img标签的onerror事件

    #情景分析: 有时,img标签中的src图片加载失败,原来的图片位置会出现一个碎片图标,这样让人很不爽,如何变得美观些呢? #解决方案: 可以借用img标签的onerror事件,img标签支持oner ...

  4. Java面试题全集(下)转载

    Java面试题全集(下)   这部分主要是开源Java EE框架方面的内容,包括hibernate.MyBatis.spring.Spring MVC等,由于Struts 2已经是明日黄花,在这里就不 ...

  5. JAVA核心技术I---JAVA回顾

    一:基础类型运算 大部分的指令都没有支持byte.char.short,没有任何指令支持boolean类型.编译器在编译期或者运行期将byte和short类型的数据带符号扩展为相应的int类型数据,将 ...

  6. nginx的rewrite指令

    需求:我们在接收到一个/api/upload 开头的url的时候 要转发到另外一个地址.但是又不想追加/api/upload  只想追加/upload 地址. Nginx提供了rewrite指令,用于 ...

  7. IntelliJ IDEA(2017)安装和破解

    IDEA 全称 IntelliJ IDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本工具( ...

  8. 068、Calico的网络结构是什么?(2019-04-11 周四)

    参考https://www.cnblogs.com/CloudMan6/p/7520164.html     root@host1:~# docker run -itd --name bbox1 -- ...

  9. python保存文件到数据库

    效果 model class WEIGUI_IMG(Base): __tablename__ = 'DW_ZHS_D_WEIGUI_IMG' # 表名 id = Column(), primary_k ...

  10. [Android] [putty连接Android设备] [Android设备网络调试]

    file: system/core/adb/adb.c line: 921 /* for the device, start the usb transport if the ** android u ...