Let’s notice that if we multiply the numbers
a
1
⋅
a
2
⋅
⋅
⋅
⋅
⋅
⋅
a
n
a_1⋅a_2 ⋅ ⋅⋅ ⋅ ⋅ ⋅ a_n
a1⋅a2⋅⋅⋅⋅⋅⋅an, then the power of two of the product is equal to the sum of the powers of two in each of the numbers.
Let’s calculate the initial sum of the powers of twos in the product. This can be done as follows: let’s take the element
a
i
a_i
ai and divide it by
2
2
2 as long as we can, while remembering to increase our counter by the number of occurrences of a power of two.
Now let’s move on to operations and note that choosing the index
i
i
i will increase the degree of occurrence of two by a fixed number (that is, it does not matter when to apply this operation). Choosing an index
i
i
i will increment the counter by a number
x
x
x such that
i
i
i is divisible by
2
x
2^x
2x but not by
2
x
+
1
2^{x+1}
2x+1 — you can find this
x
x
x, again , by dividing by
2
2
2 while we can.
Since we want to minimize the number of operations used, at each moment of time we will try to use an index that increases the counter by the largest number. To do this, it is enough to sort the indices by this index of increase and take them greedily from the largest increase to the smallest.
We get the solution in
O
(
n
l
o
g
n
+
n
l
o
g
A
)
.
O(nlogn+nlogA) .
O(nlogn+nlogA).
问所有的数字相乘之后,最多还可与
1
−
n
1-n
1−n 各个数字相乘,能否整除
2
n
2^n
2n ,就是判断每个数字分解之后有多少个2。如果不够再加上
1
−
n
1-n
1−n 之间的
i
i
i ,在进行计算。
Codeforces Round #828 (Div. 3) D. Divisibility by 2^n
# [Codeforces Round #828 (Div. 3) D. Divisibility by 2^n](https://codeforces.com/contest/1744/problem/D)- Let's notice that if we multiply the numbers $a_1⋅a_2 ⋅ ⋅⋅ ⋅ ⋅ ⋅ a_n$, then the power of two of the product is equal to the sum of the powers of t