#P13804. [SWERC 2023] In-order
[SWERC 2023] In-order
题目描述
:::align{center}
:::
The opening ceremony for the Olympic Games will take place on the river with teams on boats. The layout of the athletes on top of the boat has been designed in a very specific way: for each team, the athletes (conveniently numbered from 1 to ) are arranged as a binary tree.
The organiser has also designed the pre-order traversal, post-order traversal, and a (possibly empty) consecutive part of the in-order traversal of the binary tree that each team must follow.
Now, to make sure there are enough tree layouts so that each team can have a distinct one, you are asked to calculate the quantity of different possible in-order traversals, say , modulo the prime number .
输入格式
The input consists of four lines. The first line contains the number . Each subsequent line contains a list of space-separated integers. The second line contains a list , where is the number of the athlete found in pre-order traversal. The third line contains a list , where is the number of the athlete found in post-order traversal. The fourth line contains a list , where is either the number of the athlete found in in-order traversal, or 0 if the organiser did not say who that athlete should be.
Limits
- ;
- there exists at least one binary tree with such pre-order, post-order and in-order traversals;
- the integers k for which form a (possibly empty) sub-interval of the set ; in other words, whenever and both and are positive, all the integers are positive.
输出格式
The output should contain a single line, consisting of a single integer : this is the only integer such that and for which is divisible by .
8
1 2 3 5 6 4 7 8
5 6 3 8 7 4 2 1
0 0 6 2 4 0 0 0
2
3
1 2 3
3 2 1
0 0 0
4
4
1 2 3 4
4 3 2 1
0 4 0 0
3
提示
Sample Explanation 1
The graphs given above the problem statement are the two possible binary trees. Their in-order traversals are:
Sample Explanation 2
The four possible in-order traversals are:
Sample Explanation 3
The three possible in-order traversals are: