#P6212. 「SWTR-4」Lining up
「SWTR-4」Lining up
Background
As the class monitor, Little S is directing a group of classmates to line up on the playground. Lining up is not an easy task.
Problem Description
There are students standing in a single line on the playground. Each student is either a boy, denoted by B, or a girl, denoted by G.
We define the satisfaction of two adjacent students as follows:
-
If the two adjacent students have the same gender, then they chat like ordinary classmates, producing satisfaction.
-
If the student in front is a boy and the one behind is a girl, then nothing happens. The students are very active and do not want it to be so boring, producing satisfaction.
-
If the student in front is a girl and the one behind is a boy, then they chat very happily, producing satisfaction.
Since Little S is nearsighted, he cannot tell the gender of some students, denoted by ?.
To improve his status in everyone’s eyes, Little S wants to ensure that the sum of satisfaction over all adjacent pairs is at least .
He wants to know the probability that the condition “the sum of satisfaction over all adjacent pairs is at least ” holds, modulo .
Input Format
The first line contains four integers — as described in the statement.
The second line contains a string of length representing the line — the -th character represents the -th student from front to back.
Output Format
Output one line containing the probability.
3 1 2 1
BG?
500000004
3 -1 4 3
???
750000006
5 5 7 3
G??B?
625000005
6 10 9 4
??GB??
937500007
20 20 15 10
B?G?B?G?????BBBG?GG?
78125001
Hint
[Sample Explanation]
There is lineup that satisfies the condition: . The probability is .
[Sample Explanation]
There are lineups that satisfy the condition: . The probability is .
[Sample Explanation]
The actual answer is .
[Constraints and Notes]
This problem uses bundled testdata.
| Subtask ID | Special property | Score | |
|---|---|---|---|
No ? |
|||
| None | |||
| None |
For all testdata: , , , .
Please note the special memory limit.
[Tips]
If you do not know how to take a fraction modulo, you can read here.
[Source]
idea: ET2006, std: Isaunoya, validator: Isaunoya & FrenkiedeJong21 & chenxia25
Translated by ChatGPT 5