#abc248g. G - GCD cost on the tree
G - GCD cost on the tree
Score : points
问题描述
你被给定了一棵包含 个顶点的无向树。
我们将这些顶点依次标记为顶点 1、顶点 2、……、顶点 。对于每个 ,第 条边连接着顶点 和顶点 。
另外,每个顶点都被赋予了一个正整数:顶点 被赋予了数值 。
两个不同顶点 和 之间的代价 定义如下:
让 , , , 表示连接顶点 和顶点 的简单路径上的顶点序列,其中 是路径上顶点的数量(包括端点)。
那么,令 $C(s,t)=k\times \gcd (A_{p_1},A_{p_2},\ldots,A_{p_k})$,
其中 表示 的最大公约数。
求 对 取模的结果。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given an undirected tree with vertices.
Let us call the vertices Vertex , Vertex , , Vertex . For each , the -th edge connects Vertex and Vertex .
Additionally, each vertex is assigned a positive integer: Vertex is assigned .
The cost between two distinct vertices and , , is defined as follows.
Let , , , be the vertices of the simple path connecting Vertex and Vertex , where is the number of vertices in the path (including the endpoints).
Then, let $C(s,t)=k\times \gcd (A_{p_1},A_{p_2},\ldots,A_{p_k})$,
where denotes the greatest common divisor of .
Find , modulo .
Constraints
- All values in input are integers.
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
Output
Print , modulo .
Sample Input 1
4
24 30 28 7
1 2
1 3
3 4
Sample Output 1
47
There are edges directly connecting Vertex and , Vertex and , and Vertex and . Thus, the costs are computed as follows.
Thus, the sought value is $\displaystyle\sum_{i=1}^{3}\sum_{j=i+1}^4 C(i,j)=(12+8+3)+(6+4)+14=47$ modulo , which is .
Sample Input 2
10
180 168 120 144 192 200 198 160 156 150
1 2
2 3
2 4
2 5
5 6
4 7
7 8
7 9
9 10
Sample Output 2
1184
update @ 2024/3/10 10:38:07