Codeforces Round #828 (Div. 3) D. Divisibility by 2^n

Codeforces Round #828 (Div. 3) D. Divisibility by 2^n

  • Let’s notice that if we multiply the numbers a 1 ⋅ a 2 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ a n a_1⋅a_2 ⋅ ⋅⋅ ⋅ ⋅ ⋅ a_n a1a2an, 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 1n 各个数字相乘,能否整除 2 n 2^n 2n ,就是判断每个数字分解之后有多少个2。如果不够再加上 1 − n 1-n 1n 之间的 i i i ,在进行计算。
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int T;cin>>T;
    while(T--)
    {
        int n;cin>>n;
        int sum=0;
        for(int i=0;i<n;i++)
        {
            int x;cin>>x;
            sum+=__builtin_ctz(x);
        }

        vector<int> cnt(20);
        for(int i=1;i<=n;i++)
            cnt[__builtin_ctz(i)]++;

        int ans=0;
        for(int i=19;i>=0;i--)
            while(sum<n && cnt[i])
            {
                sum+=i;
                cnt[i]--;
                ans++;
            }

        if(sum<n) ans=-1;
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值