201083: [AtCoder]ARC108 D - AB

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

Given are an integer $N$ and four characters $c_{\mathrm{AA}}$, $c_{\mathrm{AB}}$, $c_{\mathrm{BA}}$, and $c_{\mathrm{BB}}$. Here, it is guaranteed that each of those four characters is A or B.

Snuke has a string $s$, which is initially AB.

Let $|s|$ denote the length of $s$. Snuke can do the four kinds of operations below zero or more times in any order:

  1. Choose $i$ such that $1 \leq i < |s|$, $s_{i}$ = A, $s_{i+1}$ = A and insert $c_{\mathrm{AA}}$ between the $i$-th and $(i+1)$-th characters of $s$.
  2. Choose $i$ such that $1 \leq i < |s|$, $s_{i}$ = A, $s_{i+1}$ = B and insert $c_{\mathrm{AB}}$ between the $i$-th and $(i+1)$-th characters of $s$.
  3. Choose $i$ such that $1 \leq i < |s|$, $s_{i}$ = B, $s_{i+1}$ = A and insert $c_{\mathrm{BA}}$ between the $i$-th and $(i+1)$-th characters of $s$.
  4. Choose $i$ such that $1 \leq i < |s|$, $s_{i}$ = B, $s_{i+1}$ = B and insert $c_{\mathrm{BB}}$ between the $i$-th and $(i+1)$-th characters of $s$.

Find the number, modulo $(10^9+7)$, of strings that can be $s$ when Snuke has done the operations so that the length of $s$ becomes $N$.

Constraints

  • $2 \leq N \leq 1000$
  • Each of $c_{\mathrm{AA}}$, $c_{\mathrm{AB}}$, $c_{\mathrm{BA}}$, and $c_{\mathrm{BB}}$ is A or B.

Input

Input is given from Standard Input in the following format:

$N$
$c_{\mathrm{AA}}$
$c_{\mathrm{AB}}$
$c_{\mathrm{BA}}$
$c_{\mathrm{BB}}$

Output

Print the number, modulo $(10^9+7)$, of strings that can be $s$ when Snuke has done the operations so that the length of $s$ becomes $N$.


Sample Input 1

4
A
B
B
A

Sample Output 1

2
  • There are two strings that can be $s$ when Snuke is done: ABAB and ABBB.

Sample Input 2

1000
B
B
B
B

Sample Output 2

1
  • There is just one string that can be $s$ when Snuke is done.

Input

题意翻译

给出四个大写字母 $c_{AA}$、$c_{AB}$、$c_{BA}$、$c_{BB}$ 和一个初始字符串 $s=AB$ ,每次操作可以选择字符串中相邻的两个字母 $s_i$、$s_{i+1}$ 并按下列规则在两个字母之间插入一个新的字母。 - 若 $s_i=A$ 且 $s_{i+1}=A$,则在两者之间插入字母 $c_{AA}$。 - 若 $s_i=A$ 且 $s_{i+1}=B$,则在两者之间插入字母 $c_{AB}$。 - 若 $s_i=B$ 且 $s_{i+1}=A$,则在两者之间插入字母 $c_{BA}$。 - 若 $s_i=B$ 且 $s_{i+1}=B$,则在两者之间插入字母 $c_{BB}$。 保证 $c_{AA}$、$c_{AB}$、$c_{BA}$、$c_{BB}$ 均为 $A$ 或 $B$。 求当 $s$ 的长度被添加至 $n$ 后,所有可能的字符串共有多少种? 样例一:可能出现的有 ```ABAB``` 和 ```ABBB```。 样例二:只可能出现 ```ABBBB...BBBB``` 一种。

加入题单

算法标签: