AT_arc229_b.Halving Subtraction
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a length-N sequence of non-negative integers A=(A1,A2,…,AN).
You can perform the following operation on A any number of times, possibly zero:
- Choose a positive integer x0, and set x=x0. Then, for i=1,2,…,N in this order, do the following:
- Subtract x from Ai, and replace x with ⌊2x⌋.
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.
You are given T test cases; solve each of them.
给你一个长度为 N 的非负整数序列 A=(A1,A2,…,AN)。
你可以对 A 执行以下操作任意多次(包括零次):
- 选择一个正整数 x0,并令 x=x0。然后,按 i=1,2,…,N 的顺序依次执行以下步骤:
- 从 Ai 中减去 x,并将 x 更新为 ⌊2x⌋。
判断是否可能将 A 的所有元素都变为 0;若可能,求出实现该目标所需的最少操作次数。
你将得到 T 组测试数据,请分别求解每组数据。
输入格式
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each test case is given in the following format:
N
A1 A2 … AN
输入从标准输入中按以下格式给出:
T
case1
case2
⋮
caseT
每个测试用例按以下格式给出:
N
A1 A2 … AN
输出格式
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 A equal to 0, output the minimum number of operations required to do so; if it is not possible, output -1.
按顺序输出各测试用例的答案,答案之间用换行符分隔。
对于每个测试用例,如果可以将数组 A 的所有元素都变为 0,则输出实现该目标所需的最少操作次数;否则输出 -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 A equal to 0 in two operations:
- Choose x0=3. A=(10,4) becomes A=(7,3).
- Choose x0=7. A=(7,3) becomes A=(0,0).
It is impossible to make all elements of A equal to 0 in fewer than two operations, so output 2 on the first line.
Constraints
- 1≤T≤104
- 1≤N≤30
- 0≤Ai≤109
- All input values are integers.
样例 1 解释:
考虑第一个测试用例。
通过如下操作,可在两次操作内使数组 A 的所有元素均变为 0:
- 选择 x0=3,则 A=(10,4) 变为 A=(7,3);
- 选择 x0=7,则 A=(7,3) 变为 A=(0,0)。
无法在少于两次操作内使 A 的所有元素变为 0,因此第一行输出 2。
限制条件
- 1≤T≤104
- 1≤N≤30
- 0≤Ai≤109
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?