#GL0000395. Đếm cặp nguyên tố cùng nhau trong cây con (Coprime Pairs in Rooted Subtrees)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm cặp nguyên tố cùng nhau trong cây con (Coprime Pairs in Rooted Subtrees)
Coprime Pairs in Rooted Subtrees
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree with vertices, rooted at vertex . Each vertex holds a positive integer value .
For every vertex , count the number of unordered pairs of distinct vertices such that both vertices belong to the subtree rooted at and
The subtree includes itself and all descendants of . In this problem, the values are used in the GCD, not the vertex indices. Each unordered pair is counted once.
Input
- The first line contains .
- The second line contains integers .
- Each of the next lines contains two vertices connected by an edge.
Edges can be listed in any order. They form a valid tree.
Output
Print lines. Line must contain the number of qualifying pairs in the subtree of . Use 64-bit integers for the answers.
Subtasks
-
Subtask 1 (20 points): ; ; , ; valid tree.
-
Subtask 2 (30 points): ; ; , ; valid tree.
-
Subtask 3 (50 points): ; ; , ; valid tree.
Examples
Example 1 (official)
Input
8
6 1 10 1 15 7 6 14
1 2
1 3
2 4
2 5
3 6
6 7
6 8
Output
18
3
2
0
0
1
0
0
Explanation
The subtree of vertex contains values ; all three unordered pairs have GCD . The subtree of vertex has values and only qualifies. Vertex contributes two valid pairs in its own subtree, while the root contains valid pairs in total.
Example 2 (official)
Input
3
6 10 15
1 2
2 3
Output
0
0
0
Explanation
Each pair among has GCD greater than , even though the GCD of all three values is .