Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)
链接:
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)的更多相关文章
- 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 ...
- Codeforces Round #588 (Div. 2)
传送门 A. Dawid and Bags of Candies 乱搞. Code #include <bits/stdc++.h> #define MP make_pair #defin ...
- Codeforces Round #588 (Div. 1) 简要题解
1. 1229A Marcin and Training Camp 大意: 给定$n$个对$(a_i,b_i)$, 要求选出一个集合, 使得不存在一个元素好于集合中其他所有元素. 若$a_i$的二进制 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #588 (Div. 1)
Contest Page 因为一些特殊的原因所以更得不是很及时-- A sol 不难发现当某个人diss其他所有人的时候就一定要被删掉. 维护一下每个人会diss多少个人,当diss的人数等于剩余人数 ...
- Codeforces Round #588 (Div. 2) D题【补题ING】
思路:先找出现次数>=2数.然后在取跑所有数,需要考虑一般情况(当一个人比另一个人的ai小且他们的与运算等于小的那个人的ai那么可以知道大的那个人必定强于ai小的那个人). 则可以用位运算实现判 ...
随机推荐
- 【深入浅出-JVM】(9): 方法区
概念 方法区是虚拟机规范定义的,是所有线程共享的内存区域,保存系统的类的信息.比如:类的字段.方法.常量池.构造函数的字节码内容.代码.JIT 代码 永久代.metaspace 是对方法区的实现. H ...
- 基于TCP 协议的socket 简单通信
DNS 服务器:域名解析 socket 套接字 : socket 是处于应用层与传输层之间的抽象层,也是一组操作起来非常简单的接口(接受数据),此接口接受数据之后,交由操作系统 为什么存在 soc ...
- 怎样初始化XMLHttpRequest实例对象xhr
xhr.open() 接收5个参数, 用于初始化一个http请求, 它接收5个参数: 1. method: 请求类型; 2. url: 请求的url; 3. async: 是否为异步, 默认为true ...
- 01 Java 内存分配全面浅析
http://blog.csdn.net/shimiso/article/details/8595564 Java 内存分配全面浅析 本文将由浅入深详细介绍Java内存分配的原理,以帮助新手更轻松的 ...
- WINAPI与CALLBACK
#define WINAPI __stdcall #define CALLBACK __stdcall 都是__stdcall,无本质区别. CALLBACK只是为了告诉我们这是一个回调函数.
- linux--查看磁盘空间大小使用情况
1. linux查看磁盘空间大小命令 df -h Df命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息, 命令格式: df -hl 显示格式为: 文件系统 容量 ...
- C++ STL 之 内建函数对象
STL 内建了一些函数对象.分为:算数类函数对象,关系运算类函数对象,逻辑运算类仿函数.这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能.使用内建函数对象 ...
- liunx pyinotify的安装和使用
介绍此功能是检测目录的操作的事件 1.安装 在百度云盘下载或者在gits上下载安装包 链接:https://pan.baidu.com/s/1Lqt872YEgEo_bNPEnEJMaw 提取码:bj ...
- RabbitMQ的特点与应用场景(二)
1.RabbitMQ的主要特点 (1)可靠性:RabbitMQ可通过队列持久化,交换机持久化,消息持久化及ACK回应等机制保证可靠性 (2)支持多种语言与协议:RabbitMQ几乎支持所有的编程语 ...
- python 把函数的值赋给变量
本博文的知识点一个是模块的调用和一个自定义函数返回值赋值给变量 编写一个简单的函数模块: [root@bigdata zw]# more d.py #!/usr/bin/python # -*- co ...