Al3XS0n

Untitled

Jun 1st, 2024 (edited)
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned int uint;
  9.  
  10.  
  11. int main() {
  12.     uint n, x;
  13.     cin >> n >> x;
  14.     vector<uint> t;
  15.     vector<vector<uint>> bit_counter(n);
  16.     for (int i = 0; i < n; ++i) {
  17.         int v;
  18.         cin >> v;
  19.         t.push_back(v);
  20.         for (int bit = 0; bit < 32; ++bit) {
  21.             bit_counter[i].push_back(v & (1 << bit));
  22.         }
  23.         if (i != 0) {
  24.             for (int bit = 0; bit < 32; ++bit) {
  25.                 bit_counter[i][bit] += bit_counter[i - 1][bit];
  26.             }    
  27.         }
  28.     }
  29.    
  30.     string s;
  31.     int cur_x = x;
  32.     for (int i = n - 1; i >= 0; --i) {
  33.         if ((cur_x | t[i]) > cur_x) {
  34.             s += "^";
  35.             cur_x = cur_x ^ t[i];
  36.         } else {
  37.             bool need_xor = false;
  38.             for (int bit = 0; bit < 32; ++bit) {
  39.                 if (!(cur_x & (1 << bit))) {
  40.                     continue;
  41.                 }
  42.                 if (!(t[i] & (1 << bit))) {
  43.                     continue;
  44.                 }
  45.                 if (bit_counter[i][bit] == 1) {
  46.                     need_xor = true;
  47.                 }
  48.             }
  49.            
  50.             if (need_xor) {
  51.                 s += "^";
  52.                 cur_x = cur_x ^ t[i];
  53.             } else {
  54.                 s += "|";
  55.                 cur_x = cur_x - t[i];
  56.             }
  57.         }
  58.     }
  59.    
  60.     reverse(s.begin(), s.end());
  61.     cout << s;
  62.  
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment