Official

F - Both Reversible Editorial by evima


First, let’s define some terms.
The string formed by \(n\) repetitions of a string \(T\) is called the power of \(T\) and is denoted as \(T^n\).
Furthermore, a string \(T\) is said to be primitive if it cannot be expressed as \(T = u^n\) using some string \(u\) and an integer \(n\) not less than \(2\). Then, for a string \(S\), there is a unique primitive string \(T\) that can be represented as \(S = T^n\) using a positive integer \(n\). This is called the primitive root of the string \(S\).

Now, let’s first consider the properties of non-empty strings \(A\) and \(B\) such that both \(A + \mathrm{rev}(B)\) and \(\mathrm{rev}(A) + B\) are palindromes.
Assume \(|A| \leq |B|\). Since the suffix of \(\mathrm{rev}(B)\) matches \(A\), if we set \(\mathrm{rev}(B) = C + \mathrm{rev}(A)\), then:

  • \(A + C + \mathrm{rev}(A)\) is a palindrome \(\to\) \(C\) is a palindrome.
  • \(\mathrm{rev}(A) + B = \mathrm{rev}(B) + A = C + \mathrm{rev}(A) + A\) is a palindrome.

Therefore, if we set \(C = x\) and \(\mathrm{rev}(A) + A = y\), this relationship holds: \(x\), \(y\), and \(x + y\) are all palindromes.

Here, when all of \(x\), \(y\), and \(x + y\) are palindromes, it can be confirmed that they can be expressed as \(x = T^n\), \(y = T^m\), and \(x + y = T^{n+m}\) using a primitive string \(T\) and non-negative integers \(n\) and \(m\).

  • (Proof) Without loss of generality, we can assume \(|x| \leq |y|\). Since \(x\) is a prefix of \(y\), so if we set \(y=x+z\), then \(z\) is also a palindrome since \(x+z+x\) is a palindrome, and this relationship holds: \(z\), \(x\), and \(z + x\) are all palindromes. By recursively repeating this operation, it can be confirmed that there is a palindrome \(U\) such that both \(x\) and \(y\) are powers of \(U\). Let \(T\) be the primitive root of \(U\), and \(x\), \(y\), and \(x + y\) are also powers of \(T\). (End of proof)

Therefore, by dividing into cases based on the parity of \(m\), we get the following:

  • When \(m\) is even: \(A = T^{m/2}\) and \(B = T^{n + m/2}\).
  • When \(m\) is odd: Let \(T = \mathrm{rev}(X) + X\), and \(A = X + T^{\lfloor m/2 \rfloor}\) and \(B = X + T^{\lfloor m/2 \rfloor + n}\).

The same conclusion can be drawn for the case \(|A| \geq |B|\), so the conditions for \(A\) and \(B\) are as follows:

  • Let \(T\) be the primitive root of \(\mathrm{rev}(A) + B\). (\(T\) will be a palindrome.) The conditions are satisfied in the following two cases:
    1. There are positive integers \(n\) and \(m\) such that \(A = T^n\) and \(B = T^m\).
    2. There is \(X\) satisfying \(T = \mathrm{rev}(X) + X\) and non-negative integers \(n\) and \(m\) such that \(A = X + T^n, B = X + T^m\).

Note that in case 2., \(X \neq \mathrm{rev}(X)\) (if \(X = \mathrm{rev}(X)\), it would contradict the fact that \(T\) is primitive).

Next, for a given good string \(S\), consider the number of ways to split it into \(A\) and \(B\) to satisfy the conditions.
If \(S\) is a palindrome, it can be seen that only case 1 is possible. Therefore, when \(S = T^n\), there are \(n - 1\) positions for the split.
If \(S\) is not a palindrome, only case 2 is possible.
In fact, it can be proven that when a good string \(S\) is not a palindrome, there is only one way to split it into \(A\) and \(B\).

