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.



Sample input:
3
0 2 1
1 2 3


Sample output:
1 3

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def z_algorithm(S):
    """Zアルゴリズム
    https://snuke.hatenablog.com/entry/2014/12/03/214243 の二番目のコード
    """
    N = len(S)
    Z = [0]*N

    c = 0
    for i in range(1, N):
        if i+Z[i-c] < c+Z[c]:
            Z[i] = Z[i-c]
        else:
            j = max(0, c+Z[c]-i)
            while i+j < N and S[j] == S[i+j]:
                j += 1
            Z[i] = j
            c = i
    Z[0] = N
    return Z


def zfindall(text, pattern, separator="$"):
    N = len(pattern)
    conc = pattern+separator+text
    Z = z_algorithm(conc)
    return {i-N-1 for i in range(len(conc)) if Z[i] == N}


def solve(N: int, a: "List[int]", b: "List[int]"):

    a1 = [0]*(2*N)
    b1 = [0]*N
    b2 = [0]*N
    k_cand = [True]*N
    for keta in range(30):
        for i, aa in enumerate(a):
            a1[i] = a1[i+N] = (aa >> keta) & 1
        for i, bb in enumerate(b):
            b1[i] = v = (bb >> keta) & 1
            b2[i] = 1-v
        k_cand1 = zfindall(a1, b1, separator=[-1])
        k_cand2 = zfindall(a1, b2, separator=[-1])
        k_cand1 = k_cand1 | k_cand2
        for i in range(N):
            if k_cand[i]:
                k_cand[i] = i in k_cand1

    for i in range(N):
        if k_cand[i]:
            print(i, b[0] ^ a[i])
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


B = 10**7+7
MOD = 10**9+7


def z_algorithm(S):
    """Zアルゴリズム
    https://snuke.hatenablog.com/entry/2014/12/03/214243 の二番目のコード
    """
    N = len(S)
    Z = [0]*N

    c = 0
    for i in range(1, N):
        if i+Z[i-c] < c+Z[c]:
            Z[i] = Z[i-c]
        else:
            j = max(0, c+Z[c]-i)
            while i+j < N and S[j] == S[i+j]:
                j += 1
            Z[i] = j
            c = i
    Z[0] = len(S)
    return Z


def zfindall(text, pattern, separator="$"):
    """テキストからパターンと一致する部分文字列を探す。
    開始インデックスを返す
    Zアルゴリズムを利用する
    """
    N = len(pattern)
    conc = pattern+separator+text
    Z = z_algorithm(conc)
    return {i-N-1 for i in range(len(conc)) if Z[i] == N}


def solve(N: int, a: "List[int]", b: "List[int]"):

    c = [a[i] ^ a[(i+1) % N]for i in range(N)]
    d = [b[i] ^ b[(i+1) % N]for i in range(N)]*2
    i = -1
    a1 = [0]*N
    a2 = [0]*N
    b1 = [0]*(2*N)
    k_cand = [True]*N
    for keta in range(30):
        mask = 1 << keta
        for i, aa in enumerate(a):
            a1[i] = (aa & mask) >> keta
            a2[i] = (~aa & mask) >> keta
        for i, bb in enumerate(b):
            b1[i] = (bb & mask) >> keta
            b1[i+N] = (bb & mask) >> keta
        k_cand1 = zfindall(b1, a1, separator=[-1])
        k_cand2 = zfindall(b1, a2, separator=[-1])
        for i in range(N):
            k_cand[i] = k_cand[i] & ((i in k_cand1) or (i in k_cand2))

    ans = []
    if len(k_cand) == 0:
        return
    if k_cand[0] == True:
        print(0, b[0] ^ a[0])
        for i, k in enumerate(k_cand[:0:-1], start=1):
            if k:
                print(i, b[0] ^ a[i])
    else:
        for i, k in enumerate(k_cand[::-1], start=1):
            if k:
                print(i, b[0] ^ a[i])
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


B = 10**7+7
MOD = 10**9+7


def contain(T, S):
    # TはSに含まれているか
    if len(T) > len(S):
        return len(S)
    t = 1
    for i in range(len(T)):
        t *= B
        t %= MOD
    th, sh = 0, 0
    for i in range(len(T)):
        th = th*B+T[i]
        th %= MOD
    for i in range(len(T)):
        sh = sh*B+S[i]
        sh %= MOD

    for i in range(len(S)-len(T)):
        if th == sh:
            if T == S[i:i+len(T)]:
                return i
        if i + len(T) < len(S):
            sh = sh*B+S[i+len(T)]-S[i]*t
            sh %= MOD
    return len(S)


def solve(N: int, a: "List[int]", b: "List[int]"):

    c = [a[i] ^ a[(i+1) % N]for i in range(N)]
    d = [b[i] ^ b[(i+1) % N]for i in range(N)]*2
    i = -1
    k_cand = []
    # print(c, d)
    while i < N:
        i = i+1+contain(c, d[i+1:])
        if i < N:
            k_cand.append(i)
    # print(k_cand)

    ans = []
    if len(k_cand) == 0:
        return
    if k_cand[0] % N == 0:
        print(0, b[0] ^ a[0])
        for k in k_cand[:0:-1]:
            print(-k % N, b[0] ^ a[-k % N])
    else:
        for k in k_cand[::-1]:
            print(-k % N, b[0] ^ a[-k % N])
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


B = 10**7+7
MOD = 10**9+7


def contain(T, S):
    # TはSに含まれているか
    if len(T) > len(S):
        return len(S)
    t = 1
    for i in range(len(T)):
        t *= B
        t %= MOD
    th, sh = 0, 0
    for i in range(len(T)):
        th = th*B+T[i]
        th %= MOD
    for i in range(len(T)):
        sh = sh*B+S[i]
        sh %= MOD

    for i in range(len(S)-len(T)):
        if th == sh:
            if T == S[i:i+len(T)]:
                return i
        if i + len(T) < len(S):
            sh = sh*B+S[i+len(T)]-S[i]*t
            sh %= MOD
    return len(S)


def solve(N: int, a: "List[int]", b: "List[int]"):

    c = [a[i] ^ a[(i+1) % N]for i in range(N)]
    d = [b[i] ^ b[(i+1) % N]for i in range(N)]*2
    i = -1
    k_cand = [False]*N
    # print(c, d)
    while i < N:
        i = i+1+contain(c, d[i+1:])
        if i < N:
            k_cand[i] = True
    # print(k_cand)

    ans = []
    for i, k in enumerate(k_cand):
        if k == True:
            ans.append(((N-i) % N, b[0] ^ a[(N-i) % N]))
    ans.sort()
    for a, b in ans:
        print(a, b, sep=" ")
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


B = 10**7+7
MOD = 10**9+7


def contain(T, S):
    # TはSに含まれているか
    if len(T) > len(S):
        return len(S)
    t = 1
    for i in range(len(T)):
        t *= B
        t %= MOD
    th, sh = 0, 0
    for i in range(len(T)):
        th = th*B+T[i]
        th %= MOD
    for i in range(len(T)):
        sh = sh*B+S[i]
        sh %= MOD

    for i in range(len(S)-len(T)):
        if th == sh:
            if T == S[i:i+len(T)]:
                return i
        if i + len(T) < len(S):
            sh = sh*B+S[i+len(T)]-S[i]*t
            sh %= MOD
    return len(S)


def solve(N: int, a: "List[int]", b: "List[int]"):

    K = [True]*N
    for i in range(3):
        a_pat = []
        a_pat2 = []
        b_pat = []
        for n, (aa, bb) in enumerate(zip(a, b)):
            a_pat.append((aa >> i) & 1)
            a_pat2.append(1-((aa >> i) & 1))
            b_pat.append((bb >> i) & 1)
        b_pat = b_pat*2
        cand = [False]*(2*N)
        cand2 = [False]*(2*N)
        j = -1
        while j < N:
            j = j+1+contain(a_pat, b_pat[j+1:])
            if j < 2*N:
                cand[j] = True
        j = -1
        while j < N:
            j = j+1+contain(a_pat2, b_pat[j+1:])
            if j < 2*N:
                cand2[j] = True
        for j in range(N):
            if cand[j] == False and cand2[j] == False:
                K[j] = False
        # print("K", K)

    ans = []
    for i, k in enumerate(K):
        if k == True:
            ans.append(((N-i) % N, b[0] ^ a[(N-i) % N]))
    ans.sort()
    for a, b in ans:
        print(a, b, sep=" ")
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def z_algorithm(S):
    """Zアルゴリズム
    https://snuke.hatenablog.com/entry/2014/12/03/214243 の二番目のコード
    """
    N = len(S)
    Z = [0]*N

    c = 0
    for i in range(1, N):
        if i+Z[i-c] < c+Z[c]:
            Z[i] = Z[i-c]
        else:
            j = max(0, c+Z[c]-i)
            while i+j < N and S[j] == S[i+j]:
                j += 1
            Z[i] = j
            c = i
    Z[0] = len(S)
    return Z


