CF2147E.Maximum OR Popcount

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array of nn non-negative integers.

You want to answer given qq independent scenarios. In the ii-th scenario, you are allowed to perform the following operation at most bib_i times:

  • Choose an element of the array and increase it by 11.

Your goal is to maximize the number of bits that are equal to 11 in the bitwise OR of all numbers in the array. Find this number for each scenario.

给你一个包含 nn 个非负整数的数组。

你需要回答 qq 个相互独立的询问。在第 ii 个询问中,你最多可以执行以下操作 bib_i 次:

  • 选择数组中的一个元素,并将其加 11。

你的目标是最大化该数组中所有数字的按位或结果中值为 11 的比特位数量。对每个询问,求出该最大值。

输入格式

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

The first line of each test case contains two integers nn and qq (1≤n,q≤1051 \leq n, q \leq 10^{5}) — the size of the array and the number of scenarios.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤1090 \leq a_i \leq 10^{9}) — the elements of the array.

The ii-th of the next qq lines contains a single integer bib_i (0≤bi≤1090 \leq b_i \leq 10^{9}) — the maximum number of operations allowed in the ii-th scenario.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^{5}, and the sum of qq over all test cases does not exceed 10510^{5}.

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

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n,q≤1051 \leq n, q \leq 10^{5})—— 分别表示数组的大小和场景数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤1090 \leq a_i \leq 10^{9})—— 表示数组的元素。

接下来的 qq 行中,第 ii 行包含一个整数 bib_i(0≤bi≤1090 \leq b_i \leq 10^{9})—— 表示第 ii 个场景中允许的最大操作次数。

保证所有测试用例的 nn 之和不超过 10510^{5},且所有测试用例的 qq 之和不超过 10510^{5}。

输出格式

For each test case, output qq lines, the ii-th of them containing a single integer — the maximum possible number of bits equal to 11 in the bitwise OR in the ii-th scenario.

对于每个测试用例,输出 qq 行,其中第 ii 行包含一个整数——即第 ii 个场景下按位或(bitwise OR)结果中等于 11 的比特位的最大可能数量。

输入输出样例

  • 输入#1

    3
    1 3
    0
    0
    2
    4
    2 2
    1 3
    0
    3
    2 1
    1000000000 1000000000
    1000000000

    输出#1

    0
    1
    2
    2
    3
    31

说明/提示

Visualizer link

In the first test case:

  • In the first scenario, we don't have any operations, and therefore the answer is equal to the number of 11-bits in the bitwise OR of the original array, 00, which has 00 bits set in its binary representation.
  • In the second scenario, one way of achieving 11 bit set in the bitwise OR is increasing a1a_1 by 11 twice, obtaining a bitwise OR of 2=(10)22={(10)}_2. It can be shown that it is the best possible value we can obtain by performing the operation at most twice.
  • In the third scenario, one way of achieving a result of 22 is adding 11 to a1a_1 three times, which can be shown to be optimal. Note that you don't have to apply the operation 44 times.

In the second test case:

  • In the first scenario, we don't have any operations, and therefore the answer is equal to the number of 11-bits in the bitwise OR of the original array, which is 22.
  • In the second scenario, one way of achieving a result of 33 is adding 11 to a2a_2 three times, which can be shown to be optimal.

可视化链接

在第一个测试用例中:

  • 在第一种情形下,不执行任何操作,因此答案等于原数组按位或结果(即 00)的二进制表示中 11 的个数,为 00。
  • 在第二种情形下,一种使按位或结果中 11 的个数达到 11 的方法是将 a1a_1 增加 11 两次,得到按位或结果为 2=(10)22={(10)}_2。可以证明,在最多执行两次操作的前提下,这是所能达到的最优值。
  • 在第三种情形下,一种使结果达到 22 的方法是将 a1a_1 增加 11 三次,可以证明该方案是最优的。注意:你无需恰好执行 44 次操作。

在第二个测试用例中:

  • 在第一种情形下,不执行任何操作,因此答案等于原数组按位或结果的二进制表示中 11 的个数,为 22。
  • 在第二种情形下,一种使结果达到 33 的方法是将 a2a_2 增加 11 三次,可以证明该方案是最优的。

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

首页