Problem p02816 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
using graph = vector<vector<ll>>;

const int inf = 1e9 + 7;
const long long MOD = 1000000007;

// ローリングハッシュ
struct RollingHash {
  static const int base1 = 1007, base2 = 2009;
  static const int mod1 = 1000000007, mod2 = 1000000009;
  vector<long long> hash1, hash2, power1, power2;

  // construct
  RollingHash(const vector<ll> &S) {
    int n = (int)S.size();
    hash1.assign(n + 1, 0);
    hash2.assign(n + 1, 0);
    power1.assign(n + 1, 1);
    power2.assign(n + 1, 1);
    for (int i = 0; i < n; ++i) {
      hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
      hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
      power1[i + 1] = (power1[i] * base1) % mod1;
      power2[i + 1] = (power2[i] * base2) % mod2;
    }
  }

  // get hash of S[left:right]
  inline pair<long long, long long> get(int l, int r) const {
    long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
    if (res1 < 0)
      res1 += mod1;
    long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
    if (res2 < 0)
      res2 += mod2;
    return {res1, res2};
  }
};

int main() {
  int n;
  vector<int> k;
  cin >> n;
  vector<ll> a(n), b(n), A(n * 2), B(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n - 1) {
    A[i] = a[i] ^ a[i + 1];
    B[i] = b[i] ^ b[i + 1];
    A[i + n] = a[i] ^ a[i + 1];
  }
  A[n - 1] = a[n - 1] ^ a[0];
  B[n - 1] = b[n - 1] ^ b[0];
  A[2 * n - 1] = a[n - 1] ^ a[0];
  RollingHash rha(A), rhb(B);
  p pb = rhb.get(0, n);
  rep(i, n) {
    if (pb == rha.get(i, i + n)) {
      k.push_back(i);
    }
  }
  rep(i, k.size()) {
    int x = a[k[i]] ^ b[0];
    bool flg = true;
    repd(j, 1, n) {
      if ((a[(k[i] + j) % n] ^ b[j]) != x)
        flg = false;
    }
    if (flg)
      cout << k[i] << " " << x << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
using graph = vector<vector<ll>>;

const int inf = 1e9 + 7;
const long long MOD = 1000000007;

// ローリングハッシュ
struct RollingHash {
  static const int base1 = 1007, base2 = 2009;
  static const int mod1 = 1000000007, mod2 = 1000000009;
  vector<long long> hash1, hash2, power1, power2;

  // construct
  RollingHash(const vector<ll> &S) {
    int n = (int)S.size();
    hash1.assign(n + 1, 0);
    hash2.assign(n + 1, 0);
    power1.assign(n + 1, 1);
    power2.assign(n + 1, 1);
    for (int i = 0; i < n; ++i) {
      hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
      hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
      power1[i + 1] = (power1[i] * base1) % mod1;
      power2[i + 1] = (power2[i] * base2) % mod2;
    }
  }

  // get hash of S[left:right]
  inline pair<long long, long long> get(int l, int r) const {
    long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
    if (res1 < 0)
      res1 += mod1;
    long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
    if (res2 < 0)
      res2 += mod2;
    return {res1, res2};
  }
};

int main() {
  int n;
  vector<int> k;
  cin >> n;
  vector<ll> a(n), b(n), A(n * 2), B(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n - 1) {
    A[i] = a[i] ^ a[i + 1];
    B[i] = b[i] ^ b[i + 1];
    A[i + n] = a[i] ^ a[i + 1];
  }
  A[n - 1] = a[n - 1] ^ a[0];
  B[n - 1] = b[n - 1] ^ b[0];
  A[2 * n - 1] = a[n - 1] ^ a[0];
  RollingHash rha(A), rhb(B);
  p pb = rhb.get(0, n);
  rep(i, n) {
    if (pb == rha.get(i, i + n)) {
      k.push_back(i);
    }
  }
  rep(i, k.size()) {
    int x = a[k[i]] ^ b[0];
    bool flg = true;

    if (flg)
      cout << k[i] << " " << x << endl;
  }
  return 0;
}