链接:

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. bootstrap-table删除指定行注意事项

    方法有两种: 1.使用官方文档的数据(反正我试了2个小时都不行,如有大神请指导下):使用events和operate相结合的方式 2.不使用events,在formatter里面定义事件的实现. 上面 ...

  2. java中package包

    一个.java文件内部有一个.而且只能有一个public类,类名必须与文件名完全一致. 在一个.java文件的开头使用package关键字,作用是指出这个编译单元属于该package的一个库的一部分. ...

  3. 小菜鸟之oracle触发器

    1.触发器说明 触发器是一种在事件发生时隐式地自动执行的PL/SQL块,不能接受参数,不能被显式调用 2.触发器类型 根据触发器所创建的语句及所影响的对象的不同,将触发器分为以下3类 (1)DML触发 ...

  4. MVCC原理 4步 什么是MVCC、事务ACID、事物隔离级别、Innodb存储引擎是如何实现MVCC的

    MVCC是来处理并发的问题,提高并发的访问效率,读不阻塞写.事物A 原子性C 一致性I 隔离性D 持久性高并发的场景下的问题脏读不可重复读幻读事物隔离级别RU读未提交 脏读/不可重复读/幻读 .不适用 ...

  5. c++:论如何成功把自己搞懵【二叉树特辑①】(不定期更新)

    并不正经的前言 以前我这个小白看OI的书,老觉得有些东西很高端(看不懂的自然就很高端[滑稽]):什么栈啊,位运算啊,二叉树啊.有些东西我学了之后也很迷糊(真的不是因为傻?),做题的时候总是可以把自己搞 ...

  6. JavaScript数组方法之reduce

    又见到数组方法了,在前面已经的多次写到过数组方法,甚至都使用原生方法重构了一遍数组的各个方法,可是随着数组方法reduce的应用,发现reduce真的是妙用无穷啊!还是很值得再拿出来说一遍的. 我们再 ...

  7. spring-cloud 学习三 服务提供者

    基于spring-boot创建一个module提供服务 使用mysql数据库,dao使用mybatis,数据库连接池使用阿里的druid 添加maven依赖 <parent> <gr ...

  8. lua堆栈

    lua堆栈 来源 https://blog.csdn.net/suhuaiqiang_janlay/article/details/56702381 来源 https://blog.csdn.net/ ...

  9. 求两个等长的已排序数组的中位数(《算法导论》P113习题9.3-8)

    [问题]设X[1...n]和Y[1...n]为两个数组,每个都包含n个已排序好的数.给出一个求数组X和Y中所有2n个元素的中位数的.O(lgn)时间的算法. [解析]O(lgn)的时间复杂度就是二分查 ...

  10. linux文件目录详细介绍

    linux文件目录 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里 /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点 ...