Nice Prefixes
Time limit1sMemory limit128 MB
Count length-L strings over a K-letter alphabet where every prefix keeps all symbol counts within 2 of each other, modulo 1e9+7, with L up to 1e18.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Combinatorics, Math, Matrix
- Solved
- No attempts yet
Problem
Consider strings formed from an alphabet of size . For example, if the alphabet might be , and one such string is .
For a string , let be the number of times the symbol occurs in . For example, and .
A prefix of a string is any string obtained by deleting zero or more trailing characters of . For example, the prefixes of are the empty string, , , and .
A string has nice prefixes if for every prefix of and every two alphabet symbols and , . For example, has nice prefixes, but does not, because and .
Count the number of strings of length over an alphabet of size that have nice prefixes. This number can be large, so print it modulo .
Input
A single line with two integers and separated by a space, where and .
Output
Print a single line with the number of length- strings over an alphabet of size that have nice prefixes, taken modulo .