1257F - Make Them Similar - CodeForces Solution


bitmasks brute force hashing meet-in-the-middle *2400

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

const int NMAX = 100;
const int BITSMAX = 30;
const int MIDDLE = BITSMAX / 2;

int a[NMAX];
int n;

int cnt[NMAX];

int diff[NMAX - 1];

int ans = -1;

struct Trie {
  struct node {
    int end = -1;
    map<int, int> link;
  } newNode;

  vector<node> trie;

  Trie() {
    trie.push_back(newNode);
  }

  void add(int mask) {
    int node = 0;
    for (int i = 0; i < n - 1; i++) {
      int p = diff[i];
      if (trie[node].link[p] == 0) {
        trie[node].link[p] = trie.size();
        trie.push_back(newNode);
      }
      node = trie[node].link[p];
    }
    // trie[node].end = mask;
    trie[node].end = (trie[node].end == -1 ? mask : trie[node].end);
  }

  int query() {
    int node = 0;
    for (int i = 0; i < n - 1; i++) {
      int p = diff[i];
      if (trie[node].link[p] == 0)
        return -1;
      node = trie[node].link[p];
    }
    return trie[node].end;
  }
};

void init_read() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
}

void read_data() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
}

/// @brief returns how many bits of 1 x has
/// @param x
int popcount(int x) {
  x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
  x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
  x = ((x >> 4) + x) & 0x0f0f0f0f;
  x = ((x >> 8) + x) & 0x00ff00ff;
  x = ((x >> 16) + x) & 0x0000ffff;
  return x;
}

/// @brief builds vector of consecutive popcount differences
/// @param mask - the digits of the half-th half get xor-ed by mask
/// @param half - 0 for the first half, 1 for the second half
void build_diff(int mask, int half) {
  for (int i = 0; i < n; i++)
    cnt[i] = popcount(((a[i] >> (MIDDLE * half)) & ((1 << MIDDLE) - 1)) ^ mask);

  int sign = (half == 0 ? +1 : -1);

  for (int i = 1; i < n; i++)
    diff[i - 1] = sign * (cnt[i] - cnt[i - 1]);
}

void solve() {
  Trie trie;
  for (int mask = 0; mask < (1 << MIDDLE); mask++) {
    build_diff(mask, 0);
    trie.add(mask);
  }
  for (int mask = 0; mask < (1 << MIDDLE); mask++) {
    build_diff(mask, 1);
    int mask2 = trie.query();
    if (mask2 != -1) {
      ans = (mask << MIDDLE) + mask2;
      return;
    }
  }
}

void check(int mask) {
  if (mask == -1) {
    cout << "OK\n";
    return;
  }
  int checker = popcount(a[0] ^ mask), i = 1;
  while (i < n && popcount(a[i] ^ mask) == checker)
    i++;

  cout << (i == n ? "OK\n" : "WRONG\n");
}

void print_data() {
  cout << ans << '\n';
  // check(ans);
}

int main() {
  init_read();
  read_data();
  solve();
  print_data();

  return 0;
}


Comments

Submit
0 Comments
More Questions

152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh
1721E - Prefix Function Queries
977E - Cyclic Components
1140D - Minimum Triangulation
75C - Modified GCD
1722A - Spell Check
1722B - Colourblindness
1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets
933A - A Twisty Movement
1722F - L-shapes
1196B - Odd Sum Segments
1325D - Ehab the Xorcist
552B - Vanya and Books