CF2240A.Another Popcount Problem

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two integers nn and kk.

Your task is to construct a sequence aa consisting of kk non-negative integers a1,a2,…,aka_1, a_2, \ldots, a_k such that:

  • ∑i=1kai≤n\sum_{i=1}^{k} a_i \le n
  • The total number of set bits, i.e., ∑i=1kpopcount⁡(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i), is as large as possible.

You only need to output the maximum possible value of ∑i=1kpopcount⁡(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i).

Here, popcount⁡(x)\operatorname{popcount}(x) denotes the number of 11 bits in the binary representation of xx. For example, popcount⁡(6)=popcount⁡((110)2)=2\operatorname{popcount}(6) = \operatorname{popcount}((110)_2) = 2, and popcount⁡(0)=0\operatorname{popcount}(0) = 0.

给你两个整数 nn 和 kk。

你的任务是构造一个由 kk 个非负整数 a1,a2,…,aka_1, a_2, \ldots, a_k 组成的序列 aa,满足:

  • ∑i=1kai≤n\sum_{i=1}^{k} a_i \le n
  • 所有数的二进制表示中 11 的总个数(即 ∑i=1kpopcount⁡(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i))尽可能大。

你只需输出 ∑i=1kpopcount⁡(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i) 的最大可能值。

其中,popcount⁡(x)\operatorname{popcount}(x) 表示 xx 的二进制表示中 11 的个数。例如,popcount⁡(6)=popcount⁡((110)2)=2\operatorname{popcount}(6) = \operatorname{popcount}((110)_2) = 2,且 popcount⁡(0)=0\operatorname{popcount}(0) = 0。

输入格式

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.

Each of the next tt lines contains two integers nn and kk (1≤n,k≤1061 \le n, k \le 10^6) — the maximum allowed sum of the sequence and the length of the sequence, respectively.

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

接下来的 tt 行中,每行包含两个整数 nn 和 kk(1≤n,k≤1061 \le n, k \le 10^6),分别表示序列元素和的最大允许值以及序列的长度。

输出格式

For each test case, output a single integer — the maximum possible value of ∑i=1kpopcount⁡(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i).

对于每个测试用例,输出一个整数——∑i=1kpopcount⁡(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i) 的最大可能值。

输入输出样例

  • 输入#1

    6
    2 1
    3 1
    6 2
    14142 137205
    1000000 100
    1000000 1000000

    输出#1

    1
    2
    4
    14142
    1322
    1000000

说明/提示

In the first test case, n=2n=2 and k=1k=1. We can choose a=[1]a = [1] or a=[2]a = [2]. In both cases, the sum of popcounts is 11.

In the second test case, n=3n=3 and k=1k=1. We can choose a=[3]a = [3], since (3)2=(11)2(3)_2 = (11)_2, popcount⁡(3)=2\operatorname{popcount}(3) = 2.

In the third test case, n=6n=6 and k=2k=2. We can choose a=[3,3]a = [3, 3]. The sum is 3+3=6≤63 + 3 = 6 \le 6, and the total popcount is popcount⁡(3)+popcount⁡(3)=2+2=4\operatorname{popcount}(3) + \operatorname{popcount}(3) = 2 + 2 = 4.

在第一个测试用例中,n=2n=2 且 k=1k=1。我们可以选择 a=[1]a = [1] 或 a=[2]a = [2]。在这两种情况下,popcount 之和均为 11。

在第二个测试用例中,n=3n=3 且 k=1k=1。我们可以选择 a=[3]a = [3],因为 (3)2=(11)2(3)_2 = (11)_2,popcount⁡(3)=2\operatorname{popcount}(3) = 2。

在第三个测试用例中,n=6n=6 且 k=2k=2。我们可以选择 a=[3,3]a = [3, 3]。其和为 3+3=6≤63 + 3 = 6 \le 6,且总 popcount 为 popcount⁡(3)+popcount⁡(3)=2+2=4\operatorname{popcount}(3) + \operatorname{popcount}(3) = 2 + 2 = 4。

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

首页