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: Wrong Answer

Solution code:
class Binary_Indexed_Tree():
    def __init__(self, n):
        self.n = n
        self.data = [0]*(n+1)

    def add(self, i, x):
        i += 1
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get(self, i):
        return self.sum_range(i, i)

    def sum(self, i):
        i += 1
        ret = 0
        while i:
            ret += self.data[i]
            i &= i-1
        return ret

    def sum_range(self, l, r):
        return self.sum(r)-self.sum(l-1)

    def lower_bound(self, w):
        if w<=0:
            return 0
        i = 0
        k = 1<<(self.n.bit_length())
        while k:
            if i+k <= self.n and self.data[i+k] < w:
                w -= self.data[i+k]
                i += k
            k >>= 1
        return i

n = int(input())
a = list(map(lambda x: int(x)-1, input()))
b = a[:]
if 1 in b:
    BIT = Binary_Indexed_Tree(n+1)
    for i, ai in enumerate(a):
        x = i
        if ai == 1:
            BIT.add(i, 1)
        else:
            s = BIT.sum(i)
            if s == 0:
                continue
            i = BIT.lower_bound(s)
        s = BIT.sum(i)
        if s > 1:
            j = BIT.lower_bound(s-1)
            for k in range(j+1, i):
                BIT.add(k,1)
            BIT.add(j, -1)
        else:
            for k in range(i):
                BIT.add(k,1)
    ans = BIT.get(0)
else:
    prev = -1 if b[0]==0 else 0
    for i in range(1,n):
        if b[i] == 0:
            continue
        else:
            for j in range(prev+1, i+1):
                b[j] = 2
            prev = i
    ans = b[0]

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np


def array_subtraction(arr):
  while len(arr) !=1:
    arr = np.abs(arr[1:]-arr[:-1])
    yield arr
  yield arr


def main():
  input()
  z = np.array([int(i) for i in input()])
  for num in array_subtraction(z):
      print(*num)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
s=s.replace('1','0').replace('2','1').replace('3','2')

if '1' not in s:
  while len(s)>=2:
    s=s.replace('0000', '')
    new_s = ''
    for i in range(len(s)-1):
      if s[i]==s[i+1]:
        new_s+='0'
      else:
        new_s+='2'
    s=new_s
  print(s)  
else:
  while len(s)>=2:
    s=s.replace('0000', '')
    new_s = ''
    for i in range(len(s)-1):
      if s[i]==s[i+1]:
        new_s+='0'
      elif s[i:i+2]=='02' or s[i:i+2]=='20':
        new_s+='2'
      else:
        new_s+='1'
    s=new_s
  print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input()
import random
print(random.randint(0, 2))

Submission status: Wrong Answer

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

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

def next(A):
    B=[]
    for i in range(1,len(A)):
        B.append(abs(A[i]-A[i-1]))
    return B

A=next(A)

if len(A)<=5:
    while len(A)>1:
        A=next(A)
    print(A[0])

else:
    if not (1 in A):
        if A==A[::-1]:
            print(0)
        else:
            print(2)
    else:
        LEN=len(A)
        SUM=0
        COMBI=1

        for i in range(LEN):
            SUM=(SUM+A[i]*COMBI)%2
            COMBI=COMBI*(LEN-1-i)//(i+1)
            #print(i,COMBI)

        if SUM%2==1:
            print(1)
        else:
            print(0)

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *
sys.setrecursionlimit(10 ** 6)

def CalcList(list):
    bfnum = -1
    afterList =[]
    for i in range(len(list)):
        if bfnum == -1:
            bfnum = list[i]
        else:
            now = abs(list[i] -bfnum)
            if now >0 and i < len(list) -1 :
                afterList.append(now)
            bfnum = list[i]
    return afterList

def CheckList(list):
    bfnum = -1
    count = 0
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            if i == bfnum:
                count = count +1
                if count < 3:
                    afterList.append(i)
            else:
                count = 0
                afterList.append(i)
    return afterList


if __name__ == '__main__':
    n = int(input())
    s = input()
    list =list(map(lambda x: int(x), s))

    while(len(list) >1):
        '''
        list =  CheckList(list)
        '''
        list = CalcList(list)

    if len(list) ==0:
        print(0)
    else:
        print(list[0])

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
S = input().strip()
ss = []
for c in S:
    ss.append(int(c))
if N == 2:
    print(abs(ss[0]-ss[1]))
    sys.exit()

X = []
X2 = []
for i in range(N-1):
    c = abs(ss[i] - ss[i+1])
    X.append(int(c)%2)
    X2.append(1-int(c==1))


#print(X2)
N=N-2

i = 0
Y = []
while N:
    if N%2:
        Y.append(i)
    i += 1
    N=N//2

#print(Y)
C = [0]
for y in Y[::-1]:
    y = 2**y
#    print(y)
    tmp = []
    for c in C:
        tmp.append(c+y)
    for t in tmp:
        C.append(t)
#print(sorted(C))
r = 0
r2 = 1
#print(C)
for c in C:
    r = r^X[c]
    r2 = r2*X2[c]
print(r2*2+r)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = input()
x_list = []

pre_num = -1
for v in x:
  v = int(v)
  if pre_num == v:
    continue
  else:
    x_list.append(v)
    pre_num = v
  
con_flg = False
while True:
  if len(x_list) <= 1:
    if con_flg:
      x_list[0] = 0
    break
  lst = []
  pre_num = -1
  count = 0
  for j in range(len(x_list)-1):
    num = abs(x_list[j]-x_list[j+1])
    if num == pre_num:
      count += 1
      continue
    else:
      lst.append(num)
      pre_num = num
      
  if count == len(x_list)-2:
    con_flg = True
    
  x_list = lst

  
if len(x_list) == 1:
  print(x_list[0])
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
l=[]
for i in range(n-1):
    if abs(int(s[i+1])-int(s[i]))==0 and n-3>=i>=1:continue
    if len(l)>=1 and abs(int(s[i+1])-int(s[i]))==l[-1]:continue
    l.append(abs(int(s[i+1])-int(s[i])))
if len(l)==1:print(l[0])
elif len(l)>=3 or len(l)==0:print(0)
else:print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
string = input()
a = []
for i in range(n):
    if int(string[i])-1 != 0:
        a.append(int(string[i])-1)

a_new = a
a = []
while len(a_new) != 1 and len(a_new) != 0:
    a = a_new
    a_new = []
    a_next = []
    for i in range(len(a)):
        if a[i] != 0: a_next.append(a[i])
    for i in range(len(a_next)-1):
        a_new.append(abs(a_next[i]-a_next[i+1]))

if a_new == []:
    print(0)
else:
    print(a_new[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, list(input())))
x = [max(a[i], a[i+1]) - min(a[i], a[i+1]) for i in range(N-1)]
f0,f1,f2 = False, False, False
for n in x:
	if n == 0:
		f0 = True
	elif n == 1:
		f1 = True
	else:
		f2 = True
	if f0 and f1 and f2:
		break
candidate = 1 if f1 else 2
if f1:
	x = [0 if x[i] == 2 else x[i] for i in range(N-1)]
else:
	x = [1 if x[i] == 2 else x[i] for i in range(N-1)]
current = x[-1]
g = 1
bef = x[0]
for i in range(1, N-1):
	if bef == x[i]:
		continue
	else:
		bef = x[i]
		g += 1

print(0 if g == 0 or (g+N) % 2 == 1 else candidate)

Submission status: Wrong Answer

Solution code:
n = int(input())
st = input()
if n==1:
    print(st[0])
elif n==2:
    print(int(st[1])-int(st[0]))
else:
    ans1 = abs(int(st[1])-int(st[0]))
    ans2 = abs(int(st[-1])-int(st[-2]))
    print(abs(ans2-ans1))

Submission status: Wrong Answer

Solution code:
N = int(input())
array = tuple(map(int, input()))
if N == 2:
  print(abs(array[0]-array[1]))
  quit()
print(abs(abs(array[0]-array[1])-abs(array[-2]-array[-1])))

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())
A = input()
a = np.array([int(A[i]) for i in range(len(A))])
while 1:
  a = np.diff(np.absolute(a),1)
  if len(a) == 1:
    print(a[0])
    break

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

# フェルマーの小定理
def nCr(n, r, mod=MOD):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod

MAXN = 10**6
f = [1]
for i in range(MAXN):
    f.append(f[-1] * (i+1) % MOD)
def nCr(n, r, mod=MOD):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

# print(nCr(1000000, 0))

N = int(input())
a = input()
ans = 0
for i in range(N):
    now = int(a[i])
    if now%2:
        ans += now * (nCr(N-1, i) % 2)
        ans %= 2
