#P10121. 『STA - R4』保险丝
『STA - R4』保险丝
Background
APJ: "? Why is the fuse in my house gone again?"
Problem Description
You are given a rooted tree with nodes, where the root is node .
Define the distance between two node sets as the minimum distance between a node chosen from each set, i.e., $\displaystyle\operatorname{dist}(S_1,S_2)=\min_{\substack{u\in S_1\\v\in S_2}}\operatorname{dist}(u,v)$, where is the distance between nodes and .
Define as the set of all nodes on the simple path from to . Let be the set of all leaves.
For a fixed positive integer , define the set of nodes that satisfy the following conditions to be the semi-neighborhood of , denoted by :
- is in the subtree of .
- $\operatorname{dist}(u,v)\le\operatorname{dist}(\operatorname{path}(1,v),\mathcal L)$.
That is, the semi-neighborhood contains all nodes in the subtree of such that the distance from to is no more than the distance from any node on the path from to the root to its nearest leaf node.
Then define:
$$f(x)=\sum_{u\in\mathring U(x)}\prod_{\substack{v\in\operatorname{subtree}(u)\\v\in\mathring U(x)}}F_{\deg v}$$where is the set of all nodes in the subtree of , is the degree of (the number of nodes adjacent to ), and is the Fibonacci sequence:
$$F_n=\begin{cases}1&n\le 2\\F_{n-1}+F_{n-2}&n\ge 3\end{cases}$$In other words, is the sum of contributions of nodes in the semi-neighborhood of . The contribution of a node to is computed as follows: take every node in the subtree of that is also in the semi-neighborhood of ; if the degree of is , then multiply the contribution of by . The final result is the sum of contributions of all such .
You need to compute . To reduce the output size, you only need to output the XOR of these values modulo , i.e., .
Input Format
The first line contains a positive integer , the number of nodes.
The second line contains positive integers. The -th integer represents the parent of node .
Output Format
One line, the value of .
7
1 1 2 2 3 3
8
14
1 2 3 3 2 6 6 6 9 9 10 11 12
25
Hint
Sample Explanation
In the first sample, the values of at are: .
In the second sample, the values of at are: .
Constraints
This problem uses bundled testdata.
- Subtask 1 (10 pts): .
- Subtask 2 (20 pts): the number of leaves in the tree is at most .
- Subtask 3 (20 pts): there is no node in the tree that has exactly one child.
- Subtask 4 (50 pts): no special restrictions.
For all testdata, , and for each non-root node, its parent index is smaller than its own index.
Translated by ChatGPT 5