#P5672. 「SWTR-2」Crystal Balls
「SWTR-2」Crystal Balls
Description
has crystal balls. Now he arranges these crystal balls in a row. Each crystal ball has an energy value, and is either green or purple.
- In the following, stands for purple, and stands for green.
will take away these crystal balls in the following way:
-
Take away the leftmost crystal ball.
-
Suppose the removed crystal ball has color and energy , the current leftmost remaining crystal ball has color and energy , and the number of times a crystal ball has been removed is (including this time).
-
If , then will combine these two crystal balls into one large crystal ball (the crystal ball removed this time is still counted in the total answer; see samples for details), with color and energy value , and put it at the left end of the crystal ball sequence.
-
If , then will flip the colors of the remaining crystal balls (i.e., green becomes purple, and purple becomes green).
-
If the above conditions still cannot be satisfied, then will reverse the remaining sequence of crystal balls.
He keeps doing this until only one ball remains. Then will directly take away the last ball. Find the sum of the energy values of all removed crystal balls.
Since the answer can be very large, take it modulo .
Input Format
The first line contains two positive integers , representing the number of crystal balls and the modulus.
The second line contains positive integers , where is the energy value of the -th crystal ball.
The third line contains a string consisting of characters and . If the -th character is P, then the crystal ball is purple; otherwise it is green.
Output Format
Output one integer, the sum of the energy values of the removed crystal balls modulo .
4 998244353
1 2 3 4
GPGG
19
3 998244353
3 7 5
PGG
45
10 998244353
12345 23456 34567 45678 56789 67890 78901 89012 90123 101234
GPPGPGGGPG
104157290
Hint
Sample Explanation
Sample :
first removes the leftmost crystal ball, whose color is G. Add the number written on it to the answer, i.e., . Then the remaining crystal balls are reversed, and the sequence becomes GGP. (Because , and the removal count is odd, it does not satisfy conditions 1 or 2, so the sequence is reversed.)
Then remove the leftmost crystal ball, whose color is G. Add to the answer. Next, combine the current leftmost remaining crystal ball with the removed one into a large crystal ball with number . The sequence becomes GP.
Remove the leftmost crystal ball, whose color is G. Add to the answer. Reverse the remaining sequence, which becomes P.
Remove the last crystal ball. Add to the answer. The final answer is .
Sample :
First remove , , so flip the colors.
Remove , , multiply by , getting .
Remove , and the final answer is .
Constraints and Notes
This problem uses .
$\mathrm{Subtask}\ 1:n\leq 2000,a_i\leq 10^9,p\leq 10^9,15\%$.
$\mathrm{Subtask}\ 2:n\leq 5\times 10^4,a_i\leq 10^9,p\leq 10^9,15\%$.
$\mathrm{Subtask}\ 3:n\leq 5\times 10^4,a_i\leq 10^{18},p\leq 10^{18},20\%$.
$\mathrm{Subtask}\ 4:n\leq 10^6,a_i\leq 10^9,p\leq 10^9,20\%$.
$\mathrm{Subtask}\ 5:n\leq 10^6,a_i\leq 10^{18},p\leq 10^{18},30\%$.
For all testdata, the time limit is , and the memory limit is .
Translated by ChatGPT 5
京公网安备 11011102002149号