#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. win7安装mysql数据库

    1. 软件准备,以64位系统为例如果是32位的下载32位压缩包即可] https://dev.mysql.com/downloads/mysql/ 2.下载解压到本地,将解压路径的bin目录配置到环境 ...

  2. main函数的参数详解

    1.定义 C语言规定main函数的参数只能有两个,习惯上这两个参数写为argc和argv.因此,main函数的函数头可写为: main (argc,argv)C语言还规定argc(第一个形参)必须是整 ...

  3. FPGA-HPS

    最近在做DE1的图像方面实验,用到了HPS,所以简要谈一谈什么是HPS. 由图可知,DE1的板子就是有fpga+hps组成的: 参考自:http://bbs.eeworld.com.cn/thread ...

  4. Labview初识

    Labview2013安装教程 请访问http://wenku.baidu.com/link?url=Nw4pYpRqMupd9Bn3OfkFBoYM6Hhw9TqWvffZHX-GDQYPCTtqo ...

  5. 吴裕雄 python 机器学习——支持向量机SVM非线性分类SVC模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  6. 前端——语言——Core JS——《The good part》读书笔记——第三章节(Object)

    本章介绍对象. 在学习Java时,对象理解为公共事物的抽象,实例为具体的个例,对象为抽象的概念,例如人为抽象的概念,具体的个例为张三,李四. Java对象种类多,包含普通类,JavaBean,注解,枚 ...

  7. iOS 10.3+ 动态修改 App 图标

    支持系统: iOS 10.3+ tvOS 10.2+ Apple 官方文档 官方 API: @interface UIApplication (UIAlternateApplicationIcons) ...

  8. mybatis - buildSqlSessionFactory()

    buildSqlSessionFactory() 这个方法比较长, 干的事情也比较多. 包括一些别名, 插件, 类型处理器等的解析. 从主流程上来看, 最主要的其实是干了两件事:1. 对 mapper ...

  9. 批处理设置IP

    @echo off title 静态IP设置 set netName=本地连接 set address=192.168.1.202 netsh netsh interface ipv4 set dns ...

  10. 2020qbxt D1T3 停车

    嗯... 题目: [问题描述] 市中心有一个环形的停车场,编号1到n,现在有m个车要停,停在每个位置会有不同的费用.为了方便,不允许两辆车停在相邻的位置,请问停好所有车的最小花费是多少? [输入格式] ...