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: Accepted)

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
local vd = {}
while step < n do
  local stepdst = {}
  for i = 1, n do
    local dst = i + step
    if n < dst then dst = dst - n end
    stepdst[i] = dst
  end
  local left, major = 1, v[idx[1]]
  local minor_min = v[stepdst[idx[1]]]
  local minor_max = minor_min
  local cur = 0
  for i = 1, n do
    local minor = v[stepdst[idx[i]]]
    minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    local isend = i == n or major ~= v[idx[i + 1]]
    if isend then
      local right = i
      if minor_min ~= minor_max then
        local t = {}
        for j = left, right do
          t[j - left + 1] = idx[j]
        end
        -- R-L=A, Max-Min=B: O(A+B) or O(AlogA)
        if minor_max - minor_min < 100000 then
          local box = {}
          for b = 1, minor_max - minor_min + 1 do box[b] = {} end
          for ti = 1, right - left + 1 do
            local tminor = v[stepdst[t[ti]]]
            table.insert(box[tminor - minor_min + 1], t[ti])
          end
          local tcur = 1
          for b = 1, minor_max - minor_min + 1 do
            for bb = 1, #box[b] do
              t[tcur] = box[b][bb]
              tcur = tcur + 1
            end
          end
        else
          table.sort(t, function(x, y) return v[stepdst[x]] < v[stepdst[y]] end)
        end
        local v_prv = 0
        for j = left, right do
          idx[j] = t[j - left + 1]
          local v_nxt = v[stepdst[idx[j]]]
          if j == left or v_nxt ~= v_prv then
            cur = cur + 1
          end
          vd[idx[j]] = cur
          v_prv = v_nxt
        end
      else
        cur = cur + 1
        for j = left, right do
          vd[idx[j]] = cur
        end
      end
      if i < n then
        left, major = i + 1, v[idx[i + 1]]
        minor_min = v[stepdst[idx[i + 1]]]
        minor_max = minor_min
      end
    else
      -- local minor = v[stepdst[idx[i]]]
      -- minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    end
  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())