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++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
int n,a[200005],b[200005],aa[200005],bb[200005],rnk[200005],x[200005],cnt[200005];
vector<pair<pair<int,int>,int> > t1[200005],t2[200005];
void sort(vector<pair<pair<int,int>,int> > &v)
{
	for (int i=0;i<n;i++)
	{
		t1[i].clear();
		t2[i].clear();
	}
	for (auto p:v)
	t1[p.first.second].push_back(p);
	for (int i=0;i<n;i++)
	{
		for (auto p:t1[i])
		t2[p.first.first].push_back(p);
	}
	v.clear();
	for (int i=0;i<n;i++)
	{
		for (auto p:t2[i])
		v.push_back(p);
	}
}
void go(int b,int val)
{
	for (int i=0;i<n;i++)
	rnk[i]=aa[i];
	vector<pair<pair<int,int>,int> > v;
	for (int k=0;k<20;k++)
	{
		v.clear();
		for (int i=0;i<n;i++)
		v.push_back({{rnk[i],rnk[(i+(1<<k))%n]},i});
		sort(v);
		int cr=0;
		for (int i=0;i<n;i++)
		{
			if (!i)
			rnk[v[i].second]=0;
			else if (v[i].first!=v[i-1].first)
			cr++;
			rnk[v[i].second]=cr;
		}
	}
	int st=0,en=n;
	while (st!=en)
	{
		int mid=(st+en)/2;
		bool eq=1;
		for (int i=0;i<n;i++)
		{
			if (aa[(v[mid].second+i)%n]!=bb[i])
			{
				if (aa[(v[mid].second+i)%n]<bb[i])
				st=mid+1;
				else
				en=mid;
				eq=0;
				break;
			}
		}
		if (eq)
		en=mid;
	}
	for (int i=0;i<n;i++)
	{
		if (aa[(v[st].second+i)%n]!=bb[i])
		return;
	}
	for (int i=0;i<n;i++)
	{
		if (rnk[i]==rnk[v[st].second])
		{
			cnt[i]++;
			x[i]|=(val<<b);
		}
	}
}
int main()
{
	scanf("%d",&n);
	for (int i=0;i<n;i++)
	scanf("%d",&a[i]);
	for (int i=0;i<n;i++)
	scanf("%d",&b[i]);
	for (int i=0;i<30;i++)
	{
		for (int j=0;j<n;j++)
		{
			aa[j]=((a[j]>>i)&1);
			bb[j]=((b[j]>>i)&1);
		}
		for (int j=0;j<2;j++)
		{
			go(i,j);
			for (int k=0;k<n;k++)
			aa[k]^=1;
		}
	}
	for (int i=0;i<n;i++)
	{
		if (cnt[i]==30)
		printf("%d %d\n",i,x[i]);
	}
}