#P11006. [2016杭电多校]Born Slippy
[2016杭电多校]Born Slippy
Born Slippy
Problem Description
Professor Zhang has a rooted tree, whose vertices are conveniently labeled by . And the -th vertex is assigned with weight . For each , Professor Zhang wants find a sequence of vertices such that:
- and is the ancestor of .
- the value $f(s)=w_{v_1}+\sum\limits_{i=2}^{m}w_{v_i} \text{ opt } w_{v_{i-1}}$ is maximum. Operation denotes bitwise AND, OR or XOR operation of two numbers.
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case: The first line contains an integer and a string $(2 \le n \le 2^{16}, opt \in \{\text{AND}, \text{OR}, \text{XOR}\}$) -- the number of vertices and the operation. The second line contains integers . The thrid line contain integers , where is the father of vertex . There are about test cases and the sum of in all the test cases is no more than .
Output
For each test case, output an integer $S=(\sum\limits_{i=1}^{n}{i \cdot f(i)}) \text{ mod } (10^9 + 7)$.
Sample Input
3
5 AND
5 4 3 2 1
1 2 2 4
5 XOR
5 4 3 2 1
1 2 2 4
5 OR
5 4 3 2 1
1 2 2 4
Sample Output
91
139
195
Author
zimpha
Source
2016 Multi-University Training Contest 2