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

CF EDU 1101D GCD Counting

题意

   有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1。

思路

  由于每个数的质因子很少,题目的数据200000<2*3*5*7*11*13*17=510510。所以每个节点的质因子个数不多。那么树形DP的时候直接枚举每种因子即可。

//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 998244353;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x=f?-x:x;
}
/*-----------------------showtime----------------------*/

            const int maxn = 2e5+9;
            int a[maxn];

            vector<int>mp[maxn];
            int vis[maxn];
            int dp[maxn];
            int ans = 0;
            vector<int>p[maxn],t[maxn];

            void dfs(int u, int fa){
                for(int i=0; i<mp[u].size(); i++){
                    int v = mp[u][i];
                    if(v == fa)continue;
                    dfs(v,u);

                    for(int j=0; j<p[u].size(); j++){
                        for(int k=0; k<p[v].size(); k++){
                            if(p[u][j] == p[v][k]){
                                ans = max(ans, t[u][j] + t[v][k]);
                                t[u][j] = max(t[u][j], t[v][k] + 1);
                            }
                        }
                    }
                }
                if(a[u] > 1) ans = max(ans, 1);
            }

int main(){
            int n;  scanf("%d", &n);
            int flag = 1;
            for(int i=1; i<=n; i++) {
                scanf("%d", &a[i]);
                int x = a[i];
                for(ll j=2; j*j <=x; j++){
                    if(x%j == 0){
                        p[i].pb(j);
                        t[i].pb(1);
                        while(x%j==0) x/=j;
                    }
                }
                if(x > 1){
                    p[i].pb(x);
                    t[i].pb(1);
                }
                if(a[i] > 1) flag = 0;
            }

            if(flag) {
                puts("0");
                return 0;
            }
            for(int i=1; i<n; i++){
                int u,v;
                scanf("%d%d", &u, &v);
                mp[u].pb(v);
                mp[v].pb(u);
            }

            dfs(1,-1);
            printf("%d\n", ans);
            return 0;
}

原文地址:https://www.cnblogs.com/ckxkexing/p/10263235.html

时间: 2024-07-31 14:10:07

CF EDU 1101D GCD Counting 树形DP + 质因子分解的相关文章

CF 337D Book of Evil 树形DP 好题

Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some p

CF 461B Appleman and Tree 树形DP

Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then

CF 161D Distance in Tree 树形DP

一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j][0]:在tree(i)中,经过节点i,长度为j,端点不在i的路径的个数 则目标:∑(dp[i][k][0]+dp[i][k][1])初始化:dp[i][0][1]=1,其余为0 siz[i]:tree(i)中,i与离i最远的点的距离递推:dp[i][j][0]+=dp[i][j-l][1]*dp[

cf842C 树形dp+gcd函数

树形dp用一下就好了 /* dp[i]表示不删节点的gcd值 每个结点开个vector用来存储删一个点之后的最大值 然后排序 去重 */ #include<bits/stdc++.h> #include<vector> using namespace std; #define maxn 200005 struct Edeg{int to,nxt;}edge[maxn<<1]; int n,a[maxn],head[maxn],tot,dp[maxn]; vector&l

CF 219D Choosing Capital for Treeland 树形DP 好题

一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次

CF 486D vailid set 树形DP

As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it. We call a set S of tree nodes valid if following con

CF 1000G Two-Paths (树形DP)

题目大意:给你一棵树,点有点权$a_{i}$,边有边权$w_{e}$,定义一种路径称为$2-path$,每条边最多经过2次且该路径的权值为$\sum _{x} a_{x}\;-\;\sum_{e}w_{e}\cdot k_{e}$,$k_{e}$为边的经过次数,一共$Q$次询问,每次查询经过$x,y$的$2-path$权值最大的路径的权值 看题解之前感觉不可做...有点思路但感觉不靠谱,都被我否掉了 LiGuanlin神犇提供了一种树形DP的解法 定义$f[x][0]$表示该子树内,经过$x$点

CF1101D GCD Counting

CF1101D GCD Counting 又被trick了 不用什么点分治 直接树形dp即可 开始的想法: f[x][j]x为根的子树gcd至少为j(j是x的一个约数)的最长链 然后对y合并.类似于树的直径 但是复杂度还是很大的... 这个题的关键是:我们只关心gcd是不是1,并不关心gcd是什么! gcd不是1,意味着一定有公共质因子! 而质因子个数非常少 可以f[x][j]表示,x为根的子树,往下走,公共质因子为j的最长链 然后甚至可以暴力合并! 显然最优解可以被处理到! 代码: #incl

HDU 4714 Tree2cycle (树形DP)

题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定要把它从父结点上剪下来是最优的, 因为如果这样剪下来再粘上花费是2(先不管另一端),如果分别剪下来再拼起来,肯定是多花了,因为多了拼起来这一步,知道这,就好做了.先到叶子结点, 然后再回来计算到底要花多少. 代码如下: #pragma comment(linker, "/STACK:10240000