LCA - 2
The hanging tree with n (1 ≤ n ≤ 10^5
) vertices numbered from 0 to n - 1 is given. Answer m (1 ≤ m ≤ 10^7
) queries about Least Common Ancestor for pair of vertices.
The queries are numbered in the next way. Given values a[1]
, a[2]
and x, y и z. Numbers a[3]
, ..., a[2m]
are generated in the next way:
a[i]
= (x * a[i-2]
+ y * a[i-1]
+ z) mod n.
First query has the form <a[1]
, a[2]
>. If the answer to the (i - 1)-th query is v, then the i-th query has the form <(a[2i-1]
+ v) mod n, a[2i]
>.
Input
First line contains two numbers n and m. The root of the tree has number 0. Second line contains n - 1 integers, i-th of these numbers equals to the parent of the vertex i. Third line contains two integers in the range from 0 to n - 1: a[1]
and a[2]
. Fourth line contains three integers: x, y and z, they are non-negative and no more than 10^9
.
Output
Print the sum of the vertex numbers - the answers to all queries.