CF2234D.XOR, Expression and Two Binary Numbers

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • Suppose that before the ii-th step, the numbers with indices p1<p2<…<pmp_1 \lt p_2 \lt \ldots \lt p_m have already been filled. (Before the first step, these are the numbers with indices 1,2k+11, 2^{k} + 1).
  • Then for each jj from 11 to m−1m - 1, the assignment apj+pj+12:=apj⊕apj+1a_{\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=2k = 2 and n=3n = 3, where initially a1=010,a5=110a_1 = \texttt{010}, a_5 = \texttt{110}:

  • Before the first step, the numbers with indices 1,51, 5 are filled. Therefore, this operation will perform a3:=a1⊕a5=010⊕110=100a_3 := a_1 \oplus a_5 = \texttt{010} \oplus \texttt{110} = \texttt{100}.
  • Before the second step, the numbers with indices 1,3,51, 3, 5 are filled. Therefore, this operation will perform a2:=a1⊕a3=110a_2 := a_1 \oplus a_3 = \texttt{110} and a4=a3⊕a5=010a_4 = a_{3} \oplus a_{5} = \texttt{010}

You need to compute the following expression: x1⋅y1+x2⋅y2+…+x2k+1⋅y2k+1x_1 \cdot y_1 + x_2 \cdot y_2 + \ldots + x_{2^k + 1} \cdot y_{2^k + 1}, where xix_i is the number of set bits in the ii-th number, and yiy_i is the number of zero bits in the ii-th number.

∗^{\text{∗}}x⊕yx \oplus y denotes the bitwise exclusive OR of numbers xx and yy

给你一个整数 kk。存在一个由 nn 位二进制数组成的序列 a1,a2,…,a2k+1a_1, a_2, \ldots, a_{2^k + 1}。其中,a1a_1 和 a2k+1a_{2^k + 1} 已知,其余项未知。随后,未知项将通过 kk 步填充,规则如下:

  • 假设在第 ii 步之前,下标为 p1<p2<…<pmp_1 \lt p_2 \lt \ldots \lt p_m 的项已被填入(初始时,即第 1 步之前,仅有下标为 11 和 2k+12^{k} + 1 的项已填入)。
  • 然后,对每个 j=1,2,…,m−1j = 1, 2, \ldots, m - 1,执行赋值操作:apj+pj+12:=apj⊕apj+1a_{\frac{p_j + p_{j + 1}}{2}} := a_{p_j} \oplus a_{p_{j + 1}}∗^{\text{∗}}。
  • 所有这些赋值操作是同时进行的;完成之后,所有被赋值的项也变为已填入状态。

可以证明,该过程总能将所有项完全填满。

下图展示了当 k=2k = 2、n=3n = 3 且初始值为 a1=010, a5=110a_1 = \texttt{010},\ a_5 = \texttt{110} 时的过程:

  • 第 1 步前,已填入下标为 1,51, 5 的项。因此,执行 a3:=a1⊕a5=010⊕110=100a_3 := a_1 \oplus a_5 = \texttt{010} \oplus \texttt{110} = \texttt{100}。
  • 第 2 步前,已填入下标为 1,3,51, 3, 5 的项。因此,执行 a2:=a1⊕a3=110a_2 := a_1 \oplus a_3 = \texttt{110} 和 a4:=a3⊕a5=010a_4 := a_3 \oplus a_5 = \texttt{010}。

你需要计算如下表达式:

x1⋅y1+x2⋅y2+…+x2k+1⋅y2k+1,x_1 \cdot y_1 + x_2 \cdot y_2 + \ldots + x_{2^k + 1} \cdot y_{2^k + 1},

其中 xix_i 表示第 ii 个数中**置位比特(1 的个数)的数量,yiy_i 表示第 ii 个数中零比特(0 的个数)**的数量。

∗^{\text{∗}}x⊕yx \oplus y 表示数字 xx 与 yy 的按位异或(bitwise exclusive OR)。

输入格式

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

The first line of each test case contains two integers n,kn, k (1≤n≤1051 \leq n \leq 10^5, 1≤k≤301 \leq k \leq 30) — the length of the binary numbers and the number determining the number of binary numbers in the sequence.

The second line of each test case contains a binary string ss of length nn (si∈0,1s_i \in {\texttt{0}, \texttt{1}}) — the value of a1a_1.

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

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

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

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss(si∈0,1s_i \in {\texttt{0}, \texttt{1}})——即 a1a_1 的值。

每个测试用例的第三行包含一个长度为 nn 的二进制字符串 zz(zi∈0,1z_i \in {\texttt{0}, \texttt{1}})——即 a2k+1a_{2^k + 1} 的值。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

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

对于每个测试用例,输出一个整数——即题目陈述中表达式的值。

输入输出样例

  • 输入#1

    4
    3 2
    010
    110
    1 1
    0
    0
    2 2
    01
    00
    7 30
    1010111
    0011010

    输出#1

    10
    0
    3
    12169074016

说明/提示

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

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

在第一个测试用例中,题目陈述中已描述了整个过程。最终得到的二进制数序列为 [010,110,100,010,110][\texttt{010}, \texttt{110}, \texttt{100}, \texttt{010}, \texttt{110}]。此时,题目陈述中的表达式值为 1⋅2+2⋅1+1⋅2+1⋅2+2⋅1=101 \cdot 2 + 2 \cdot 1 + 1 \cdot 2 + 1 \cdot 2 + 2 \cdot 1 = 10。

在第二个测试用例中,第一步有 a2=a1⊕a3=0⊕0=0a_2 = a_1 \oplus a_3 = \texttt{0} \oplus \texttt{0} = \texttt{0}。因此,最终得到的数字序列为 [0,0,0][\texttt{0}, \texttt{0}, \texttt{0}]。对该序列,表达式的值为零。

输入解题思路,AI测评打分。不知道怎么写?

首页