#GL0000257. Chi phí GCD trên cây (GCD cost on the tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chi phí GCD trên cây (GCD cost on the tree)
GCD cost on the tree
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree with vertices numbered from to . Vertex has an associated positive integer . Any pair of vertices is connected by exactly one simple path, which never visits a vertex twice.
For two distinct vertices , let denote the vertices on their simple path, in order. Here counts both endpoints. Define
$$C(s,t)=k\cdot\gcd(A_{p_1},A_{p_2},\ldots,A_{p_k}).$$The greatest common divisor is taken over every vertex value on the path. Since the tree is undirected, .
Find the total cost over all unordered pairs of distinct vertices, each counted once:
Print modulo .
Input
- The first line contains the integer , the number of vertices.
- The second line contains exactly positive integers , the values assigned to vertices through .
- Each of the next lines contains integers , denoting an undirected edge between these vertices. It is guaranteed that and all edges form a tree.
Output
Print one integer: the remainder of upon division by .
Subtasks
- Subtask 1 (20 points): ; for every ; for every ; the edges form an undirected tree.
- Subtask 2 (30 points): ; for every ; for every ; the edges form an undirected tree.
- Subtask 3 (50 points): ; for every ; for every ; the edges form an undirected tree.
Example 1
Input
4
24 30 28 7
1 2
1 3
3 4
Output
47
Explanation
The edges are , and . We enumerate all six unordered pairs:
- : two vertices and give .
- : two vertices and give .
- : the path has three vertices; , giving .
- : the path has three vertices; , giving .
- : the path has four vertices; , giving .
- : two vertices and give .
Their sum is , which is already smaller than .
Example 2
Input
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
Output
1184
Explanation
There are vertices and edges forming a tree, with associated values .
For instance, the path between and is : and , so its cost is . The path between and is : and , so its cost is . The edge path between and contributes .
Applying the definition to all unordered pairs yields the total . Since is below the modulus, the output is 1184.