Official

A - AtCoder Reverse Contest Editorial by evima


First, consider a string \(S\) of length \(99\) where the even-indexed characters are R, and the odd-indexed characters are A or C. For this \(S\), define an integer sequence \(A=(A_1,\ldots,A_{50})\) by setting \(A_i=1\) if \(S_{2i-1}=\) A, and \(A_i=0\) if \(S_{2i-1}=\) C. With this definition, \(f(S)\) coincides with the number of inversions of \(A\).

Among length-\(50\) integer sequences consisting of \(0\)s and \(1\)s, the number of inversions is maximized when \(25\) \(1\)s are lined up followed by \(25\) \(0\)s, in which case the number of inversions is \(25^2=625\). Therefore, starting from this sequence \(A\) with \(625\) inversions, we can repeat \(625-X\) times the operation of replacing a contiguous subsequence \(1,0\) with \(0,1\) to make the number of inversions exactly \(X\). From that string with exactly \(X\) inversions, we can reconstruct the string \(S\), which satisfies \(f(S)=X\).

Sample implementation (Python3)

a = [1] * 25 + [0] * 25
for _ in range(625 - int(input())):
    for i in range(len(a) - 1):
        if (a[i], a[i + 1]) == (1, 0):
            a[i], a[i + 1] = 0, 1
            break
print("R".join("CA"[x] for x in a))

posted:
last update: