#P17361. [ECNA 2024] Marching Orders

    ID: 19779 远端评测题 1000ms 2048MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>数学2024数论最大公约数 gcd扩展欧几里德算法不定方程中国剩余定理 CRTICPC

[ECNA 2024] Marching Orders

Problem Description

Dean Bob Roberts is in charge of the order in which the professors of his college march in the graduation ceremonies. Because of complaints among certain professors from the newly created DEI Studies Department, it has been decided that the order in which they march should not be based on seniority but should be random. Bob thinks this is fine, and to be totally transparent he has communicated the following method he will use to create the marching list: He starts with an alphabetically ordered list of nn professors numbered 0,1,,n10, 1, \ldots, n-1 and a non-negative integer m<109m < 10^{9}. Then the first person in the marching list is the one in position m mod nm~\textrm{mod}~n in the alphabetic list. This shortens the alphabetic list by one, shifting down all those in positions greater than m mod nm~\textrm{mod}~n. The second person in the marching list is the one in position m mod (n1)m~\textrm{mod}~(n-1), and so on.

For example, assume we have 66 professors A, B, C, D, E and F. If m=11679m = 11679, then the marching list is created as follows:

nn mmodnm\bmod n Alphabetic list Marching list
66 33 A B C D E F D
55 44 A B C E F D F
44 33 A B C E D F E
33 00 A B C D F E A
22 11 B C D F E A C
11 00 B D F E A C B

This sounds fair, but is not as transparent as some professors would like as Dean Roberts does not make public the value of mm that he uses. This makes it difficult to determine if he is actually following the method he has stated or has just selected the marching order based on his personal whims and biases. What the faculty would like to know is, for a given marching order, is there a value of mm which would produce that order?

Input Format

The first line of input contains a single decimal integer nn (5n20)(5 \leq n \leq 20), the number of professors who will be marching. The second line consists of a string containing a permutation of the first nn uppercase letters of the alphabet, giving the proposed marching order.

Output Format

If the given marching order could not have come from the algorithm, output a single line containing the word NO. Otherwise, output two lines, the first containing the word YES and the second containing the smallest non-negative value of mm which could produce the given marching order.

6
DFEACB
YES
39
7
DFEGACB
NO