Beingamanforever

Hackerrank Great XOR

Nov 12th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-11-12 21:54:31
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     int n;
  28.     cin >> n;
  29.     // TODO : since k ^ n >= n, then if we have a 0 bit in n at mth position, we think how many numbers can be formed, i.e 2^m
  30.     int ans = 0;
  31.     int m = 1;
  32.     while (n)
  33.     {
  34.         // if its off then we think how many numbers have 1 at that position = 2^m
  35.         if (!(n & 1))
  36.         {
  37.             ans |= m;
  38.         }
  39.         n >>= 1;
  40.         m <<= 1;
  41.     }
  42.     cout << ans << endl;
  43.     return;
  44. }
  45.  
  46. signed main()
  47. {
  48.     NeedForSpeed;
  49.     int t = 1;
  50.     cin >> t;
  51.     while (t--)
  52.     {
  53.         solve();
  54.     }
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment