atcoder#ARC157D. [ARC157D] YY Garden
[ARC157D] YY Garden
Score : points
Problem Statement
There is a grid with rows and columns where each square has one of the characters X
and Y
written on it.
Let denote the square at the -th row from the top and -th column from the left.
The characters on the grid are given as strings : the -th character of is the character on square .
You can set up fences across the grid between adjacent rows and columns. Fences may intersect. After setting up fences, a block is defined as the set of squares reachable from some square by repeatedly moving up, down, left, or right to the adjacent square without crossing fences. (See also Sample Output 1.)
There are ways to set up fences. How many of them satisfy the following condition, modulo ?
Condition: Each block has exactly two squares with Y
written on it.
Constraints
- is a string of length consisting of
X
andY
.
Input
The input is given from Standard Input in the following format:
Output
Print the number of ways to set up fences to satisfy the condition, modulo .
2 3
XYY
YXY
2
Below are the eight ways to set up fences.
X Y Y X|Y Y X Y|Y X|Y|Y
| | | |
Y X Y Y|X Y Y X|Y Y|X|Y
X Y Y X|Y Y X Y|Y X|Y|Y
----- -+--- ---+- -+-+-
Y X Y Y|X Y Y X|Y Y|X|Y
For instance, if a fence is set up between the -nd and -rd columns, the blocks are:
XY
YX
Y
Y
Each of these blocks has exactly two Y
s, so the condition is satisfied.
If a fence is set up between the -st and -nd rows and the -st and -nd columns, the blocks are:
X
YY
Y
XY
These blocks, except the second, do not have exactly two Y
s, so the condition is not satisfied.
2 3
XYX
YYY
0
There is no way to set up fences to satisfy the condition.
2 58
YXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY
YXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY
164036797
Print the number of ways modulo .