#CF2237D. 钢之位运算术师 / D. Fullmetal Bitchemist

    ID: 18578 传统题 2000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>CodeforcesOrder Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)

钢之位运算术师 / D. Fullmetal Bitchemist

D. Fullmetal Bitchemist

Source: Codeforces 2237D
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 2 seconds
Memory limit: 256 megabytes

Statement

A binary string is a string consisting only of characters 00 and 11. The two characters 00 and 11 are called opposite values.

Consider a binary string tt. Let t|t| be the length of tt. When t2|t| \ge 2, for every 1i<t1 \le i \lt |t|, the characters tit_i and ti+1t_{i+1} are adjacent.

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

  • Choose two equal adjacent characters, remove both of them, and insert one character with the opposite value in their place.

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

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

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

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

Input

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

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

The second line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

Output

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

Note

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

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

In the third test case, the beautiful substrings are:

  • s[1,1]=0s[1,1]=\mathtt{0};
  • s[2,2]=1s[2,2]=\mathtt{1};
  • s[3,3]=0s[3,3]=\mathtt{0};
  • s[4,4]=0s[4,4]=\mathtt{0};
  • s[5,5]=1s[5,5]=\mathtt{1};
  • s[3,4]=00s[3,4]=\mathtt{00}, which can become 1\mathtt{1};
  • s[2,4]=100s[2,4]=\mathtt{100}, which can become 11\mathtt{11}, then 0\mathtt{0};
  • s[3,5]=001s[3,5]=\mathtt{001}, which can become 11\mathtt{11}, then 0\mathtt{0};
  • s[1,4]=0100s[1,4]=\mathtt{0100}, which can become 011\mathtt{011}, then 00\mathtt{00}, then 1\mathtt{1};
  • s[1,5]=01001s[1,5]=\mathtt{01001}, which can become 0111\mathtt{0111}, then 001\mathtt{001}, then 11\mathtt{11}, and finally 0\mathtt{0}.

Thus, there are 1010 beautiful substrings.

Examples

10
1
0
2
01
5
01001
3
001
6
011110
9
010110110
12
010000101001
16
1010011010010110
20
11110101101101001110
30
000101100011111001111100000010
1
2
10
5
15
30
47
81
139
316