#P5298. [PKUWC2018] Minimax
[PKUWC2018] Minimax
Description
Little C has a rooted tree with nodes. The root is node , and each node has at most two children.
Define the value of a node as follows:
-
If has no children, then its value is given in the input. It is guaranteed that all such nodes have pairwise distinct values.
-
If has children, then its value equals the maximum of its children’s values with probability , and equals the minimum of its children’s values with probability .
Now Little C wants to know the following. Suppose the value of node has possible outcomes. Let the value of the -th smallest outcome be , and its probability be . Compute:
You need to output the answer modulo .
Input Format
The first line contains a positive integer .
The second line contains integers. The -th integer is the index of the parent of node . The parent of node is .
The third line contains integers. If node has no children, then the -th number is its value; otherwise, the -th number is . It is guaranteed that is a positive integer.
Output Format
Output the answer.
3
0 1 1
5000 1 2
748683266
Hint
Sample Explanation
The value of node is with probability , and with probability , so the answer is .
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- Another of the testdata guarantees that the shape of the tree is random.
- For of the testdata, , .
For all testdata, , so it is easy to prove that every leaf value has a positive probability of being chosen by the root.
Translated by ChatGPT 5
京公网安备 11011102002149号