Teza Round 1 (Codeforces Round 1015, Div. 1 + Div. 2)
H
Turtle and Nediam 2
Codeforces
3500
原题
← G2→
翻译中…

You are given a binary sequence $s$ of length $n$ which only consists of $0$ and $1$.

翻译中…

You can do the following operation at most $n - 2$ times (possibly zero):

翻译中…
  • Let $m$ denote the current length of $s$. Choose an integer $i$ such that $1 \le i \le m - 2$.
  • Let the median$^{\text{∗}}$ of the subarray $[s_i, s_{i + 1}, s_{i + 2}]$ be $x$, and let $j$ be the smallest integer such that $j \ge i$ and $s_j = x$.
  • Remove $s_j$ from the sequence and concatenate the remaining parts. In other words, replace $s$ with $[s_1, s_2, \ldots, s_{j - 1}, s_{j + 1}, s_{j + 2}, \ldots, s_m]$.
翻译中…

Note that after every operation, the length of $s$ decreases by $1$.

翻译中…

Find how many different binary sequences can be obtained after performing the operation, modulo $10^9 + 7$.

翻译中…

$^{\text{∗}}$The median of an array of odd length $k$ is the $\frac{k + 1}{2}$-th element when sorted.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^6$) — the length of the binary sequence.

翻译中…

The second line of each test case contains a string $s$ of length $n$, consisting of only $0$ and $1$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^6$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the number of binary sequences that can be obtained, modulo $10^9 + 7$.

翻译中…

Note

翻译中…

In the first test case, the following binary sequences can be obtained: $[1, 1]$, $[1, 1, 1]$, $[1, 1, 1, 1]$, $[1, 1, 1, 1, 1]$.

翻译中…

In the second test case, the following binary sequences can be obtained: $[0, 1]$, $[0, 1, 1]$, $[1, 0, 1]$, $[1, 0, 0, 1]$, $[1, 0, 1, 1]$, $[1, 0, 0, 0, 1]$, $[1, 0, 0, 1, 1]$, $[1, 0, 0, 0, 1, 1]$. For example, to obtain $[0, 1, 1]$, you can:

翻译中…
  • Choose $i = 2$. The median of $[0, 0, 0]$ is $0$. Remove $s_2$. The sequence becomes $[1, 0, 0, 1, 1]$.
  • Choose $i = 1$. The median of $[1, 0, 0]$ is $0$. Remove $s_2$. The sequence becomes $[1, 0, 1, 1]$.
  • Choose $i = 1$. The median of $[1, 0, 1]$ is $1$. Remove $s_1$. The sequence becomes $[0, 1, 1]$.
翻译中…

样例

Input #1
5
5
11111
6
100011
9
000111000
14
11001111111000
16
0010000110100011
Output #1
4
8
30
114
514
InputCopy OutputCopy
题目信息
题号
2084H
时间限制
2 s
内存限制
512 MB
标签
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。