链接:

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. macos 更改罗技k810无线键盘的映射

    在mac系统中,command键非常关键,但k810接入后, win键被映射为Command,而Alt的位置却是mac内置键盘的Command的位置. 为方便使用,可以把Win键和Alt键做一个对换. ...

  2. Docker下mysql容器开启binlog日志(保留7天)

    现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...

  3. 对JSON.parse()中存在转义字符的解决以及js中替换函数replace()的认识

    在工作中,遇到对页面数据进行转存json格式数据后存储在数据库中.然而在显示数据时遇到无法显示json中的数据,产生的bug 问题抛出: 1.首先认识下,在JSON.parse()将后台传过来的字符串 ...

  4. java 复制指定目录中的所有文件和文件夹到另一个指定文件夹中

    package com.test; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream ...

  5. 阿里云 负载均衡 HTTP转HTTPS

    一.相关文档 1.证书服务 2.简单路由-HTTP 协议变为 HTTPS 协议 二.阿里云操作界面 1.云盾证书服务管理控制台(查询CA证书服务) 2.负载均衡管理控制台 三.相关文档 1.Syman ...

  6. 【opencv 源码剖析】 三、 morphOp 数学形态学滤波函数, 腐蚀和膨胀就是通过这个函数得到的

    // //_kernel : 形态学滤波的核 //anchor: 锚点再滤波核的位置 //iterations: 迭代次数 static void morphOp( int op, InputArra ...

  7. C语言——指针总结

    在创建指针时,我们首先要做的是先初始化它,没有初始化的指针是很危险的,因为指针可以指向一个地址后直接改变它的值,所以为了避免我们的指针在创建后指向一个危险区域(即可能指向系统文件等),我们会先给它一个 ...

  8. window上mongoDB的安装及常用mongodb命令

    前几天在学习node操作数据库时使用的mongoDB数据库,今天来对mongodb的安装过程及配置以及后面需要使用的一些常用命令做一下总结. 安装MongoDB (可参考菜鸟教程中的安装步骤) 首先, ...

  9. mybatis+Oracle 批量插入数据,有数据做更新操作

    <!-- 批量添加 -->     <insert id="batchAdd" parameterType="java.util.List"& ...

  10. vue常用知识点

    vue中图片路径写法 <img :src="avatorSrc" alt=""> <img :src="avatorSrc2&quo ...