HDU5599GTW likes tree
题目
题意:给出一棵点有权的树,求 ∏i,jgcd(val[i],....val[j]) \prod_{i,j}gcd(val[i],....val[j]),就是求所有路径gcd的乘积,保证 1<=val[i]<=105 1
分析:一个简单的思路就是暴力树dp,开一个vector记录每个点向下连出去的gcd的情况,通过前缀积的做法可以做到 O(n∗fac) O(n* fac),然而比较难写,而且空间未必能开得下。
另外一个做法就是点分治,但是复杂度并不优秀。
考虑每个质因子对答案的贡献,由于 val[i]<=105 val[i]一个数不同的质因数不会超过6个.从大到小枚举质因子的幂次,由于我们只关心每个连通块的大小,只需要用并查集维护即可。
#include<bits/stdc++.h>
using namespace std;
const int Maxn=200020,M=1e9+7,N=100020;
typedef pair<int,int>pi;
int isp[N],cnt_pri,pri[N];
vector<int>yinzi[N];
vector<int>G[Maxn];
vector<pi>pro[N];
int n,tl;
int is[Maxn],id[Maxn],ct[22];
int f[Maxn],sz[Maxn],in[Maxn];
int find(int x)
{int a;for(a=x;f[a]!=a;a=f[a]);for(;f[x]!=a;){int t=f[x];f[x]=a;x=t;}return a;
}
int powmod(int x,int y)
{int ret=1;while(y){if(y&1)ret=1LL*ret*x%M;y>>=1;x=1LL*x*x%M;}return ret;
}
int solve()
{int ret=1;for(int it=0;it<cnt_pri;it++){int cur=0;int p=pri[it];if(pro[p].empty())continue;tl++;for(int i=0;i<pro[p].size();i++){int u=pro[p][i].second,w=pro[p][i].first;if(is[u])continue;cur=(cur+w)%(M-1);sz[u]=1;f[u]=u;in[u]=tl;for(int j=0;j<G[u].size();j++){int v=G[u][j];if(in[v]<tl)continue;int uu=find(u),vv=find(v);cur+=1LL*sz[uu]*sz[vv]%(M-1)*w%(M-1);if(cur>=M-1)cur-=M-1;if(sz[uu]>sz[vv]){f[vv]=uu;sz[uu]+=sz[vv];}else{f[uu]=vv;sz[vv]+=sz[uu];}}}ret=1LL*ret*powmod(p,cur)%M;}return ret;
}
int main()
{for(int i=2;i<N;i++){if(!isp[i]){for(int j=i;j<N;j+=i){yinzi[j].push_back(i);isp[j]=1;}pri[cnt_pri++]=i;}}int _;scanf("%d",&_);while(_--){scanf("%d",&n);for(int i=1;i<=n;i++){G[i].clear();is[i]=0;}for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);}for(int i=0;i<cnt_pri;i++)pro[pri[i]].clear();for(int i=1;i<=n;i++){int x;scanf("%d",&x);for(int j=0;j<yinzi[x].size();j++){int cnt=0,tp=x,p=yinzi[x][j];while(tp%p==0)cnt++,tp/=p;pro[p].push_back(pi(cnt,i));}}for(int i=0;i<cnt_pri;i++)sort(pro[pri[i]].begin(),pro[pri[i]].end(),greater<pi>());int ans1=solve();int m;scanf("%d",&m);for(int i=0;i<m;i++){int x;scanf("%d",&x);is[x]=1;}int ans2=solve();ans1=1LL*ans1*powmod(ans2,M-2)%M;printf("%d\n",ans1);}
}
HDU5599GTW likes tree相关推荐
- 【CodeForces - 1153D】Serval and Rooted Tree(树形dp)
题干: Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on ...
- 【POJ - 2486】Apple Tree (树形背包,dp)
题干: Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There ...
- 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)
题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...
- 2015年辽宁省赛Interesting Tree
题目描述 Recently, Miss Huang want to receive a Tree as her birthday gift! (What a interesting person!) ...
- Codeforces1153D-Serval and Rooted Tree(树形dp)
原题链接:http://codeforces.com/problemset/problem/1153/D 题目原文: D. Serval and Rooted Tree time limit per ...
- Apple Tree(dfs序+树状数组)
题目(传送门poj3321) Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40714 Accepted: ...
- POJ 3321 Apple Tree【树状数组】
题目 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tr ...
- POJ 3321 Apple Tree 题解
POJ 3321 Apple Tree 题解 POJ 3321 题目 There is an apple tree outside of kaka's house. Every autumn, a l ...
- Apple Tree(树状数组+dfs序+邻接表数组(链式前向星) )
链接:http://poj.org/problem?id=3321 Description There is an apple tree outside of kaka's house. Every ...
最新文章
- [转]UI设计小技巧
- 基于线性表邻接矩阵结构的图的深度/广度优先搜索算法
- shell 文本后几行_shell_wc(统计数目)、head(查看前几行)、tail(查看末尾几行)...
- 排序算法(二)--堆排序(JAVA)
- 奇怪的电梯(洛谷-P1135)
- python中的点的作用_Python基础学习中关键点的作用(三),python,重点,之,函数,3
- 关于ST-Link下载STM32程序的使用
- 以命令行的格式读取音频文件信息,并将读取的内容写到输出文件中
- APP版本更新通知流程测试要点
- python 读取txt中的英文内容 分析词频 可视化显示
- 5年级用计算机器探索规律,《小数除法》用计算器探索规律
- 计算机应用技术5g通信,浅谈5g网络通信技术【浅谈多媒体通信技术的应用】
- 人脸识别活体检测技术探讨:多物理特征融合加分类器的算法技术
- 前端开发学习(七七)
- 亚控科技笔试面试记录
- 拟一维喷管流动的数值解——全亚声速等熵喷管流动的非守恒型CFD解法(MacCormack方法)
- 转 Java知识——精华总结
- ORA-32017 ORA-16179
- No module named 'urllib3'
- 无界函数的极限审敛法
热门文章
- 基于YOLOv8模型+CnOCR识别技术实现汽车车牌识别
- confluence 制作流程图_要什么第三方流程图制作软件,微软的Visio 2016不香吗?
- C++入门教程||C++ 动态内存||C++ 命名空间
- 调制速率和数据信号速率
- JAVA,OpenLDAP使用心得(2)
- 无人机视频与GIS融合三维实景怎么实现?
- Ckeditor 首行缩进
- 计划赴美IPO“黑马”极兔速递凭什么杀出重围?
- vb.net设置分辨率和缩放比例_【华鹏客服维修部】配置高不一定性能强,Win 10做好这些设置才能“6到飞起”!...
- audioread.exceptions.NoBackendError in librosa