def zfindall(text, pattern, separator="$"):
    """テキストからパターンと一致する部分文字列を探す。
    開始インデックスを返す
    Zアルゴリズムを利用する
    """
    N = len(pattern)
    conc = pattern+separator+text
    Z = z_algorithm(conc)
    ans = [False]*(len(text)-1)
    for i in range(len(conc)):
        if Z[i] == N:
            ans[i-N-1] = True
    return ans


def solve(N: int, a: "List[int]", b: "List[int]"):

    def bit_most(v):
        # 最高位の桁を得る
        if v == 0:
            return 0
        else:
            return len(bin(v))-2

    K = [True]*N
    for i in range(30):
        a_pat = []
        a_pat2 = []
        b_pat = []
        for n, (aa, bb) in enumerate(zip(a, b)):
            a_pat.append(str((aa >> i) & 1))
            a_pat2.append(str(1-((aa >> i) & 1)))
            b_pat.append(str((bb >> i) & 1))
        a_pat = "".join(a_pat)
        a_pat2 = "".join(a_pat2)
        b_pat = "".join(b_pat)*3
        # print(a_pat, b_pat)
        # print(a_pat2, b_pat)
        zzz = zfindall(b_pat, a_pat)
        zzz2 = zfindall(b_pat, a_pat2)
        # print(zzz)
        # print(zzz2)
        for i in range(N):
            if not ((zzz[i] == True and zzz[i+N] == True) or
                    (zzz2[i] == True and zzz2[i+N] == True)):
                K[i] = False

    ans = []
    for i, k in enumerate(K):
        if k == True:
            ans.append(((N-i) % N, b[0] ^ a[(N-i) % N]))
    ans.sort()
    for a, b in ans:
        print(a, b, sep=" ")
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def f(A,N,k,x):
  C=[A[(i+k)%N]^x for i in range(N)]
  return C

import numpy as np
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

M=max(A+B)
Msup=2**(int(np.log2(M)+1))

for k in range(0,N):
  for x in range(0,Msup+1):
    if f(A,N,k,x)==B:
      print(k,"",x)

Submission status: Time Limit Exceeded

