Problem list
Log in
#r03637 · IOI 2008

Linear Garden

Platinum II 2.0 s 256 MB

August 16 – 23, Cairo

Contest Day 2 ‐ Linear Garden

English 1.1

1 Page of 2 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 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 2.

A garden can be represented as a string of letters ‘L’ (lotus) and ‘P’ (papyrus). For example, for N=5 there are 14 possible balanced gardens. In alphabetical order, these are: LLPLP, LLPPL, LPLLP, LPLPL, LPLPP, LPPLL, LPPLP, PLLPL, PLLPP, PLPLL, PLPLP, PLPPL, PPLLP, and PPLPL.

The possible balanced gardens of a certain length can be ordered alphabetically, and then numbered starting from 1. For example, for N=5, garden number 12 is the garden PLPPL.

TASK Write a program that, given the number of plants N and a string that represents a balanced garden, calculates the number assigned to this garden modulo some given integer M. Note that for solving the task, the value of M has no importance other than simplifying computations.

EXAMPLE

Sample input 1 Sample output 1 5 7 PLPPL 5 The actual number assigned to PLPPL is 12. So, the output is 12 modulo 7, which is 5.

August 16 – 23, Cairo

Contest Day 2 ‐ Linear Garden

English 1.1

2 Page of 2

Sample input 2 Sample output 2 12 10000 LPLLPLPPLPLL 39

Input

Your program must read from the standard input the following data:

  • Line 1 contains the integer N, the number of plants in the garden.
  • Line 2 contains the integer M.
  • Line 3 contains a string of N characters ‘L’ (lotus) or ‘P’ (papyrus) that represents a balanced garden.

Output

Your program must write to the standard output a single line containing one integer between 0 and M‐1 (inclusive), the number assigned to the garden described in the input, modulo M.

Constraints

1 <= N <= 1,000,000 7 <= M <= 10,000,000

GRADING In inputs worth a total of 40 points, N will not exceed 40.

Source
IOI 2008
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code