Codeforces 1101 D. GCD Counting

D

题意 给你一棵树 每个点有一个权值 定义dist是距离 g(x,y)是 x到y经过所有点的gcd求 g(x,y)>1的最大dist(x,y)

做法 很容易想到边上的gcd就是质数 然后写了一下发现7个质数大概是5e5大于2e5了 所以就很好解决 但是最后一个解决不了 比如一个本身是质数 存进去就会很大 所以还是看了学长的题解 发现用map能解决这个问题

树形dp的想法还是比较好想的 因为刚做了一道差不多的

pos是找子树中的位置 

//学长的解释 学长的博客是 lajiyuan

首先要想到,gcd是质因子就足够了,也就是说,肯定存在一条最长路径公约数是质数。
之后对于一个小于2∗105 2*10^52∗10 
5
 的数,他最多存在7个质因子
这里是因为,2∗3∗5∗7∗11∗13∗17=510510>2∗105 2*3*5*7*11*13*17=510510>2*10^52∗3∗5∗7∗11∗13∗17=510510>2∗1e5
 
所以对于树上的每个点,只需要求这些质因子最多能向下延伸的长度。
我们就dp[i][j]为以i为起点的向i的子树延伸的gcd为a[i]的第j个质因子的最长链的长度。
那么每次更新dp[i][j]一定是他某个儿子也含有这个质因子,我们想要更新dp[i][j]
就要知道这个质因子是他儿子的第几个质因子,
所以我们预处理的时候用map存储,map[pair<i,j>]=k表示i在j中是第k个质因子。
存储质因子用vector存储,G[i][j]表示i的第j个质因子是哪个。
这样我们就可以得到更新dp[i][j]的转移方程
设当前访问的结点为rt,要转移的质因子为第j个,当前访问的儿子为to
 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 200025;
int ans,arr[MAX_N];
vector<int > G[MAX_N],vt[MAX_N];
map<pair<int ,int >,int > mp;
int dp[MAX_N][10];
void dfs(int rt,int fa)
{
    int maxx1[10],maxx2[10];
    int sz = G[rt].size(),sz_ = vt[rt].size();
    for(int i = 0;i<sz_;++i) dp[rt][i] = 1;
    for(int i = 0;i<10;++i)
        maxx1[i] = maxx2[i] = 0;
    for(int i = 0;i<sz;++i)
    {
        int to = G[rt][i];
        if(to==fa) continue;
        dfs(to,rt);
        for(int j = 0;j<sz_;j++)
        {
            if(arr[to]%vt[rt][j]==0)
            {
                int pos = mp[pll(vt[rt][j],to)];
                int tmp = dp[to][pos];
                if(maxx1[j]<tmp)
                {
                    maxx2[j] = maxx1[j];
                    maxx1[j] = tmp;
                }
                else if(maxx2[j]<tmp)
                {
                    maxx2[j] = tmp;
                }
            }
        }
    }
    for(int i = 0;i<sz_;++i)
    {
        dp[rt][i] += maxx1[i];
        ans = max(ans,maxx1[i]+maxx2[i]+1);
    }
    return ;
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,a,b;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i)
    {
        scanf("%d",&arr[i]);
        int k = arr[i];
        for(int j = 2;j*j<=k;j++)
        {
            if(k%j==0)
            {
                vt[i].push_back(j);
                mp[pll(j,i)] = vt[i].size() - 1;
                while(k%j==0) k/=j;
            }

        }
        if(k>1)
        {
            vt[i].push_back(k);
            mp[pll(k,i)] = vt[i].size()-1;
        }
    }
    for(int i = 1;i<n;++i)
    {
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(1,-1);
    printf("%d\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/88678057