题意是求树中所有直链的gcd的总和。

做法是dfs把前缀的所有信息传下去,暴力求值。

为什么可以暴力呢?x = gcd(x,a);x会怎么变呢?要么不变,x,a的最大公因数就是x,要么x变为x的一个因子,也就是说最大是x/2,不出log次计算,x就会变成1

这也就是说维护每个点到根的后缀gcd,最多只有log个不同的数,用链表存起来就可以。

#include <iostream>
#include <queue>
#include <cstring>
#include <list>
#include <algorithm>using namespace std;#define MAXX 201000
typedef long long LL;struct E {int to,next;
}g[MAXX];
int front[MAXX],tot;void Add(int from,int to) {g[++tot].to = to;g[tot].next = front[from];front[from] = tot;
}LL gcd(LL a,LL b) {return b==0 ? a:gcd(b,a%b);
}LL v[MAXX],ans;
const LL Mod = 1e9+7;
typedef list<pair<LL,int> > List;
#define val first
#define num second
void dfs(int t,int fa,List st) {
/*  if(!st.empty()) {List::reverse_iterator it = st.rbegin();  (*it).val = gcd((*it).val,v[t]);for (it++; it != st.rend(); it++) (*it).val = gcd(v[t],(*it).val);st.push_back({v[t],1});} else st.push_back({v[t],1});List::iterator tp = st.begin(),tpp = tp,del;for (tp++; tp != st.end(); )if ((*tp).val == (*tpp).val) {(*tpp).num += (*tp).num;del = tp;tp++;st.erase(del);} else tp++;*/List stt;stt.emplace_back(v[t],1);for (auto it : st) {LL n_va = gcd(it.val,v[t]);if (n_va == stt.back().val) stt.back().num += it.num;else stt.emplace_back(n_va,it.num);}  st.swap(stt);for (auto it : st)ans = (ans+it.val*it.num%Mod)%Mod;for (int i = front[t]; i; i = g[i].next) {int to = g[i].to;if (to != fa) dfs(to,t,st);}st.clear();
} int main() {int n; cin >> n;for (int i = 1; i <= n; i++) scanf("%I64d",&v[i]);for (int i = 1; i < n; i++) {int u,v;scanf("%d%d",&u,&v);Add(u,v);Add(v,u);}List tp;dfs(1,-1,tp);printf("%I64d",ans);return 0;
}

Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached 100100 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 nn vertices connected by n−1n−1 edges. The tree is rooted at vertex 11. A vertex uu is called an ancestor of vv if it lies on the shortest path between the root and vv. In particular, a vertex is an ancestor of itself.

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

Your task is to find the sum

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

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

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

Input

The first line contains a single integer nn (2≤n≤1000002≤n≤100000) — the number of vertices in the tree.

The following line contains nn integers x1,x2,…,xnx1,x2,…,xn (0≤xi≤10120≤xi≤1012). The value xvxv denotes the beauty of vertex vv.

The following n−1n−1 lines describe the edges of the tree. Each of them contains two integers a,ba,b (1≤a,b≤n1≤a,b≤n, a≠ba≠b) — the vertices connected by a single edge.

Output

Output the sum of the beauties on all paths (u,v)(u,v) such that uu is ancestor of vv. This sum should be printed modulo 109+7109+7.

Examples

input

Copy

5
4 5 6 0 8
1 2
1 3
1 4
4 5

output

Copy

42

input

Copy

7
0 2 3 0 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7

output

Copy

30

Note

The following figure shows all 1010 possible paths for which one endpoint is an ancestor of another endpoint. The sum of beauties of all these paths is equal to 4242:

