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:
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])
    sys.exit()

def in2(x):
    if x==0:
        return 0
    ANS=0
    while x%2==0:
        ANS+=1
        x//=2
    return ANS


if not (1 in A):
    LEN=len(A)
    SUM=0
    COMBI=1

    for i in range(LEN):
        if COMBI==0:
            SUM+=A[i]//2

        COMBI=COMBI+in2(LEN-1-i)-in2(i+1)

    if SUM%2==1:
        print(2)
    else:
        print(0)
else:
    LEN=len(A)
    SUM=0
    COMBI=1

    for i in range(LEN):
        if COMBI==0:
            SUM+=A[i]

        COMBI=COMBI+in2(LEN-1-i)-in2(i+1)

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

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

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

# nC0~nCn odd or even
def nCk(n):
    num = [0]*(n+1)
    for i in range(n+1):
        now = 2
        while now <= i:
            num[i] += i//now
            now *= 2
    cumnum = [num[0]]
    for i in range(1,n+1):
        cumnum.append(cumnum[-1]+num[i])
    ret = [0]*(n+1)
    for i in range(n+1):
        if cumnum[n] > cumnum[i]+cumnum[n-i]:
            ret[i] = 0
        else:
            ret[i] = 1
    return ret

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

nlist = nCk(N-2)

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

Submission status: Wrong Answer

Solution code:
i=int(input())
s=input()
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=[]
  for k in range(len(a)-1):
    d=0
    b=abs(a[k]-a[k+1])
    if k>0:
      if d == 0:
        if b == 0:
          continue
        else:
          d=1
    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: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

mod = 10**9+7 #出力の制限

# a = cmb(n,r)
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()
N = n
def cmb(n, r):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
def p(n,r):
    if ( r<0 or r>n ):
        return 0
    return g1[n] * g2[n-r] % mod

g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )
a = list(input())

po = 0
for i in range(n):
    po = abs(po-cmb(n,i)*(int(a[i])))
    po %= 3
print(-po%3)

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)


def chanto(arr_rev):
    inp = arr_rev
    diff_1 = np.array([calc(inp[x], inp[x + 1]) for x in range(len(inp) - 1)])
    diff_1_nonz = diff_1[diff_1 > 0]
    
    if len(diff_1_nonz) == 0:
        return 0
    if len(diff_1_nonz) == 1:
        return diff_1_nonz[0]

    return (diff_1_nonz[0] + diff_1_nonz[len(diff_1_nonz) - 1]) % 2


print(chanto(arr_rev))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x)-1 for x in input()]

am = [a[i] % 2 for i in range(n)]


tmp = 0
ans = am[0] % 2

for i in range(1, n):
    ind = 0
    k = (n-i) & -(n-i)
    while k > 1:
        k = k // 2
        ind += 1
    kk = i & -i
    while kk > 1:
        kk = kk // 2
        ind -= 1
    tmp += ind
    if tmp == 0:
        ans += am[i]
        ans %= 2

print(ans)

Submission status: Wrong Answer

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

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

step = 1
length = N
total = 0

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

print(abs(b - a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,list(input())))
cmb = [1]
x = 1
for i in range(1,n):
    x = x*(n-i)//i
    cmb.append(x)
ans = 0
for i in range(n):
    if i % 2 == 0:
        ans += a[i] * cmb[i]
    else:
        ans -= a[i] * cmb[i]
ans %= 4
if ans < 0:
    ans += 4
if ans == 0:
    print(0)
elif ans == 2:
    print(2)
else:
    print(1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import numpy as np


def solve(*args: str) -> str:
    n = int(args[0])
    A = np.array([int(a) for a in args[1]])

    for i in range(n-1):
        A = np.abs(A[:-1]-A[1:])
        if np.max(A) <= 1:
            A[0] ^= sum(A[1::2]) % 2
            break

    return str(A[0])


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):
  # sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(a)
      a=x
      c=1
  res.append(a)

  return res
# Summarize count of factor within list --- END ---

