ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1615-B
    코드포스 2021. 12. 25. 21:08
     

    Problem - B - 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
    #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;
     
    vector<int> two;
     
    void make_two(vector<int>& two)
    {
        int t = 1;
        ooop(i, 19){
            two.emplace_back(t);
            t *= 2;
        }
        return;
    }
     
    int zero_cnt(int bit, int num)
    {
        int cycle = two[bit+1];
        int q = num/cycle;
        int r = num%cycle;
        int ret = two[bit]*q;
        if(r < two[bit]){
            ret += r+1;
            return ret;
        }
        else{
            ret += two[bit];
            return ret;
        }
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int t;
        cin >> t;
        make_two(two);
        while(t--){
            int l, r;
            cin >> l >> r;
     
            int minval = INT_MAX;
            ooop(bit, 18){
                int tmp = zero_cnt(bit, r) - zero_cnt(bit, l-1);
                if(tmp < minval) minval = tmp;
            }
     
            cout << minval << endl;
        }
     
        return 0;

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

    1303-B, *1400  (0) 2021.12.26
    1615-C  (0) 2021.12.25
    1615-A  (0) 2021.12.25
    1301-B, *1500  (0) 2021.12.24
    1301-C, *1700  (0) 2021.12.24

    댓글

Designed by Tistory.