#P5404. Infinite Repetition and Meaningful Fragment
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 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 and is lexicographically smaller than is called a meaningful semantic fragment.
Given an integer and a string (of length ), count how many different basic sentences (i.e. –letter strings) have the following property: when repeated infinitely, at least one fragment of length (obtained from some starting position) is lexicographically smaller than .
It can be shown that for any basic sentence , the set of all fragments of length in (i.e. concatenated with itself) is exactly the set of its cyclic rotations. Hence the problem is equivalent to counting the number of –letter strings whose lexicographically minimal rotation is smaller than .
inputFormat
The input consists of two lines. The first line contains an integer . The second line contains a string of length consisting of lowercase letters.
outputFormat
Output a single integer: the number of basic sentences (i.e. strings of length ) for which, after infinite repetition, there exists at least one substring of length that is lexicographically smaller than .
sample
2
ba
25