Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
long long a[100007];
vector<int>edge[100007];
map<long long,long long>mp[100007];//key记录gcd的大小,value记录gcd出现的次数
long long ans;
const long long mod= 1e9+7;
void dfs(int u,int fa){
++mp[u][a[u]];
ans+=a[u];//自己和自己组成的点对gcd就是自己
for(auto it:mp[fa]){//遍历向上的路径
long long tmp=__gcd(it.first,a[u]);//求路径上点对的gcd和u的gcd
ans=(ans+tmp*it.second)%mod;//更新答案,加上gcd和这个gcd一共在以u为起点的向上的路径中出现过几次的乘积
mp[u][tmp]+=it.second;//更新这个gcd的次数,如果还有向下的结点,轮到它遍历时上面的gcd次数将会继承
}
for(auto it:edge[u])//dfs
if(it!=fa)
dfs(it,u);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i];
int u,v;
for(int i=1;i<n;++i){
cin>>u>>v;
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1,0);
cout<<ans;
return 0;
}
Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)的更多相关文章
- Codeforces Round #588 (Div. 2)C(思维,暴力)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[27],b[27];int vis ...
- Codeforces Round #588 (Div. 2)D(思维,多重集)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[7007],b[700 ...
- Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和
Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #381 (Div. 2) D dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)
链接: https://codeforces.com/contest/1230/problem/E 题意: Kamil likes streaming the competitive programm ...
- Codeforces Round #588 (Div. 2) C. Anadi and Domino(思维)
链接: https://codeforces.com/contest/1230/problem/C 题意: Anadi has a set of dominoes. Every domino has ...
- Codeforces Round #588 (Div. 2) D. Marcin and Training Camp(思维)
链接: https://codeforces.com/contest/1230/problem/D 题意: Marcin is a coach in his university. There are ...
随机推荐
- (转)Java中的String与常量池
Java中的String与常量池 转自:http://developer.51cto.com/art/201106/266454.htm string是java中的字符串.String类是不可变的,对 ...
- Linux上查看当前系统各内存分区信息
命令 ulimit -a -a 查看所有信息,同理,也可以例如 ulimit -s 只查看栈占内存信息
- C++继承、多态与虚表
继承 继承的一般形式 子类继承父类,是全盘继承,将父类所有的东西都继承给子类,除了父类的生死,就是父类的构造和析构是不能继承的. 继承的访问权限从两方面看: 1.对象:对象只能直接访问类中公有方法和成 ...
- Java - 集合 - List
1.List实现类:ArrayList.LinkedList.Vector ArrayList使用: void test() { //声明 List<String> testlist = ...
- 使用SQL命令行更改数据库字段类型
ALTER TABLE 表名 MODIFY COLUMN 字段名 数据类型 添加列 ALTER TABLE students ADD COLUMN address VARCHAR(100) DEFAU ...
- 每天进步一点点------altium designer 实用的快捷键
1.设计浏览器快捷键: 鼠标左击 选择鼠标位置的文档鼠标双击 编辑鼠标位置的文档鼠标右击 ...
- javaScript中的toFix(n)方法
定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法 NumberObject.toFixed(num) 返回值 返回 NumberObject 的字符串表示, ...
- 关于为什么使用React新特性Hook的一些实践与浅见
前言 关于Hook的定义官方文档是这么说的: Hook 是 React 16.8 的新增特性.它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性. 简单来说,就是在 ...
- web前端技能考核(阿里巴巴)
- 在服务器CentOS7版本安装Nginx
简介 经常用使用Nginx来部署我们的网站,我的服务器是CentOS7.我不喜欢使用下载一个Nginx解压包然后解压的那种,我喜欢下面的这种. 安装 yum包管理工具是不带nginx,所以得先添加,在 ...