Advertisement
pb_jiang

ABC321E WA

Dec 13th, 2023
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     ll t, n, x, k;
  18.     cin >> t;
  19.     while (t--) {
  20.         cin >> n >> x >> k;
  21.         dbg(n, x, k);
  22.         auto lsb = [](ll r, ll d) -> ll { return r << d; };
  23.         auto rsb = [](ll r, ll d) -> ll {
  24.             if (std::__lg(r) + d >= 63)
  25.                 return LLONG_MAX;
  26.             return (r << d) + (1ll << d) - 1ll;
  27.         };
  28.         function<ll(ll, ll)> get_child_cnt = [n, &get_child_cnt, &lsb, &rsb](ll root, ll dep) -> ll {
  29.             if (dep == 0)
  30.                 return ll(root <= n);
  31.             if (root > n || root == 0)
  32.                 return 0ll;
  33.             ll ls = root * 2, rs = root * 2 + 1;
  34.             ll cnt = 0;
  35.  
  36.             if (rsb(ls, dep - 1) <= n) {
  37.                 cnt += 1ll << (dep - 1);
  38.                 if (rsb(rs, dep - 1) <= n)
  39.                     cnt += 1ll << (dep - 1);
  40.                 else
  41.                     cnt += get_child_cnt(rs, dep - 1);
  42.             } else
  43.                 cnt += get_child_cnt(ls, dep - 1);
  44.             return cnt;
  45.         };
  46.  
  47.         ll ans = get_child_cnt(x, k);
  48.         dbg(x, k, ans);
  49.         ll os = x ^ 1, dep = k - 2;
  50.         while (dep >= 0 && os) {
  51.             ans += get_child_cnt(os, dep);
  52.             os = ((os / 2) ^ 1), dep -= 1;
  53.         }
  54.         // ans += ll((x >> k) != 0) - ll(k == 0);
  55.         cout << ans << endl;
  56.     }
  57.     return 0;
  58. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement