

Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 点
問題文
A
, R
, C
からなる文字列 から始めて、「連続する三文字を選んで ARC
に上書きする」操作を 回以下行ったところ、文字列 が得られました。
さて、最初の文字列 として何通りがあり得るでしょうか?これを で割った余りを求めてください。
制約
- は
A
,R
,C
からなる文字列
入力
入力は以下の形式で標準入力から与えられます。
出力
答えを出力してください。
入力例 1Copy
ARCCARC 1
出力例 1Copy
53
例えば、最初の文字列 が次のようなとき、 回以下の操作で文字列 = ARCCARC
を得ることができます。
- =
ARCCARC
のとき:操作を行わないでも文字列ARCCARC
が得られる. - =
CRACARC
のとき: の 文字目を選んでARC
に上書きすると、文字列ARCCARC
が得られる。 - =
ARCCCCC
のとき: の 文字目を選んでARC
に上書きすると、文字列ARCCARC
が得られる。
上に挙げたもの以外にもたくさんあり、 としてあり得るものは全部で 通りです。
入力例 2Copy
ARARCRCA 5
出力例 2Copy
2187
最初の文字列 が AAAAAAAA
のとき、例えば次のような 回以下の操作で文字列 = ARARCRCA
を得ることができます。
- ステップ :まず、 文字目を選んで
ARC
に上書きすると、文字列AAARCAAA
が得られる。 - ステップ :次に、 文字目を選んで
ARC
に上書きすると、文字列AAARARCA
が得られる。 - ステップ :次に、 文字目を選んで
ARC
に上書きすると、文字列ARCRARCA
が得られる。 - ステップ :最後、 文字目を選んで
ARC
に上書きすると、文字列ARARCRCA
が得られる。
それ以外にも としてあり得るものはたくさんあり、全部で 通りです。
入力例 3Copy
AARCRRARCC 0
出力例 3Copy
1
回の操作で文字列 = AARCRRARCC
を得られるのは、最初の時点で のとき、すなわち = AARCRRARCC
の 通りしかありません。
入力例 4Copy
AAAAARRRRRCCCCC 131
出力例 4Copy
1
この入力例では、 としてあり得るものは AAAAARRRRRCCCCC
の 通りだけです。
入力例 5Copy
CAARCACRAAARARARCRCRARCARARCRRARC 9
出力例 5Copy
797833187
最初の文字列 としてあり得るものは 通りあるので、これを で割った余りである を出力してください。
Score : points
Problem Statement
On a string consisting of A
, R
, and C
, we did the following operation at most times: choose three consecutive characters and overwrite them with ARC
. The result is the string .
How many strings are there that could be the initial string ? Find this count modulo .
Constraints
- is a string consisting of
A
,R
, andC
.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
ARCCARC 1
Sample Output 1Copy
53
Below are some examples of the initial string from which we can get the string = ARCCARC
with at most operation.
- =
ARCCARC
: we can choose to do nothing to getARCCARC
. - =
CRACARC
: we can choose the -st, -nd, -rd characters and overwrite them withARC
to getARCCARC
. - =
ARCCCCC
: we can choose the -th, -th, -th characters and overwrite them withARC
to getARCCARC
.
There are many other strings that could be , for a total of .
Sample Input 2Copy
ARARCRCA 5
Sample Output 2Copy
2187
If the intial string is AAAAAAAA
, one way to get = ARARCRCA
with at most operations is as follows.
- Step : choose the -rd, -th, -th characters and overwrite them with
ARC
to get the stringAAARCAAA
. - Step : choose the -th, -th, -th characters and overwrite them with
ARC
to get the stringAAARARCA
. - Step : choose the -st, -nd, -rd characters and overwrite them with
ARC
to get the stringARCRARCA
. - Step : choose the -rd, -th, -th characters and overwrite them with
ARC
to get the stringARARCRCA
.
There are many other strings that could be , for a total of .
Sample Input 3Copy
AARCRRARCC 0
Sample Output 3Copy
1
We can get = AARCRRARCC
with operations in only one case in which from the beginning, or = AARCRRARCC
.
Sample Input 4Copy
AAAAARRRRRCCCCC 131
Sample Output 4Copy
1
In this case, there is just one string that could be : AAAAARRRRRCCCCC
.
Sample Input 5Copy
CAARCACRAAARARARCRCRARCARARCRRARC 9
Sample Output 5Copy
797833187
There are strings that could be , so print this count modulo , which is .