print(ans)

Submission status: Wrong Answer

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():
    N = I()
    A = SI()
    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)
    print(0)
    return
    if S[0]==S[-1]:
        print(0)
    else:
        print(1)
    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: Wrong Answer

Solution code:
import math

n = int(input())
nums = int(input())
str_list = list(str(nums))

nums_list = [ int(i) for i in str_list ]

def keisan(nums_list):
  result = [ abs(nums_list[i] - nums_list[i+1]) for i in range(len(nums_list)-1) ]
  print(result)
  if len(result) > 1:
    return keisan(result)
  else:
    return result

nums_list = keisan(nums_list)  
print(nums_list[0])

Submission status: Wrong Answer

Solution code:
import math
import numpy as np

N = int(input())
arr_rev = list(map(int, list(input())))
#arr_rev.reverse()
arr_rev = np.array(arr_rev)

def calc(a, b):
    return abs(-a + b)

first_diff = calc(0,1)
last_diff = calc(len(arr_rev)-2 ,len(arr_rev)-1 )
out = calc(first_diff, last_diff)
print(out)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9+7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# nCk
## 0!~n!をmodしつつ求める
def fact_all(n,M=mod):
    f = [1]*(n+1)
    ans = 1
    for i in range(1,n+1):
        ans = (ans*i)%M
        f[i] = ans
    return f

## inv(0!)~inv(n!)をmodしつつ求める
def fact_inv_all(fact_all,M=mod):
    N = len(fact_all)
    finv = [0]*N
    finv[-1] = pow(fact_all[-1],M-2,M)
    for i in range(N-1)[::-1]:
        finv[i] = finv[i+1]*(i+1)%M
    return finv

## nCkをmodしつつ返す
def nCk(n,k,fact_list=[],inv_list=[],M=mod):
    # a!をmodしつつ求める
    def fact(a,M=mod):
        ans = 1
        for i in range(2,a+1):
            ans = (ans*i)%M
        return ans

    # 互いに素なa,bについて、a*x+b*y=1の一つの解[x,y]を出力
    def extgcd(a,b):
        r = [1,0,a]
        w = [0,1,b]
        while w[2] != 1:
            q = r[2]//w[2]
            r2 = w
            w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
            r = r2
            w = w2
        return [w[0],w[1]]

    # aの逆元(mod M)を求める（aとMは互いに素）
    def mod_inv(a,M=mod):
        x = extgcd(a,M)[0]
        return (M+x%M)%M
    
    if fact_list and inv_list:
        return (((fact_list[n]*inv_list[k])%M)*inv_list[n-k])%M
    else:
        x = fact(n,M)
        y1 = mod_inv(fact(k,M),M)
        y2 = mod_inv(fact(n-k,M),M)
        return ((x*y1)%M*y2)%M

N = I()
A = list(input())
a = [0]*N
for i in range(N):
    a[i] = int(A[i])

b = [0]*(N-1)
for i in range(N-1):
    b[i] = abs(a[i]-a[i+1])

flag = False
for i in range(N-1):
    if b[i] == 1:
        flag = True
        break

fact_list = fact_all(N-2)
inv_list = fact_inv_all(fact_list)

ans = 0
if not flag:
    for i in range(N-1):
        ans += nCk(N-2,i,fact_list,inv_list)*b[i]
        ans %= 4
    print(ans)
else:
    for i in range(N-1):
        ans += nCk(N-2,i,fact_list,inv_list)*(b[i]%2)
        ans %= 2
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_ = input()
s = "".join([str(int(x)-1) for x in s_])

if len(s) % 2 == 1:
    if "1" not in s:
        if (int(s[0]) - int(s[-1])) == 0:
            print(0)
            exit()
        else:
            print(2)
            exit()

    if abs(int(s[0]) - int(s[-1])) % 2 == 0:
        print(0)
    else:
        print(1)
elif len(s) == 2:
    print(abs(int(s[0])-int(s[1])))
else:
    if "1" not in s:
        if (int(s[0]) + int(s[1]) + int(s[-2]) + int(s[-1])) %2 == 0:
            print(0)
            exit()
        else:
            print(2)
            exit()

    if (int(s[0]) + int(s[1]) + int(s[-2]) + int(s[-1])) %2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input()
print(2)

Submission status: Wrong Answer

