CF1744D.Divisibility by 2^n

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array of positive integers a1,a2,…,ana_1, a_2, \ldots, a_n.

Make the product of all the numbers in the array (that is, a1⋅a2⋅…⋅ana_1 \cdot a_2 \cdot \ldots \cdot a_n) divisible by 2n2^n.

You can perform the following operation as many times as you like:

  • select an arbitrary index ii (1≤i≤n1 \leq i \leq n) and replace the value aia_i with ai=ai⋅ia_i=a_i \cdot i.

You cannot apply the operation repeatedly to a single index. In other words, all selected values of ii must be different.

Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by 2n2^n. Note that such a set of operations does not always exist.

给你一个正整数数组 a1,a2,…,ana_1, a_2, \ldots, a_n。

要求使数组中所有数的乘积(即 a1⋅a2⋅…⋅ana_1 \cdot a_2 \cdot \ldots \cdot a_n)能被 2n2^n 整除。

你可以执行以下操作任意多次:

  • 任选一个下标 ii(1≤i≤n1 \leq i \leq n),并将 aia_i 替换为 ai=ai⋅ia_i = a_i \cdot i。

但你不能对同一个下标重复执行该操作。换言之,所有被选中的 ii 值必须互不相同。

求使数组所有元素的乘积能被 2n2^n 整除所需的最少操作次数。注意:这样的操作集合并不总是存在。

输入格式

The first line of the input contains a single integer tt (1≤t≤104(1 \leq t \leq 10^4) — the number test cases.

Then the descriptions of the input data sets follow.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the length of array aa.

The second line of each test case contains exactly nn integers: a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \leq 10^9).

It is guaranteed that the sum of nn values over all test cases in a test does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 表示测试用例的数量。

随后是各测试用例的输入数据描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)—— 表示数组 aa 的长度。

每个测试用例的第二行包含恰好 nn 个整数:a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \leq a_i \leq 10^9)。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print the least number of operations to make the product of all numbers in the array divisible by 2n2^n. If the answer does not exist, print -1.

对于每个测试用例,输出使数组中所有数的乘积能被 2n2^n 整除所需的最少操作次数。如果不存在这样的答案,则输出 −1-1。

输入输出样例

  • 输入#1

    6
    1
    2
    2
    3 2
    3
    10 6 11
    4
    13 17 1 1
    5
    1 1 12 1 1
    6
    20 7 14 18 3 5

    输出#1

    0
    1
    1
    -1
    2
    1

说明/提示

In the first test case, the product of all elements is initially 22, so no operations needed.

In the second test case, the product of elements initially equals 66. We can apply the operation for i=2i = 2, and then a2a_2 becomes 2⋅2=42\cdot2=4, and the product of numbers becomes 3⋅4=123\cdot4=12, and this product of numbers is divided by 2n=22=42^n=2^2=4.

In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by 2n2^n — it will be (13⋅1)⋅(17⋅2)⋅(1⋅3)⋅(1⋅4)=5304(13\cdot1)\cdot(17\cdot2)\cdot(1\cdot3)\cdot(1\cdot4)=5304, which does not divide by 2n=24=162^n=2^4=16.

In the fifth test case, we can apply operations for i=2i = 2 and i=4i = 4.

在第一个测试用例中,所有元素的乘积初始为 22,因此无需进行任何操作。

在第二个测试用例中,元素的乘积初始为 66。我们可以对 i=2i = 2 执行操作,此时 a2a_2 变为 2⋅2=42\cdot2=4,数字的乘积变为 3⋅4=123\cdot4=12,而该乘积可被 2n=22=42^n=2^2=4 整除。

在第四个测试用例中,即使执行所有可能的操作,我们仍无法使数字的乘积被 2n2^n 整除——其结果为 (13⋅1)⋅(17⋅2)⋅(1⋅3)⋅(1⋅4)=5304(13\cdot1)\cdot(17\cdot2)\cdot(1\cdot3)\cdot(1\cdot4)=5304,而 53045304 不能被 2n=24=162^n=2^4=16 整除。

在第五个测试用例中,我们可以对 i=2i = 2 和 i=4i = 4 执行操作。

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

首页