AT_arc229_b.Halving Subtraction

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a length-NN sequence of non-negative integers A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N).

You can perform the following operation on AA any number of times, possibly zero:

  • Choose a positive integer x0x_0, and set x=x0x=x_0. Then, for i=1,2,…,Ni=1,2,\ldots,N in this order, do the following:
    • Subtract xx from AiA_i, and replace xx with ⌊x2⌋\displaystyle \left\lfloor \frac x2\right\rfloor.

Determine whether it is possible to make all elements of AA equal to 00, and if it is possible, find the minimum number of operations required to do so.

You are given TT test cases; solve each of them.

给你一个长度为 NN 的非负整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N)。

你可以对 AA 执行以下操作任意多次(包括零次):

  • 选择一个正整数 x0x_0,并令 x=x0x=x_0。然后,按 i=1,2,…,Ni=1,2,\ldots,N 的顺序依次执行以下步骤:
    • 从 AiA_i 中减去 xx,并将 xx 更新为 ⌊x2⌋\displaystyle \left\lfloor \frac x2\right\rfloor。

判断是否可能将 AA 的所有元素都变为 00;若可能,求出实现该目标所需的最少操作次数。

你将得到 TT 组测试数据,请分别求解每组数据。

输入格式

The input is given from Standard Input in the following format:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

Each test case is given in the following format:

NN
A1A_1 A2A_2 …\ldots ANA_N

输入从标准输入中按以下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

每个测试用例按以下格式给出:

NN
A1A_1 A2A_2 …\ldots ANA_N

输出格式

Output the answers for the test cases in order, separated by newlines.

For each test case, if it is possible to make all elements of AA equal to 00, output the minimum number of operations required to do so; if it is not possible, output -1.

按顺序输出各测试用例的答案,答案之间用换行符分隔。

对于每个测试用例,如果可以将数组 AA 的所有元素都变为 00,则输出实现该目标所需的最少操作次数;否则输出 -1。

输入输出样例

  • 输入#1

    4
    2
    10 4
    2
    0 1
    3
    0 0 0
    5
    16 7 2 1 0

    输出#1

    2
    -1
    0
    3

说明/提示

Sample 1 Explanation:
Consider the first test case.

By performing the operations as follows, you can make all elements of AA equal to 00 in two operations:

  • Choose x0=3x_0=3. A=(10,4)A=(10,4) becomes A=(7,3)A=(7,3).
  • Choose x0=7x_0=7. A=(7,3)A=(7,3) becomes A=(0,0)A=(0,0).

It is impossible to make all elements of AA equal to 00 in fewer than two operations, so output 22 on the first line.

Constraints

  • 1≤T≤1041\le T\le 10^4
  • 1≤N≤301\le N\le 30
  • 0≤Ai≤1090\le A_i\le 10^9
  • All input values are integers.

样例 1 解释:
考虑第一个测试用例。

通过如下操作,可在两次操作内使数组 AA 的所有元素均变为 00:

  • 选择 x0=3x_0=3,则 A=(10,4)A=(10,4) 变为 A=(7,3)A=(7,3);
  • 选择 x0=7x_0=7,则 A=(7,3)A=(7,3) 变为 A=(0,0)A=(0,0)。

无法在少于两次操作内使 AA 的所有元素变为 00,因此第一行输出 22。

限制条件

  • 1≤T≤1041\le T\le 10^4
  • 1≤N≤301\le N\le 30
  • 0≤Ai≤1090\le A_i\le 10^9
  • 所有输入值均为整数。

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

首页