Problem:
Score : 600 points

Problem Statement

Given are two sequences a=\{a_0,\ldots,a_{N-1}\} and b=\{b_0,\ldots,b_{N-1}\} of N non-negative integers each.

Snuke will choose an integer k such that 0 \leq k < N and an integer x not less than 0, to make a new sequence of length N, a'=\{a_0',\ldots,a_{N-1}'\}, as follows:

a_i'= a_{i+k \mod N}\ XOR \ x

Find all pairs (k,x) such that a' will be equal to b.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq a_i,b_i < 2^{30}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_0 a_1 ... a_{N-1}
b_0 b_1 ... b_{N-1}

Output

Print all pairs (k, x) such that a' and b will be equal, using one line for each pair, in ascending order of k (ascending order of x for pairs with the same k).

If there are no such pairs, the output should be empty.

Sample Input 1

3
0 2 1
1 2 3

Sample Output 1

1 3

If (k,x)=(1,3),

a_0'=(a_1\ XOR \ 3)=1

a_1'=(a_2\ XOR \ 3)=2

a_2'=(a_0\ XOR \ 3)=3

and we have a' = b.

Sample Input 2

5
0 0 0 0 0
2 2 2 2 2

Sample Output 2

0 2
1 2
2 2
3 2
4 2

Sample Input 3

6
0 1 3 7 6 4
1 5 4 6 2 3

Sample Output 3

2 2
5 5

Sample Input 4

2
1 2
0 0

Sample Output 4

No pairs may satisfy the condition.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); };
auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); };
auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; };
LL gcd(LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)

// ---- ----

using str = vector<int>;

vector<int> rotation_array(str const & s) {
	int n = SI(s);
	vector<int> r(n), a(n);
	inc(i, n) {
		r[i] = s[i];
		a[i] = i;
	}
	for(int k = 1; k < n; k <<= 1) {
		auto comp = [&](int x, int y) -> bool {
			int x1 = r[x], x2 = r[x + k - (x + k < n ? 0 : n)];
			int y1 = r[y], y2 = r[y + k - (y + k < n ? 0 : n)];
			return (MP(x1, x2) < MP(y1, y2));
		};
		stable_sort(ALL(a), comp);
		vector<int> rr(n);
		inc(i, n - 1) { rr[a[i + 1]] = rr[a[i]] + (comp(a[i], a[i + 1]) ? 1 : 0); }
		r = rr;
	}
	return a;
}

vector<int> rotation_lcp_array(str const & s, vector<int> const & RA) {
	int n = SI(s);
	vector<int> r(n), l(n); // l.back() == 0
	inc(i, n) { r[RA[i]] = i; }
	int c = 0;
	inc(i, n) {
		if(c > 0) { c--; }
		if(r[i] + 1 == n) { continue; }
		int j = RA[r[i] + 1];
		while(c < n && s[(i + c) % n] == s[(j + c) % n]) { c++; }
		l[r[i]] = c;
		if(j + 1 == n) { c = 0; }
	}
	return l;
}

int main() {
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	inc(i, n) { cin >> a[i]; }
	inc(i, n) { cin >> b[i]; }
	
	auto RA   = rotation_array(a);
	auto RLCP = rotation_lcp_array(a, RA);
	
	vector<pair<int, int>> ans;
	int s = 0;
	dec(i, n) {
		setmin(s, RLCP[i]);
		int x = a[RA[i]] ^ b[0];
		incID(j, s, n) {
			if((a[mo(j + RA[i], n)] ^ b[j]) == x) { setmax(s, j + 1); } else { break; }
		}
		if(s == n) { ans.EB(RA[i], x); }
	}
	sort(ALL(ans));
	
	RF(e, ans) { cout << e.FI << " " << e.SE << "\n"; }
	
	return 0;
}