PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000012. Tổ tiên thứ k với truy vấn sinh tự động (K-th Ancestor on Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 11 through nn. The kk-th ancestor of xx is reached after exactly kk parent steps; the 00-th ancestor is xx itself. Every generated query is guaranteed to have an answer.

The qq queries are generated inside your program using a 32-bit unsigned seed ss.

Let ans0=0ans_0=0. For i=1,…,qi=1,\ldots,q, call get() twice in succession:

x_i=((get()\oplus ans_{i-1})mod n)+1, k_i=(get()\oplus ans_{i-1})mod depth[x_i],

where the root has depth 11 and ⊕\oplus denotes bitwise XOR. Define ansians_i as the kik_i-th ancestor of xix_i.

Function get() updates the 32-bit unsigned state ss:

unsigned int get() {
    s ^= s << 13;
    s ^= s >> 17;
    s ^= s << 5;
    return s;
}

Output the bitwise XOR of i⋅ansii\cdot ans_i over all ii. Perform multiplication in at least 64-bit integer arithmetic.

Input

  • The first line contains n,q,sn,q,s.
  • The second line has nn parent indices p1,…,pnp_1,\ldots,p_n; pi=0p_i=0 for the unique root, otherwise pip_i is vertex ii's parent.

Output

Print one nonnegative 64-bit integer: the requested XOR value.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; q≤60q\le60; 2≤n≤5⋅1052\le n\le5\cdot10^5; 1≤q≤5⋅1061\le q\le5\cdot10^6; 1≤s<2321\le s<2^{32}.
  • Subtask 2 (30 points): n≤1000n\le1000; q≤3000q\le3000; 2≤n≤5⋅1052\le n\le5\cdot10^5; 1≤q≤5⋅1061\le q\le5\cdot10^6; 1≤s<2321\le s<2^{32}.
  • Subtask 3 (50 points): 2≤n≤5⋅1052\le n\le5\cdot10^5; 1≤q≤5⋅1061\le q\le5\cdot10^6; 1≤s<2321\le s<2^{32}.

Examples

Input

6 3 1
0 1 1 2 2 3

Output

7

Explanation

The generated queries depend on the current state ss and the previous answer. Preserve the order of the two get() calls; output only the final XOR.