CF1101D GCD Counting 点分治+质因数分解
题意:求最长的树上路径点值的 $gcd$ 不为 $1$ 的长度.
由于只要求 $gcd$ 不为一,所以只要 $gcd$ 是一个大于等于 $2$ 的质数的倍数就可以了.
而我们发现 $2\times 10^5$ 以内的数最多只会有 $7$~$8$ 个本质不同的质因子,所以我们在点分治的时候暴力拆质因子并维护一些桶即可.
#include <cstdio>
#include <vector>
#include <algorithm>
#define N 200004
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
int n,tot,edges,sn,root,tl,answer;
vector<int>v[N];
int prime[N],is[N],num[N];
int val[N],hd[N],to[N<<1],nex[N<<1];
int size[N],mx[N],vis[N],f[N],g[N],tmp[N],depth[N],cur[N],number[N];
void add(int u,int v)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;
}
void getroot(int u,int ff)
{
size[u]=1,mx[u]=0;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
getroot(to[i],u),size[u]+=size[to[i]],mx[u]=max(mx[u],size[to[i]]);
mx[u]=max(mx[u],sn-size[u]);
if(mx[u]<mx[root]) root=u;
}
void dfs(int u,int ff,int dep)
{
number[u]=tmp[++tl]=__gcd(val[u],number[ff]),depth[tl]=dep;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
dfs(to[i],u,dep+1);
}
void calc(int u)
{
int i,j,re;
if(val[u]>1) answer=max(answer,1);
tl=0;
number[u]=val[u];
for(i=hd[u];i;i=nex[i])
{
if(vis[to[i]]) continue;
re=tl+1,dfs(to[i],u,1);
for(j=re;j<=tl;++j)
{
int a=tmp[j],b=depth[j];
if(a>1)
{
for(int k=0;k<v[a].size();++k)
g[v[a][k]]=max(g[v[a][k]],b),answer=max(answer,g[v[a][k]]+f[v[a][k]]+1);
}
}
for(j=re;j<=tl;++j)
{
int a=tmp[j];
if(a>1)
{
for(int k=0;k<v[a].size();++k) f[v[a][k]]=max(f[v[a][k]],g[v[a][k]]);
}
}
for(j=re;j<=tl;++j)
{
int a=tmp[j];
if(a>1) for(int k=0;k<v[a].size();++k) g[v[a][k]]=0;
}
}
for(i=1;i<=tl;++i)
{
int a=tmp[i];
if(a>1) for(j=0;j<v[a].size();++j) f[v[a][j]]=g[v[a][j]]=0;
}
}
void solve(int u)
{
vis[u]=1,calc(u);
for(int i=hd[u];i;i=nex[i])
if(!vis[to[i]])
sn=size[to[i]],root=0,getroot(to[i],u),solve(root);
}
void init()
{
int i,j;
for(i=2;i<N;++i)
{
if(!is[i]) prime[++tot]=i;
for(j=1;j<=tot&&i*prime[j]<N;++j)
{
is[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
for(i=2;i<N;++i) num[i]=i;
for(i=1;i<=tot;++i)
for(j=prime[i];j<N;j+=prime[i])
{
v[j].push_back(prime[i]);
while(num[j]%prime[i]==0) num[j]/=prime[i];
}
}
int main()
{
int i,j;
init();
// setIO("input");
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d",&val[i]);
for(i=1;i<n;++i)
{
int a,b;
scanf("%d%d",&a,&b),add(a,b),add(b,a);
}
mx[root=0]=sn=n,getroot(1,0),solve(root);
printf("%d\n",answer);
return 0;
}
CF1101D GCD Counting 点分治+质因数分解的更多相关文章
- CF1101D GCD Counting
题目地址:CF1101D GCD Counting zz的我比赛时以为是树剖或者点分治然后果断放弃了 这道题不能顺着做,而应该从答案入手反着想 由于一个数的质因子实在太少了,因此首先找到每个点的点权的 ...
- CF1101D GCD Counting(数学,树的直径)
几个月的坑终于补了…… 题目链接:CF原网 洛谷 题目大意:一棵 $n$ 个点的树,每个点有点权 $a_i$.一条路径的长度定义为该路径经过的点数.一条路径的权值定义为该路径经过所有点的点权的 GC ...
- BZOJ-1705 Longge的问题 一维GCD SUM 乱搞+质因数分解+...
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1871 Solved: 1172 [Submit][ ...
- CF990G GCD Counting 点分治+容斥+暴力
只想出来 $O(nlogn\times 160)$ 的复杂度,没想到还能过~ Code: #include <cstdio> #include <vector> #includ ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- 质因数分解的rho以及miller-rabin
一.前言 质因数分解,是一个在算法竞赛里老生常谈的经典问题.我们在解决许多问题的时候需要用到质因数分解来辅助运算,而且质因数分解牵扯到许许多多经典高效的算法,例如miller-rabin判断素数算法, ...
- Vijos P1786 质因数分解【暴力】
质因数分解 背景 NOIP2012普及组第一题 描述 已知正整数n是两个不同的质数的乘积试求出较大的那个质数. 格式 输入格式 输入只有一行包含一个正整数n. 输出格式 输出只有一行包含一个正整数p, ...
- CF1139D Steps to One(DP,莫比乌斯反演,质因数分解)
stm这是div2的D题……我要对不住我这个紫名了…… 题目链接:CF原网 洛谷 题目大意:有个一开始为空的序列.每次操作会往序列最后加一个 $1$ 到 $m$ 的随机整数.当整个序列的 $\gcd ...
- [学习笔记] Miller-Rabin质数测试 & Pollard-Rho质因数分解
目录 Miller-Rabin质数测试 & Pollard-Rho质因数分解 Miller-Rabin质数测试 一些依赖的定理 实现以及正确率 Pollard-Rho质因数分解 生日悖论与生日 ...
随机推荐
- 使用javascript完成一个简单工厂设计模式。
在JS中创建对象会习惯的使用new关键字和类构造函数(也是可以用对象字面量). 工厂模式就是一种有助于消除两个类依赖性的模式. 工厂模式分为简单工厂模式和复杂工厂模式,这篇主要讲简单工厂模式. 简单工 ...
- Maven设置阿里仓库镜像增加访问速度
修改maven的setting.xml 在mirrors节点下面添加子节点 <mirror> <id>nexus-aliyun</id> <mirrorOf& ...
- ASP.NET Core中使用EF Core(MySql)Database First
⒈创建数据库,在数据中执行以下脚本. CREATE DATABASE Blogging; USE Blogging; CREATE TABLE Blog ( BlogId int not null P ...
- C++练习 | 文件流应用(1)
#include <iostream> #include <cmath> #include <cstring> #include <string> #i ...
- Linux就该这么学——安装配置VM虚拟机
Vm虚拟机下载地址 : https://cloud.189.cn/t/zAfaQvJZRziu (访问码:6717) rehl镜像下载地址 : https://cloud.189.cn/t/67BJ ...
- Java回调实现异步
在正常的业务中使用同步线程,如果服务器每处理一个请求,就创建一个线程的话,会对服务器的资源造成浪费.因为这些线程可能会浪费时间在等待网络传输,等待数据库连接等其他事情上,真正处理业务逻辑的时间很短很短 ...
- The Party and Sweets CodeForces - 1159C (拓排)
优化连边然后拓排. #include <iostream> #include <sstream> #include <algorithm> #include < ...
- access数据库根据指定日期进行查询
获取指定日期的记录 1.select Field1 from A where format("yyyy-MM-dd",Field1)=#2011-10-07# 有时不能获取记录 ...
- Linux下nouveau操作和GPU的操作,nouveau拯救
之前也提到了,旧机器上有一块NVIDIA的显卡,装了ubuntu16.04后一直没有安装驱动.这周算是有点时间,就差点作了死. 首先必须澄清,这个不是正确的安装过程,起码我就报了memory erro ...
- docker使用国内镜像加速
在daemon.json文件里以下国内镜像 { "registry-mirrors": [ "https://registry.docker-cn.com", ...