def main():
  n=I()
  _l=list(S())

  if n==2:
    return abs(int(_l[0])-int(_l[1]))

  l=[]
  for i in range(n-1):
    x=abs(int(_l[i+1])-int(_l[i]))
    if x!=0:
      l.append(x)

  if len(l)==0:
    return 0

  ll=l

  # # TLE
  # while True:
  #   if len(l)==1:
  #     # return l[0]
  #     print(l[0])
  #     break
  #   a=[]
  #   x=-1
  #   for i in range(len(l)-1):
  #     b=abs(int(l[i+1])-int(l[i]))
  #     if x!=b:
  #       a.append(b)
  #   l=a

  ans=summarize_list(ll)
  # print(ans)
  # print(ll)

  if len(ans)==2:
    return 1
  return 0

# main()
print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
a = input()
count = 0
flag = 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
        else:
            if now != chk:
                count += 1
                now = chk
            else:
                flag = 1
if count == 0:
    print(0)
elif count == 1:
    if flag == 0:
        print(now)
    else:
        print(0)
else:
    if count == 2:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
num_list = list(map(int,input()))
num_list2 = []
for i in range(N):
    if(i != 0):
        if(len(num_list2)==0 or len(num_list2)==1):
            break
        num_list = copy.copy(num_list2)
        num_list2 = []
    for j in range(len(num_list)-1):
        num = num_list[j] - num_list[j+1]
        if num == 0:
            continue
            
        num_list2.append(abs(num))
        
if(len(num_list2)==0):
    print(0)
else:
    print(num_list2[0])

Submission status: Wrong Answer

Solution code:
input()
z=list(map(int,input().split()))
while len(z)!=1:
    t=[]
    for x,y in zip(z[:-1],z[1:]):
        t+=[abs(x-y)]
    z=t
print(*z)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(input())
for i in range(N):
    a[i]=int(a[i])
#print(a)
while len(a)>2:
    num=a[0]
    A=[]
    for i in range(1,len(a)):
        if i==1:
            A.append(abs(a[i]-a[i-1]))
            num=a[i]
        if i==len(a)-1:
            A.append(abs(a[i]-a[i-1]))
        elif a[i]!=num:
            A.append(abs(a[i]-num))
            num=a[i]
    #print(A)
    a=A
print(abs(a[0]-a[1]))

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)


def chanto(arr_rev):
    inp = arr_rev
    diff_1 = np.array([calc(inp[x], inp[x + 1]) for x in range(len(inp) - 1)])
    diff_1_nonz = diff_1[diff_1 > 0]
    if len(diff_1_nonz) == 0:
        return 0

    return (diff_1_nonz[0] + diff_1_nonz[len(diff_1_nonz) - 1]) % 2


print(chanto(arr_rev))

Submission status: Wrong Answer

Solution code:
N=input()
N = int(N)
a = input()
b = list(a)
c =list(map(int,b))
d= []
for s in range(N-1):
  r = abs(c[s] -c[s+1])
  d.append(r)
for i in range(N-2):
  for p in range(N-i):
    
    l = abs(d[-N+1] - d[-N+2])
    d.append(l)
print(d[-1])

Submission status: Wrong Answer

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

print(3)

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(abs(l[0]-l[1]))

Submission status: Wrong Answer

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

tri = [A]
while len(tri[-1]) > 1:
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        _abs = abs(nw[j]-nw[j-1])
        if _abs:
            nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)

if tri[-1] == []:
    print(0)
else:
    if len(tri[-1]) == 2:
        print(tri[-1][0]-tri[-1][1])
    else:
        print(tri[-1][0])

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(input())
for i in range(N):
    a[i]=int(a[i])
#print(a)
while len(a)>2:
    num=a[0]
    A=[]
    for i in range(1,len(a)):
        if i==1:
            A.append(abs(a[i]-a[i-1]))
        if i==len(a)-1:
            A.append(abs(a[i]-a[i-1]))
        elif a[i]!=num:
            A.append(abs(a[i]-num))
            num=a[i]
    #print(A)
    a=A
print(abs(a[0]-a[1]))

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())

a = list(map(int,list(sys.stdin.readline().strip())))

b = []
for i in a:
	if b==[]:
		b.append(i)
		tmp=i
	elif tmp!=i:
		b.append(i)
N = len(b)
while(len(b)>1):
	b = [abs(a[i]-a[i+1]) for i in range(N-1)]
	N -= 1


print(b[0])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input()))
if n>100:
    print(0)
    exit()
for i in range(n-1):
    b=[]
    for j in range(n-i-1):
        b.append(abs(a[j]-a[j+1]))
    a=b
print(a[0])

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    a = input()
    l = N
    while l > 1:
        b = ""
        for i in range(l-1):
            s,t = int(a[i]),int(a[i+1])
            if s == t:continue
            else:u = abs(s-t);b+=str(u)
        l = len(b)
        if l == 0:print(0);exit()
        a = b
    print(int(a))

if __name__ == "__main__":
    main()

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)


def chanto(arr_rev):
    inp = arr_rev
    diff_1 = np.array([calc(inp[x], inp[x + 1]) for x in range(len(inp) - 1)])
    diff_1_nonz = diff_1[diff_1 > 0]
    if len(diff_1_nonz) == 0:
        return 0

    return diff_1_nonz[0] + diff_1_nonz[len(diff_1_nonz) - 1] % 2


print(chanto(arr_rev))

Submission status: Wrong Answer

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

print(1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = int(input())


L = [int(s) for s in input()]

a = np.array(L)
for i in range(N-1):
  a = np.abs(np.diff(a))
  a = a[np.nonzero(a)] 
  if len(a) == 1 or len(a) == 0:
    break
try:
  print(a[0])
except:
  print(0)

Submission status: Wrong Answer

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

print(0)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import numpy as np

n = ni()
a = list(map(int, list(ns())))
#n = 10**5
#a = [2]*(10**5)
b = np.array(a)

if n > 10*4:
    print(0)
    exit()

for i in range(n-1):
    b = np.abs(b[1:]-b[:-1])

print(b[0])

Submission status: Wrong Answer

Solution code:
N=int(input())
a=str(input())
if N==2:
  print(abs(int(a[0])-int(a[1])))
else:
  v1=abs(int(a[0])-int(a[1]))
  v2=abs(int(a[-1])-int(a[-2]))
  print(abs(v1-v2))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = input()
count = 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
        else:
            if now != chk:
                count += 1
                now = chk
if count == 0:
    print(0)
elif count == 1:
    print(now)
else:
    if count == 2:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

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

tri = [A]
while len(tri[-1]) > 1:
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        _abs = abs(nw[j]-nw[j-1])
        if _abs:
            nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)

if tri[-1] == []:
    print(0)
else:
    print(tri[-1][0])

Submission status: Wrong Answer

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

if len(a) == 1 :
    print(a)
    exit()
elif len(set(a)) == 1 :
    print(0)
    exit()

b = ""
for i in range(1,len(a)) :
    if a[i] != a[i-1] :
        b += str(abs(int(a[i]) - int(a[i-1])))

if len(b) == 1 :
    print(b)
    exit()
elif len(set(b)) == 1 :
    print("0")
    exit()

c = 0
for i in range(1,len(b)) :
    if b[i] != b[i-1] :
        c += 1

if c == 1 :
    print(1)
else :
    print(0)

Submission status: Wrong Answer

Solution code:
def diff(A):
    if len(A)==1:
        return A[0]
    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 copy
N = int(input())
num_list = list(map(int,input()))
num_list2 = []
for i in range(1,N):
    if(len(num_list2)==0 or len(num_list2)==1):
        break
    if(i != 1):
        num_list = copy.copy(num_list2)
        num_list2 = []
    for j in range(len(num_list)-1):
        num = num_list[j] - num_list[j+1]
        if num == 0:
            continue
            
        num_list2.append(abs(num))
        
if(len(num_list2)==0):
    print(0)
else:
    print(num_list2[0])

Submission status: Wrong Answer

Solution code:
num = int(input())
a = list(input())
a_i = [int(i) for i in a]

def calcabs(list):
    result = []
    for x in range(len(list)):
        if x == 0: continue
        elif abs(list[x] - list[x - 1]) == 0: continue
        else: 
            val = abs(list[x] - list[x - 1])
            if len(result) == 0: result.append(val)
            else: result.append(val)
    return result

while len(a_i) > 1:
    a_i = calcabs(a_i)
if len(a_i) == 0: print(0)
elif len(a_i) == 1: print(a_i[0])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)
N= int(input())
an=  [int(x) for x in list(input())]

#print(an)
#print(len(an))

