Count valid bracket strings of 2N brackets over K colors that equal their own reversal, modulo 1e9+7.
Hard9CombinatoricsMathDynamic programmingNumber theoryNo attempts yetTime limit2sMemory limit512 MBA valid bracket string is defined by the following rules.
Someone likes bracket strings very much. Using a single kind of bracket got dull, so he decided to paint the brackets with K colors that can be told apart. An opening bracket of color i pairs only with a closing bracket of color i. For example, if K=3 and the colors are red, green and blue, the second rule above grows into this.
For a larger K, 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 2N brackets in K colors that are equal to their own reversal.
The first line contains two integers N, the number that fixes how many brackets are used, and K, the number of colors, separated by a space. (1≤N≤106, 1≤K≤106)
Print how many valid bracket strings built from 2N brackets in K colors are equal to their own reversal. This number can grow very large, so print it modulo 1,000,000,007.
For N=2 and K=2 there are 6 strings that satisfy the condition. Writing the two colors as 1 and 2 in subscripts, they are:
(1(1)1)1, (1(2)2)1, (2(1)1)2, (2(2)2)2, (1)1(1)1, (2)2(2)2
