第 1123 场 Codeforces Round(Div. 2)
F1
异或变换(简单版)
XOR Transformations (Easy Version)
Codeforces
1700
原题
← EF2 →

异或变换(简单版)AI (CI翻译)

This is the easy version of the problem. The difference between the versions is that in this version, the constraints on $n$ and $q$ are smaller. You can make hacks only if you have solved all versions of this problem.

这是本题的简单版。不同版本的区别在于,本版本中 $n$ 和 $q$ 的约束范围更小。只有解决了本题的所有版本,你才能进行 hack。AI (CI翻译)

For an array $b$ consisting of $m$ integers, define a transformation as follows:

对于一个由 $m$ 个整数组成的数组 $b$,定义一次变换如下:AI (CI翻译)

  • Write down the values $b_i\oplus b_j$ for all $1\le i\lt j\le m$, where $\oplus$ denotes the bitwise XOR operation..
  • Take the $m$ smallest among the written values.
  • Replace the elements of the array with the taken $m$ values.
  • 写出所有满足 $1\le i\lt j\le m$ 的 $b_i\oplus b_j$ 的值,其中 $\oplus$ 表示按位异或运算。
  • 从写出的值中选取最小的 $m$ 个值。
  • 用选出的 $m$ 个值替换数组中的元素。
AI (CI翻译)

For example, consider the transformation of the array $[6, 7, 8, 9, 15]$. We write down the values $1, 1, 6, 7, 8, 9, 14, 14, 15, 15$. After the transformation, the array becomes $[1, 1, 6, 7, 8]$ — the $5$ smallest elements.

例如,考虑对数组 $[6, 7, 8, 9, 15]$ 进行一次变换。我们写出这些值:$1, 1, 6, 7, 8, 9, 14, 14, 15, 15$。变换后,数组变为 $[1, 1, 6, 7, 8]$,即其中最小的 $5$ 个元素。AI (CI翻译)

You are given an array $a$ consisting of $n$ non-negative integers. Let $\max(a)$ denote the maximum element of the array $a$, and $\min(a)$ — the minimum. Your task is to answer $q$ queries, each of which gives you one integer $x$. For each query, find the value of $\max(a) - \min(a)$ after $x$ transformations on the array. Note that the queries are independent, i.e. before each query, the array $a$ is restored to its original state.

给定一个由 $n$ 个非负整数组成的数组 $a$。用 $\max(a)$ 表示数组 $a$ 中的最大元素,$\min(a)$ 表示最小元素。你的任务是回答 $q$ 次询问,每次询问给出一个整数 $x$。对于每次询问,求对数组进行 $x$ 次变换后 $\max(a) - \min(a)$ 的值。注意,各次询问相互独立,即每次询问前,数组 $a$ 都会恢复到初始状态。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 100$). The description of the test cases follows.

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

The first line of each test case contains two integers $n$ and $q$ ($5\le n\le 2000, 1\le q\le 2000$) — the size of the array and the number of queries.

每组测试用例的第一行包含两个整数 $n$ 和 $q$($5\le n\le 2000, 1\le q\le 2000$),分别表示数组的长度和询问次数。AI (CI翻译)

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0\le a_i\lt 2^{30}$).

每组测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($0\le a_i\lt 2^{30}$)。AI (CI翻译)

The next $q$ lines of each test case contain an integer $x$ ($0\le x\lt 2^{30}$).

每组测试用例接下来的 $q$ 行,每行包含一个整数 $x$($0\le x\lt 2^{30}$)。AI (CI翻译)

It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $2000$.

保证所有测试用例的 $n$ 之和以及 $q$ 之和均不超过 $2000$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output $q$ integers — the answer to each query.

对于每组测试用例,输出 $q$ 个整数,分别表示每次询问的答案。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, after one transformation, the array becomes $[0, 0, 0, 0, 1]$. Here $\max(a) - \min(a) = 1 - 0 = 1$.

在第一组测试用例中,经过一次变换后,数组变为 $[0, 0, 0, 0, 1]$。此时 $\max(a) - \min(a) = 1 - 0 = 1$。AI (CI翻译)

In the second test case, the initial array is $[6, 7, 8, 9, 15]$. Initially, $\max(a) - \min(a) = 15 - 6 = 9$. After the transformation, the array becomes $[1, 1, 6, 7, 8]$, where $\max(a) - \min(a) = 8 - 1 = 7$.

在第二组测试用例中,初始数组为 $[6, 7, 8, 9, 15]$。初始时,$\max(a) - \min(a) = 15 - 6 = 9$。经过一次变换后,数组变为 $[1, 1, 6, 7, 8]$,此时 $\max(a) - \min(a) = 8 - 1 = 7$。AI (CI翻译)

样例

Input #1
4
5 1
0 0 1 1 1
1
5 2
6 7 8 9 15
0
1
8 2
102 92 15 19 25 54 62 36
2
1
10 1
56 73 81 23 17 92 50 34 67 78
2
Output #1
1
9
7
12
31
7
InputCopy OutputCopy
题目信息
题号
2267F1
时间限制
4 s
内存限制
512 MB
标签
位运算
暴力
模拟
排序

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