题目大意是求

\(\sum_{v,fa,lca(v,fa)=fa}gcd(v \to fa)\)

容易发现 \(\gcd\) 只会变小,所以根据这玩意是从上到下的,每次暴力一下就可以了,\(\gcd\)数量不会超过\(\log\),所以复杂度大概是 \(n \log n\)

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define int long long
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp<class T> void cmax(T& x, const T& y) {
if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
if (x > y) x = y;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
sort(all(v));
v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
~FILEIN() {}
char qwq[SZ], *S = qwq, *T = qwq, ch;
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
FILEIN& operator>>(char& c) {
while (isspace(c = GETC()))
;
return *this;
}
FILEIN& operator>>(string& s) {
while (isspace(ch = GETC()))
;
s = ch;
while (!isspace(ch = GETC())) s += ch;
return *this;
}
Tp<class T> void read(T& x) {
bool sign = 1;
while ((ch = GETC()) < 0x30)
if (ch == 0x2d) sign = 0;
x = (ch ^ 0x30);
while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
x = sign ? x : -x;
}
FILEIN& operator>>(int& x) { return read(x), *this; }
FILEIN& operator>>(signed& x) { return read(x), *this; }
FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
const static int LIMIT = 0x114514;
char quq[SZ], ST[0x114];
signed sz, O;
~FILEOUT() { flush(); }
void flush() {
fwrite(quq, 1, O, stdout);
fflush(stdout);
O = 0;
}
FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
FILEOUT& operator<<(string str) {
if (O > LIMIT) flush();
for (char c : str) quq[O++] = c;
return *this;
}
Tp<class T> void write(T x) {
if (O > LIMIT) flush();
if (x < 0) {
quq[O++] = 0x2d;
x = -x;
}
do {
ST[++sz] = x % 0xa ^ 0x30;
x /= 0xa;
} while (x);
while (sz) quq[O++] = ST[sz--];
return;
}
FILEOUT& operator<<(int x) { return write(x), *this; }
FILEOUT& operator<<(signed x) { return write(x), *this; }
FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out; int n, ans;
const int maxn = 1e5 + 10;
vector<int> g[maxn];
int val[maxn];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
const int mod = 1e9 + 7;
void dfs(vector<pii> vc, int u, int p) {
vector<pii> nvc;
nvc.pb(val[u], 1ll);
ans = (ans + val[u]) % mod;
for (pii x : vc) {
int y = x.first;
int c = x.second;
int ny = gcd(y, val[u]);
ans = (ans + ny * c) % mod;
if (ny == nvc.back().first)
nvc.back().second += c;
else
nvc.pb(ny, c);
}
nvc.swap(vc);
for (int v : g[u])
if (v != p) dfs(vc, v, u);
}
signed main() {
#ifdef _WIN64
freopen("testdata.in", "r", stdin);
#else
ios_base ::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
#endif
// code begin.
in >> n;
for (int i = 0; i < n; i++) {
in >> val[i];
}
for (int i = 1; i < n; i++) {
int u, v;
in >> u >> v;
--v, --u;
g[u].pb(v);
g[v].pb(u);
}
dfs({}, 0, -1);
out << ans << '\n';
return 0;
// code end.
}

CF1230E Kamil and Making a Stream的更多相关文章

  1. Kamil and Making a Stream

    E. Kamil and Making a Stream 参考:Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上 ...

  2. E. Kamil and Making a Stream 区间gcd

    E. Kamil and Making a Stream 这个题目要用到一个结论,就是区间一个区间长度为n的不同的gcd不会超过logn 个, 其实就是知道这个题目可以暴力就好了. 然后就是对于每一个 ...

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

  4. CF1230 E. Kamil and Making a Stream gcd+暴力

    比赛的时候TLE,第二天发现合并方向合并错了~ 改了一下顺序就切了~ 又掉分了,好难过QAQ...... Code: #include <bits/stdc++.h> #define N ...

  5. Codeforces 1229B. Kamil and Making a Stream

    传送门 注意到只要考虑祖先和后代之间的贡献 发现对于一个节点,他和所有祖先最多产生 $log$ 个不同的 $gcd$ 所以每个节点开一个 $vector$ 维护祖先到自己所有不同的 $gcd$ 和这个 ...

  6. 【CF1210C】Kamil and Making a Stream(vector,数论,树)

    题意:给定一棵n个点带点权的树,i号点的点定义f(i,j)为i到j路径上所有点的gcd,其中i是j的一个祖先,求所有f(i,j)之和mod1e9+7 2<=n<=1e5,0<=a[i ...

  7. Codeforces Round #588 (Div. 2)

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

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

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

  9. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

随机推荐

  1. Sea.js 手册与文档

    Sea.js 手册与文档 首页 | 索引 目录 何为 CommonJS 何为 CommonJS 模块 为何封装模块 何为 CommonJS? CommonJS 是一个有志于构建 JavaScript ...

  2. Python中heapq与优先队列【详细】

    本文始发于个人公众号:TechFlow, 原创不易,求个关注 今天的文章来介绍Python当中一个蛮有用的库--heapq. heapq的全写是heap queue,是堆队列的意思.这里的堆和队列都是 ...

  3. Struts(六)

    JSON(JavaScript Object Notation)    1.一种轻量级的数据交换格式    2.通常用于在客户端和服务器之间传递数据    3.jQuery的所有参数都是以JSON格式 ...

  4. 【VC++开发实战】迅雷晒密及批量查询流量程序

    迅雷第一次登录是不需要验证码的,就很方便了.首先还是要解决登录的问题,也不是那么容易解决的,这个是在POST是提交的表单[u=joneeky%40qq.com&p=cf1e22e61d987c ...

  5. 搭建一个V 2ray的方法

    VPS构建VPN教程 (由于博客限制有些敏感词 V 2ray中间会打空格或者(删掉我)图片中的敏感词进行了马赛克处理) 关于自建VPN翻墙教程,此处是利用V 2 ray的一个VPS搭建VPN教程.便于 ...

  6. POJ_1485_dp

    题目描述: 每组数据给n个点,点按一维坐标升序给出,要求划分成k块,在每一块中,取一个站,要求每个块中所有的点到站的距离的和的总和最小. 思路: dp题,dp[i][j]表示i个点分成j块的最小距离, ...

  7. windows系统快速安装pytorch的详细教程

    pip和conda的区别 之前一直使用conda和pip ,有时候经常会两者混用.但是今天才发现二者装的东西不是在一个地方的,所以发现有的东西自己装了,但是在运行环境的时候发现包老是识别不了,一直都特 ...

  8. learn about sqlserver partitition and partition table --- add or remove table partitions

    demo/* add partitions */ alter database xxx add filegroup FG_=fff_201708;alter database xxx add file ...

  9. 1. 学习Linux操作系统

    1.熟练使用Linux命令行(鸟哥的Linux私房菜.Linux系统管理技术手册) 2.学会Linux程序设计(UNIX环境高级编程) 3.了解Linux内核机制(深入理解LINUX内核) 4.阅读L ...

  10. c++ 初始化列表和构造函数初始化区别

    先上代码 #include <iostream> class MyContruct { public: MyContruct() { std::cout << "My ...