Under construction

Still being built. Things may change

Marching Orders

Time limit1sMemory limit2048 MB

Summary
Given n and a proposed order of professors, decide whether some m below 10^9 produces that order under the removal method, and print the smallest such m.
Level

Medium6 of 10

Topics
Number theory, Backtracking, Simulation
Solved
No attempts yet

Problem

Dean Bob Roberts is in charge of the order in which the professors of his college march in the graduation ceremonies. After complaints from some professors in the newly created DEI Studies Department about seniority-based ordering, the dean decided the marching order should be random. Bob thinks this is fine. To be transparent, he has published the method he uses to build the marching list.

He starts with an alphabetically ordered list of nn professors numbered 0,1,…,n−10, 1, \ldots, n-1 and a non-negative integer m<109m < 10^{9}. The first person in the marching list is the one at position m mod nm \bmod n in the alphabetic list. That person is removed, which shortens the list by one and shifts down everyone at positions greater than m mod nm \bmod n. The second person is the one at position m mod (n−1)m \bmod (n-1), and so on.

For example, assume there are six professors A, B, C, D, E and F. If m=11679m = 11679, the marching list is created as follows:

nnm mod nm \bmod nalphabetic listmarching list
63A B C D E FD
54A B C E FD F
43A B C ED F E
30A B CD F E A
21B CD F E A C
10BD F E A C B

The method sounds fair, but it is hard to verify. Dean Roberts does not publish the value of mm he uses, so the faculty cannot tell whether he follows the stated method or picked the order by personal preference. The faculty wants to know whether some value of mm produces a given marching order.

Input

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

Output

If the given marching order could not have come from the algorithm, print a single line containing NO. Otherwise, print two lines. The first line contains YES. The second line contains the smallest non-negative value of mm that produces the given marching order.

Examples2

  1. Example 1

    Input
    6
    DFEACB
    
    Expected output
    YES
    39
    
  2. Example 2

    Input
    7
    DFEGACB
    
    Expected output
    NO