#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,树)的更多相关文章

  1. Codeforces Round #588 (Div. 2)C(思维,暴力)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[27],b[27];int vis ...

  2. Codeforces Round #588 (Div. 2)D(思维,多重集)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[7007],b[700 ...

  3. 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 ...

  4. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  5. 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 ...

  6. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Bootstrap入门(1)简介

    作者:赵盼盼 出处:https://www.cnblogs.com/zhaopanpan/ Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快 ...

  2. 解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题

    npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

  3. MyBatis(6)——分页的实现

    分页的实现 a)通过mysql的分页查询语句: 说明:sql的分页语句格式为select * from aaa limit #{startIndex},#{pageSize} //---------- ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. 【原】linux增加开机自启动脚本

    在rc.local脚本中添加开机自启动程序

  6. Python 特殊列表操作记录

    1.列表中的NaN数据处理 由于在Excel中筛选数据时,会出现NaN这种数据,所以需要将其删除掉,处理函数如下: 由于NaN数据类型与其他数据类型不一样,所以需要需要根据类型将其去除掉. 2列表关于 ...

  7. Spring - 周边设施 - H2 数据库启动时写入数据

    1. 概述 之前讲到了 H2 的引入 这下我想说说 H2 启动时的 数据导入 2. 场景 需求 启动项目后, H2 启动起来 环境数据会自动注入 H2 数据库 可以验证是否成功 3. 环境 os wi ...

  8. c# 异常:值不能为 null。 参数名: source

    异常详细信息: System.ArgumentNullException: 值不能为 null.参数名: source 其实问题那就出在 Select() 方法,在 Select 上按 F12 查看定 ...

  9. 安装rpm包时提示错误:依赖检测失败的解决方法

    安装rpm包时提示错误:依赖检测失败 解决方法: 命令末尾加上--nodeps --force

  10. Centos6.10-FastDFS-Storage.conf配置示例

    Centos610系列配置 # is this config file disabled # false for enabled # true for disabled disabled = fals ...