D - Mirror and Order Editorial by evima
Since \(1\) to \(N\) appear twice each at the beginning of \(s\) and \(t\), the first term of \(s_i\) is determined by \((A_i + 1)/2\) (rounded down). If there are duplicates in the first terms, the rules for generating \(s\) and \(t\) are not satisfied, so obviously there are \(0\) ways. Hereafter, we consider the case where all first terms are distinct.
For pairs where the first term of \(s_i\) and the first term of \(t_j\) (which is the last term of \(s_j\)) are the same number, their order is determined by the middle term. Now, suppose the \(N\) sequences are fixed, and consider the following directed graph corresponding to them.
- The vertices \(1,2,\ldots,N\) correspond to the \(N\) sequences.
- If the first term of \(s_i\) and the last term of \(t_j\) are the same, draw an edge from \(i\) to \(j\) if \(a_i < b_j\), and in the reverse direction otherwise.
Let’s consider the properties of this graph.
First, from the conditions, the degree of each vertex is \(2\). That is, each connected component forms a loop. Also, self-loops cannot occur. This is because it would mean that the sequence corresponding to that vertex has the same first and last terms, making the order between \(s_i\) and \(t_i\) undefined.
Furthermore, since the direction of the edges represents the order of the middle terms, there must exist an assignment of middle terms that does not contradict this. This is possible by taking an appropriate topological order if all strongly connected components have size \(1\) (we have already excluded self-loops that would cause contradictions). Since all connected components in our graph are loops, unless the entire graph is strongly connected—that is, all edges are aligned in the same direction—the condition is satisfied.
Here, the given \(A\) determines the direction of the edge corresponding to the first term of each vertex \(i\). Let’s call a vertex \(i\) a white vertex if the edge goes out from \(i\), and a black vertex if it goes into \(i\). If a connected component consists only of vertices of the same color, we cannot reconstruct the corresponding sequences based on the above considerations. Conversely, if it includes even one vertex of a different color, we can reconstruct the original \(N\) sequences.
The sequences \(B\) in this problem correspond one-to-one with the possible shapes of the graph considered above, so we count the shapes of the graph. By connecting some vertices using the already determined elements of \(B\), we can obtain:
- \(W\) paths consisting only of white vertices (including isolated vertices)
- \(B\) paths consisting only of black vertices (including isolated vertices)
- \(G\) paths consisting of both white and black vertices (hereafter called gray paths)
- Loops
If there are loops consisting only of vertices of one color, they cannot be generated, so in this case the answer is \(0\). Otherwise, we consider the constructed paths as vertices, and think of them as \(W\) white vertices, \(B\) black vertices, and \(G\) gray vertices. Then, we need to count the number of ways to form loops such that there are no loops consisting only of white vertices or only of black vertices. This can be calculated as follows.
[Method 1] Connecting Maximal Paths of Each Color
In the constructed graph, extract the paths consisting of white vertices and black vertices so that they are maximal, and suppose there are \(w\) and \(b\) such paths, respectively.
First, consider making some loops using only gray vertices, and then inserting white and black paths afterward. The number of ways to create loops from the gray vertices is:
- \(G!\) ways
Next, create \(w\) white paths. Arrange the \(W\) vertices in a line and cut them at \(w-1\) places. Since we count the same arrangement \(w!\) times, we adjust by dividing:
- \(W! \times {}_{W-1}C_{w-1} / w!\) ways
- However, if \(W=0, w=0\) (no white vertices), then \(1\) way
Let’s denote this as \(f(W, w)\).
Next, if \(s\) out of the \(w\) white paths are connected to black vertices immediately afterward, the remaining \(w - s\) paths are connected to gray vertices. The number of ways to connect them is:
- \( {}_wC_{s} \times \lbrace b!/(b-s)!\rbrace \times\lbrace G!/(G-w+s)!\rbrace \) ways
Furthermore, for the \(b\) black paths, the number of ways to connect them to the remaining \(G + s\) vertices (white or gray) is:
- \((G+s)!/(G+s-b)!\) ways
Putting it all together, we get:
\[ (G!)^2 \times (G+s)! \times \Big\{ \frac{f(W,w)\times {}_wC_s}{(G-w+s)!} \Big\} \times \Big\{ \frac{f(B,b)\times b! }{(b-s)!\times (G+s-b)!} \Big\} \]
The sum over all \(s, w, b\) is the answer.
Here, since the formula can be separated into parts that depend only on \(s\) and \(w\) (the first curly brackets) and parts that depend only on \(s\) and \(b\) (the second curly brackets), we can precompute the sums for fixed \(s\) and then combine them for each \(s\), allowing us to compute the sum in \(O(N^2)\) time.
[Method 2] Using the Principle of Inclusion-Exclusion
Impose a condition on \(w_a\) of the \(W\) white vertices to “belong to loops consisting only of vertices of the same color” (①). Then, among the remaining vertices, let \(w_b\) belong to the same loops as ① (②), and the remaining \(w_c\) do not (③).
First, the number of ways to partition the white vertices into ①②③ is:
- \(W! / (w_a! w_b! w_c!)\) ways
Next, the number of ways to create loops using only ① vertices is:
- \(w_a!\) ways
Then, the number of ways to insert the ② vertices into these loops is:
- \((w_a + w_b - 1)! / (w_a - 1)!\) ways
- Exceptionally, when \(w_a = 0, w_b = 0\), there is \(1\) way
Furthermore, if we use the ③ vertices, \(b_c\) black vertices corresponding to ③, and \(G\) gray vertices to create loops, there are:
- \((w_c + b_c + G)!\) ways
For black vertices, the grouping and ways to create loops for ① and ② are the same. Finally, we multiply by the inclusion-exclusion coefficient:
- \((-1)^{w_a + b_a}\)
Organizing everything, we have:
\[ \Big\{ (-1)^{w_a} \times \frac{W!\times (w_a+w_b-1)!}{w_b!\times w_c!\times (w_a-1)!} \Big\} \times \Big\{ (-1)^{b_a} \times \frac{B!\times (b_a+b_b-1)!}{b_b!\times b_c!\times (b_a-1)!} \Big\} \times (w_c+b_c+G)! \]
We want to compute the sum over all \(w_a, w_b, w_c, b_a, b_b, b_c\).
Here, by precomputing the sums of parts that depend only on \(w_a, w_b\) when \(w_c\) is fixed (the parts inside the first curly brackets), we can then sum over combinations of \(w_c\) and \(b_c\), both of which can be computed in \(O(N^2)\) time.
Moreover, since each part of the calculation can be expressed as convolutions, it is possible to compute in \(O(N \log N)\) time.
Sample implementation (Method 1): https://atcoder.jp/contests/arc188/submissions/60033190
posted:
last update: