链接:

https://codeforces.com/contest/1230/problem/E

题意:

Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached 100 million subscribers. In order to celebrate this, he posted a video with an interesting problem he couldn't solve yet. Can you help him?

You're given a tree — a connected undirected graph consisting of n vertices connected by n−1 edges. The tree is rooted at vertex 1. A vertex u is called an ancestor of v if it lies on the shortest path between the root and v. In particular, a vertex is an ancestor of itself.

Each vertex v is assigned its beauty xv — a non-negative integer not larger than 1012. This allows us to define the beauty of a path. Let u be an ancestor of v. Then we define the beauty f(u,v) as the greatest common divisor of the beauties of all vertices on the shortest path between u and v. Formally, if u=t1,t2,t3,…,tk=v are the vertices on the shortest path between u and v, then f(u,v)=gcd(xt1,xt2,…,xtk). Here, gcd denotes the greatest common divisor of a set of numbers. In particular, f(u,u)=gcd(xu)=xu.

Your task is to find the sum

∑u is an ancestor of vf(u,v).

As the result might be too large, please output it modulo 109+7.

Note that for each y, gcd(0,y)=gcd(y,0)=y. In particular, gcd(0,0)=0.

思路:

暴力题..map记录每个点有多少个gcd的值, 从父节点继承下来.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
const int MOD = 1e9+7; LL a[MAXN], ans = 0;
vector<int> G[MAXN];
unordered_map<LL, int> Mp[MAXN];
int n; void Dfs(int u, int fa)
{
for (auto it: Mp[fa])
{
LL gcd = __gcd(a[u], it.first);
Mp[u][gcd] += it.second;
}
Mp[u][a[u]]++;
for (auto it: Mp[u])
ans = (ans + (it.first*it.second)%MOD)%MOD;
for (auto x: G[u])
{
if (x == fa)
continue;
Dfs(x, u);
}
} int main()
{
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;
G[u].push_back(v);
G[v].push_back(u);
}
Dfs(1, 0);
cout << ans << endl; return 0;
}

Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)的更多相关文章

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

  2. Codeforces Round #588 (Div. 2)

    传送门 A. Dawid and Bags of Candies 乱搞. Code #include <bits/stdc++.h> #define MP make_pair #defin ...

  3. Codeforces Round #588 (Div. 1) 简要题解

    1. 1229A Marcin and Training Camp 大意: 给定$n$个对$(a_i,b_i)$, 要求选出一个集合, 使得不存在一个元素好于集合中其他所有元素. 若$a_i$的二进制 ...

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

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

  6. Codeforces Round #588 (Div. 2) B. Ania and Minimizing(构造)

    链接: https://codeforces.com/contest/1230/problem/B 题意: Ania has a large integer S. Its decimal repres ...

  7. Codeforces Round #588 (Div. 2) A. Dawid and Bags of Candies

    链接: https://codeforces.com/contest/1230/problem/A 题意: Dawid has four bags of candies. The i-th of th ...

  8. Codeforces Round #588 (Div. 1)

    Contest Page 因为一些特殊的原因所以更得不是很及时-- A sol 不难发现当某个人diss其他所有人的时候就一定要被删掉. 维护一下每个人会diss多少个人,当diss的人数等于剩余人数 ...

  9. Codeforces Round #588 (Div. 2) D题【补题ING】

    思路:先找出现次数>=2数.然后在取跑所有数,需要考虑一般情况(当一个人比另一个人的ai小且他们的与运算等于小的那个人的ai那么可以知道大的那个人必定强于ai小的那个人). 则可以用位运算实现判 ...

随机推荐

  1. 自定义的C4P数据库连接池

    要想自己写一个数据库连接池 需要实现DataSoure这个类,然后重写getConnection 这个方法 public class C4p0 implements DataSource{ priva ...

  2. PHP后台开发小经验

    js页面传参数 js的参数传输是关键,尤其是当一个页面的数据需要分步骤上传时. 同样的删除功能,不会操作批量删除时可以尝试单个删除,功能差不多,实现功能的方法也千千万,先做成它是第一位. 主页面很多条 ...

  3. 研究旧项目, 常用 sql 语句

    1. select all table select TABLE_NAME from CodingSystem.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ...

  4. java实现4种内部排序

    两种插入类排序: 直接插入排序: public static void insertSort(int[] arr, int len){ for(int i=1; i<len; i++){ int ...

  5. Attribute预定义特性

    转载自:http://blog.csdn.net/wangyy130/article/details/44241173 一.什么是Attribute Attribute 类将预定义的系统信息或用户定义 ...

  6. LeetCode 腾讯精选50题--求众数

    由于众数是指数组中相同元素的个数超过数组长度的一半,所以有两种思路,一. 先排序,后取排序后的数组的中间位置的值:二. 统计,设定一个变量统计相同元素出现的次数,遍历数组,若与选定的元素相同,统计变量 ...

  7. 使用Seaborn展示多变量两两之间的关系

    数据展示: 1. FacetGrid FacetGrid是一个储存我们想怎样展示信息的东西,如下所示,我们想观察位置中SK和GK的分布. 在这里我们使用map方法把数据填充到图表中 计算类别在某一特征 ...

  8. vue runtime报错问题

    Webpack中导入vue和普通网页中导入vue的区别1. 普通网页导入vue方式 <script></script> 2. Webpack导入vue方式 Import Vue ...

  9. React会自动把虚拟DOM数组展开

    React会自动把虚拟DOM数组展开,放在父级虚拟DOM中,这个特性还是我同事帮我解决一个问题的时候,偶然发现的. 如何将一个数据数组转换为一个虚拟DOM的数组,需要使用map,如下: const n ...

  10. Webstorm2017.3.3软件的安装使用

    下载 ▶进入jetbrains的官方网站点击download,即下载开始.官方网站链接:http://www.jetbrains.com/webstorm/ 安装 ▶双击刚下载完成的.exe文件开始进 ...