Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
D
全金属比特炼金术士
Fullmetal Bitchemist
Codeforces
1600
原题
← CE →

全金属比特炼金术士AI (CI翻译)

A binary string is a string consisting only of characters $0$ and $1$. The two characters $0$ and $1$ are called opposite values.

二进制字符串是仅由字符 $0$ 和 $1$ 组成的字符串。字符 $0$ 和 $1$ 互为相反值。AI (CI翻译)

Consider a binary string $t$. Let $|t|$ be the length of $t$. When $|t| \ge 2$, for every $1 \le i \lt |t|$, the characters $t_i$ and $t_{i+1}$ are adjacent.

给定一个二进制字符串 $t$。用 $|t|$ 表示 $t$ 的长度。当 $|t| \ge 2$ 时,对于每个 $1 \le i \lt |t|$,字符 $t_i$ 和 $t_{i+1}$ 相邻。AI (CI翻译)

A binary string $t$ is called beautiful if it can be reduced to a string of length exactly $1$ by applying the following operation any number of times, possibly zero:

如果一个二进制字符串 $t$ 可以通过任意次(也可以是零次)应用以下操作,变为长度恰好为 $1$ 的字符串,则称 $t$ 是 美丽的:AI (CI翻译)

  • Choose two equal adjacent characters, remove both of them, and insert one character with the opposite value in their place.
  • 选择两个相邻且相同的字符,将它们删除,并在原位置插入一个与它们值相反的字符。
AI (CI翻译)

For example, the string $\mathtt{10001}$ can become $\mathtt{1101}$ by replacing the first adjacent pair $\mathtt{00}$ with $\mathtt{1}$. Then it can become $\mathtt{001}$, then $\mathtt{11}$, and finally $\mathtt{0}$. Therefore, $\mathtt{10001}$ is beautiful.

例如,字符串 $\mathtt{10001}$ 可以通过将第一个相邻字符对 $\mathtt{00}$ 替换为 $\mathtt{1}$,变为 $\mathtt{1101}$。之后它可以依次变为 $\mathtt{001}$、$\mathtt{11}$,最后变为 $\mathtt{0}$。因此,$\mathtt{10001}$ 是美丽的。AI (CI翻译)

On the other hand, $\mathtt{111}$ is not beautiful. After one operation, it becomes $\mathtt{01}$, and then no operation can be applied.

另一方面,$\mathtt{111}$ 不是美丽的。进行一次操作后,它会变为 $\mathtt{01}$,此后无法再进行任何操作。AI (CI翻译)

You are given a binary string $s$. Compute the number of non-empty beautiful substrings$^{\text{∗}}$ of $s$.

给定一个二进制字符串 $s$。请计算 $s$ 中非空美丽子串$^{\text{∗}}$的数量。AI (CI翻译)

$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

$^{\text{∗}}$如果字符串 $a$ 可以通过删除字符串 $b$ 开头的若干个(可以为零个或全部)字符,以及结尾的若干个(可以为零个或全部)字符得到,则称 $a$ 是 $b$ 的子串。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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.

每个输入包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains an integer $n$ ($1 \le n \le 10^6$) — the length of $s$.

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 10^6$),表示 $s$ 的长度。AI (CI翻译)

The second line of each test case contains a binary string $s$ of length $n$.

每个测试用例的第二行包含一个长度为 $n$ 的二进制字符串 $s$。AI (CI翻译)

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

保证所有测试用例的 $n$ 之和不超过 $10^6$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the number of beautiful substrings of $s$.

对于每个测试用例,输出一个整数,表示 $s$ 中美丽子串的数量。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the only non-empty substring is $\mathtt{0}$, which is already a binary string of length $1$. Therefore, it is beautiful.

在第一个测试用例中,唯一的非空子串是 $\mathtt{0}$,它本身就是长度为 $1$ 的二进制字符串。因此,它是美丽的。AI (CI翻译)

In the second test case, the beautiful substrings are $\mathtt{0}$ and $\mathtt{1}$. The substring $\mathtt{01}$ is not beautiful, because its two characters are not equal, so no operation can be applied.

在第二个测试用例中,美丽子串是 $\mathtt{0}$ 和 $\mathtt{1}$。子串 $\mathtt{01}$ 不是美丽的,因为它的两个字符不相同,无法进行任何操作。AI (CI翻译)

In the third test case, the beautiful substrings are:

在第三个测试用例中,美丽子串如下:AI (CI翻译)

  • $s[1,1]=\mathtt{0}$;
  • $s[2,2]=\mathtt{1}$;
  • $s[3,3]=\mathtt{0}$;
  • $s[4,4]=\mathtt{0}$;
  • $s[5,5]=\mathtt{1}$;
  • $s[3,4]=\mathtt{00}$, which can become $\mathtt{1}$;
  • $s[2,4]=\mathtt{100}$, which can become $\mathtt{11}$, then $\mathtt{0}$;
  • $s[3,5]=\mathtt{001}$, which can become $\mathtt{11}$, then $\mathtt{0}$;
  • $s[1,4]=\mathtt{0100}$, which can become $\mathtt{011}$, then $\mathtt{00}$, then $\mathtt{1}$;
  • $s[1,5]=\mathtt{01001}$, which can become $\mathtt{0111}$, then $\mathtt{001}$, then $\mathtt{11}$, and finally $\mathtt{0}$.
  • $s[1,1]=\mathtt{0}$;
  • $s[2,2]=\mathtt{1}$;
  • $s[3,3]=\mathtt{0}$;
  • $s[4,4]=\mathtt{0}$;
  • $s[5,5]=\mathtt{1}$;
  • $s[3,4]=\mathtt{00}$,它可以变为 $\mathtt{1}$;
  • $s[2,4]=\mathtt{100}$,它可以变为 $\mathtt{11}$,再变为 $\mathtt{0}$;
  • $s[3,5]=\mathtt{001}$,它可以变为 $\mathtt{11}$,再变为 $\mathtt{0}$;
  • $s[1,4]=\mathtt{0100}$,它可以变为 $\mathtt{011}$,再变为 $\mathtt{00}$,最后变为 $\mathtt{1}$;
  • $s[1,5]=\mathtt{01001}$,它可以依次变为 $\mathtt{0111}$、$\mathtt{001}$、$\mathtt{11}$,最后变为 $\mathtt{0}$。
AI (CI翻译)

Thus, there are $10$ beautiful substrings.

因此,美丽子串共有 $10$ 个。AI (CI翻译)

样例

Input #1
10
1
0
2
01
5
01001
3
001
6
011110
9
010110110
12
010000101001
16
1010011010010110
20
11110101101101001110
30
000101100011111001111100000010
Output #1
1
2
10
5
15
30
47
81
139
316
InputCopy OutputCopy
题目信息
题号
2237D
时间限制
2 s
内存限制
256 MB
标签
构造
贪心
数学

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