#if len(an)==1:
#    print(an[0])
#    exit()

ans_array=[]
def calc(prm):
    global ans_array
    bn=[]
    hasZero=False
    hasOne=False
    hasTwo=False
    threeKind = False
    for i in range(len(prm)-1):
        b = abs(prm[i]-prm[i+1])
        if b==0:
            hasZero=True
        elif b==1:
            hasOne=True
        else:
            hasTwo=True
        bn.append(b)
    if len(bn)==1:
        #print("要素数が1だよ")
        #print(bn)
        ans_array=bn
        return
    elif hasZero and hasOne and hasTwo:
        threeKind=True
        #print("3種類あるよ")
        #print(bn)
        calc(bn)
    else:
        #print("2種類以下だよ")
        #print(bn)
        ans_array=bn
        return
calc(an)

if len(ans_array)==1:
    print(ans_array[0])
else:
    appears=[]
    num_zero = ans_array.count(0)
    num_one = ans_array.count(1)
    num_two = ans_array.count(2)
    appears.append(num_one)
    appears.append(num_zero)
    appears.append(num_two)
    appears.sort()
    minor = appears[1]
    if (len(ans_array)%2==0) and (minor%2==0):
        print(0)
    elif (len(ans_array)%2==1) and (minor%2==1):
        print(0)
    else:
        if num_zero==0:
            print(1)
        elif num_one==0:
            print(2)
        else:
            print(1)

Submission status: Wrong Answer

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

D = [0] * (N - 1)

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

L = list(set(D))
L.sort()

if len(L) == 2:
  if L[0] == 0 and L[1] == 2:
    print(2)
    exit()

if abs(D[0] - D[-1]) == 1:
  print(1)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
num = int(input())
a = list(input())
a_i = [int(i) for i in a]

def calcabs(list):
    result = []
    for x in range(len(list)):
        if x == 0: continue
        elif abs(list[x] - list[x - 1]) == 0: continue
        else: 
            val = abs(list[x] - list[x - 1])
            if len(result) == 0: result.append(val)
            else: result.append(val)
    return result

while len(a_i) > 1:
    print(a_i)
    a_i = calcabs(a_i)
if len(a_i) == 0: print(0)
elif len(a_i) == 1: print(a_i[0])

Submission status: Wrong Answer

Solution code:
print("0")

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,list(input())))
cmb = [1]
x = 1
for i in range(1,n):
    x = x*(n-i)//i
    cmb.append(x)
print(cmb)
ans = 0
for i in range(n):
    if i % 2 == 0:
        ans += i * cmb[i]
    else:
        ans -= i * cmb[i]
ans %= 4
if ans < 0:
    ans += 4
if ans == 0:
    print(0)
elif ans == 2:
    print(2)
else:
    print(1)

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):
  # sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(a)
      a=x
      c=1
  res.append(a)

  return res
# Summarize count of factor within list --- END ---

def main():
  n=I()
  _l=list(S())

  if n==2:
    return abs(int(_l[0])-int(_l[1]))

  l=[]
  for i in range(n-1):
    x=abs(int(_l[i+1])-int(_l[i]))
    l.append(x)

  if len(l)==0:
    return 0

  ll=l

  # # TLE
  # while True:
  #   if len(l)==1:
  #     # return l[0]
  #     print(l[0])
  #     break
  #   a=[]
  #   x=-1
  #   for i in range(len(l)-1):
  #     b=abs(int(l[i+1])-int(l[i]))
  #     if x!=b:
  #       a.append(b)
  #   l=a

  ans=summarize_list(ll)
  # print(ans)
  # print(ll)
  # if len(ans)==1:
  #   return ans[0]

  if len(ans)%2==0:
    return 1
  return 0

# main()
print(main())

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = list(map(lambda x: int(x)-1, list(input())))

