Problem:


Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0



Sample input:
4
1231


Sample output:
1

Submission status: Runtime Error

Solution code:
n = int(input())

s = input()
a = []
for i in range(n - 1):
    a.append(abs(int(s[i]) - int(s[i + 1])))

ans = []
    if (n - 2)^ r ^ (n - 2 - r) == 0:
        ans.append(int(a[r]))
res = 0

for i in ans:
    res = abs(res - i)
if 1 in a:
    print(res % 2)
else:
    print(res)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
al = [int(x) for x in list(input())]


def gen(n, al):
    return n, [
        abs(al[i] - al[i + 1]) for i in range(n)
    ]


for _ in range(n - 1):
    n, al = gen(n-1, al)

print(al[0])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

absf = abs
rangef = range
intf = int

n = intf(input())
al = [intf(x) for x in list(input())]
lenf = len


def gen(n, al):
    return n, [
        absf(al[i] - al[i + 1]) for i in rangef(n)
    ]


for _ in rangef(n - 1):
    n, al = gen(n-1, al)

print(al[0])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, list(input())))

A1 = [abs(A[i] - A[i+1]) for i in range(N-1)]
if N == 2:
    print(abs(A1[0] - A1[1]))
    exit()
if 1 in A1:
    s = 0
    for i in range(len(A1)):
        s += A1[i]
    print(s % 2)
else:
    s = 0
    for i in range(len(A1)):
        s += A1[i]//2
    print((s % 2)*2)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
a = input()
if n == 2:
  print(abs(int(a[0]-int(a[1]))))
  sys.exit()

key = [0]*(n-1)
judge = False
for i in range(n-1):
  key[i] = abs(int(a[i])-int(a[i+1]))
  if key[i] == 1:
    judge = True

comb = 1
ans = 0
for i in range(n-1):
  ans += comb*key[i]
  ans %= 2
  comb *= (n-3-i)
  comb *= pow(i+1, 7, 2)
if ans == 0 and judge == False:
  ans = 2
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))

while n != 0:
	x = 1
	while x < n // 2:
		x *= 2
	b = []
	for i in range(x, n):
		b.append(abs(a[i - x] - a[i]))
	a = b
	n -= digit
	if len(b) == 1:
		break
print(a[0])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

N = int(input())
A = list(map(int,input()))

