Problem p02816 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using i128 = boost::multiprecision::int128_t;
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define rep(i, n) rep2(i, 0, n)
#define drep(i, n) drep2(i, n, 0)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &e : v)
    os << e << ' ';
  return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
  return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int fLog2(const int x) {
  return 31 - __builtin_clz(x);
} // floor(log2(x))
inline int fLog2(const ll x) { return 63 - __builtin_clzll(x); }
inline int cLog2(const int x) {
  return (x == 1) ? 0 : 32 - __builtin_clz(x - 1);
} // ceil(log2(x))
inline int cLog2(const ll x) {
  return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const ld PI = acos(-1.0);
// const int MOD  = int(1e9)+7;
// const int MOD  = 998244353;

struct RollingHash {
  using ui64 = uint64_t;
  using ui128 = __uint128_t;
  static const ui64 mod = (1ull << 61) - 1;
  vector<ui64> hash, power;
  ui64 base;

  static inline ui64 add(ui64 a, ui64 b) {
    if ((a += b) >= mod)
      a -= mod;
    return a;
  }
  static inline ui64 mul(ui64 a, ui64 b) {
    ui128 c = ui128(a) * b;
    return add(c >> 61, c & mod);
  }
  static inline ui64 generate_base() {
    mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<ui64> rand(2, mod - 2);
    return rand(mt);
  }

  template <typename T> RollingHash(const T &s, ui64 b = 0) : base(b) {
    if (base == 0)
      base = generate_base();
    int n = s.size();
    hash.assign(n + 1, 0);
    power.assign(n + 1, 1);
    rep(i, n) {
      power[i + 1] = mul(power[i], base);
      hash[i + 1] = add(mul(hash[i], base), s[i]);
    }
  }

  ui64 get(int l, int r) const {
    return add(hash[r], mod - mul(hash[l], power[r - l]));
  }

  // longest common prefix of s[l1:r1] and s[l2:r2]
  int lcp(int l1, int r1, int l2, int r2) const {
    int len = min(r1 - l1, r2 - l2);
    int ok = 0, ng = len + 1;
    while (ng - ok > 1) {
      int md = (ok + ng) >> 1;
      if (get(l1, l1 + md) == get(l2, l2 + md))
        ok = md;
      else
        ng = md;
    }
    return ok;
  }
};

int main() {
  ll n;
  cin >> n;
  V a(n), b(n);
  cin >> a >> b;

  V c(n), d(n);
  rep(i, n) c[i] = a[i] ^ a[(i + 1) % n];
  rep(i, n) d[i] = b[i] ^ b[(i + 1) % n];

  copy(all(c), back_inserter(c));

  RollingHash rh1(c);
  RollingHash rh2(d, rh1.base);

  rep(k, n) if (rh1.get(k, k + n) == rh2.get(0, n)) {
    cout << k << ' ' << (a[k] ^ b[0]) << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using i128 = boost::multiprecision::int128_t;
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define rep(i, n) rep2(i, 0, n)
#define drep(i, n) drep2(i, n, 0)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &e : v)
    os << e << ' ';
  return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
  return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int fLog2(const int x) {
  return 31 - __builtin_clz(x);
} // floor(log2(x))
inline int fLog2(const ll x) { return 63 - __builtin_clzll(x); }
inline int cLog2(const int x) {
  return (x == 1) ? 0 : 32 - __builtin_clz(x - 1);
} // ceil(log2(x))
inline int cLog2(const ll x) {
  return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const ld PI = acos(-1.0);
// const int MOD  = int(1e9)+7;
// const int MOD  = 998244353;

struct RollingHash {
  using ui64 = uint64_t;
  using ui128 = __uint128_t;
  static const ui64 mod = (1ull << 61) - 1;
  vector<ui64> hash, power;
  ui64 base;

  static inline ui64 add(ui64 a, ui64 b) {
    if ((a += b) >= mod)
      a -= mod;
    return a;
  }
  static inline ui64 mul(ui64 a, ui64 b) {
    ui128 c = ui128(a) * b;
    return add(c >> 61, c & mod);
  }
  static inline ui64 generate_base() {
    mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<ui64> rand(2, mod - 2);
    return rand(mt);
  }

  template <typename T> RollingHash(const T &s, ui64 b = 0) : base(b) {
    if (base == 0)
      base = generate_base();
    int n = s.size();
    hash.assign(n + 1, 0);
    power.assign(n + 1, 1);
    rep(i, n) {
      power[i + 1] = mul(power[i], base);
      hash[i + 1] = add(mul(hash[i], base), s[i]);
    }
  }

  ui64 get(int l, int r) const {
    return add(hash[r], mod - mul(hash[l], power[r - l]));
  }

  // longest common prefix of s[l1:r1] and s[l2:r2]
  int lcp(int l1, int r1, int l2, int r2) const {
    int len = min(r1 - l1, r2 - l2);
    int ok = 0, ng = len + 1;
    while (ng - ok > 1) {
      int md = (ok + ng) >> 1;
      if (get(l1, l1 + md) == get(l2, l2 + md))
        ok = md;
      else
        ng = md;
    }
    return ok;
  }
};

int main() {
  ll n;
  cin >> n;
  V a(n), b(n);
  cin >> a >> b;

  V c(n), d(n);
  rep(i, n) c[i] = a[i] ^ a[(i + 1) % n];
  rep(i, n) d[i] = b[i] ^ b[(i + 1) % n];
  c.insert(c.end(), all(c));

  RollingHash rh1(c);
  RollingHash rh2(d, rh1.base);

  rep(k, n) if (rh1.get(k, k + n) == rh2.get(0, n)) {
    cout << k << ' ' << (a[k] ^ b[0]) << '\n';
  }

  return 0;
}