Solution code:
def diff(A):
    A = [a for a in A if a!=0]
    m = len(A)
    if not A:
        return 0
    if m==1:
        return A[0]
    return diff([abs(int(A[i])-int(A[i+1])) for i in range(m-1)] )
def main():
    n = int(input())
    A = input()
    A =[abs(int(A[i])-int(A[i+1])) for i in range(n-1)] #0,1,2のいずれか
    print(diff(A))
main()

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *
sys.setrecursionlimit(10 ** 6)

def CalcList(list):
    bfnum = -1
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
        else:
            now = abs(i -bfnum)
            if now >0:
                afterList.append(now)
            bfnum = i
    return afterList

def CheckList(list):
    bfnum = -1
    count = 0
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            if i == bfnum:
                count = count +1
                if count < 3:
                    afterList.append(i)
            else:
                count = 0
                afterList.append(i)
    return afterList


if __name__ == '__main__':
    n = int(input())
    s = input()
    list =list(map(lambda x: int(x), s))

    while(len(list) >1):
        '''
        list =  CheckList(list)
        '''
        list = CalcList(list)

    if len(list) ==0:
        print(0)
    else:
        print(list[0])

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=input()
x=[0,0,0]
for i in range(n):
    p=int(s[i])-1
    x[p]+=1

if x[1]==0:
    a=0
    while True:
        a+=1
    if x[2]>0:
        if x[2]==1: print(2)
        else: print(0)
    else:
        print(0)
    sys.exit()


for i in range(3):
    if x[i]!=0: p=i

if x[p]==1: print(1)
else: print(0)

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *
sys.setrecursionlimit(10 ** 6)

def CalcList(list):
    bfnum = -1
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
        else:
            now = abs(i -bfnum)
            if now >0:
                afterList.append(now)
            bfnum = i
    return afterList

def CheckList(list):
    bfnum = -1
    count = 0
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            if i == bfnum:
                count = count +1
                if count < 3:
                    afterList.append(i)
            else:
                count = 0
                afterList.append(i)
    return afterList


if __name__ == '__main__':
    n = int(input())
    s = input()
    list =list(map(lambda x: int(x), s))

    while(len(list) >1):
        list =  CheckList(list)
        list = CalcList(list)

    if len(list) ==0:
        print(0)
    else:
        print(list[0])

Submission status: Wrong Answer

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())))
    a.sort(reverse=True)

    lis = [0]*(N-2)
    for i in range(N-2):
        lis[i] = abs(a[i]-2*a[i+1]+a[i+2])
    # print(lis)
    ans = 0
    for i in range(N-3):
        if i%2 == 0:
            ans += abs(lis[i]-lis[i+1])
        else:
            ans -= abs(lis[i]-lis[i+1])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    A = []
    for a, b in zip(S, S[1:]):
        A.append(abs(int(a) - int(b)))

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
print(100)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
l=[]
for i in range(n-1):
    if abs(int(s[i+1])-int(s[i]))==0:continue
    if len(l)>=1 and abs(int(s[i+1])-int(s[i]))==l[-1]:continue
    l.append(abs(int(s[i+1])-int(s[i])))
if len(l)==1:print(l[0])
elif len(l)>=3 or len(l)==0:print(0)
else:print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, list(input())))
c = 2
while len(a)>1 and c > 1:
    b = []
    c = 0
    for i in range(1, len(a)):
        if a[i]!=a[i-1]:
            b.append(abs(a[i]-a[i-1]))
            c += 1
        else:
            b.append(0)
    a = b.copy()
