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: Time Limit Exceeded

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = input()
    a = input()
    pl = 7

    ptns = tuple(itertools.product([0, 1, 2, 3], repeat=pl))

    calc = dict()
    temp = [0] * pl

    for ptn in ptns:
        k = ptn
        temp = [_ for _ in k]
        for i in range(pl-1,0,-1):
            for j in range(i):
                temp[j] = abs(temp[j] - temp[j + 1])
        nm = "".join(str(x) for x in k)
        calc[nm] = str(temp[0])

    l = len(a)

    while l > pl:
        templ = [0] * (l + 1 - pl)

        for ii in range(l + 1 - pl):
            pp = a[ii:ii + pl]
            templ[ii] = calc[pp]

        a = "".join(templ)
        l = len(a)

    temp = [int(_) for _ in a]
    for i in range(l-1, 0, -1):
        for j in range(i):
            temp[j] = abs(temp[j] - temp[j + 1])

    print(temp[0])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
s=input()
nums=[int(i) for i in s]
while len(nums)>1:
  nums=''.join(map(str, [abs(nums[i+1]-nums[i]) for i in range(len(nums)-1)]))
  nums.strip('0')
  if nums=='':
    print(0)
    exit()
  nums=list(map(int, list(nums)))
print(nums[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(4100000)

def calc(l, j, N):
    tmp = []
    for index in range(len(l)):
        if index == len(l) - 1:
            break
        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: Time Limit Exceeded

Solution code:
def main():
  # -*- coding: utf-8 -*-
  import math
  from functools import lru_cache
  
  n = int(input())
  nums = int(input())
  str_list = list(str(nums))
  nums_list = [ int(i) for i in str_list ]
  tuples = tuple(nums_list)
 
  @lru_cache(maxsize=100000)
  def keisan(t):
    nums_list = list(t)
    result = [ abs(nums_list[i] - nums_list[i+1]) for i in range(len(nums_list)-1) ]
    if len(result) == result.count(result[0]) and not len(result) == 1:
      tmp = []
      tmp.append(0)
      return tmp
    if len(result) > 1:
      return keisan(tuple(result))
    else:
      return result
    
  nums_list = keisan(tuples)
  print(nums_list[0])
    
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(input())
x=[[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N-i):
        if i==0:
            x[i][j]=int(A[j])
        else:
            x[i][j]=abs(x[i-1][j]-x[i-1][j+1])
print(x[-1][0])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from itertools import product
from math import log2

N = int(input())
A = tuple(int(c) - 1 for c in input())

memo = defaultdict(dict)
for x in product([0, 1, 2], repeat = 3):
  memo[3][x] = abs(abs(x[1]-x[0])-abs(x[2]-x[1]))

def f(t):
  if t not in memo[len(t)]:
    l = (len(t)-1) // 2 + 1
    memo[len(t)][t] = f(tuple(f(t[i:i+l]) for i in range(l)))
  return memo[len(t)][t]

def g(t):
  if len(t) == 1:
    return t[0]
  elif len(t) == 2:
    return abs(t[1]-t[0])
  else:
    n = 2**int(log2(len(t)-1)) + 1
    return g(tuple(f(t[i:i+n]) for i in range(len(t) - n + 1)))

print(g(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def loop(array):
    if len(array) == 2:
        return np.abs(np.diff(array))
    return loop(np.abs(np.diff(array)))

if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input()))
    a = np.array(a)
    lst = loop(a)
    print(lst[0])

Submission status: Time Limit Exceeded

Solution code:
def calc(array, N):
    for i in range(N-1):
        array = [abs(x-y) for x, y in zip(array[:-1], array[1:])]
    return array[0]
def main():
    N = int(input())
    array = [0 for i in range(N)]
    s = input()
    for j in range(N):
        array[j] = int(s[j])
    print(calc(array, N))
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ln = [int(el) for el in s]

#def compac( lst , nm ):
#  for j in range(nm ):
#

cp = 0   
k = n - 1
for i in range(n-1):
  #print(ln,k)
  l = 0
  jk = k
  for j in range(k):
    if ln[j] == ln[j+1] :
      ln[l] = 0
      if cp == 1 :
        l = l -1
        jk = jk -1
      cp = 1
    else:
      ln[l] = abs( ln[j] - ln[j+1] )
      cp = 0
    l = l + 1
  for m in range(k,n):
    ln[m] = 9
  k = jk -1
  if k == 0 :
    break
#print(ln,k)
print(ln[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_string = input()
temp = []
for s in a_string:
    temp.append(int(s))

for i in range(n-1):
    len_temp = len(temp)
    pre = temp
    temp = []
    for j in range(len_temp-1):
        temp.append(abs(pre[j] - pre[j+1]))
    

print(temp[0])

Submission status: Time Limit Exceeded

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

def make_x(l):
	n = len(l)
	if n == 1:
		return l
	l2 = [l[0]]
	cnt = 1
	length = 1
	for i in range(1, n):
		if l[i] != l2[-1]:
			if cnt % 2 == 0:
				l2.append(l2[-1])
			cnt = 1
			l2.append(l[i])
			length += 1
		else:
			cnt += 1
	if length == 1:
		return [0]
	return l2

def next_x(l):
	n = len(l)
	return [max(l[i], l[i+1]) - min(l[i], l[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

if f1:
	x = [n if n != 2 else 0 for n in x]
while len(x) > 1:
	x = make_x(next_x(x))
print(x[0])

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = input().rstrip()
    b = a[0]
    for i in range(1, n):
        #print(b)
        c = a[i]
        b += c
        for j in reversed(range(len(b)-1)):
            b = b[:j] + str(abs(int(b[j]) - int(b[j+1]))) + b[j+1:]
        l = 1
        f = False
        r = 1
        while r < len(b):
            if f and b[r] != "0":
                if r - l > 3:
                    b = b[:l] + b[r:]
                f = False
            elif not f and b[r] == "0":
                l = r
                f = True
            r += 1
    #print(b)
    print(b[0])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
s=input()
nums=[int(s[i]) for i in range(N)]
while len(nums)>1:
  nums=''.join(map(str, [abs(nums[i+1]-nums[i]) for i in range(len(nums)-1)]))
  nums.strip('0')
  nums=[int(i) for i in nums]
print(nums[0])

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    num = [int(x) for x in list(str(int(input())))]
    length= len(num)
    count_stopper = length
    cnt = 1
    big_count = 0
    while big_count != length-1:
        new = []
        while cnt != count_stopper:
            a = 0
            a = num[cnt - 1] - num[cnt]
            new.append(abs(a))
            cnt += 1
        cnt = 1
        num = new
        big_count += 1
        count_stopper -= 1
    print(num[0])
main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
queue = list(input())
num_queue = [int(i) for i in queue]
ans_queue = []
for i in range(N - 1):
	for j in range(len(num_queue) - 1):
		ans_queue.append(abs(num_queue[j] - num_queue[j + 1]))
    
	num_queue = ans_queue
	ans_queue = []
print(num_queue[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
mat_dat = [[0 for i in range(N)] for j in range(N)]

# 1項目
dat = list(map(int, input()))
for i in range(N):
    mat_dat[0][i] = dat[i]

# 2項目~
for j in range(1, N):
    for i in range(N-1):
        mat_dat[j][i] = abs(mat_dat[j-1][i] - mat_dat[j-1][i+1])

print(mat_dat[N-1][0])

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np

N = int(input())
seq = list(map(int, input()))
seq = np.array(seq)
def get_sub_seq(li1):
    li2 = copy.copy(li1)
    li1 = np.delete(li1, -1)
    li2 = np.delete(li2, 0)
    new = li1 - li2
    new = list(map(abs, new))
    return new
while len(seq) > 1:
    seq = get_sub_seq(seq)
print(seq[0])

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = input()
    a = input()

    ptns = tuple(itertools.product([0, 1, 2, 3], repeat=5))

    calc = dict()
    temp = [0] * 5

    for ptn in ptns:
        k = ptn
        temp = [_ for _ in k]
        for i in range(4,0,-1):
            for j in range(i):
                temp[j] = abs(temp[j] - temp[j + 1])
        nm = "".join(str(x) for x in k)
        calc[nm] = str(temp[0])

    l = len(a)
    tempn = ""

    while l > 5:
        tempn = ""

        for ii in range(l - 4):
            pp = a[ii:ii + 5]
            tempn += calc[pp]

        a = tempn
        l = len(a)

    temp = [int(_) for _ in a]
    for i in range(l-1, 0, -1):
        for j in range(i):
            temp[j] = abs(temp[j] - temp[j + 1])

    print(temp[0])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def c(s):
    result = ''
    for i in range(len(s) - 1):
        result += str(abs(int(s[i]) - int(s[i + 1])))
    return result.strip('0')


while len(a) > 1:
    a = c(a)

if len(a) == 0:
    print(0)
else:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
def main():
    l = int(input())
    li = []
    b = input()
    for i in range(l):
      li.append(int(b[i]))
    for j in range(l):
      for i in range(l-j-1):
        li[i] = abs(li[i] - li[i+1])
    print(li[0])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(1000000000)
N = int(input())
numbers = input()
input_arr = np.array([int(i) for i in numbers])
# input_arr = np.random.choice(3, 1000)


def memoize(f): #メモ化関数
    table = {}
    def func(*args):
        if not len(args) in table:
            table[len(args)] = f(*args)
        return table[len(args)]
    return func

@memoize
def myfunc(arr):
    if(len(arr)) == 1:
        return arr[0]
    return myfunc(np.abs(arr[1:] - arr[:-1]))

print(myfunc(input_arr))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
a = [0]*N
for k in range(N):
    a[k] = A[k]
for i in range(N-1):
    b = [0] * (N-1-i)
    for j in range(N-1-i):
        b[j] = abs(int(a[j]) - int(a[j+1]))
    a = b
print(a[0])

Submission status: Time Limit Exceeded

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

def minus(x,y):
    B = []
    for i in range(y-1):
        B.append(abs(x[i])-abs(x[i+1]))
    return B

for n in range(N-1):
    A = minus(A,N-n)

print(abs(int(A[0])))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


def calc(A):
    B = np.abs(A[1:] - A[:-1])
    return B


N = int(input())
A = input()

A = np.array([int(a) for a in A])
for i in range(N - 1):
    A = calc(A)
    # print(A)
    if A[0] == 1 and np.sum(A[1:]) == 0:
        print(1)
        sys.exit()
    elif np.sum(A) == 0:
        print(0)
        sys.exit()
print(A[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
b = str(input())
int_list =list(map(int, list(b)))
arr = np.array(int_list)
for k in range(N-1):
    arr = np.delete(abs(arr-np.delete(np.concatenate([[0], arr]),-1,0)),0,0)

print(arr[0])

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
data = [int(s) for s in input()]

while len(data) > 1:
    data = [abs(a - b) for a, b in zip(data[:-1], data[1:])]
        
print(data[0])

Submission status: Time Limit Exceeded

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

pre_num = -1
con_flg = False
first_flg = True
count = 0

for i,v in enumerate(x):
  v = int(v)
  if pre_num == v:
    if i == 1 or i == n-1 or first_flg:
    	x_list.append(v)
    count += 1
    continue
  else:
    x_list.append(v)
    if len(x_list) >= 2:
      first_flg = False
    pre_num = v

if count == n-1:
  con_flg = True
  
while True:
  if len(x_list) <= 1:
    if con_flg:
      x_list[0] = 0
    break
  lst = []
  pre_num = -1
  count = 0
  first_flg = True
  for i,j in enumerate(range(len(x_list)-1)):
    num = abs(x_list[j]-x_list[j+1])
    if num == pre_num:
      if i == 1 or i == len(x_list)-2 or first_flg:
        lst.append(num)
      count += 1
      continue
    else:
      lst.append(num)
      if not first_flg and lst>=2:
        first_flg = False
      pre_num = num
      
  if count == len(x_list)-2 and len(x_list) != 2:
    con_flg = True
    
  x_list = lst
  
if len(x_list) == 1:
  print(x_list[0])
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N = int(input())
a = np.array([int(x) for x in list(input().strip())])

while len(a) > 1:
    b = a[:-1] - a[1:]
    a = np.apply_along_axis(abs, 0, b)

if len(a):
    print(a[0])
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
num = map(int, list(input()))
input_list = list(map(int, list(input())))


def check(check_list):
    #print(check_list)
    if len(check_list) == 2:
        print(abs(check_list[0]-check_list[1]))
        #return(abs(check_list[0]-check_list[1]))
    else:
        next_check_list = []
        count = 0
        for l in check_list:
            if count == 0:
                pass
            else:
                next_check_list.append(abs(check_list[count - 1] - check_list[count]))
            count += 1
        check(next_check_list)

check(input_list)

Submission status: Time Limit Exceeded

Solution code:
def main():
  # -*- coding: utf-8 -*-
  import math
  from functools import lru_cache
  
  n = int(input())
  nums = int(input())
  str_list = list(str(nums))
  nums_list = [ int(i) for i in str_list ]
  tuples = tuple(nums_list)

  @lru_cache(maxsize=100000)
  def keisan(t):
    nums_list = list(t)
    result = [ abs(nums_list[i] - nums_list[i+1]) for i in range(len(nums_list)-1) ]
    if len(result) == result.count(result[0]) and not len(result) == 1:
      tmp = []
      tmp.append(0)
      return tmp
    if len(result) > 1:
      return keisan(tuple(result))
    else:
      return result
    
  nums_list = keisan(tuples)
  print(nums_list[0])
    
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def calc(l, j, N):
    tmp = []
    for index in range(len(l)):
        if index == len(l) - 1:
            break
        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: Time Limit Exceeded

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(1000000)
N = int(input())
numbers = input()
input_arr = np.array([int(i) for i in numbers])

def memoize(f): #メモ化関数
    table = {}
    def func(*args):
        if not len(args) in table:
            table[len(args)] = f(*args)
        return table[len(args)]
    return func

@memoize
def myfunc(arr):
    if(len(arr)) == 1:
        return arr[0]
    # print(arr)
    return myfunc(np.abs(arr[1:] - arr[:-1]))

print(myfunc(input_arr))

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    iS = input().rstrip()

    S = []
    for i in range(N-1):
        S.append(abs(int(iS[i])-int(iS[i+1])))
    N -= 1

    f = FactorialUtils(N)
    n1 = n2 = 0
    for i in range(N):
        t = f.choose(N-1,i)
        #print(t,S[i])
        if S[i] == 1: n1 += t
        elif S[i] == 2: n2 += t
    if n1&1 and n2&1: print(1)
    elif n1&1: print(1)
    elif n2&1: print(2)
    else: print(0)
    #print(n1,n2)

class FactorialUtils:
    def __init__(self, n):
        self.fac = [1] * (n+1)
        for i in range(2, n+1): self.fac[i] = self.fac[i-1] * i

    def choose(self, n, r):
        if r < 0 or r > n: return 0
        return (self.fac[n] // self.fac[n-r]) // self.fac[r]

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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=[]
  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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
 
N=int(input())
A = input()
a = np.array([int(A[i]) for i in range(N)])
while len(a) >1:
  a = np.absolute(np.diff(np.absolute(a),1))
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
def diff(A):
    m = len(A)
    if m==1:
        return A[0]
    A = [A[i] for i in range(m-1) if not A[i]==A[i+1]==0] + [A[-1]]
    #print(A)
    m = len(A)
    if m==1: return A[0]
    if not A:
        return 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: Time Limit Exceeded

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)

if n > 1000:
    #a = a[:1000]
    #n = 1000
    print(1)

b = np.array(a)

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

print(b[0])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a_s=list(input())
a=[int(i) for i in a_s]
b=[]
for i in range(N-1):
  if i%2==0:
    for j in range(len(a)-1):
      b.append(abs(a[j]-a[j+1]))
    a=[]
  else:
    for j in range(len(b)-1):
      a.append(abs(b[j]-b[j+1]))
    b=[]

if N%2==0:
  print(b[0])
else:
  print(a[0])

Submission status: Time Limit Exceeded

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

num = 1

while num <= N-1:
    for i in range(N-num):
        min = abs(l[i] - l[i+1])
        del l[0]
        l.append(min)
    num+=1

ans = l[-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=list(str(input()))
b=list(reversed(a))

l=len(a)
if a[0:l//2]==b[0:l//2]:
  print(0)
  exit()

def x_number(s):
  if len(s) > 1:
    X=[]
    l=len(s)-1
    for i in range(l):
      x=abs(int(s[i])-int(s[i+1]))
      X.append(x)
  else:
    return s[0]
  return x_number(X)

print(x_number(a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=int(input())
lista=[]
for i in range(N):
  lista.append(a%10)
  a=a//10
for i in range(N-1):
  listb=[]
  for j in range(N-i-1):   
    listb.append(abs(lista[j+1]-lista[j]))
  lista=listb
print (listb[0])

Submission status: Time Limit Exceeded

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

def calcabs(list):
    result = []
    tempcount = 0
    for x in range(len(list)):
        val = abs(list[x] - list[x - 1])
        if x == 0: continue
        elif val == 0: 
            tempcount += 1
        else: 
            if len(result) == 0: result.append(val)
            else: 
                for x in range(tempcount):
                    result.append(0)
                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: Time Limit Exceeded

Solution code:
#
import math


def main():
    n = int(input())
    a = input()
    array = list(map(int, a))
    for i in range(1, n):
        for j in range(n - i):
            array[j] = abs(array[j] - array[j + 1])
    print(array[0])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    N = int(input())
    a = int(input())

    la = []
    while(a>0):
        la.append(a%10)
        a //= 10
    la = np.array(la)


    na, nd = np.abs, np.diff
    def f(l1):
        return na(nd(l1))

    for _ in range(N-1):
       la =  f(la)
    print(*la)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

k = []
num = 1

while num <= N-1:
    for i in range(N-num):
        min = abs(l[i] - l[i+1])
        k.append(min)
    l = k
    k = []
    num += 1
ans = l[0]
print(ans)

Submission status: Time Limit Exceeded

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


def gen(n, al):
    return n-1, [
        (al[i] - al[i + 1]) if (al[i] - al[i + 1]) > 0
        else (al[i + 1] - al[i]) for i in range(n - 1)
    ]


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

print(al[0])

Submission status: Time Limit Exceeded

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

f = 1 in a
if f:
    a = [int(i==1) for i in a]
else:
    a = [int(i==2) for i in a]

tmp = 1
ans = 0
for i in range(n):
    ans += tmp*a[i]
    tmp = tmp*(n-i-1)//(i+1)

ans %= 2
if not f:
    ans *= 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n//2):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
      if len(b) == 1:
        print(b[0])
        exit()
    a = [0]*(len(b)-1)
    for i in range(len(b)-1):
      a[i]=abs(b[i]-b[i+1])
      if len(a) == 1:
        print(a[0])
        exit()
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
data = [int(s) for s in input()]

while len(data) > 1:
    for i in range(len(data) - 1):
        data[i] = abs(data[i] - data[i+1])
    del data[-1]
        
print(data[0])

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    for j in range(len(a) - 1):
        a[j] = abs(a[j] - a[j + 1])
    a.pop()

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    a = list(map(int, list(input())))
    # N = 1000000
    # a = [1 for _ in range(N)]

    _x = [-1 for _ in range(N)]
    x = [_x for _ in range(N)]
    for i in range(N):
        if i == 0:
            x[i] = a
            continue
        for j in range(N-i):
            x[i][j] = abs(x[i-1][j] - x[i-1][j+1])
    print(x[N-1][0])

main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
numlist = list(input())

l_numlist = len(numlist)

for b in range(1, n):
 
    for a in range(0, l_numlist-1):
        d = int(numlist[a]) - int(numlist[a+1])

        numlist.append(abs(d))
 
    numlist = numlist[l_numlist:]
    l_numlist = len(numlist)
 
print(numlist[0])

Submission status: Time Limit Exceeded

Solution code:
import sys, random, collections
input = sys.stdin.readline
N = int(input())
S = input().strip()
A = list(map(int, list(S)))

def f_naive(A):
  for i in range(N):
    for j in range(N-i-1):
      A[j] = abs(A[j]-A[j+1])
  return A[0]

def f(A):
  for i in range(N):
    h = collections.defaultdict(int)
    for j in range(N-i-1):
      v = abs(A[j]-A[j+1])
      h[v] += 1
      A[j] = v
    if len(h) == 1:
      if h[list(h.keys())[0]] >= 2:
        return 0
    elif len(h) == 2:
      keys = list(h.keys())
      if h[keys[0]] >= 2 and h[keys[1]] >= 2:
        if h[keys[0]] == h[keys[1]]:
          return 0
    elif len(h) == 3:
      if h[0] >= 2 and h[1] >= 2 and h[2] >= 2:
        if h[0] == h[1] and h[1] == h[2]:
          return 0
  return A[0]

print(f(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
num = [int(x) for x in list(str(int(input())))]
length= len(num)
count_stopper = length
cnt = 1
big_count = 0
while big_count != length-1:
    new = []
    while cnt != count_stopper:
        a = 0
        a = num[cnt - 1] - num[cnt]
        new.append(abs(a))
        cnt += 1
    cnt = 1
    num = new
    big_count += 1
    count_stopper -= 1
print(num[0])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def work(s):
    t = []

    for i in range(len(s) - 1):
        t.append(abs(s[i] - s[i + 1]))

    if len(t) == 1:
        print(t[0])
        return
    else:
        work(t)


n = int(input())
s = input()

lst = []

for i in range(n):
    lst.append(int(s[i]) - 1)

work(lst)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math

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

nums_list = [ int(i) for i in str_list ]
tuples = tuple(nums_list)
from functools import lru_cache

@lru_cache(maxsize=100000)
def keisan(t):
  nums_list = list(t)
  result = [ abs(nums_list[i] - nums_list[i+1]) for i in range(len(nums_list)-1) ]
  if len(result) == result.count(result[0]) and not len(result) == 1:
    tmp = []
    tmp.append(0)
    return tmp
  if len(result) > 1:
    return keisan(tuple(result))
  else:
    return result

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = list(str(input()))
x = [int(i) for i in x]

for i in range(n+5):
    tmp = []
    try:
        if x[0] == x[1] and x[1] == x[2]:
            s = 3
            g = len(x)
        elif x[-1] == x[-2] and x[-2] == x[-3]:
            s = 1
            g = len(x) - 2
        else:
            s = 1
            g = len(x)
    except IndexError:
        s = 1
        g = len(x)
    for j in range(s,len(x)):
        tmp.append(abs(x[j]-x[j-1]))
    x = tmp
    if len(x)==1:
        print(max(x))
        exit()
    elif len(x)==0:
        print(0)
        exit()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a_s=list(input())
a=[int(i) for i in a_s]
b=[]
for i in range(N-1):
  if i%2==0:
    for j in range(len(a)-1):
      b.append(abs(a[j]-a[j+1]))
    a.clear()
  else:
    for j in range(len(b)-1):
      a.append(abs(b[j]-b[j+1]))
    b.clear()

if N%2==0:
  print(b[0])
else:
  print(a[0])

Submission status: Time Limit Exceeded

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

for j in range(1,N):
    for i in range(N-j):
        min = abs(l[i] - l[i+1])
        del l[0]
        l.append(min)
        
ans = l[-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    N = int(input())
    a = input()
    la = np.array([int(c) for c in a])

    na, nd = np.abs, np.diff
    def f(l1):
        return na(nd(l1))

    for _ in range(N-1):
       la =  f(la)
    print(*la)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
A = [int(s) for s in input()]
for i in range(N-1):
    nxtA = []
    for j in range(N - i - 1):
        nxtA.append(abs(A[j] - A[j + 1]))
    A = copy.deepcopy(nxtA)

print(A[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import deque
N = int(input())
a = str(input())
a = deque([int(a[i]) for i in range(N)])

def f(b):
    c = deque()
    for i in range(len(b)-1):
        if b[i] != b[i+1]:
            c.append(abs(b[i] - b[i+1]))
    return c

while len(a) > 1:
    a = f(a)
if len(a) == 0:
    print(0)
else:
    print(a[0])