Codeforces 1744D D.Divisibility by 2^n

D. Divisibility by 2^n
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array of positive integers a1,a2,…,an.

Make the product of all the numbers in the array (that is, a1⋅a2⋅…⋅an) divisible by 2n.

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

select an arbitrary index i (1≤i≤n) and replace the value ai with ai=ai⋅i.
You cannot apply the operation repeatedly to a single index. In other words, all selected values of i 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 2n. Note that such a set of operations does not always exist.

Input
The first line of the input contains a single integer t (1≤t≤104) — the number test cases.

Then the descriptions of the input data sets follow.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of array a.

The second line of each test case contains exactly n integers: a1,a2,…,an (1≤ai≤109).

It is guaranteed that the sum of n values over all test cases in a test does not exceed 2⋅105.

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

Example
inputCopy
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
outputCopy
0
1
1
-1
2
1
Note
In the first test case, the product of all elements is initially 2, so no operations needed.

In the second test case, the product of elements initially equals 6. We can apply the operation for i=2, and then a2 becomes 2⋅2=4, and the product of numbers becomes 3⋅4=12, and this product of numbers is divided by 2n=22=4.

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

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

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5;
int main(void) {
	int t;
	cin >> t;
	int n, x;
	while (t--) {
		cin >> n;
		vector<int>v(n);
		int cnt = 0, cnt1;
		for (int i = 1; i <= n; i++) {
			cin >> x;
			cnt1 = 0;
			while (x % 2 == 0) {
				cnt++;
				x /= 2;
			}
			int j = i;
			while (j % 2 == 0) {
				cnt1++;
				j /= 2;
			}
			if (cnt1 != 0)v.push_back(cnt1);
		}
		sort(v.begin(), v.end());
		int ans = 0;
		for (int i = v.size() - 1; i >= 0 && cnt < n; i--) {
			cnt += v[i];
			ans++;
		}
		if (cnt < n)cout << -1 << endl;
		else cout << ans << endl;
	}
}
//code by yxisme
//code by 01100_10111
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值