(Proof)
Let’s assume that both \((A, B)\) and \((C, D)\) are splits that satisfy the conditions. Now, suppose \(A + \mathrm{rev}(B) = C + \mathrm{rev}(D)\) holds. In this case, it can be proven that \((A, B) = (C, D)\).

  • (Proof) Let \(A + \mathrm{rev}(B) = C + \mathrm{rev}(D) = S'\). One can restore \(X\) from \(T\) by finding the primitive root \(T\) of \(S'\). Once \(X\) is determined, the method to convert \(S\) to \(S'\) is uniquely determined. Therefore, \((A, B) = (C, D)\). (End of proof)

Therefore, showing that \(A + \mathrm{rev}(B) = C + \mathrm{rev}(D)\) (or \(\mathrm{rev}(A) + B = \mathrm{rev}(C) + D\)) holds for any split \((A, B), (C, D)\) proves that the split is unique.

When \(|A| \geq |B|\), since \(A + \mathrm{rev}(B)\) is a palindrome, it has the first half of \(S\) folded. The same applies to \(C + \mathrm{rev}(D)\) when \(|C| \geq |D|\). Therefore, \(A + \mathrm{rev}(B) = C + \mathrm{rev}(D)\) holds when \(|A| \geq |B|\) and \(|C| \geq |D|\).
The case of \(|A| \leq |B|\) and \(|C| \leq |D|\) can be similarly confirmed to satisfy \(\mathrm{rev}(A) + B = \mathrm{rev}(C) + D\).

From now on, we assume \(|A| < |B|\) and \(|C| > |D|\) (the case of \(|A| > |B|\) and \(|C| < |D|\) is similar).
Let the decomposition of \(S\) using \(X\) corresponding to \((A, B)\) be:

\[S = X + \mathrm{rev}(X) + X + \dots + X + X + \dots + X + \mathrm{rev}(X) + X\]

and similarly, let the decomposition of \(S\) using \(Y\) corresponding to \((C, D)\) be:

\[S = Y + \mathrm{rev}(Y) + Y + \dots + Y + Y + \dots + Y + \mathrm{rev}(Y) + Y\]

Here, the \(+\) of \(X + X\) corresponds to the boundary between \(A\) and \(B\), and the \(+\) of \(Y + Y\) corresponds to the boundary between \(C\) and \(D\). (Note that \(X \neq \mathrm{rev}(X)\) and \(Y \neq \mathrm{rev}(Y)\).)

Since \(|A| < |B|\), the boundary in \(X + X\) is to the left of the overall midpoint. Also, since there are an even number of clusters of \(X\) and \(\mathrm{rev}(X)\) in the entire \(S\), they do not cross the center of \(S\). Therefore, \(X + X\) falls into the left half of \(S\).
By a similar argument, \(Y + Y\) falls into the right half of \(S\).

Next, consider the parity of \(|S| / 2|X|\) and \(|S| / 2|Y|\).
Now, suppose \(|S| / 2|Y|\) is even. The left half of \(S\) is \(Y + \mathrm{rev}(Y) + \dots + Y + \mathrm{rev}(Y)\) and is a palindrome. On the other hand, the left half of \(S\) contains \(X + X\), so the left half of \(S\) is one of the following:

  • \(X + \mathrm{rev}(X) + \dots + X + X + \dots + \mathrm{rev}(X) + X\)
  • \(X + \mathrm{rev}(X) + \dots + X + X + \dots + X + \mathrm{rev}(X)\)

Considering that this must be a palindrome, in the first case, looking at both ends reveals that \(X = \mathrm{rev}(X)\), which is a contradiction. In the second case, as well, looking from both ends, either \(X = \mathrm{rev}(X)\) at some point, or \(X\) must be a palindrome, which contradicts \(X \neq \mathrm{rev}(X)\). Thus, the case where \(|S| / 2|Y|\) is even leads to a contradiction.

A similar method shows that a contradiction arises when \(|S| / 2|X|\) is even. Thus, in the remaining case, both \(|S| / 2|X|\) and \(|S| / 2|Y|\) are odd.
Here, let \(\vert\) represent the central boundary of \(S\). Since \(|S| / 2|X|\) and \(|S| / 2|Y|\) are odd, we have:

\[S = X + \dots + \mathrm{rev}(X) \vert X + \dots + X\]

\[S = Y + \dots + Y \vert \mathrm{rev}(Y) + \dots + Y\]

Consider the case \(|X| \leq |Y|\). By focusing on the left side of the center and the end of \(S\), we obtain:

\[\mathrm{rev}(X) = (\text{a suffix of } Y) = X\]

which is a contradiction. Similarly, in the case \(|X| \geq |Y|\), by focusing on the right side of the center and the beginning of \(S\), we obtain:

\[\mathrm{rev}(Y) = (\text{a prefix of } X) = Y\]

which is a contradiction.

Thus, it has been confirmed that the case \(|A| < |B|\) and \(|C| > |D|\) leads to a contradiction.
Therefore, it has been proved that \(A + \mathrm{rev}(B) = C + \mathrm{rev}(D)\) or \(\mathrm{rev}(A) + B = \mathrm{rev}(C) + D\) holds for any split \((A, B), (C, D)\), from which it is proved that the split is unique. (End of proof)

Based on the above discussion and using the inclusion-exclusion principle, the problem can be reduced to counting the splits in both cases of palindromes and non-palindromes, which can then be solved. (This part is also somewhat complex, but we will omit the details.) The computational complexity is sufficiently small at about \(\mathrm{O}(N \times (\text{number of divisors of } N))\) or \(\mathrm{O}(N \log N \times (\text{number of divisors of } N))\).

posted:
last update: