[Solution] Divisibility by 2^n Codeforces Solution
You are given an array of positive integers .
Make the product of all the numbers in the array (that is, ) divisible by .
You can perform the following operation as many times as you like:
- select an arbitrary index () and replace the value with .
You cannot apply the operation repeatedly to a single index. In other words, all selected values of 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 . Note that such a set of operations does not always exist.
The first line of the input contains a single integer ) — the number test cases.
Then the descriptions of the input data sets follow.
The first line of each test case contains a single integer () — the length of array .
The second line of each test case contains exactly integers: ().
It is guaranteed that the sum of values over all test cases in a test does not exceed .
For each test case, print the least number of operations to make the product of all numbers in the array divisible by . If the answer does not exist, print -1.
In the first test case, the product of all elements is initially , so no operations needed.
In the second test case, the product of elements initially equals . We can apply the operation for , and then becomes , and the product of numbers becomes , and this product of numbers is divided by .
In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by — it will be , which does not divide by .
In the fifth test case, we can apply operations for and
No comments:
Post a Comment