Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- typedef unsigned int uint;
- int main() {
- uint n, x;
- cin >> n >> x;
- vector<uint> t;
- vector<vector<uint>> bit_counter(n);
- for (int i = 0; i < n; ++i) {
- int v;
- cin >> v;
- t.push_back(v);
- for (int bit = 0; bit < 32; ++bit) {
- bit_counter[i].push_back(v & (1 << bit));
- }
- if (i != 0) {
- for (int bit = 0; bit < 32; ++bit) {
- bit_counter[i][bit] += bit_counter[i - 1][bit];
- }
- }
- }
- string s;
- int cur_x = x;
- for (int i = n - 1; i >= 0; --i) {
- if ((cur_x | t[i]) > cur_x) {
- s += "^";
- cur_x = cur_x ^ t[i];
- } else {
- bool need_xor = false;
- for (int bit = 0; bit < 32; ++bit) {
- if (!(cur_x & (1 << bit))) {
- continue;
- }
- if (!(t[i] & (1 << bit))) {
- continue;
- }
- if (bit_counter[i][bit] == 1) {
- need_xor = true;
- }
- }
- if (need_xor) {
- s += "^";
- cur_x = cur_x ^ t[i];
- } else {
- s += "|";
- cur_x = cur_x - t[i];
- }
- }
- }
- reverse(s.begin(), s.end());
- cout << s;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment