Parentheses
Time limit1sMemory limit512 MB
Count parentheses strings of length n by their reversal-and-flip operation distance, then output the weighted sum of those counts modulo m.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math, Dynamic programming
- Solved
- No attempts yet
Problem
A correct parentheses sequence is defined recursively as follows:
- The empty string is a correct sequence.
- If and are correct sequences, then (the concatenation of and ) is a correct sequence.
- If is a correct sequence, then is a correct sequence.
Every correct parentheses sequence can be derived using the rules above.
For a parentheses sequence, you can perform some operations on it.
- Each time, choose two indices and such that . The operation modifies the characters at indices from to , inclusive.
- First, the order of these characters is reversed.
- Then, each character is toggled to the opposite one. That is, each '
(' in the range becomes ')' and each ')' becomes '('.
The value of a parentheses sequence is the minimum number of operations required to change it into a correct parentheses sequence. If it is impossible, the value is .
For example, the value of "()((" is , the value of "()()" is , and the value of "(((" is .
You are given an integer . For each , find the number of distinct parentheses sequences of length whose value is , then compute the sum .
The answer may be very large, so print it modulo the given integer .
Input
The first line contains two integers and (, ).
Output
Print one integer: the answer to the problem.