Problem p02816 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

#define int long long
// region TEMPLATE
#define double long double
typedef long long ll;
typedef unsigned long long ull;

template <typename T> void __read(T &a) { cin >> a; }

template <typename T, typename... Args> void __read(T &a, Args &...args) {
  cin >> a;
  __read(args...);
}

#define make(t, ...)                                                           \
  t __VA_ARGS__;                                                               \
  __read(__VA_ARGS__)
#define makev(a, n)                                                            \
  VI a(n);                                                                     \
  FOR(n) cin >> a[i];
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define FORIT(p) for (auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for (signed i = 0; i < (b); i++)
#define FORE(i, l, r) for (int i = (l); i < (r); i++)
#define NFOR(i, n) for (int i = (((int)n) - 1); i >= 0; i--)
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
constexpr ll M7 = 1000000007ll;
constexpr ll M9 = 1000000009ll;
constexpr ll MFFT = 998244353ll;
template <class T> void outv(T &a) {
  for (auto &x : a)
    cout << x << ' ';
}
static mt19937
    rnd(static_cast<unsigned>(chrono::steady_clock::now()
                                  .time_since_epoch()
                                  .count())); // NOLINT(cert-err58-cpp)
template <class T>
static auto __super_speed__ =
    (ios_base::sync_with_stdio(0),
     cin.tie(
         0)); // NOLINT(cert-err58-cpp,cppcoreguidelines-interfaces-global-init)
// endregion

const int MAXN = 200'000;
const int P = 10000 + rnd() % 1000;
int D[MAXN + 1];

void initial() {
  D[0] = 1;
  for (int i = 1; i <= MAXN; ++i) {
    D[i] = (D[i - 1] * P) % M7;
  }
}

VI calc_hash(const VI &a) {
  VI h(30);
  for (int b = 0; b < 30; ++b) {
    for (int i = 0; i < a.size(); ++i) {
      h[b] = (h[b] + ((a[i] >> b) & 1) * D[i]) % M7;
    }
  }
  return h;
}

int32_t main() {
  make(int, n);
  makev(a, n);
  makev(b, n);
  initial();
  auto ha = calc_hash(a);
  auto hb = calc_hash(b);
  int z = accumulate(D, D + n, 0ll) % M7;
  for (int i = 0; i < n; ++i) {
    int X = a[i] ^ b[0];
    VI th = ha;
    for (int b = 0; b < 30; ++b) {
      if ((X >> b) & 1) {
        th[b] = (z - th[b] + M7) % M7;
      }
    }
    cerr << X << endl;
    for (int b = 0; b < 5; ++b) {
      cerr << th[b] << ' ' << hb[b] << endl;
    }
    cerr << endl;
    if (th == hb) {
      cout << i << ' ' << X << '\n';
    }
    for (int b = 0; b < 30; ++b) {
      if ((a[i] >> b) & 1) {
        ha[b] = (ha[b] + D[i] * (D[n] + M7 - 1)) % M7;
      }
      hb[b] = hb[b] * P % M7;
    }
    z = z * P % M7;
  }

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

#define int long long
// region TEMPLATE
#define double long double
typedef long long ll;
typedef unsigned long long ull;

template <typename T> void __read(T &a) { cin >> a; }

template <typename T, typename... Args> void __read(T &a, Args &...args) {
  cin >> a;
  __read(args...);
}

#define make(t, ...)                                                           \
  t __VA_ARGS__;                                                               \
  __read(__VA_ARGS__)
#define makev(a, n)                                                            \
  VI a(n);                                                                     \
  FOR(n) cin >> a[i];
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define FORIT(p) for (auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for (signed i = 0; i < (b); i++)
#define FORE(i, l, r) for (int i = (l); i < (r); i++)
#define NFOR(i, n) for (int i = (((int)n) - 1); i >= 0; i--)
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
constexpr ll M7 = 1000000007ll;
constexpr ll M9 = 1000000009ll;
constexpr ll MFFT = 998244353ll;
template <class T> void outv(T &a) {
  for (auto &x : a)
    cout << x << ' ';
}
static mt19937
    rnd(static_cast<unsigned>(chrono::steady_clock::now()
                                  .time_since_epoch()
                                  .count())); // NOLINT(cert-err58-cpp)
template <class T>
static auto __super_speed__ =
    (ios_base::sync_with_stdio(0),
     cin.tie(
         0)); // NOLINT(cert-err58-cpp,cppcoreguidelines-interfaces-global-init)
// endregion

const int MAXN = 200'000;
const int P = 10000 + rnd() % 1000;
int D[MAXN + 1];

void initial() {
  D[0] = 1;
  for (int i = 1; i <= MAXN; ++i) {
    D[i] = (D[i - 1] * P) % M7;
  }
}

VI calc_hash(const VI &a) {
  VI h(30);
  for (int b = 0; b < 30; ++b) {
    for (int i = 0; i < a.size(); ++i) {
      h[b] = (h[b] + ((a[i] >> b) & 1) * D[i]) % M7;
    }
  }
  return h;
}

int32_t main() {
  make(int, n);
  makev(a, n);
  makev(b, n);
  initial();
  auto ha = calc_hash(a);
  auto hb = calc_hash(b);
  int z = accumulate(D, D + n, 0ll) % M7;
  for (int i = 0; i < n; ++i) {
    int X = a[i] ^ b[0];
    VI th = ha;
    for (int b = 0; b < 30; ++b) {
      if ((X >> b) & 1) {
        th[b] = (z - th[b] + M7) % M7;
      }
    }
    if (th == hb) {
      cout << i << ' ' << X << '\n';
    }
    for (int b = 0; b < 30; ++b) {
      if ((a[i] >> b) & 1) {
        ha[b] = (ha[b] + D[i] * (D[n] + M7 - 1)) % M7;
      }
      hb[b] = hb[b] * P % M7;
    }
    z = z * P % M7;
  }

  return 0;
}