#LCA0000012. Tổ tiên thứ k với truy vấn sinh tự động (K-th Ancestor on Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổ tiên thứ k với truy vấn sinh tự động (K-th Ancestor on Tree)
K-th Ancestor on Tree
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a rooted tree on vertices through . The -th ancestor of is reached after exactly parent steps; the -th ancestor is itself. Every generated query is guaranteed to have an answer.
The queries are generated inside your program using a 32-bit unsigned seed .
Let . For , call get() twice in succession:
where the root has depth and denotes bitwise XOR. Define as the -th ancestor of .
Function get() updates the 32-bit unsigned state :
unsigned int get() {
s ^= s << 13;
s ^= s >> 17;
s ^= s << 5;
return s;
}
Output the bitwise XOR of over all . Perform multiplication in at least 64-bit integer arithmetic.
Input
- The first line contains .
- The second line has parent indices ; for the unique root, otherwise is vertex 's parent.
Output
Print one nonnegative 64-bit integer: the requested XOR value.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
6 3 1
0 1 1 2 2 3
Output
7
Explanation
The generated queries depend on the current state and the previous answer. Preserve the order of the two get() calls; output only the final XOR.