#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. Eqaulize Prices

    There are n products in the shop. The price of the ii-th product is aiai. The owner of the shop want ...

  2. c#项目调用Python模块的方法

    将Python模块用pyinstaller打包成exe程序 下载安装UPX((http://upx.sourceforge.net/)) ,并把路径加到环境变量中. UPX是开源的加壳和压缩exe的程 ...

  3. 普及C组第一题(8.9)

    2297. [noip普及组2(放到第一题)]棋盘 (好像重名了)(File IO): input:chess.in output:chess.out 题目描述 众所周知,国际象棋的棋盘是一个网格.国 ...

  4. 普及C组第四题(8.2)

    1342. [南海2009初中]cowtract(网络) (Standard IO) 题目:  Bessie受雇来到John的农场帮他们建立internet网络.农场有 N (2<= N < ...

  5. 【游戏体验】Haunted House(鬼屋历险记)

    >>>点此处可试玩无敌版<<< 这款游戏可谓是经典,很多人的童年都有过它的陪伴,不妨重拾当年的快乐 个人测评 游戏性 9/10 音乐 7/10 剧情 4/10 总评 ...

  6. go基础_控制语句

    if控制语句 说明:(1)if后面的条件语句不用加括号 (2)if后面可以跟一个简单的初始化语句,并以分号分割,初始化语句中的变量的作用域是整个if语句块 (3)if语句的条件语句需要尽量简单 (4) ...

  7. Linux 下使用 ffmpeg 大批量合并 ts 文件, mp4切割文件为m3u8

    见范例 ffmpeg -i "concat:file001.ts|file002.ts|file003.ts|file004.ts......n.ts" -acodec copy ...

  8. ARM64架构下面安装mysql5.7.22

    MySQL下载地址为: https://obs.cn-north-4.myhuaweicloud.com/obs-mirror-ftp4/database/mysql-5.7.27-aarch64.t ...

  9. 使用node.js实现多人聊天室(socket.io、B/S)

    通过B/S架构实现多人聊天,客户端连接服务器,发送信息,服务器接收信息之后返回给客户端. 主要是通过socket.io实现浏览器和服务器之间进行实时,双向和基于事件的通信. socket.io官方文档 ...

  10. MyBatis学习总结(一)--mybatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...