Official

D - Amidakuji 2 Editorial by evima


For a permutation \(P\), let \(ord(P)\) denote the smallest positive integer \(k\) satisfying \(P^k = (1,2,\dots,N)\). Also, let us call the condition that \(Q\) must satisfy simply “the condition.”

Suppose there is a \(Q\) satisfying the condition. Take one such \(Q\). For each \(i\), let \(k_i\) denote the smallest positive integer \(k\) satisfying \(Q^k = P_i\).

First, for each \(i\), \(ord(Q)\) is a multiple of \(ord(P_i)\). That is, \(ord(Q)\) is a multiple of \(L = \mathrm{lcm}(ord(P_1),ord(P_2),\dots,ord(P_M))\).

Furthermore, we can prove the existence of a \(Q\) satisfying \(ord(Q) = L\). Suppose \(ord(Q) = cL\,(c \ge 2)\). Then \(k_i\) is a multiple of \(\frac{cL}{ord(P_i)}\). Noting that \(L \bmod ord(P_i) = 0\), we get \(ord(Q^c) = L\), and \(Q^c\) satisfies the condition. This proves the existence of a \(Q\) satisfying \(ord(Q) = L\). We assume this from now on.

Suppose \(L\) factors as \(L = \prod_{i=1}^{m} p_i^{e_i}\). By the definition of \(L\), there exists a \(P_j\) satisfying \(ord(P_j) \bmod p_i^{e_i} = 0\). Define \(e'_i = \frac{ord(P_j)}{p_i^{e_i}},R_i = P_j^{e'_i}\). With this definition, \(ord(R_i) = p_i^{e_i}\). That is, writing \(R_i = Q^k\), we get \(k = \frac{d_iL}{p_i^{e_i}}\) (where \(d_i\) is an integer not divisible by \(p_i\)).

Then, \(R' = \prod_{i=1}^{m} R_i\) satisfies the condition. Let us prove this. Let \(R' = Q^r\). If \(gcd(r,L) = 1\), then \(R'\) satisfies the condition. Here, \(\displaystyle r = \sum_{i=1}^{m} \frac{d_iM}{p_i^{e_i}}\). We have \(gcd(r,L) = 1 \iff r \bmod p_i \neq 0\) for every \(i\), which is easily proved from \(d_i \bmod p_i \neq 0\).

Now, constructing \(R'\) itself is possible regardless of whether \(Q\) exists: compute \(L\) in factorized form, compute \(R_i\) for each prime factor \(p_i\) of \(L\), and take their product.

For the \(R'\) thus constructed, we need to determine whether there exists \(k\) satisfying \(R'^k = P_i\); looking at each cycle, this yields several conditions of the form \(k \bmod a = b\), so it suffices to determine whether these can be simultaneously satisfied, which can be checked in \(\mathrm{O}(N)\) for one \(P\).

For the part that constructs \(R'\), compute \(L\) in \(\mathrm{O}(NM)\), and compute \(R_i\) and \(R'\) in \(\mathrm{O}(Nm)\). Thus, the whole problem can be solved in \(\mathrm{O}(N^2 + NM)\).


As a bonus, there is also a randomized solution to this problem.

Take \(x_i\) at random, compute \(Q = \prod_{i=1}^{N} P_i^{x_i}\), and check whether \(Q\) satisfies the condition. If \(Q\) satisfies the condition, output it; otherwise, repeat up to \(C\) times.

If there is no \(Q\) satisfying the condition, this algorithm always outputs No. If one exists, consider the probability of success for a single computation of \(Q\). Suppose \(R = Q^k\). We have \(gcd(k,L) = 1 \iff k \bmod p_i \neq 0\) for every \(i\), and since \(x_i\) is chosen randomly, the probability that \(k \bmod p_i = 0\) is \(\frac{1}{p_i}\). That is, we need to estimate the maximum of \(\prod_{i=1}^{c} \frac{p_i-1}{p_i}\) over sets of primes \(P=\lbrace p_1,p_2,\dots,p_c \rbrace\) whose sum is at most \(N\).

Using Mertens’ theorem, we get \(\prod_{p \le n} \frac{p-1}{p}= \frac{e^{-\gamma} + \mathrm{o}(1)}{\log n}\). This value is about \(0.089\) for \(N = 500\). Also, including \(2,3,\dots,59\) gives a sum of \(440\), succeeding about \(13.3\%\) of the time.

By setting \(C = 50\), you can get AC from our judge.