luogu#P7739. [NOI2021] 密码箱
[NOI2021] 密码箱
Problem Description
Yelekastee is a famous archaeologist in Country U. In a recent excavation, he unearthed an ancient combination lock. After careful and rigorous research, Yelekastee learned that the lock’s password is related to a certain sequence . The sequence can be constructed as follows:
- Initially, the sequence has length and .
- Perform a number of operations on the sequence in order. Each operation is one of the following two types:
- Type
W: add to the last term of the sequence. - Type
E: if the last term of the sequence is , add to the second-to-last term; otherwise, first subtract from the last term, then append two more terms to the end of the sequence, both equal to .
Due to technical limitations, the lock cannot fully inspect the entire sequence. Therefore, the lock’s password is defined as the value obtained by applying a function to the sequence , where is defined as:
$$f(a_0, \ldots , a_{k - 1}, a_k) = \begin{cases} a_0, & k = 0 \\ f \! \left( a_0, a_1, \ldots , a_{k - 2}, a_{k - 1} + \frac{1}{a_k} \right) \! , & k \ge 1 \end{cases}$$Yelekastee is not good at calculations, so he came to you and hopes you can compute the lock’s password from the operation sequence he provides. Unfortunately, his memory is not very good, so he will modify the operation sequence from time to time. These modifications include the following three types:
APPEND c: append one operation of typecto the end of the current operation sequence, wherecis the characterWorE.FLIP l r: flip the -th to the -th operations (indices start from , and the modification includes both endpoints and , same below), i.e., change everyWtoEand everyEtoW.REVERSE l r: reverse the order of the -th to the -th operations, i.e., reverse this segment of operations.
Input Format
The first line contains two positive integers , denoting the initial length of the operation sequence and the number of modifications, respectively.
The second line contains a string of length consisting only of uppercase letters W and E, representing the initial operation sequence.
The next lines each describe one modification. The format of each modification is as described in the Description.
Output Format
Output a total of lines, each containing two integers. The first line is the password corresponding to the initial operation sequence. The next lines output the password corresponding to the operation sequence after each modification.
It is easy to see that the password must be a positive rational number. If the true password is , where and , then on the corresponding line you should output and modulo , in this order.
2 3
WE
APPEND E
FLIP 1 2
REVERSE 2 3
2 3
3 4
5 3
5 2
Hint
Sample Explanation #1
| Operation sequence | Sequence | Password | |
|---|---|---|---|
| Initial | WE |
||
| After the 1st modification | WEE |
||
| After the 2nd modification | EWE |
||
| After the 3rd modification | EEW |
Sample #2
See the attachments code/code2.in and code/code2.ans.
This sample satisfies the same constraint conditions as testdata .
Sample #3
See the attachments code/code3.in and code/code3.ans.
This sample satisfies the same constraint conditions as testdata .
Sample #4
See the attachments code/code4.in and code/code4.ans.
This sample satisfies the same constraint conditions as testdata .
Sample #5
See the attachments code/code5.in and code/code5.ans.
This sample satisfies the same constraint conditions as testdata .
Constraints
For all test points: , .
For APPEND modifications, it is guaranteed that c is an uppercase letter W or E.
For FLIP and REVERSE modifications, it is guaranteed that , where is the current length of the operation sequence.
Note that due to the APPEND operation, the maximum possible length of the operation sequence is .
| Test point ID | Special constraints | |
|---|---|---|
| None | ||
| A | ||
| B, C | ||
| C | ||
| None |
Special constraint A: it is guaranteed that at any time, there will not be two identical consecutive characters in the operation sequence.
Special constraint B: it is guaranteed that there are no FLIP modifications.
Special constraint C: it is guaranteed that there are no REVERSE modifications.
Translated by ChatGPT 5