Codeforces Round 1102 (Div. 2)
D
异或、表达式与两个二进制数
XOR, Expression and Two Binary Numbers
Codeforces
1500
原题
← CE →

异或、表达式与两个二进制数AI (CI翻译)

You are given an integer $k$. There is a sequence of $n$-bit binary numbers $a_1, a_2, \ldots, a_{2^k + 1}$. The numbers $a_1$ and $a_{2^k + 1}$ are given to you, and the others are unknown. Then the unknown numbers are filled in as follows over $k$ steps:

翻译中…
  • Suppose that before the $i$-th step, the numbers with indices $p_1 \lt p_2 \lt \ldots \lt p_m$ have already been filled. (Before the first step, these are the numbers with indices $1, 2^{k} + 1$).
  • Then for each $j$ from $1$ to $m - 1$, the assignment $a_{\frac{p_j + p_{j + 1}}{2}} := a_{p_j} \oplus a_{p_{j + 1}}$$^{\text{∗}}$ is performed.
  • These assignments happen simultaneously, and after that all these numbers also become filled.
翻译中…

It can be shown that this process always fills all numbers completely.

翻译中…

This is what the process looks like for $k = 2$ and $n = 3$, where initially $a_1 = \texttt{010}, a_5 = \texttt{110}$:

翻译中…
  • Before the first step, the numbers with indices $1, 5$ are filled. Therefore, this operation will perform $a_3 := a_1 \oplus a_5 = \texttt{010} \oplus \texttt{110} = \texttt{100}$.
  • Before the second step, the numbers with indices $1, 3, 5$ are filled. Therefore, this operation will perform $a_2 := a_1 \oplus a_3 = \texttt{110}$ and $a_4 = a_{3} \oplus a_{5} = \texttt{010}$
翻译中…

You need to compute the following expression: $x_1 \cdot y_1 + x_2 \cdot y_2 + \ldots + x_{2^k + 1} \cdot y_{2^k + 1}$, where $x_i$ is the number of set bits in the $i$-th number, and $y_i$ is the number of zero bits in the $i$-th number.

翻译中…

$^{\text{∗}}$$x \oplus y$ denotes the bitwise exclusive OR of numbers $x$ and $y$

翻译中…

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 two integers $n, k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 30$) — the length of the binary numbers and the number determining the number of binary numbers in the sequence.

每个测试用例的第一行包含两个整数 $n, k$($1 \leq n \leq 10^5$,$1 \leq k \leq 30$),分别表示二进制数的长度以及决定序列中二进制数数量的参数。AI (CI翻译)

The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in \{\texttt{0}, \texttt{1}\}$) — the value of $a_1$.

每个测试用例的第二行包含一个长度为 $n$ 的二进制字符串 $s$($s_i \in \{\texttt{0}, \texttt{1}\}$),表示 $a_1$ 的值。AI (CI翻译)

The third line of each test case contains a binary string $z$ of length $n$ ($z_i \in \{\texttt{0}, \texttt{1}\}$) — the value of $a_{2^k + 1}$.

每个测试用例的第三行包含一个长度为 $n$ 的二进制字符串 $z$($z_i \in \{\texttt{0}, \texttt{1}\}$),表示 $a_{2^k + 1}$ 的值。AI (CI翻译)

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

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

Output

输出格式

AI (CI翻译)

For each test case, output one integer — the value of the expression from the statement.

对于每个测试用例,输出一个整数,即题目描述中表达式的值。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the process was described in the statement. The resulting sequence of binary numbers is $[\texttt{010}, \texttt{110}, \texttt{100}, \texttt{010}, \texttt{110}]$. Then the expression in the statement equals $1 \cdot 2 + 2 \cdot 1 + 1 \cdot 2 + 1 \cdot 2 + 2 \cdot 1 = 10$.

在第一个测试用例中,题目描述中给出了具体的过程。最终得到的二进制数序列为 $[\texttt{010}, \texttt{110}, \texttt{100}, \texttt{010}, \texttt{110}]$。因此,题目描述中的表达式等于 $1 \cdot 2 + 2 \cdot 1 + 1 \cdot 2 + 1 \cdot 2 + 2 \cdot 1 = 10$。AI (CI翻译)

In the second test case, at the first step we have $a_2 = a_1 \oplus a_3 = \texttt{0} \oplus \texttt{0} = \texttt{0}$. Therefore, the resulting sequence of numbers is $[\texttt{0}, \texttt{0}, \texttt{0}]$. For it, the value of the expression is zero.

在第二个测试用例中,第一步有 $a_2 = a_1 \oplus a_3 = \texttt{0} \oplus \texttt{0} = \texttt{0}$。因此,最终得到的数序列为 $[\texttt{0}, \texttt{0}, \texttt{0}]$,表达式的值为零。AI (CI翻译)

样例

Input #1
4
3 2
010
110
1 1
0
0
2 2
01
00
7 30
1010111
0011010
Output #1
10
0
3
12169074016
InputCopy OutputCopy
题目信息
题号
2234D
时间限制
2 s
内存限制
256 MB
标签
位运算
分治
动态规划
数学
数论

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