Colored Brackets
Time limit2sMemory limit512 MB
Count valid bracket strings of 2N brackets over K colors that equal their own reversal, modulo 1e9+7.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math, Dynamic programming, Number theory
- Solved
- No attempts yet
Problem
A valid bracket string is defined by the following rules.
- The empty string is a valid bracket string.
- If is a valid bracket string, then is a valid bracket string. Putting an opening bracket in front of a valid bracket string and a closing bracket behind it keeps it valid.
- If and are valid bracket strings, then is a valid bracket string. Concatenating valid bracket strings keeps them valid.
Someone likes bracket strings very much. Using a single kind of bracket got dull, so he decided to paint the brackets with colors that can be told apart. An opening bracket of color pairs only with a closing bracket of color . For example, if and the colors are red, green and blue, the second rule above grows into this.
- If is a valid bracket string, then wrapped in red brackets, wrapped in green brackets and wrapped in blue brackets are all valid bracket strings. The opening bracket and the closing bracket of one pair always have the same color.
For a larger , add more distinguishable colors and extend the definition the same way.
Reversing a string means writing it down the way it looks in a mirror. The order of the characters is reversed, and each bracket becomes the bracket of the opposite direction in the same color. The colors stay where they are. With a single color, reversing (())() gives ()(()). That string differs from its reversal, so it must not be counted. ()(())() stays ()(())() after reversing, so it must be counted.
Count the valid bracket strings built from brackets in colors that are equal to their own reversal.
Input
The first line contains two integers , the number that fixes how many brackets are used, and , the number of colors, separated by a space. (, )
Output
Print how many valid bracket strings built from brackets in colors are equal to their own reversal. This number can grow very large, so print it modulo 1,000,000,007.
Hint
For and there are 6 strings that satisfy the condition. Writing the two colors as 1 and 2 in subscripts, they are:
, , , , ,
