Problem p02816 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define pb(a) push_back
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
// #define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
const int MOD = 998244353;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const ll INF = 2e18;

bool cnt[2][200020][33];
template <typename T, T MOD1, T MOD2, T B1, T B2> struct RollingHash {
  vector<T> hash[2], p[2];
  T MOD[2] = {MOD1, MOD2};
  T B[2] = {B1, B2};
  // RollingHash(const string &s) {
  //	int N = s.size();
  //	for (int k = 0; k < 2; k++) {
  //		hash[k].assign(N + 1, 0);
  //		p[k].assign(N + 1, 1);
  //		for (int i = 0; i < N; i++) {
  //			hash[k][i + 1] = (hash[k][i] * B[k] + s[i]) % MOD[k];
  //			p[k][i + 1] = p[k][i] * B[k] % MOD[k];
  //		}
  //	}
  // }
  void init(const string &s) {
    int N = s.size();
    for (int k = 0; k < 2; k++) {
      hash[k].assign(N + 1, 0);
      p[k].assign(N + 1, 1);
      for (int i = 0; i < N; i++) {
        hash[k][i + 1] = (hash[k][i] * B[k] + s[i]) % MOD[k];
        p[k][i + 1] = p[k][i] * B[k] % MOD[k];
      }
    }
  }

  // S[l,r)
  pair<T, T> find(int l, int r) {
    T res1 = hash[0][r] + MOD[0] - hash[0][l] * p[0][r - l] % MOD[0];
    res1 = (res1 >= MOD[0]) ? res1 - MOD[0] : res1;
    T res2 = hash[1][r] + MOD[1] - hash[1][l] * p[1][r - l] % MOD[1];
    res2 = (res2 >= MOD[1]) ? res2 - MOD[1] : res2;

    return make_pair(res1, res2);
  }
  T find1(int l, int r) {
    T res1 = hash[0][r] + MOD[0] - hash[0][l] * p[0][r - l] % MOD[0];
    return (res1 >= MOD[0]) ? res1 - MOD[0] : res1;
  }
  T find2(int l, int r) {
    T res2 = hash[1][r] + MOD[1] - hash[1][l] * p[1][r - l] % MOD[1];
    return (res2 >= MOD[1]) ? res2 - MOD[1] : res2;
  }
};
using RH = RollingHash<ll, (ll)1e9 + 7, (ll)1e9 + 9, 1234567, 7654343>;
// RH rh(S);
RH rhS[30], rhT[30], rhTrev[30];
int A[200020], B[200020];
string S[30], T[30], Trev[30];
void solve() {
  int N;
  cin >> N;
  REP(i, N) cin >> A[i];
  REP(i, N) cin >> B[i];
  REP(i, N) {
    REP(j, 30) {
      if (A[i] & (1 << j))
        cnt[0][i][j] = 1;
      if (B[i] & (1 << j))
        cnt[1][i][j] = 1;
    }
  }
  bool found = false;

  REP(j, 30) {
    REP(i, N) {
      if (cnt[0][i][j])
        S[j] += '1';
      else
        S[j] += '0';
      if (cnt[1][i][j]) {
        T[j] += '1';
        Trev[j] += '0';
      } else {
        T[j] += '0';
        Trev[j] += '1';
      }
    }
  }
  // using RH = RollingHash<ll, (ll)1e9 + 7, (ll)1e9 + 9, 1234567, 7654343>;

  REP(j, 30) {
    rhS[j].init(S[j]);
    rhT[j].init(T[j]);
    rhTrev[j].init(Trev[j]);
  }
  ll ans = 0;
  vector<P> v;
  int k = 0;
  int c = N - k;
  REP(j, 30) {
    if ((rhS[j].find1(0, N - k) == rhT[j].find1(k, N) &&
         rhS[j].find1(N - k, N) == rhT[j].find1(0, k)) ||
        (rhS[j].find1(0, N - k) == rhTrev[j].find1(k, N) &&
         rhS[j].find1(N - k, N) == rhTrev[j].find1(0, k))) {
    } else
      goto END2;
  }
  pe(0);
  print(B[0] ^ A[0]);
END2:
  FORR(k, N, 1) {
    c = N - k;
    REP(j, 30) {
      if ((rhS[j].find1(0, N - k) == rhT[j].find1(k, N) &&
           rhS[j].find1(N - k, N) == rhT[j].find1(0, k)) ||
          (rhS[j].find1(0, N - k) == rhTrev[j].find1(k, N) &&
           rhS[j].find1(N - k, N) == rhTrev[j].find1(0, k))) {
      } else
        goto END;
    }
    pe(c);
    print(B[0] ^ A[c]);
    // v.push_back({ c,B[0] ^ A[c] });
  END:;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int q;cin>>q;
  // while(q--)
  solve();
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define pb(a) push_back
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
// #define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
const int MOD = 998244353;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const ll INF = 2e18;

bool cnt[2][200020][33];
template <typename T, T MOD1, T MOD2, T B1, T B2> struct RollingHash {
  vector<T> hash[2], p[2];
  T MOD[2] = {MOD1, MOD2};
  T B[2] = {B1, B2};
  // RollingHash(const string &s) {
  //	int N = s.size();
  //	for (int k = 0; k < 2; k++) {
  //		hash[k].assign(N + 1, 0);
  //		p[k].assign(N + 1, 1);
  //		for (int i = 0; i < N; i++) {
  //			hash[k][i + 1] = (hash[k][i] * B[k] + s[i]) % MOD[k];
  //			p[k][i + 1] = p[k][i] * B[k] % MOD[k];
  //		}
  //	}
  // }
  void init(const string &s) {
    int N = s.size();
    for (int k = 0; k < 1; k++) {
      hash[k].assign(N + 1, 0);
      p[k].assign(N + 1, 1);
      for (int i = 0; i < N; i++) {
        hash[k][i + 1] = (hash[k][i] * B[k] + s[i]) % MOD[k];
        p[k][i + 1] = p[k][i] * B[k] % MOD[k];
      }
    }
  }

  // S[l,r)
  pair<T, T> find(int l, int r) {
    T res1 = hash[0][r] + MOD[0] - hash[0][l] * p[0][r - l] % MOD[0];
    res1 = (res1 >= MOD[0]) ? res1 - MOD[0] : res1;
    T res2 = hash[1][r] + MOD[1] - hash[1][l] * p[1][r - l] % MOD[1];
    res2 = (res2 >= MOD[1]) ? res2 - MOD[1] : res2;

    return make_pair(res1, res2);
  }
  T find1(int l, int r) {
    T res1 = hash[0][r] + MOD[0] - hash[0][l] * p[0][r - l] % MOD[0];
    return (res1 >= MOD[0]) ? res1 - MOD[0] : res1;
  }
  T find2(int l, int r) {
    T res2 = hash[1][r] + MOD[1] - hash[1][l] * p[1][r - l] % MOD[1];
    return (res2 >= MOD[1]) ? res2 - MOD[1] : res2;
  }
};
using RH = RollingHash<ll, (ll)1e9 + 7, (ll)1e9 + 9, 1234567, 7654343>;
// RH rh(S);
RH rhS[30], rhT[30], rhTrev[30];
int A[200020], B[200020];
string S[30], T[30], Trev[30];
void solve() {
  int N;
  cin >> N;
  REP(i, N) cin >> A[i];
  REP(i, N) cin >> B[i];
  REP(i, N) {
    REP(j, 30) {
      if (A[i] & (1 << j))
        cnt[0][i][j] = 1;
      if (B[i] & (1 << j))
        cnt[1][i][j] = 1;
    }
  }
  bool found = false;

  REP(j, 30) {
    REP(i, N) {
      if (cnt[0][i][j])
        S[j] += '1';
      else
        S[j] += '0';
      if (cnt[1][i][j]) {
        T[j] += '1';
        Trev[j] += '0';
      } else {
        T[j] += '0';
        Trev[j] += '1';
      }
    }
  }
  // using RH = RollingHash<ll, (ll)1e9 + 7, (ll)1e9 + 9, 1234567, 7654343>;

  REP(j, 30) {
    rhS[j].init(S[j]);
    rhT[j].init(T[j]);
    rhTrev[j].init(Trev[j]);
  }
  ll ans = 0;
  vector<P> v;
  int k = 0;
  int c = N - k;
  REP(j, 30) {
    if ((rhS[j].find1(0, N - k) == rhT[j].find1(k, N) &&
         rhS[j].find1(N - k, N) == rhT[j].find1(0, k)) ||
        (rhS[j].find1(0, N - k) == rhTrev[j].find1(k, N) &&
         rhS[j].find1(N - k, N) == rhTrev[j].find1(0, k))) {
    } else
      goto END2;
  }
  pe(0);
  print(B[0] ^ A[0]);
END2:
  FORR(k, N, 1) {
    c = N - k;
    REP(j, 30) {
      if ((rhS[j].find1(0, N - k) == rhT[j].find1(k, N) &&
           rhS[j].find1(N - k, N) == rhT[j].find1(0, k)) ||
          (rhS[j].find1(0, N - k) == rhTrev[j].find1(k, N) &&
           rhS[j].find1(N - k, N) == rhTrev[j].find1(0, k))) {
      } else
        goto END;
    }
    pe(c);
    print(B[0] ^ A[c]);
    // v.push_back({ c,B[0] ^ A[c] });
  END:;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int q;cin>>q;
  // while(q--)
  solve();
}