atcoder#ABC238H. [ABC238Ex] Removing People
[ABC238Ex] Removing People
Score : points
Problem Statement
people numbered to are standing in a circle, in the clockwise order of Person , Person , , Person .
The direction each person faces is given by a string of length . For each , Person is facing in the counter-clockwise direction if L
, and clockwise direction if R
.
The following operation will be repeated times.
- Choose one of the remaining people with equal probability, and remove from the circle the nearest person seen from the chosen person. This incurs a cost equal to the distance from the chosen person to the removed person.
Here, the distance from Person to Person is defined as follows.
- When Person is facing in the clockwise direction:- if ;
- if .
- When Person is facing in the counter-clockwise direction:- if ;
- if .
Find the expected value of the total cost incurred, modulo (see Notes).
Notes
It can be proved that the sought expected value is always a rational number. Additionally, under the Constraints of this problem, when that value is expressed as using two coprime integers and , there is a unique integer such that and . Find this .
Constraints
- is an integer.
- is a string of length consisting of
L
andR
.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
3
LLR
831870297
The sought expected value is . We have , so should be printed.
For your reference, here is one possible scenario.
- Person is chosen. The nearest person seen from Person remaining in the circle is Person , who gets removed from the circle.
- Person is chosen again. The nearest person seen from Person remaining in the circle is Person , who gets removed from the circle.
In this case, the total cost incurred is .
10
RRRRRRLLRR
460301586