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 Go (1.6) (judge verdict: Time Limit Exceeded)

package main

import "fmt"

const D int = 30

type BinaryVector [D]bool

func to_binary_vector(a int) BinaryVector {
	v := BinaryVector{}
	for d := 0; d < D; d++ {
		v[d] = a%2 == 1
		a /= 2
		if a == 0 {
			break
		}
	}
	return v
}

func (v *BinaryVector) print() {
	for d := D - 1; d >= 0; d-- {
		x := 0
		if v[d] {
			x = 1
		}
		fmt.Printf("%d ", x)
	}
	fmt.Println()
}

func (v *BinaryVector) to_int() int {
	y := 0
	if v[D-1] {
		y = 1
	}
	for d := D - 2; d >= 0; d-- {
		if v[d] {
			y = y*2 + 1
		} else {
			y = y * 2
		}

	}
	return y
}

func xor(u, v *BinaryVector) BinaryVector {
	x := BinaryVector{}
	for d := 0; d < D; d++ {
		x[d] = u[d] != v[d]
	}
	return x
}

func equal(u, v *BinaryVector) bool {
	for d := 0; d < D; d++ {
		if u[d] != v[d] {
			return false
		}
	}
	return true
}

func kmp_table(W []BinaryVector, N int) []int {
	T := make([]int, N+1)
	i, j := 2, 0
	T[0], T[1] = -1, 0
	for i < N+1 {
		if equal(&W[i-1], &W[j]) {
			T[i] = j + 1
			i++
			j++
		} else if j > 0 {
			j = T[j]
		} else {
			T[i] = 0
			i++
		}
	}
	return T
}

func kmp_search(S, W []BinaryVector, N int) []int {
	var list_k []int
	// len(S') = 2N - 1,  S'[m] = S[m%N]
	m, i := 0, 0
	T := kmp_table(W, N)

	// fmt.Print("T: ")
	// for i := 0; i < N+1; i++ {
	// 	fmt.Printf("%d ", T[i])
	// }
	// fmt.Println()

	for m < N {
		if equal(&W[i], &S[(m+i)%N]) {
			i++
			if i == N {
				list_k = append(list_k, m)
			} else {
				continue
			}
		}
		m = m + i - T[i]
		if i > 0 {
			i = T[i]
		}
	}
	return list_k
}

func main() {
	var N int
	fmt.Scan(&N)
	var buff int
	A := make([]BinaryVector, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&buff)
		A[i] = to_binary_vector(buff)
	}
	B := make([]BinaryVector, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&buff)
		B[i] = to_binary_vector(buff)
	}
	C := make([]BinaryVector, N)
	for i := 0; i < N; i++ {
		C[i] = xor(&A[i], &A[(i+1)%N])
	}
	D := make([]BinaryVector, N)
	for i := 0; i < N; i++ {
		D[i] = xor(&B[i], &B[(i+1)%N])
	}

	// fmt.Print("C: ")
	// for i := 0; i < 2*N-1; i++ {
	// 	fmt.Printf("%d ", C[i%N].to_int())
	// }
	// fmt.Println()
	// fmt.Print("D: ")
	// for i := 0; i < N; i++ {
	// 	fmt.Printf("%d ", D[i].to_int())
	// }
	// fmt.Println()

	k_list := kmp_search(C, D, N)

	for _, k := range k_list {
		x := xor(&A[k], &B[0])
		fmt.Printf("%d %d\n", k, x.to_int())
	}
}