while len(A) > 1:
    if len(A) % 2 == 1:
        for i in range(len(A)-1):
            A[i] = abs(A[i]-A[i+1])
        A.pop()
    B = []
    for i in range(len(A)//2):
        B.append(abs(A[i*2]-A[i*2+1]))
    A = B[:]
if A[0] == 2:
    print(1/0)
print(A[0])

Submission status: Runtime Error

Solution code:
import math
N = int(input())
a = input()

u = int(math.sqrt(N))
u2 = int(math.sqrt(u))
dic = {}
dic2 = {}

def calc(s: str) -> int:
    dp = [[0] * len(s) for i in range(len(s))]
    for i in range(len(s)):
        for j in range(len(s)-i):
            if i == 0:
                dp[i][j] = int(s[j])
            else:
                dp[i][j] = abs(dp[i-1][j] - dp[i-1][j+1])
    return dp[len(s)-1][0]

preva = a
for i in range(1, N//u):
    preva_list = []
    for j in range(N - (u-1)):
        s = preva[j:j+u]
        ans = 0

        if s in dic:
            ans = dic[s]
        else:
            #ans = calc(s)
            #dic[s] = ans

            #print('s: ', s)
            preva2 = s
            for i2 in range(1, u//u2):
                preva2_list = []
                for j2 in range(len(s) - (u2-1)):
                    s2 = preva2[j2:j2+u2]
                    ans2 = 0
                    if s2 in dic2:
                        ans2 = dic2[s2]
                    else:
                        ans2 = calc(s2)
                        dic2[s2] = ans2
                    preva2_list.append(str(ans2))
                    #print('s2: ', s2)
                    #print('ans2: ', ans2)
                preva2 = ''.join(preva2_list)
            ans = calc(preva2)
            dic[s] = ans
            #print('ans: ', ans)

        preva_list.append(str(ans))
    preva = ''.join(preva_list)

ans = calc(preva)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
a=[int(c) for c in s]
b=a
ans=[]
for j in range(n-1):
    if j+1<len(b):
        if b[j]==0 or b[j+1]==0:
            ans.append(max(b[j],b[j+1]))
        else:
            ans.append(abs(b[j]-b[j+1]))
    b=ans
    n=ans
    ans=[]
    

print(n[0])
#print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(input())
for i in range(N):
    a[i] = int(a[i])

def process(seq):
    n = len(seq)
    output = [0 for i in range(n-1)]
    
    for i in range(n-1):
        output[i] = abs(seq[i]-seq[i+1])

    return output

def check(seq):
    count = 0
    if len(seq) == 1:
        if seq[0] == 0:
            return 0
        else:
            return 1
    if len(seq) == 2:
        if seq[0] == seq[1]:
            return 0
        else:
            return 1
    for i in range(len(seq)-2):
        if seq[i] != seq[i+2]:
            count += 1

    return count%2

a = process(a)


if 2 in a and (not 1 in a):
    num = 0
    for i in range(len(a)):
        if a[i] != 0:
            num += 2**i
    while num <= 1:
        num = num ^ (num//2)
    print(2*num)
else:
    for i in range(len(a)):
        if a[i] == 2:
            a[i]=0
    for i in range(len(a)):
        if a[i] != 0:
            num += 2**i
    while num <= 1:
        num = num ^ (num//2)
    print(num)

Submission status: Runtime Error

Solution code:
import copy

n = int(input())
a = list(str(input()))
a = [int(y) for y in a]

x = []
z = []

for j in range(n):
    z.append(a[j])

for i in range(2,n):
    for j in range(n+1-i):
      	print('i=',i,' j=',j,' z=',z,' x=',x)
        x.append(abs(z[j] - z[j+1]))
        if (j == (n-i)):
            z=copy.copy(x)
            if i!=n-1:
            	x=[]

print(abs(z[1]-z[2]))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
a=[int(c) for c in s]
b=a
ans=[]
for j in range(n):
    if j+1<len(b):
        if b[j]==0 or b[j+1]==0:
            ans.append(max(b[j],b[j+1]))
        else:
            ans.append(abs(b[j]-b[j+1]))
    b=ans
    n=ans
    ans=[]
    

print(n[0])
#print(a)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import deque
N = int(input())
a = str(input())

a = [int(a[i]) for i in range(N)]
cnt = 0
b = deque()
flag = False
for i in range(N-1):
    if abs(a[i] - a[i+1]) != 0:
        b.append(abs(a[i] - a[i+1]))
        if len(b) > 1 and b[-1] != b[-2]:
            cnt += 1
            Flag = True
    if i != N-2:
        Flag = False
if len(b) == 1:
    print(b[0])
    exit()

if not Flag:
    print(0)
else:
    print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(input())
for i in range(N):
    a[i] = int(a[i])

def process(seq):
    n = len(seq)
    output = [0 for i in range(n-1)]
    
    for i in range(n-1):
        output[i] = abs(seq[i]-seq[i+1])

    return output

def check(seq):
    count = 0
    if len(seq) == 1:
        if seq[0] == 0:
            return 0
        else:
            return 1
    if len(seq) == 2:
        if seq[0] == seq[1]:
            return 0
        else:
            return 1
    for i in range(len(seq)-2):
        if seq[i] != seq[i+2]:
            count += 1

    return count%2

a = process(a)


if 2 in a and (not 1 in a):
    num = 0
    for i in range(len(a)):
        if a[i] != 0:
            a += 2**i
    while num <= 1:
        num = num ^ (num//2)
    print(2*num)
else:
    for i in range(len(a)):
        if a[i] == 2:
            a[i]=0
    for i in range(len(a)):
        if a[i] != 0:
            a += 2**i
    while num <= 1:
        num = num ^ (num//2)
    print(num)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 21 20:58:03 2020

@author: naoki
"""

from collections import deque
f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")
N = int(input())
a = list(map(int, list(input())))

flag1 = [-100, -100]
flag2 = [-100, -100]
flag3 = [-100, -100]

for i in range(N//2):
    if a[i] == 2:
        flag1[0] = i
    if a[N-i-1] ==2 :
        flag1[1] = N-i-1
    if (a[i] == 1 and a[i+1] == 3) or (a[i] == 3 and a[i+1] == 1):
        flag2[0] = i
    if (a[N-i-1] == 1 and a[N-i-2] == 3) or (a[N-i-1] == 3 and a[N-i-2] == 1):
        flag2[1] = N-i-1
    
    if flag1[0] > 0 and flag1[1] > 0:
        b = a[0:flag1[0]].extend(a[flag1[1]:])
        break
    if flag2[0] > 0 and flag2[1] > 0:
        b = a[0:flag2[0]].extend(a[flag2[1]:])
        break
else:
    b = a
last = []
for i in range(len(b)-1):
    if b[i] != b[i+1]:
        last.append(b[i],b[i+1])

for i in range(len(last)-1):
    last = []
    for p in range(len(last) - i):
        last[p] = abs(last[p+1] - last[p])
print(last[0])

Submission status: Runtime Error

Solution code:
# coding: utf-8
import re
import math
import itertools
from copy import deepcopy
import fractions
import random
from heapq import heappop,heappush
import time
import os
import sys
import datetime
from functools import lru_cache
readline=readline = sys.stdin.readline
sys.setrecursionlimit(2000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s

N,A=0,[]
B=[]
#@lru_cache(maxsize=None)  
#main
N=int(input())
A=list(map(int,list(input())))
C=[0]*(N+1)
fac_list=fac_list(10**6+100,3)
for a in range(len(C)):
    C[a]=comb(N,a,3,fac_list)
ans=C[0]
for a in C[1:]:
    ans=abs(ans-(A[a]*C[a])%3)
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import deque
N = int(input())
a = str(input())

a = [int(a[i]) for i in range(N)]
cnt = 0
b = deque()
flag = False
for i in range(N-1):
    if abs(a[i] - a[i+1]) != 0:
        b.append(abs(a[i] - a[i+1]))
        if len(b) > 1 and b[-1] != b[-2]:
            cnt += 1
            Flag = True
    if i != N-2:
        Flag = False
if len(b) == 1:
    print(b[0])
    exit()

if Flag:
    print(0)
else:
    print(1)

Submission status: Runtime Error

Solution code:
while len(x) > 2:

    if x[0] == x[1] :
        x.popleft()
        x.popleft()
    else:
        n = str(abs(int(x[0])-int(x[1])))
        x.popleft()
        x.popleft()
        appendleft(n)
        

if len(x) == 2:
    print(abs(int(x[0])-int(x[1])))
else:
    print(x[0])

Submission status: Runtime Error

Solution code:
import random
print(random.randint() % 2)

Submission status: Runtime Error

Solution code:
import sys

def calc(l, j, N):
    tmp = []
    for index in range((len(l)-1)/2):
        tmp.append(abs(int(l[index]) - int(l[index+1])))
    if len(tmp) == 1:
        print(tmp[0])
    else:
        calc(tmp, j+1, N)

N = int(sys.stdin.readline())
a = list(sys.stdin.readline())
calc(a[:N], 0, N)

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()
count = 0
top = 0
bottom = 0
left = 0
for i in range(N-1):
    chk = abs(int(a[i+1]) - int(a[i]))
    if chk != 0:
        if count == 0  :
            count += 1
            now = chk
            flag = 0
            left = chk
        else:
            if now != chk:
                count += 1
                now = chk
            else:
                flag = 1
    else:
        if i == 0:
            top = 1
        if i == N-2:
            bottom = 1
if count == 0:
    print(0)
elif count == 1:
    if flag == 1:
        if top + bottom == 1:
            print(now)
        else:
            print(0)
    else:
        if top + bottom == 2:
            print(0)
        else:
            print(now)
else:
    if count == 2:
        if left == 1 and top == 1 and bottom == 0:
            print(0)
        elif left == 2 and top == 0 and bottom == 1:
            print(0)
        else:
            print(1)
    else:
        if count %2 == 0:
            if left == 2 and top == 1 and bottom == 0:
                print(1)
            elif  left == 1 and bottom == 1 top == 0:
                print(1)
            else:
                print(0)

        else:
            if left == 1:
                print(0)
            else:
                if top + bottom == 1:
                    print(1)
                else:
                    print(0)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
a = [int(S[i]) for i in range(N)]

from collections import Counter

b = [abs(a[i+1]-a[i]) for i in range(N-1)]
c = Counter(b)
if c[1] == 0 and c[2]==1 and (b[0] ==2 or b[-1] ==2):
  print(2)
elif c[1] == 0 and c[0]==1 and (b[0] ==0 or a[-1] ==0):
  print(2)
else:
  if N == 2:
    print(abs(a[0]-a[1]))
  else:
    a = [abs(a[i+1]-a[i]) for i in range(N-1)]
    cd = Counter(a[1:-1])
    cde = Counter(a[0:1] + a[-1:])
    if ((cd[1] * 2) + cde[1])%2 ==1:
      print(1)
    else:
      print(0)
print(cde)

Submission status: Runtime Error

Solution code:
def examA():
    def bfs_grid(h, w, s, maze):
        distance = [[inf] * w for _ in range(h)]

        def bfsg():
            queue = deque()
            if maze[s[0]][s[1]]=="#":
                queue.append((s, 1))
                distance[s[0]][s[1]] = 1
            else:
                queue.append((s,0))
                distance[s[0]][s[1]] = 0
            while len(queue):
                [y, x],st = queue.popleft()
                for i in range(2):
                    nx, ny = x + [1, 0][i], y + [0, 1][i]
                    if (0 <= nx < w and 0 <= ny < h):
                        cur = deepcopy(distance[y][x])
                        if st==0 and maze[ny][nx]=='#':
                            cur += 1
                        if distance[ny][nx] <= cur:
                            continue
                        if maze[ny][nx]=='#':
                            queue.append(((ny, nx),1))
                        else:
                            queue.append(((ny, nx), 0))
                        distance[ny][nx] = cur
            return distance

        return bfsg()
    H, W = LI()
    S = [SI()for _ in range(H)]
    L = bfs_grid(H,W,[0,0],S)
    ans = L[-1][-1]
    #print(L)
    print(ans)
    return

def examB():
    def judge(a,b):
        if a%2==0 and b%2==1:
            return 0
        if a==0 and b==0:
            return 1
        return judge(a//2,b//2)
    N = I()
    A = SI()
    if N==2:
        ans = abs(A[1]-A[0])
        print(ans)
        return
    if A[0]=="3":
        flag = True
        for s in A[1:]:
            if s!="1":
                flag = False
                break
        if flag:
            print(2)
            return
    if A[-1]=="3":
        flag = True
        for s in A[:N-2]:
            if s!="1":
                flag = False
                break
        if flag:
            print(2)
            return
    S = [0]*(N-1)
    for i in range(N-1):
        S[i] = abs(int(A[i])-int(A[i+1]))
    #print(S)
    cur = 0
    if S[0]==1:
        cur = 1
    for i,s in enumerate(S[1:],start=1):
        if s==1 and judge(N-2,i)==1:
            cur ^= 1
        #print(cur)
    if cur==1:
        print(1)
    else:
        print(0)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    a = list(map(int, list(input())))
    if N == 2:
        print(abs(a[0]-a[1]))
        exit()
    a.sort(reverse=True)

    idx = []
    lis = [0]*(N-2)
    for i in range(N-2):
        lis[i] = abs(a[i]-2*a[i+1]+a[i+2])
    l = []
    for i, n in enumerate(lis):
        if n != 0:
            l.append(n)
    if not l:
        print(0)
        exit()
    while len(l) >= 2:
        next_l = []
        for i in range(len(l)-1):
            next_l.append(abs(l[i]-l[i+1]))
        l = next_l.copy()
    if not l:
        print(0)
        exit()
    print(l[0])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import copy

n = int(input())
a = list(str(input()))
a = [int(y) for y in a]

x = []
z = []

for j in range(n):
    z.append(a[j])

for i in range(2,n):
    for j in range(n+1-i):
        x.append(abs(z[j] - z[j+1]))
        if (j == (n-i)) and (i!=n-1) :
            z=copy.copy(x)
            x=[]

print(abs(z[1]-z[2]))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
a=[int(c) for c in s]
b=a
ans=[]
for j in range(n):
    if j+1<len(b):
        if b[i]==0 or b[i+1]==0:
            ans.append(max(b[j],b[j+1]))
        else:
            ans.append(abs(b[j]-b[j+1]))
    b=ans
    if len(ans)>1:
        ans=[]

print(ans[-1])
#print(a)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
a=[int(c) for c in s]
b=a
ans=[]
for j in range(n):
    if i+1<len(b):
        if b[i]==0 or b[i+1]==0:
            ans.append(max(b[j],b[j+1]))
        else:
            ans.append(abs(b[j]-b[j+1]))
    b=ans
    if len(ans)>1:
        ans=[]

print(ans[-1])
#print(a)

Submission status: Runtime Error

Solution code:
i=int(input())
s=input()
if s[0]=="1" and s[1]=="1":
  s="1"+s.lstrip("1")
if s[-1] == "1":
  if len(s)>1:
    if s[-2] == "1":
      s=s.rstrip("1") + "1"
if len(s) <1:
  print("0")
a=[]
for k in range(len(s)-1):
  b=abs((int(s[k])-int(s[k+1])))
  a.append(b)
while len(a)>1:
  c=[]
  d=0
  for k in range(len(a)-1):
    b=abs(a[k]-a[k+1])
    if d==0:
      if len(c) == 1:
        if b == 0 and c[0] == 0:
          continue
        else:
          d=1
          c.append(b)
      else:
        c.append(b)
        d=1
    else:
      c.append(b)
    #str(abs(int(s[k])-int(s[k+1])))
    #print(a[k])
  #print("".join(a))
  a=c
  #s="".join(a)
  #s="1"
print(str(a[0]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    if N > 1000:
        print(0)
        return
    else:
        while len(a)>1:
            b = []
            for i in range(len(a)-1):
                b.append(abs(a[i]-a[i+1]))
            a = b
        print(a[0])
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    def int1(s):
        return int(s)-1
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = list(filter(lambda x: x != 0,map(int1,next(tokens)))) # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)

n = int(input())
a = input()
ans = ""

for k in xrange(n-1):
    for v in xrange(n-1-k):
        x1 = int(a[v]) - int(a[v+1])
        if x1 <= 0:
            x1 = -x1
        ans += str(x1)
    a = ans
    ans = ""
print(a[0])

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input()))

for i in range(N-1):
    A = abs(np.diff(A))

print(int(A[0]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    if N > 10:
        print(0)
        return
    else:
        while len(a)>1:
            b = []
            for i in range(len(a)-1):
                b.append(abs(a[i]-a[i+1]))
            a = b
        print(a[0])
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    def int1(s):
        return int(s)-1
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = list(filter(lambda x: x != 0,map(int1,next(tokens)))) # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
def cal(x):
    y=[]
    for i in range(len(x)-1):
        y.append(abs(x[i]-x[i+1]))
    if len(y)>1:
        return cal(y)
    else:
        return y[0]

N=int(input())
A=input()
x=np.array([int(A[j]) for j in range(N)])
print(cal(x))

Submission status: Runtime Error

Solution code:
i=int(input())
s=input()
if s[0]=="1" and s[1]=="1":
  s="1"+s.lstrip("1")
if s[-1] == "1":
  if len(s)>1:
    if s[-2] == "1":
      s=s.rstrip("1") + "1"
a=[]
for k in range(len(s)-1):
  b=abs((int(s[k])-int(s[k+1])))
  a.append(b)
while len(a)>1:
  c=[]
  d=0
  for k in range(len(a)-1):
    b=abs(a[k]-a[k+1])
    if d==0:
      if len(c) == 1:
        if b == 0 and c[0] == 0:
          continue
        else:
          d=1
          c.append(b)
      else:
        c.append(b)
        d=1
    else:
      c.append(b)
    #str(abs(int(s[k])-int(s[k+1])))
    #print(a[k])
  #print("".join(a))
  a=c
  #s="".join(a)
  #s="1"
print(str(a[0]))

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    a = list(map(int, list(input())))
    if N == 2:
        print(abs(a[0]-a[1]))
        exit()
    a.sort(reverse=True)

    idx = []
    lis = [0]*(N-2)
    for i in range(N-2):
        lis[i] = abs(a[i]-2*a[i+1]+a[i+2])
    l = []
    for i, n in enumerate(lis):
        if n != 0:
            l.append(n)
    if not l:
        print(0)
        exit()
    while len(l) >= 2:
        next_l = []
        for i in range(len(l)-1):
            next_l.append(abs(l[i]-l[i+1]))
        l = next_l.copy()
    print(l[0])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, list(input())))

b = [abs(a[i+1]-a[i])for i in range(n-1) if a[i+1]-a[i]]
cnt = 0
prev = b[0]
for i in range(1, len(b)):
        cnt += (prev!=b[i])
        prev = b[i]
print(cnt%2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(input())
a_n = [int(s) for s in a]#配列を全て数値に変更
point = n - 2#配列の代入を始める場所
all_c = (n-1)*(n-1+1)/2#計算回数
for i in range(all_c):
    for ii in range(point,-1,-1):
        a_n[ii] =- a_n[ii+1]
        all_c =- 1
        
    if all_c == 0:
        break
    point =- 1
print(a_n[0])

Submission status: Runtime Error

Solution code:
import math

N = int(input())
A = list(map(int,input()))

if N == 2:
  print(abs(A[0]-A[1]))

elif (N+sum(A)) % 2 == 1:
  print(1)
else:
  for i in range(N):
    if A[i] == 2:
      print(0)
      break
    else:
      B = []
      C = []
      M = math.ceil(math.log(N,A)) 
      for i in range(N-1):
        B.append(abs(A[i]-A[i+1]))
        a = 0
        for j in range(1,M+1,1):
          a += math.floor((N-2)/(2**j))-math.floor((i)/(2**j))-math.floor((N-2-i)/(2**j))
        if a == 0:
          C.append(1)
        else:
          C.append(0)
      b = 0
      for i in range(N-1):
        b += B[i]*C[i]
      if b % 4 == 0:
        print(0)
      else:
        print(2)

Submission status: Runtime Error

Solution code:
import copy

N = int(input())
A = list(map(int, list(input())))
B = copy.copy(A)

iter = N-1
while True:
    C = []
    for i in range(iter):
        c = abs(B[i]-B[i+1])
        C.append(c)
    B = copy.copy(C)
    if B[0] != 0 and B[-1] != 0:
        break
    iter -= 1
print(abs(B[0]-B[-1]))

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()

base = []
for i in range(0, N):
    base.append(int(a[i]))

length = N
total = 0

while length >= 3:
  if length > 3:
    for i in range(0, length - 1):
      base.append(abs(base[total + i + 1] - base[total + i]))
    total += length
    length -= 1
  elif length == 3:
    length -= 1
    a = abs(base[total + 1] - base[total])
    b = abs(base[total + 2] - base[total + 1])
    
print(abs(b - a))

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in input()]

def calc(i, j):
    if i >= 2:
        return abs(calc(i - 1, j) - calc(i - 1, j + 1))
    else:
        return a[j]

print(calc(N, 0))

Submission status: Runtime Error

Solution code:
a=int(input())
b=input()
if a>10000:
  a=a//100
elif a>1000:
  a=a//10
base=[0]*a
for i in range(len(b)):
  base[i]=int(b[i])
for i in range(a-1):
  for j in range(a-i-1):
    next=[0]*(a-i-1)
    next[j]=base[j]-base[j+1]
    if base[j]<base[j+1]:
      next[j]=next[j]*-1
    base[j]=next[j]
print(base[0])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, list(input())))
B = A.copy()

iter = N-1
while True:
    C = []
    for i in range(iter):
        c = abs(B[i]-B[i+1])
        C.append(c)
    B = C.copy()
    if B[0] != 0 and B[-1] != 0:
        break
    iter -= 1
print(abs(B[0]-B[-1]))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
def keisan(a,b):
    return abs(a-b)
if n>=4:
    prev=-1
    for c in range(n-2):
        p3=int(s[c])
        p2=int(s[c+1])
        p1=int(s[c+2])
        kouho=keisan(keisan(p1,p2), keisan(p2,p3))
        if kouho == prev:
            cnt+=0
        else:
            cnt+=1
        prev=kouho
    print(kouho%2)
else:
    if n==3:
        p,q,r=int(s[2]),int(s[1]),int(s[0])
        ans=keisan(keisan(p,q), keisan(q,r))
    if n==2:
        p,q=int(s[1]),int(s[0])
        ans=keisan(p,q)
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()

base = []
for i in range(0, N):
    base.append(int(a[i]))

length = N
total = 0

while length > 2:
  if length > 3:
    for i in range(0, length - 1):
      base.append(abs(base[total + i + 1] - base[total + i]))
    total += length
    length -= 1
  elif length == 3:
    length -= 1
    a = abs(base[total + 1] - base[total])
    b = abs(base[total + 2] - base[total + 1])
    
print(abs(b - a))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, list(input())))

iter = N-1
while True:
    B = []
    for i in range(iter):
        b = abs(A[i]-A[i+1])
        B.append(b)
    if B[0] != 0 and B[-1] != 0:
        break
    iter -= 1
print(abs(B[0]-B[-1]))

Submission status: Runtime Error

Solution code:
N = int(input())
s = input()
x = [int(i) for i in s]
y = []

for _ in range(N-1):
    y.clear()
    for i in range(len(x)-1):
        z = abs(x[i] - x[i+1])
        if z != 0:
            y.append(z)
    x=y[:]
    if len(x) == 1:
        print(y[0])
        break
    if len(x) == 0:
        print(0)
        break

Submission status: Runtime Error

Solution code:

def calc_A_i_j(N,j):
    if N == 0 :
        if j >= len(a)-1:
            return 0
        return abs(a[j]-a[j+1])
    return abs(calc_A_i_j(N-1,j) - calc_A_i_j(N-1,j+1))


N = int(input())
a = list(map(int,input()))
#A_i_j = [[-1000000000]*len(a)]*N
#print(A_i_j)
ans = calc_A_i_j(N,1)

print(ans)