def parity(n, r):
    # the parity of n choose r
    # via https://math.stackexchange.com/questions/233269/odd-and-even-numbers-in-pascals-triangle-sierpinskis-triangle
    if n == 1:
        return 1
    if r == 1:
        return n % 2
    if r == 0:
        return 1

    if n % 2 == 0:
        if r % 2 != 0:
            return 0
        else:
            return parity(n//2, r//2)
    else:
        return parity(n//2, r//2)

ans = 0
for i, a in enumerate(aa):
    if parity(n-1, i) == 1:
        ans = ans - a
        if ans < 0:
            ans *= -1
print(ans)

Submission status: Wrong Answer

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

def mod2_pascal(n):
    res = []
    for r in range(n+1):
        res.append(1 if n == (r | (n-r)) else 0)
    return res

b = []
if N >= 3:
    for i in range(N-1):
        b.append(abs(a[i]-a[i+1])%2)
    mod2_line = mod2_pascal(N-2)
    ans = 0
    for i in range(N-1):
        if mod2_line[i] == 1:
           ans += b[i]
    print(ans%2)
else:
    print(abs(a[1]-a[0]))

Submission status: Wrong Answer

Solution code:
N=input()
N = int(N)
a = input()
b = list(a)
c =list(map(int,b))
d= []
for s in range(N-1):
  r = abs(c[s] -c[s+1])
  d.append(r)
for i in range(N-2):
  for p in range(N-i):
    l = abs(d[-p] - d[-p+1])
    d.append(l)
print(d[-1])

Submission status: Wrong Answer

Solution code:
n = int(input())
string = input()
a = []
for i in range(n):
    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 = []

    a_next.append(a[0])
    for i in range(1, len(a)-1):
        if a[i] != 0:
            a_next.append(a[i])
    a_next.append(a[-1])

    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:
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**17+3

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())
A = int(input())

num = [int(x) for x in list(str(A))]
length = len(num)
count_stopper = len(num)

print(num)

cnt = 1
a = 0
big_count = 0
new = []
while big_count != length-1:
    while cnt != count_stopper:
        a = 0
        a = num[cnt - 1] - num[cnt]
        new.append(abs(a))
        cnt += 1
    cnt = 1
    num = new
    new = []
    big_count += 1
    count_stopper -= 1

print(num[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = input().strip()
al = list(map(int, a))
al2 = [abs(al[i] - al[i + 1]) for i in range(len(al) - 1)]
while len(al2) > 1:
    al2 = [abs(al2[i] - al2[i + 1]) for i in range(len(al2) - 1) if abs(al2[i] - al2[i + 1]) > 0]
print(al2[0] if len(al2) else 0)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_ = list(map(int, list(str(input()))))
a = []
if N == 2:
  print(abs(a_[0]-a_[1]))
else:
  for i in range(1, len(a_)):
    a.append(abs(a_[i]-a_[i-1])%2)
  n = (N-1)//2

  print((sum(a[:n])+sum(a[-n:]))%2)

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
num_list = list(map(int,input()))
num_list2 = []
for i in range(1,N):
    if(len(num_list2)==1):
        break
    if(i != 1):
        num_list = copy.copy(num_list2)
        num_list2 = []
    for j in range(len(num_list)-1):
        num = num_list[j] - num_list[j+1]
        if num == 0:
            continue
            
        num_list2.append(abs(num))
        
if(len(num_list2)==0):
    print(0)
else:
    print(num_list2[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(input())
for i in range(N):
    a[i]=int(a[i])
#print(a)
while len(a)>=2:
    num=a[0]
    A=[]
    for i in range(1,len(a)):
        if i==len(a)-1:
            A.append(abs(a[i]-a[i-1]))
        elif a[i]!=num:
            A.append(abs(a[i]-num))
    #print(A)
    a=A
if len(a)==0:
    print(0)
else:
    print(a[0])

Submission status: Wrong Answer

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

tri = [A]
while len(tri[-1]) > 1:
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        _abs = abs(nw[j]-nw[j-1])
        if abs:
            nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)
    if 2 not in nx:
        break

if len(tri[-1]) == 1:
    print(tri[-1][0])
    exit()

if len(tri[-1]) % 2 == 0:
    tar = tri[-2]
else:
    tar = tri[-1]

if tar[0] ^ tar[1]:
    print(1)
else:
    print(0)

Submission status: Wrong Answer

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

def solve(P):
    #Pはリスト
    #print(P)
    N=len(P)
    if N>1:
        Q=[]
        for i in range(N-1):
            if   abs( int(P[i])-int(P[i+1]) )==0:
                if i==0 or i==N-2:
                    Q.append("0")
                else:
                    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)