print(max(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in list(input())]
first = True
sp = False
if a.count(2) == 0 and a.count(3) > 0 and a.count(1) > 0:
    sp = True
while True:
    take = 2**(len(str(bin(n))[2:])-1)
    for i in range(0, n-take+1):
        if first:
            a.append((a[i:take+i].count(3) + a[i:take+i].count(1))%2)
        else:
            a.append(a[i:take+i].count(1)%2)
    del a[0:n]
    n = n-take+1
    if n == 1:
        break
if sp and a[0] == 0:
    print(2)
else:
    print(a[0])

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *
sys.setrecursionlimit(10 ** 6)

def CalcList(list):
    bfnum = -1
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            now = abs(i -bfnum)
            if now >0:
                afterList.append(now)
            bfnum = i
    return afterList

def CheckList(list):
    bfnum = -1
    count = 0
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            if i == bfnum:
                count = count +1
                if count < 3:
                    afterList.append(i)
            else:
                afterList.append(i)
    return afterList


if __name__ == '__main__':
    n = int(input())
    s = input()
    list =list(map(lambda x: int(x), s))

    while(len(list) >1):
        list =  CheckList(list)
        list = CalcList(list)

    if len(list) ==0:
        print(0)
    else:
        print(list[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

if len(s) % 2 == 1:
    if "1" not in s:
        if (int(s[0]) - int(s[-1])) == 0:
            print(0)
            exit()
        else:
            print(2)
            exit()

    if abs(int(s[0]) - int(s[-1])) % 2 == 0:
        print(0)
    else:
        print(1)
elif len(s) == 2:
    print(abs(int(s[0])-int(s[1])))
else:
    if "1" not in s:
        if (int(s[0]) + int(s[1]) + int(s[-2]) + int(s[-1])) %2 == 0:
            print(0)
            exit()
        else:
            print(2)
            exit()

    if (int(s[0]) + int(s[1]) + int(s[-2]) + int(s[-1])) %2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

def main():
    n =int(input())
    a = list(map(int,input()))
    if n<=10000:
        new = a.copy()
        for i in range(n-1):
            new1 = new.copy()
            for j in range(1,n-i):
                new[j] = abs(new1[j]-new1[j-1])
            # print(new)
            new.pop(0)
            # print(new)
        print(new[0])
    else:
        print(0)





# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    A = []
    for a, b in zip(S, S[1:]):
        A.append(abs(int(a) - int(b)))

    print(abs(A[0] - A[-1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def myfunc(length,a):
    if length > 1:
        b = []
        if length % 2 == 0:
            for i in range(length//2):
                b.append(abs(a[i*2]-a[i*2+1]))
        else:
            for i in range(length-1):
                b.append(abs(a[i]-a[i+1]))
            
        myfunc(len(b),b)
  
    else:
        print(a[0])
        return 
    
n = int(input())
a = input()
c =[]
for i in range(len(a)):
    c.append(int(a[i]))

myfunc(len(c),c)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = input()
x_list = []

pre_num = -1
for v in x:
  v = int(v)
  if pre_num == v:
    continue
  else:
    x_list.append(v)
    pre_num = v
  
while True:
  if len(x_list) <= 1:
    break
  lst = []
  pre_num = -1
  for j in range(len(x_list)-1):
    num = abs(x_list[j]-x_list[j+1])
    if num == pre_num:
      continue
    else:
      lst.append(num)
      pre_num = num
  
  x_list = lst
  
if len(x_list) == 1:
  print(x_list[0])
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input()))
if len(a)==1:
	print(a[0])
	exit()
while a:
	if len(a)==1:
		print(a[0])
		exit()
	p=[];q=[]
	if len(a)>2:
		if a[0]==a[1]:
			p=[0]
		if a[-1]==a[-2]:
			q=[0]
	a=p+[abs(x-y) for x,y in zip(a,a[1:]) if x!=y]+q
print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(input())
for i in range(n):
    A[i]=int(A[i])
if n<=10**3:
    for j in range(n-1):
        for i in range(len(A)-j-1):
            A[i]=abs(A[i]-A[i+1])
    print(A[0])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
A=[map(int, input().split())]

def list(L):
  if len(L)==1:
    print(L[0])
  else:
    m = len(L)
    B=[]
    for i in range(m-1):
      B.append(abs(L[i+1]-L[i]))
    list(B)
list(A)

Submission status: Wrong Answer

Solution code:
def calc(P):
    #Pはリスト
    Q=[]
    for str in A:
        if str=="1":
            pass
        elif str=="2":
            Q.append("1")
        else:
            Q.append("2")
    return Q

def solve(P):
    #Pはリスト
    N=len(P)
    if N>1:
        Q=[]
        for i in range(N-1):
            if   abs( int(P[i])-int(P[i+1]) )==0:
                pass
            elif abs( int(P[i])-int(P[i+1]) )==1:
                Q.append("1")
            else:
                Q.append("2")
        return solve(Q)
    elif N==0:
    	return(0)
    else:
        return P[0]

N=int(input())
A=input()
B=list(A)

#print(A)
#print(B) 
short=calc(B)
#print(short)

ans=solve(short)
print(ans)