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

local mmi, mma = math.min, math.max
local mfl = math.floor
local xor = bit.bxor
-- local function xor(x, y) return x end

local n = io.read("*n")
local ar, br = {}, {}
for i = 1, n do
  ar[i] = io.read("*n")
end
for i = 1, n do
  br[i] = io.read("*n")
end
local as, bs = {}, {}
for i = 1, n - 1 do
  as[i], bs[i] = xor(ar[i], ar[i + 1]), xor(br[i], br[i + 1])
end
as[n], bs[n] = xor(ar[n], ar[1]), xor(br[n], br[1])
local idx = {}
local v = {}
for i = 1, n do
  idx[i] = i
  v[i] = as[i]
end
-- stage 1
table.sort(idx, function(a, b) return v[a] < v[b] end)
do
  local cur = 1
  local v_nxt = {}
  for i = 1, n do v_nxt[i] = 0 end
  v_nxt[idx[1]] = 1
  for i = 2, n do
    if v[idx[i]] ~= v[idx[i - 1]] then
      cur = cur + 1
    end
    v_nxt[idx[i]] = cur
  end
  for i = 1, n do v[i] = v_nxt[i] end
end
local step = 1
while step < n do
  local vd = {}
  local left, major = 1, v[idx[1]]
  local minor_min = v[idx[1 + step]]
  local minor_max = minor_min
  for i = 1, n do
    local isend = i == n or major ~= v[idx[i + 1]]
    if isend then
      if minor_min ~= minor_max then
        local right = i
        local t = {}
        for j = left, right do
          t[j - left + 1] = idx[j]
        end
        table.sort(t, function(x, y)
          x, y = x + step, y + step
          if n < x then x = x - n end
          if n < y then y = y - n end
          return v[x] < v[y] end)
        for j = left, right do
          idx[j] = t[j - left + 1]
        end
      end
      if i < n then
        left, major = i + 1, v[idx[i + 1]]
        local tmp = idx[i + 1] + step
        if n < tmp then tmp = tmp - n end
        minor_min = v[tmp]
        minor_max = minor_min
      end
    else
      local j = idx[i] + step
      if n < j then j = j - n end
      local minor = v[j]
      minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    end
  end
  vd[idx[1]] = 1
  local cur = 1
  for i = 2, n do
    local idx_prv, idx_nxt = idx[i - 1], idx[i]
    local idx_prv_i, idx_nxt_i = idx_prv + step, idx_nxt + step
    if n < idx_prv_i then idx_prv_i = idx_prv_i - n end
    if n < idx_nxt_i then idx_nxt_i = idx_nxt_i - n end
    if v[idx_prv] ~= v[idx_nxt] or v[idx_prv_i] ~= v[idx_nxt_i] then
      cur = cur + 1
    end
    vd[idx_nxt] = cur
  end
  for i = 1, n do v[i] = vd[i] end
  step = step * 2
end

local function comp_l(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if as[ai] < bs[i] then return true
    elseif bs[i] < as[ai] then return false
    end
  end
  return false
end

local function comp_u(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if bs[i] < as[ai] then return true
    elseif as[ai] < bs[i] then return false
    end
  end
  return false
end

local function lower_bound(ary)
  local num = #ary
  if num == 0 then return 1 end
  if not comp_l(ary[1]) then return 1 end
  if comp_l(ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp_l(ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local function upper_bound(ary)
  local num = #ary
  if num == 0 then return 1 end
  if comp_u(ary[1]) then return 1 end
  if not comp_u(ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if not comp_u(ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local lb = lower_bound(idx)
local ub = upper_bound(idx)
local t = {}
for i = lb, ub - 1 do
  table.insert(t, idx[i])
end
table.sort(t)
for i = 1, #t do
  local k = t[i] - 1
  local v = xor(ar[t[i]], br[1])
  print(k .. " " .. v)
end
-- print(os.clock())