Cyclic Shifts
Time limit2sMemory limit512 MB
For each prefix of a string, find the smallest index of its lexicographically minimal cyclic shift, then sum these indices weighted by powers of 8753 modulo 1000000123.
- Level
Hard8 of 10
- Topics
- String, String matching, Math
- Solved
- No attempts yet
Statement
Let be a string of length . For any integer between 0 and , inclusive, the -th cyclic shift of is the string . For example, the 4-th cyclic shift of wellplayed is playedwell, and the 0-th cyclic shift of metro is metro itself.
For a string of length , is the index such that the -th cyclic shift of is the lexicographically smallest among all its cyclic shifts. If several such exist, is the smallest of them. For example, f(acabbac) = 2 and f(cabcab) = 1.
For a string of length , is the sum of over all from 1 to , inclusive. For each given string , find the value of modulo .
Input
The first line contains a single integer (), the number of test cases. Each of the next lines contains a non-empty string of lowercase English letters. The total length of the input strings does not exceed .
Output
For each input string, in input order, output a single integer: modulo .