#P5404. Infinite Repetition and Meaningful Fragment

    ID: 18636 Type: Default 1000ms 256MiB

Infinite Repetition and Meaningful Fragment

Infinite Repetition and Meaningful Fragment

In country A, people love repetition. A basic sentence is represented by a string of exactly mm lowercase letters. To show their affection for repetition, they repeat this sentence infinitely many times.

A contiguous substring (of the infinite repetition) that has the same length as a given string ss and is lexicographically smaller than ss is called a meaningful semantic fragment.

Given an integer mm and a string ss (of length mm), count how many different basic sentences (i.e. mm–letter strings) have the following property: when repeated infinitely, at least one fragment of length mm (obtained from some starting position) is lexicographically smaller than ss.

It can be shown that for any basic sentence TT, the set of all fragments of length mm in TTT T (i.e. TT concatenated with itself) is exactly the set of its cyclic rotations. Hence the problem is equivalent to counting the number of mm–letter strings whose lexicographically minimal rotation is smaller than ss.

inputFormat

The input consists of two lines. The first line contains an integer mm. The second line contains a string ss of length mm consisting of lowercase letters.

outputFormat

Output a single integer: the number of basic sentences (i.e. strings of length mm) for which, after infinite repetition, there exists at least one substring of length mm that is lexicographically smaller than ss.

sample

2
ba
25