eolymp
bolt
Try our new interface for solving problems
Problems

Linear Garden

Linear Garden

Ramesses II has just returned victorious from battle. To commemorate his victory, he has decided to build a majestic garden. The garden will contain a long line of plants that will run all the way from his palace at Luxor to the temple of Karnak. It will consist only of lotus plants and papyrus plants, since they symbolize Upper and Lower Egypt respectively. The garden must contain exactly \textbf{n} plants. Also, it must be balanced: in any contiguous section of the garden, the numbers of lotus and papyrus plants must not differ by more than \textbf{2}. A garden can be represented as a string of letters '\textbf{L}' (lotus) and '\textbf{P}' (papyrus). For example, for \textbf{n = 5} there are \textbf{14} possible balanced gardens. In alphabetical order, these are: \textbf{LLPLP}, \textbf{LLPPL}, \textbf{LPLLP}, \textbf{LPLPL}, \textbf{LPLPP}, \textbf{LPPLL}, \textbf{LPPLP}, \textbf{PLLPL}, \textbf{PLLPP}, \textbf{PLPLL}, \textbf{PLPLP}, \textbf{PLPPL}, \textbf{PPLLP} and \textbf{PPLPL}. The possible balanced gardens of a certain length can be ordered alphabetically, and then numbered starting from \textbf{1}. For example, for \textbf{n = 5}, garden number \textbf{12} is the garden PLPPL. Write a program that, given the number of plants \textbf{n} and a string that represents a balanced garden, calculates the number assigned to this garden modulo some given integer \textbf{m}. Note that for solving the task, the value of \textbf{M} has no importance other than simplifying computations. It is known that \textbf{1} \textbf{≤ n ≤ 1,000,000},\textbf{ 7 ≤ m ≤ 10,000,000}. \InputFile Your program must read from the standard input the following data: • Line \textbf{1} contains the integer \textbf{n}, the number of plants in the garden. • Line \textbf{2} contains the integer \textbf{m}. • Line \textbf{3} contains a string of \textbf{n} characters '\textbf{L}' (lotus) or '\textbf{P}' (papyrus) that represents a balanced garden. \OutputFile Your program must write to the standard output a single line containing one integer between \textbf{0} and \textbf{m ‐ 1} (inclusive), the number assigned to the garden described in the input, modulo \textbf{m}.
Time limit 1.5 second
Memory limit 64 MiB
Input example #1
5
7
PLPPL
Output example #1
5
Source IOI-2008