CF2240A.Another Popcount Problem
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two integers n and k.
Your task is to construct a sequence a consisting of k non-negative integers a1,a2,…,ak such that:
- ∑i=1kai≤n
- The total number of set bits, i.e., ∑i=1kpopcount(ai), is as large as possible.
You only need to output the maximum possible value of ∑i=1kpopcount(ai).
Here, popcount(x) denotes the number of 1 bits in the binary representation of x. For example, popcount(6)=popcount((110)2)=2, and popcount(0)=0.
给你两个整数 n 和 k。
你的任务是构造一个由 k 个非负整数 a1,a2,…,ak 组成的序列 a,满足:
- ∑i=1kai≤n
- 所有数的二进制表示中 1 的总个数(即 ∑i=1kpopcount(ai))尽可能大。
你只需输出 ∑i=1kpopcount(ai) 的最大可能值。
其中,popcount(x) 表示 x 的二进制表示中 1 的个数。例如,popcount(6)=popcount((110)2)=2,且 popcount(0)=0。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤103). The description of the test cases follows.
Each of the next t lines contains two integers n and k (1≤n,k≤106) — the maximum allowed sum of the sequence and the length of the sequence, respectively.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤103)。随后是测试用例的描述。
接下来的 t 行中,每行包含两个整数 n 和 k(1≤n,k≤106),分别表示序列元素和的最大允许值以及序列的长度。
输出格式
For each test case, output a single integer — the maximum possible value of ∑i=1kpopcount(ai).
对于每个测试用例,输出一个整数——∑i=1kpopcount(ai) 的最大可能值。
输入输出样例
输入#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=2 and k=1. We can choose a=[1] or a=[2]. In both cases, the sum of popcounts is 1.
In the second test case, n=3 and k=1. We can choose a=[3], since (3)2=(11)2, popcount(3)=2.
In the third test case, n=6 and k=2. We can choose a=[3,3]. The sum is 3+3=6≤6, and the total popcount is popcount(3)+popcount(3)=2+2=4.
在第一个测试用例中,n=2 且 k=1。我们可以选择 a=[1] 或 a=[2]。在这两种情况下,popcount 之和均为 1。
在第二个测试用例中,n=3 且 k=1。我们可以选择 a=[3],因为 (3)2=(11)2,popcount(3)=2。
在第三个测试用例中,n=6 且 k=2。我们可以选择 a=[3,3]。其和为 3+3=6≤6,且总 popcount 为 popcount(3)+popcount(3)=2+2=4。
输入解题思路,AI测评打分。不知道怎么写?