luogu#P4788. [BalkanOI 2018] Parentrises
[BalkanOI 2018] Parentrises
Problem Description
Translated from BalkanOI 2018 Day 2 T1 “Parentrises”.
A parentheses string is a string consisting only of ( and ). If inserting some 1 and + into a parentheses string can turn it into a correct expression, then the string is a balanced parentheses string. For example, (()) and ()() are balanced parentheses strings, while )( and ( are not. The empty string is considered a balanced parentheses string. (This is the one you saw when learning Catalan numbers.)
Color each parenthesis of a parentheses string (not necessarily balanced) with one of three colors: red, green, and blue. If there exists a coloring such that all of the following are true:
- After ignoring all blue parentheses in the string, it becomes a balanced parentheses string.
- After ignoring all red parentheses in the string, it becomes a balanced parentheses string.
Then the string is called RGB-readable.
You will receive one of two types of tasks. The task type is given by an integer , where or .
- : You will receive queries, each containing a parentheses string. Determine whether the string is RGB-readable. If it is, output one valid coloring; otherwise output
impossible. - : You will receive queries, each containing an integer . Compute how many RGB-readable balanced parentheses strings of length there are. Output the answer modulo .
Input Format
The first line contains an integer .
The second line contains an integer .
- If , then in the next lines, each line contains a parentheses string representing a query.
- If , then in the next lines, each line contains an integer representing a query.
Output Format
If , for each query:
- If the string is RGB-readable, output one valid coloring.
- If the string is not RGB-readable, output
impossible.
If , for each query, output the number of RGB-readable balanced parentheses strings of length modulo .
1
3
())(()
()(()
()))
GRBRBG
BBRBG
impossible
2
2
6
100
12
959772055
Hint
Explanation for Sample :
For query 1, after ignoring all blue parentheses, the original string becomes ()(); after ignoring all red parentheses, it also becomes ()().
For query 2, after ignoring all blue parentheses, it becomes (); after ignoring all red parentheses, it becomes ()().
Constraints for :
Let be the total length of all strings.
- Subtask #1 (5 points): , .
- Subtask #2 (11 points): .
- Subtask #3 (6 points): .
- Subtask #4 (28 points): .
Constraints for :
- Subtask #5 (6 points): .
- Subtask #6 (16 points): .
- Subtask #7 (28 points): .
Thanks to Planet6174 for providing the translation.
Translated by ChatGPT 5