#GL0000396. Đếm cặp chỉ số nguyên tố cùng nhau trong cây con (Counting Coprime Vertex Indices in a Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm cặp chỉ số nguyên tố cùng nhau trong cây con (Counting Coprime Vertex Indices in a Tree)
Counting Coprime Vertex Indices in a Tree
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a rooted tree with vertex indices and root . You must process independent subtree queries.
For a query vertex , denote the vertex set of its rooted subtree by . Count all pairs satisfying
The strict inequality ensures each unordered pair is counted exactly once. The GCD is applied to the vertex indices ; there is no array of vertex weights in this problem.
Input
- First line: integers .
- Next line: integers , where is the parent of vertex (the list is empty when ).
- Next lines: one query vertex per line.
The parents describe a valid tree rooted at . A parent index need not be smaller than its child's index.
Output
For each query, print one 64-bit integer: the number of valid pairs in the queried subtree.
Subtasks
-
Subtask 1 (20 points): ; ; valid rooted tree.
-
Subtask 2 (30 points): ; ; valid rooted tree.
-
Subtask 3 (50 points): ; ; valid rooted tree.
Examples
Example 1 (official)
Input
2 2
1
1
2
Output
1
0
Explanation
The subtree of vertex contains indices ; the only pair is coprime. The subtree of vertex contains one vertex and therefore contributes no pairs.
Example 2 (additional)
Input
4 4
4 2 1
1
4
2
3
Output
5
2
1
0
Explanation
The tree is the chain . Among all six pairs at the root, only is not coprime, so the first answer is . The subtree of vertex contains and has two coprime pairs; vertex has one coprime pair, ; the leaf vertex has none.