ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1303-D, *1900
    코드포스 2021. 12. 26. 14:33
     

    Problem - D - Codeforces

     

    codeforces.com

     

     

    풀이

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    #include <bits/stdc++.h>
    #define endl "\n"
    #define ooop(i, n) for(int i = 0; i < n; i++)
    #define loop(i, n) for(int i = 1; i <= n; i++)
    #define all(v) (v).begin(), (v).end()
     
    using namespace std;
    typedef long long ll;
    typedef pair<intint> pi;
    typedef pair<ll, ll> pl;
     
    map<ll, int> exponent;
     
    void make_exponent(map<ll, int>& exponent)
    {
        ll i = 1;
        ooop(bit, 30){
            exponent[i] = bit;
            i *= 2;
        }
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int t;
        cin >> t;
        make_exponent(exponent);
        while(t--){
            ll n, m;
            cin >> n >> m;
            vector<int> cnt(30);
            ll a, sum = 0;
            loop(i, m){
                cin >> a;
                sum += a;
                cnt[exponent[a]]++;
            }
     
            if(sum < n) cout << -1 << endl;
            else{
                int divide_cnt = 0;
                ll acc = 0;
                ooop(bit, 60){
                    ll cur = (1LL<<bit);
                    acc += cnt[bit]*cur;
                    if((cur&n) == 0continue;
                    if(acc >= cur) acc -= cur;
                    else{
                        int p = bit;
                        while(cnt[p] == 0) p++;
                        divide_cnt += p-bit;
                        cnt[p]--;
                        for(int b = p-1; b >= bit+1; b--) cnt[b]++;
                        acc += cur;
                    }
                }
                cout << divide_cnt << endl;
            }
        }
     
        return 0;

    ※ 1 << 60 은 int 라서 원하는 결과 안나옴!! (1LL << 60 ) 사용해야함!

     

    '코드포스' 카테고리의 다른 글

    1623-B  (0) 2021.12.29
    1622-B  (0) 2021.12.29
    1303-C, *1600  (0) 2021.12.26
    1303-B, *1400  (0) 2021.12.26
    1615-C  (0) 2021.12.25

    댓글

Designed by Tistory.