Problem p02816 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

// typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;

#define siz(x) (int)(x).size()
#define Y second
#define X first
#define IN insert
#define PB push_back
#define P pop_back
#define PU push
#define PO pop
// #define F find
#define ED end()
#define MP(a, b) make_pair(a, b)

// #define MOD 1000000007ll
#define INF 1000000000
#define endl "\n"
#define rep(a, i, b) for (ll i = (a); i < (b); ++i)
#define reps(a, i, b) for (ll i = (a); i <= (b); ++i)
#define repd(num, i, ed) for (ll(i) = (num)-1; (i) >= ed; --(i))
#define repkt(bg, i, num, kt) for (ll(i) = (bg); (i) < (num) && (kt); ++(i))
#define repchar(i) for (char(i) = 48; (i) <= 122; (i)++)
#define in(n) scanf("%lld", &n)
#define out(n) printf("%lld\n", n)
#define outS(s) printf("%s\n", s)
#define setA(a, n)                                                             \
  for (ll i = 0, j = sizeof(a) / sizeof(a[0]); i < j; ++i)                     \
    a[i] = n;
#define repe(num, i, ed) for (ll(i) = (num); (i) >= ed; --(i))
// const int INF=0x3f3f3f3f;
const db PI = acos(-1);
const ll mod = 1e9 + 7;
const ll N = 2e6 + 5, K = 105;
const int si = (1 << 20);
const ll add = 1e4;
const ll M = 100 + 5;
const double INFdb = 1e10;

int n;
vector<pair<int, int>> ans;

struct rollingHash {
  int hash_mod = 1000000007;
  int hash = 10007;
  vi ha;
  vi pow_hash;
  rollingHash(const vi &s) {
    int nn = (int)s.size();
    ha.assign(nn + 1, 0);
    pow_hash.assign(nn + 1, 1);
    for (int i = 0; i < nn; ++i) {
      ha[i + 1] = ((ll)ha[i] * hash + s[i]) % hash_mod;
      pow_hash[i + 1] = ((ll)pow_hash[i] * hash) % hash_mod;
    }
  }
  int get(int l, int r) {
    return (ha[r] + hash_mod - ((ll)ha[l] * pow_hash[r - l]) % hash_mod) %
           hash_mod;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  cin >> n;
  vi a(n), b(n);
  vi xor_a(n * 2), xor_b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  for (int i = 0; i < n; ++i) {
    xor_a[i] = a[i] ^ a[(i + 1) % n];
  }
  for (int i = 0; i < n; ++i) {
    xor_b[i] = b[i] ^ b[(i + 1) % n];
  }
  for (int i = 0; i < n; ++i)
    xor_a[i + n] = xor_a[i];
  // for(int i=0;i<n;++i) cout<<xor_a[i]<<" ";
  rollingHash hash_a(xor_a), hash_b(xor_b);
  for (int i = 0; i < n; ++i) {
    // cout<<hash_a.get(i,i+n)<<endl;
    if (hash_a.get(i, i + n) == hash_b.get(0, n)) {
      int x = a[i] ^ b[0];
      ans.emplace_back(i, x);
    }
  }
  for (pair<int, int> x : ans) {
    cout << x.X << " " << x.Y << endl;
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

// typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;

#define siz(x) (int)(x).size()
#define Y second
#define X first
#define IN insert
#define PB push_back
#define P pop_back
#define PU push
#define PO pop
// #define F find
#define ED end()
#define MP(a, b) make_pair(a, b)

// #define MOD 1000000007ll
#define INF 1000000000
#define endl "\n"
#define rep(a, i, b) for (ll i = (a); i < (b); ++i)
#define reps(a, i, b) for (ll i = (a); i <= (b); ++i)
#define repd(num, i, ed) for (ll(i) = (num)-1; (i) >= ed; --(i))
#define repkt(bg, i, num, kt) for (ll(i) = (bg); (i) < (num) && (kt); ++(i))
#define repchar(i) for (char(i) = 48; (i) <= 122; (i)++)
#define in(n) scanf("%lld", &n)
#define out(n) printf("%lld\n", n)
#define outS(s) printf("%s\n", s)
#define setA(a, n)                                                             \
  for (ll i = 0, j = sizeof(a) / sizeof(a[0]); i < j; ++i)                     \
    a[i] = n;
#define repe(num, i, ed) for (ll(i) = (num); (i) >= ed; --(i))
// const int INF=0x3f3f3f3f;
const db PI = acos(-1);
const ll mod = 1e9 + 7;
const ll N = 2e6 + 5, K = 105;
const int si = (1 << 20);
const ll add = 1e4;
const ll M = 100 + 5;
const double INFdb = 1e10;

int n;
vector<pair<int, int>> ans;

struct rollingHash {
  int hash_mod = 1000000007;
  int hash = 10007;
  vi ha;
  vi pow_hash;
  rollingHash(const vi &s) {
    int nn = (int)s.size();
    ha.assign(nn + 1, 0);
    pow_hash.assign(nn + 1, 1);
    for (int i = 0; i < nn; ++i) {
      ha[i + 1] = ((ll)ha[i] * hash + s[i]) % hash_mod;
      pow_hash[i + 1] = ((ll)pow_hash[i] * hash) % hash_mod;
    }
  }
  int get(int l, int r) {
    return (ha[r] + hash_mod - ((ll)ha[l] * pow_hash[r - l]) % hash_mod) %
           hash_mod;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // #endif
  cin >> n;
  vi a(n), b(n);
  vi xor_a(n * 2), xor_b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  for (int i = 0; i < n; ++i) {
    xor_a[i] = a[i] ^ a[(i + 1) % n];
  }
  for (int i = 0; i < n; ++i) {
    xor_b[i] = b[i] ^ b[(i + 1) % n];
  }
  for (int i = 0; i < n; ++i)
    xor_a[i + n] = xor_a[i];
  // for(int i=0;i<n;++i) cout<<xor_a[i]<<" ";
  rollingHash hash_a(xor_a), hash_b(xor_b);
  for (int i = 0; i < n; ++i) {
    // cout<<hash_a.get(i,i+n)<<endl;
    if (hash_a.get(i, i + n) == hash_b.get(0, n)) {
      int x = a[i] ^ b[0];
      ans.emplace_back(i, x);
    }
  }
  for (pair<int, int> x : ans) {
    cout << x.X << " " << x.Y << endl;
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}