#P5794. [THUSC 2015] 解密运算

[THUSC 2015] 解密运算

Description

For a string of length NN, we append a special character . to the end of the string. Then we treat the string as a cycle. Starting from positions 1,2,3,,N+11,2,3,\ldots,N+1, read N+1N+1 characters each time, and we can obtain N+1N+1 strings.

For example, for the string ABCAAA, we can get these N+1N+1 strings:

ABCAAA.
BCAAA.A
CAAA.AB
AAA.ABC
AA.ABCA
A.ABCAA
.ABCAAA

Next, we sort these N+1N+1 strings in lexicographical order from small to large (note that the special character . is lexicographically smaller than any other character). The result is:

.ABCAAA
A.ABCAA
AA.ABCA
AAA.ABC
ABCAAA.
BCAAA.A
CAAA.AB

Finally, take the last character of each of the sorted N+1N+1 strings, and concatenate them in order into a new string. That is, the last column of the table above, which is the encrypted ciphertext AAAC.AB.

Please recover the string before encryption from the encrypted ciphertext.

Input Format

The first line contains two integers N,MN,M, representing the length of the string before encryption and the size of the character set. The characters are numbered by integers 1,2,3,,M1,2,3,\ldots,M, and the appended special character . is numbered by 00.

The second line contains N+1N+1 integers, representing the encrypted string.

Output Format

Output a single line containing NN integers separated by spaces, representing the number of each character in the original string before encryption in order.

6 3
1 1 1 3 0 1 2
1 2 3 1 1 1

Hint

For the ii-th test point (i=14i=1 \sim 4), N=5×(i+1),M3N=5\times (i+1),M\leq 3.

For test points 565\sim 6, N,M50N,M\leq 50, and all characters in the string are distinct.

For test points 787\sim 8, N,M1000N,M\leq 1000, and all characters in the string are distinct.

For test points 9129\sim 12, N,M1000N,M\leq 1000.

For test points 132013\sim 20, N,M200000N,M\leq 200000.

Translated by ChatGPT 5