| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 1024 MB | 23 | 9 | 9 | 39.130% |
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 $N$ athletes (conveniently numbered from 1ドル$ to $N$) 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 $T,ドル modulo the prime number 999ドル,円 999,円 937$.
The input consists of four lines. The first line contains the number $N$. Each subsequent line contains a list of $N$ space-separated integers. The second line contains a list $A_1, A_2, \dots , A_N,ドル where $A_k$ is the number of the $k$th athlete found in pre-order traversal. The third line contains a list $B_1, B_2, \dots , B_N,ドル where $B_k$ is the number of the $k$th athlete found in post-order traversal. The fourth line contains a list $C_1, C_2, \dots , C_N,ドル where $C_k$ is either the number of the $k$th athlete found in in-order traversal, or 0ドル$ if the organiser did not say who that $k$th athlete should be.
The output should contain a single line, consisting of a single integer $S$: this is the only integer such that 0ドル \le S < 999,円 999,円 937$ and for which $T - S$ is divisible by 999ドル,円 999,円 937$.
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
The graphs given above the problem statement are the two possible binary trees. Their in-order traversals are:
3 1 2 3 3 2 1 0 0 0
4
The four possible in-order traversals are:
4 1 2 3 4 4 3 2 1 0 4 0 0
3
The three possible in-order traversals are: