LCA Problem Revisited (Easy)
Given the hanging tree with n (1 ≤ n ≤ 100) vertices, numbered from 0 to n - 1. You need to answer m (1 ≤ m ≤ 100) queries about the least common ancestor for the given pair of vertices.
The queries are generated next way. Given the numbers a[1]
, a[2]
and numbers x, y and z. The numbers a[3]
, ..., a[2m]
are generated next way: a[i]
= (x · a[i-2]
+ y · a[i-1]
+ z) mod n. The 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
The first line contains two integers n and m. The root of the tree has number 0. The second line contains n - 1 integers, the i-th of which equals to the parent number of vertex i. The third line contains two integers in the range from 0 to n - 1: a[1]
and a[2]
. The fourth line contains three integers x, y and z, these numbers are nonnegative and do not exceed 10^9
.
Output
Print the sum of vertex numbers - the answers to the queries.