CF1303D.Fill The Bag

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a bag of size nn. Also you have mm boxes. The size of ii-th box is aia_i, where each aia_i is an integer non-negative power of two.

You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.

For example, if n=10n = 10 and a=[1,1,32]a = [1, 1, 32] then you have to divide the box of size 3232 into two parts of size 1616, and then divide the box of size 1616. So you can fill the bag with boxes of size 11, 11 and 88.

Calculate the minimum number of divisions required to fill the bag of size nn.

你有一个容量为 nn 的袋子。此外,你有 mm 个盒子。第 ii 个盒子的大小为 aia_i,其中每个 aia_i 均为非负整数幂(即形如 2k2^k,其中 k≥0k \ge 0 的整数)。

你可以将任意一个盒子均分为两个大小相等的部分。你的目标是恰好装满整个袋子(即所选盒子(或其分割后部分)的大小之和恰好等于 nn)。

例如,若 n=10n = 10 且 a=[1,1,32]a = [1, 1, 32],则你需要先将大小为 3232 的盒子分成两个大小为 1616 的部分,再将其中一个大小为 1616 的部分继续分割(得到两个大小为 88 的部分)。最终可使用大小为 11、11 和 88 的三个盒子恰好填满袋子。

请计算填满容量为 nn 的袋子所需的最少分割次数。

输入格式

The first line contains one integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains two integers nn and mm (1≤n≤1018,1≤m≤1051 \le n \le 10^{18}, 1 \le m \le 10^5) — the size of bag and the number of boxes, respectively.

The second line of each test case contains mm integers a1,a2,…,ama_1, a_2, \dots , a_m (1≤ai≤1091 \le a_i \le 10^9) — the sizes of boxes. It is guaranteed that each aia_i is a power of two.

It is also guaranteed that sum of all mm over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤1018, 1≤m≤1051 \le n \le 10^{18},\ 1 \le m \le 10^5)—— 分别表示袋子的容量和箱子的数量。

每个测试用例的第二行包含 mm 个整数 a1,a2,…,ama_1, a_2, \dots , a_m(1≤ai≤1091 \le a_i \le 10^9)—— 表示各个箱子的大小。保证每个 aia_i 均为 2 的幂。

还保证所有测试用例中 mm 的总和不超过 10510^5。

输出格式

For each test case print one integer — the minimum number of divisions required to fill the bag of size nn (or −1-1, if it is impossible).

对于每个测试用例,输出一个整数——填满容量为 nn 的袋子所需的最少分割次数(若无法实现,则输出 −1-1)。

输入输出样例

  • 输入#1

    3
    10 3
    1 32 1
    23 4
    16 1 4 1
    20 5
    2 1 16 1 8

    输出#1

    2
    -1
    0

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

首页