Solution code:
def main():
    class rolling_hash():
        def __init__(self, seq, char_list=None):
            seq_size = self.seq_size = len(seq)  # 文字列の長さ
            mod = self.mod = 2**61-1  # mod
            base = self.base = 3
            Hash = self.Hash = [0]*(seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            for i in range(seq_size):
                Hash[i+1] = (Hash[i] + (seq[i]+1) * pow(base, i, mod)) % mod

        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            return ((self.Hash[r] - self.Hash[l])
                    * pow(self.base, l*(self.mod-2), self.mod)) % self.mod

    n = int(input())
    a, b = list(map(int, input().split())), list(map(int, input().split()))
    memo = []

    a1 = rolling_hash([a[i-1] ^ a[i] for i in range(n)] +
                      [a[i-1] ^ a[i] for i in range(n)])
    b_hash = rolling_hash([b[i-1] ^ b[i] for i in range(n)]).calc_hash(0, n)
    for i in range(n):
        if a1.calc_hash(i, i+n) == b_hash:
            memo.append(i)
    b0 = b[0]
    for i in memo:
        x = b0 ^ a[i]
        print(i, x)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    class rolling_hash():
        def __init__(self, seq, char_list=None):
            seq_size = self.seq_size = len(seq)  # 文字列の長さ
            mod = self.mod = 2**61-1  # mod
            base = self.base = 3
            Hash = self.Hash = [0]*(seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            for i in range(seq_size):
                Hash[i+1] = Hash[i] + (seq[i]+1) * pow(base, i, mod)
    
        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            return ((self.Hash[r] - self.Hash[l])
                    * pow(self.base, l*(self.mod-2), self.mod)) % self.mod
    

    n = int(input())
    a, b = list(map(int, input().split())), list(map(int, input().split()))
    memo = []

    a1 = rolling_hash([a[i-1] ^ a[i] for i in range(n)] +
                      [a[i-1] ^ a[i] for i in range(n)])
    b_hash = rolling_hash([b[i-1] ^ b[i] for i in range(n)]).calc_hash(0, n)
    for i in range(n):
        if a1.calc_hash(i, i+n) == b_hash:
            memo.append(i)
    b0 = b[0]
    for i in memo:
        x = b0 ^ a[i]
        print(i, x)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    class rolling_hash():
        def __init__(self, sequence):
            self.sequence = sequence  # 文字列
            self.seq_size = len(self.sequence)  # 文字列の長さ
            self.mod = 2**61-1  # mod
            self.base = 3  # 文字の種類数
            self.Hash_0start = [0]*(self.seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            for i in range(self.seq_size):
                self.Hash_0start[i+1] = self.Hash_0start[i] + \
                    (self.sequence[i]+1)*pow(self.base, i, self.mod)

        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            return ((self.Hash_0start[r] - self.Hash_0start[l])
                    * pow(self.base, l*(self.mod-2), self.mod)) % self.mod

    n = int(input())
    a, b = list(map(int, input().split())), list(map(int, input().split()))
    memo = []

    a1 = rolling_hash([a[i-1] ^ a[i] for i in range(n)] +
                      [a[i-1] ^ a[i] for i in range(n)])
    b_hash = rolling_hash([b[i-1] ^ b[i] for i in range(n)]).calc_hash(0, n)
    for i in range(n):
        if a1.calc_hash(i, i+n) == b_hash:
            memo.append(i)
    b0 = b[0]
    for i in memo:
        x = b0 ^ a[i]
        print(i, x)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 2**31-1
    pow3 = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 200001):
        p = p*3 % mod
        pow3[i] = p

    def rolling_hash(seq):
        h = sum([(i+1)*j for i, j in zip(seq, pow3[:-1])]) % mod
        H0s = [0]*n
        for i, j in enumerate(seq):
            H0s[i] = h
            h = (h-j-1+(j+1)*pn)*i3 % mod
        return H0s

    def rolling_hash2(seq):
        p = sum([(i+1)*j for i, j in zip(seq, pow3[:-1])]) % mod
        p2 = sum([(2-i)*j for i, j in zip(seq, pow3[:-1])]) % mod
        return p, p2

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m, cnt, pn = max(a+b), n, pow3[n]
    a1, b1, memo = [0]*n, [0]*n, [0]*n

    for k in range(len(bin(m))-2):
        k2 = 2**k
        a1 = [(j & k2) >> k for j in a]
        b1 = [(j & k2) >> k for j in b]
        b_hash, b_hash2 = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        for i, (j, k) in enumerate(zip(memo, A1)):
            if j is None:
                continue
            elif k == b_hash:
                pass
            elif k == b_hash2:
                memo[i] += k2
            else:
                memo[i] = None
                cnt -= 1
        if cnt == 0:
            return

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 2**31-1
    pow3 = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 200001):
        p = p*3 % mod
        pow3[i] = p

    def rolling_hash(seq):
        h = rolling_hash2(seq)
        H0s = [0]*n
        for i, j in enumerate(seq):
            H0s[i] = h
            h = (h-j-1+(j+1)*pn)*i3 % mod
        return H0s

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i]
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m = max(a+b)
    a1 = [0]*n
    a2 = [0]*n
    b1 = [0]*n
    memo = [0]*n
    cnt = n
    pn = pow3[n]

    for k in range(len(bin(m))-2):
        k2 = 2**k
        for i, j in enumerate(a):
            p = j & k2
            a1[i] = p
            a2[i] = p ^ k2
        for i, j in enumerate(b):
            b1[i] = j & k2
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, (j, k, l) in enumerate(zip(memo, A1, A2)):
            if j is None:
                continue
            elif k == b_hash:
                pass
            elif l == b_hash:
                memo[i] += k2
            else:
                memo[i] = None
                cnt -= 1
        if cnt == 0:
            return

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 2**61-1
    pow3 = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 200001):
        p = p*3 % mod
        pow3[i] = p

    def rolling_hash(seq):
        h = rolling_hash2(seq)
        H0s = [0]*n
        for i, j in enumerate(seq):
            H0s[i] = h
            h = (h-j-1+(j+1)*pn)*i3 % mod
        return H0s

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i]
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m = max(a+b)
    a1 = [0]*n
    a2 = [0]*n
    b1 = [0]*n
    memo = [0]*n
    cnt = n
    pn = pow3[n]

    for k in range(len(bin(m))-2):
        k2 = 2**k
        for i, j in enumerate(a):
            p = j & k2
            a1[i] = p
            a2[i] = p ^ k2
        for i, j in enumerate(b):
            b1[i] = j & k2
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, (j, k, l) in enumerate(zip(memo, A1, A2)):
            if j is None:
                continue
            elif k == b_hash:
                pass
            elif l == b_hash:
                memo[i] += k2
            else:
                memo[i] = None
                cnt -= 1
        if cnt == 0:
            return

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 2**61-1
    pow3 = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 200001):
        p = p*3 % mod
        pow3[i] = p

    def rolling_hash(seq):
        h = rolling_hash2(seq)
        seq_size = len(seq)
        H0s = [0]*seq_size
        for i, j in enumerate(seq):
            H0s[i] = h
            h = (h-j-1+(j+1)*pow3[n])*i3 % mod
        return H0s

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i] % mod
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m = max(a+b)
    a1 = [0]*n
    a2 = [0]*n
    b1 = [0]*n
    memo = [0]*n
    cnt = n

    for k in range(len(bin(m))-2):
        k2 = 2**k
        for i, j in enumerate(a):
            p = j & k2
            a1[i] = p
            a2[i] = p ^ k2
        for i, j in enumerate(b):
            b1[i] = j & k2
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, j in enumerate(memo):
            if j is None:
                continue
            elif A1[i] == b_hash:
                pass
            elif A2[i] == b_hash:
                memo[i] += k2
            else:
                memo[i] = None
                cnt -= 1
        if cnt == 0:
            return

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 2**61-1
    pow3 = [1]*400001
    pow3i = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 400001):
        p = p*3 % mod
        pow3[i] = p
    p = 1
    for i in range(1, 200001):
        p = p*i3 % mod
        pow3i[i] = p

    class rolling_hash():
        def __init__(self, seq):
            seq_size = len(seq)  # 文字列の長さ
            self.H0s = [0]*(seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            H0s, p = self.H0s, 0
            for i, j in enumerate(seq):
                p = (p+(j+1)*pow3[i]) % mod
                H0s[i+1] = p

        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            H0s = self.H0s
            return ((H0s[r]-H0s[l])*pow3i[l]) % mod

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i] % mod
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m = max(a+b)
    a += a
    a1 = [0]*(2*n)
    a2 = [0]*(2*n)
    b1 = [0]*n
    memo = [0]*n

    for k in range(len(bin(m))-2):
        k2 = 2**k
        for i, j in enumerate(a):
            p = j & k2
            a1[i] = p
            a2[i] = p ^ k2
        for i, j in enumerate(b):
            b1[i] = j & k2
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, j in enumerate(memo):
            if j is None:
                continue
            elif A1.calc_hash(i, i+n) == b_hash:
                pass
            elif A2.calc_hash(i, i+n) == b_hash:
                memo[i] += k2
            else:
                memo[i] = None

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 2**61-1
    pow3 = [1]*400001
    pow3i = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 400001):
        p = p*3 % mod
        pow3[i] = p
    p = 1
    for i in range(1, 200001):
        p = p*i3 % mod
        pow3i[i] = p

    class rolling_hash():
        def __init__(self, seq):
            seq_size = len(seq)  # 文字列の長さ
            self.H0s = [0]*(seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            H0s, p = self.H0s, 0
            for i, j in enumerate(seq):
                p = (p+(j+1)*pow3[i]) % mod
                H0s[i+1] = p

        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            H0s = self.H0s
            return ((H0s[r]-H0s[l])*pow3i[l]) % mod

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i] % mod
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a += a
    a1 = [0]*(2*n)
    a2 = [0]*(2*n)
    b1 = [0]*n
    memo = [0]*n

    for k in range(32):
        k2 = 2**k
        for i, j in enumerate(a):
            p = j & k2
            a1[i] = p
            a2[i] = p ^ k2
        for i, j in enumerate(b):
            b1[i] = j & k2
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, j in enumerate(memo):
            if j is None:
                continue
            elif A1.calc_hash(i, i+n) == b_hash:
                pass
            elif A2.calc_hash(i, i+n) == b_hash:
                memo[i] += k2
            else:
                memo[i] = None

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    class rolling_hash():
        def __init__(self, seq):
            seq_size = len(seq)  # 文字列の長さ
            self.H0s = [0]*(seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            H0s, p = self.H0s, 0
            for i, j in enumerate(seq):
                p = (p+(j+1)*pow(3, i, mod)) % mod
                H0s[i+1] = p

        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            H0s = self.H0s
            return ((H0s[r]-H0s[l])*pow(3, l*(mod-2), mod)) % mod

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow(3, i, mod) % mod
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a += a
    a1 = [0]*(2*n)
    a2 = [0]*(2*n)
    b1 = [0]*n
    mod = 2**61-1
    memo = [0]*n

    for k in range(32):
        k2 = 2**k
        for i, j in enumerate(a):
            p = (j & k2) >> k
            a1[i] = p
            a2[i] = p ^ 1
        for i, j in enumerate(b):
            b1[i] = (j & k2) >> k
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, j in enumerate(memo):
            if j is None:
                continue
            elif A1.calc_hash(i, i+n) == b_hash:
                pass
            elif A2.calc_hash(i, i+n) == b_hash:
                memo[i] += k2
            else:
                memo[i] = None

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
d = [0] * (2 * n - 1)
for i in range(n - 1):
    c[i] = b[i] ^ b[i + 1]
    d[i] = a[i] ^ a[i + 1]
c[n - 1] = b[0] ^ b[-1]
d[n - 1] = a[0] ^ a[-1]
for i in range(n - 1):
    d[n + i] = d[i]
t = [0] * (n + 1)
t[0] = -1
j = -1
for i in range(n):
    while j >= 0 and c[i] != c[j]:
        j = t[j]
    j += 1
    t[i + 1] = j
m = 0
ans = []
while m < n:
    for i in range(n):
        if c[i] != d[m + i]:
            m += i - t[i]
            if i > 0:
                i = t[i]
            break
    else:
        ans.append(m)
        m = m + n - t[n]
        i = t[n]
for k in ans:
    print(k, a[k] ^ b[0])

Submission status: Time Limit Exceeded

Solution code:
def main():
    class rolling_hash():
        def __init__(self, seq):
            seq_size = len(seq)  # 文字列の長さ
            self.H0s = [0]*(seq_size+1)  # 先頭からi-1文字目までのハッシュ値
            H0s, p = self.H0s, 0
            for i, j in enumerate(seq):
                p = (p+(j+1)*pow(3, i, mod)) % mod
                H0s[i+1] = p

        def calc_hash(self, l, r):  # l文字目からr-1文字目までのハッシュ値
            H0s = self.H0s
            return ((H0s[r]-H0s[l])*pow(3, l*(mod-2), mod)) % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    mod = 2**61-1

    memo = [0]*n

    for k in range(32):
        a1 = [0]*n
        a2 = [0]*n
        for i, j in enumerate(a):
            a1[i] = j % 2
            a2[i] = (j % 2) ^ 1
        a1 += a1
        a2 += a2
        b_hash = rolling_hash([i % 2 for i in b]).calc_hash(0, n)
        a1 = rolling_hash(a1)
        a2 = rolling_hash(a2)
        for i, j in enumerate(memo):
            if j is None:
                continue
            elif a1.calc_hash(i, i+n) == b_hash:
                pass
            elif a2.calc_hash(i, i+n) == b_hash:
                memo[i] += 2**k
            else:
                memo[i] = None
        for i in range(n):
            a[i] //= 2
            b[i] //= 2

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()