CF2165C.Binary Wine
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given n integers a1,a2,…,an within the range [0,230).
You can spend 1 coin to increase any ai by 1. You can perform this operation any number of times.
You need to solve q queries; for each query, you are given an integer c, also in the range [0,230). You would like it if there exists a sequence b of length n with the following properties:
- For every 1≤i≤n, 0≤bi≤ai.
- b1⊕b2⊕…⊕bn=c, where ⊕ denotes the bitwise XOR operation.
Please calculate the minimum number of coins you will have to spend, such that there exists a suitable b.
The queries are independent, meaning that any operations you perform on the sequence a will not impact future queries.
给你 n 个整数 a1,a2,…,an,每个数均在区间 [0,230) 内。
你可以花费 1 枚硬币将任意一个 ai 增加 1。该操作可执行任意多次。
你需要处理 q 个查询;对每个查询,你将获得一个整数 c(同样在区间 [0,230) 内)。你希望存在一个长度为 n 的序列 b,满足以下条件:
- 对每个 1≤i≤n,有 0≤bi≤ai;
- b1⊕b2⊕…⊕bn=c,其中 ⊕ 表示按位异或运算。
请计算所需的最少硬币数,使得存在满足上述条件的序列 b。
各查询相互独立,即你在某个查询中对序列 a 所做的任何操作均不会影响后续查询。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case consists of two integers n,q (1≤n≤5⋅105, 1≤q≤5⋅104) — the length of sequence a and the number of queries.
The second line of each test case contains n integers a1,a2,…,an (0≤ai<230) — the initial sequence a.
Each of the next q lines contains a single integer c (0≤c<230) — the target XOR.
It is guaranteed that the sum of n over all test cases does not exceed 5⋅105.
It is guaranteed that the sum of q over all test cases does not exceed 5⋅104.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n,q(1≤n≤5⋅105, 1≤q≤5⋅104)——分别表示序列 a 的长度和查询次数。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(0≤ai<230)——即初始序列 a。
接下来的 q 行中,每行包含一个整数 c(0≤c<230)——表示目标异或值。
保证所有测试用例的 n 之和不超过 5⋅105。
保证所有测试用例的 q 之和不超过 5⋅104。
输出格式
For each query, output a single integer — the minimum coins you will have to spend, such that there exists a suitable b.
对于每个查询,输出一个整数——即满足存在合适的 b 时,你所需花费的最少硬币数。
输入输出样例
输入#1
4 2 1 5 7 9 3 1 9 9 8 24 6 4 1 1 4 5 1 4 10 20 30 40 1 1 0 0
输出#1
1 7 3 11 16 31 0
说明/提示
In the first test case, we spend 1 coin to increase a2 by 1, resulting in sequence [5,8]. A suitable b would be [1,8]. It can be shown one cannot spend less than 1 coin to achieve the objective.
In the second test case, we can spend 7 coins to increase a1 by 7, resulting in sequence [16,9,8]. A suitable b would be [16,9,1].
在第一个测试用例中,我们花费 1 枚硬币将 a2 增加 1,得到序列 [5,8]。一个合适的 b 序列可以是 [1,8]。可以证明,无法花费少于 1 枚硬币来实现目标。
在第二个测试用例中,我们可以花费 7 枚硬币将 a1 增加 7,得到序列 [16,9,8]。一个合适的 b 序列可以是 [16,9,1]。
输入解题思路,AI测评打分。不知道怎么写?