[Codefroces 1230E] Kamil and Making a Stream(List dfs gcd)相关推荐

  1. Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream 数学 + 暴力

    传送门 文章目录 题意: 思路: 题意: 给你一颗树,其中根是111,每个点有一个点权,求每个点到根的所有路径的gcdgcdgcd之和. n≤1e5n\le1e5n≤1e5 思路: 一看到以为是个点分 ...

  2. 【CF1230E】Kamil and Making a Stream【GCD性质】【暴力vector】

    传送门 题意:给一棵带点权的有根树,求所有满足uuu是vvv的祖先的路径(u,v)(u,v)(u,v)的路径上所有点权的gcdgcdgcd的和模1e9+71e9+71e9+7. N≤100000N \ ...

  3. 【翻译】MOA - Massive Online Analysis, a Framework for Stream Classification and Clustering

    [翻译]MOA - Massive Online Analysis, a Framework for Stream Classification and Clustering. MOA - 海量在线分 ...

  4. stream流对象的理解及使用

    我的理解:用stream流式处理数据,将数据用一个一个方法去 . (点,即调用) 得到新的数据结果,可以一步达成. 有多种方式生成 Stream Source: 从 Collection 和数组 Co ...

  5. Cuda Stream流 分析

    Cuda Stream流分析 Stream 一般来说,cuda c并行性表现在下面两个层面上: • Kernel level • Grid level Stream和event简介 Cuda stre ...

  6. CUDA 7 Stream流简化并发性

    CUDA 7 Stream流简化并发性 异构计算是指高效地使用系统中的所有处理器,包括 CPU 和 GPU .为此,应用程序必须在多个处理器上并发执行函数. CUDA 应用程序通过在 streams ...

  7. 关于Adodb.Stream的使用说明

    组件:"Adodb.Stream" 有下列方法: Cancel 方法      使用方法如下      Object.Cancel      说明:取消执行挂起的异步 Execut ...

  8. stream map方法_Java Stream中map和flatMap方法

    最近看到一篇讲stream语法的文章,学习Java中map()和flatMap()方法之间的区别. 虽然看起来这两种方法都做同样的事情,都是做的映射操作,但实际上差之毫厘谬以千里. 通过演示Demo中 ...

  9. stream流map 多个字段_stream流根据对象指定字段去重

    先封装一个去重的方法 import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.fun ...

最新文章

  1. python中国余数定理_Python实现的中国剩余定理算法示例
  2. 2016百度之星 - 初赛(Astar Round2B)解题报告
  3. Java内存分配原理
  4. Mining Precision Interface From Query Logs -- 学习笔记(一)
  5. mysql+mybatis递归调用
  6. 前端的c语言面试题,前端工程师面试题汇总(选择题)
  7. oracle替换表空间,Oracle替换临时表空间
  8. VJC案例-查找最大值
  9. 房友软件注册码暴力输入程序
  10. php xml网站地图生成,PHP代码自动生成XML版网站地图
  11. ObjectiveC基础教程(第2版)
  12. 二叉查找树,平衡二叉树
  13. 通过相关系数和自由度求置信度
  14. win7-去掉快捷方式小箭头.bat
  15. vs code的php格式化
  16. PCIE及南桥芯片组
  17. 哈尔滨工业大学计算机考研专业课,2020考研哈尔滨工业大学计算机考研考试科目...
  18. 电路硬件设计基础(Altium designer的基本使用及一些板子制作流程)20220113
  19. 将Doc/View 应用程序打造成 ActiveX 控件
  20. ubuntu安装pinfi及其实操记录

热门文章

  1. iOS使用自定义字体并找到字体真实名称
  2. 刷机风暴—3Q大战第二季
  3. stm32驱动LED点阵屏(LY-LED16x16)
  4. C语言程序设计课程设计——三国杀游戏
  5. 如何做好企业内部培训?
  6. Virgo与Maven整合开发环境搭建(一)
  7. crmeb多商户系统前端uniapp取消用户短信验证登录教程
  8. EndNote的下载、安装与配置!(绝对保姆级教程)!
  9. Java公历(阳历)转换农历(阴历)工具类
  10. linux p7zip密码,p7zip (简体中文)