减半减法AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)You are given a length-$N$ sequence of non-negative integers $A=(A_1,A_2,\dots,A_N)$.
给定一个长度为 $N$ 的非负整数序列 $A=(A_1,A_2,\dots,A_N)$。AI (CI翻译)
You can perform the following operation on $A$ any number of times, possibly zero:
可以对 $A$ 执行以下操作任意次,也可以一次都不执行:AI (CI翻译)
- Choose a positive integer
$x_0$, and set$x=x_0$. Then, for$i=1,2,\ldots,N$in this order, do the following: - Subtract
$x$from$A_i$, and replace$x$with$\displaystyle \left\lfloor \frac x2\right\rfloor$.
- 选择一个正整数
$x_0$,令$x=x_0$。随后按照$i=1,2,\ldots,N$的顺序执行以下操作: - 从
$A_i$中减去$x$,并将$x$替换为$\displaystyle \left\lfloor \frac x2\right\rfloor$。
Determine whether it is possible to make all elements of $A$ equal to $0$, and if it is possible, find the minimum number of operations required to do so.
请判断是否可以通过上述操作使 $A$ 的所有元素都变为 $0$。如果可以,求所需的最少操作次数。AI (CI翻译)
You are given $T$ test cases; solve each of them.
给定 $T$ 个测试用例,请分别求解。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$1\le T\le 10^4$$1\le N\le 30$$0\le A_i\le 10^9$- All input values are integers.
$1\le T\le 10^4$$1\le N\le 30$$0\le A_i\le 10^9$- 所有输入值均为整数。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入从标准输入按照以下格式给出:AI (CI翻译)
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_TAI (CI翻译)
Each test case is given in the following format:
每个测试用例按照以下格式给出:AI (CI翻译)
N
A_1 A_2 \ldots A_N
N
A_1 A_2 \ldots A_NAI (CI翻译)
Output
输出格式
AI (CI翻译)Output the answers for the test cases in order, separated by newlines.
按照测试用例的顺序输出答案,每个答案占一行。AI (CI翻译)
For each test case, if it is possible to make all elements of $A$ equal to $0$, output the minimum number of operations required to do so; if it is not possible, output -1.
对于每个测试用例,如果可以使 $A$ 的所有元素都变为 $0$,则输出所需的最少操作次数;如果无法做到,则输出 -1。AI (CI翻译)
样例
4
2
10 4
2
0 1
3
0 0 0
5
16 7 2 1 0
2
-1
0
3
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。