Problem:


Score : 700 points

Problem Statement

We have a sequence of N \times K integers: X=(X_0,X_1,\cdots,X_{N \times K-1}).
Its elements are represented by another sequence of N integers: A=(A_0,A_1,\cdots,A_{N-1}). For each pair i, j (0 \leq i \leq K-1,\ 0 \leq j \leq N-1), X_{i \times N + j}=A_j holds.

Snuke has an integer sequence s, which is initially empty.
For each i=0,1,2,\cdots,N \times K-1, in this order, he will perform the following operation:

If s does not contain X_i: add X_i to the end of s.

If s does contain X_i: repeatedly delete the element at the end of s until s no longer contains X_i. Note that, in this case, we do not add X_i to the end of s.

Find the elements of s after Snuke finished the operations.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq K \leq 10^{12}

1 \leq A_i \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 \cdots A_{N-1}

Output

Print the elements of s after Snuke finished the operations, in order from beginning to end, with spaces in between.

Sample Input 1

3 2
1 2 3

Sample Output 1

2 3

In this case, X=(1,2,3,1,2,3).
We will perform the operations as follows:

i=0: s does not contain 1, so we add 1 to the end of s, resulting in s=(1).

i=1: s does not contain 2, so we add 2 to the end of s, resulting in s=(1,2).

i=2: s does not contain 3, so we add 3 to the end of s, resulting in s=(1,2,3).

i=3: s does contain 1, so we repeatedly delete the element at the end of s as long as s contains 1, which causes the following changes to s: (1,2,3)→(1,2)→(1)→().

i=4: s does not contain 2, so we add 2 to the end of s, resulting in s=(2).

i=5: s does not contain 3, so we add 3 to the end of s, resulting in s=(2,3).

Sample Input 2

5 10
1 2 3 2 3

Sample Output 2

3

Sample Input 3

6 1000000000000
1 1 2 2 3 3

Sample Output 3

s may be empty in the end.

Sample Input 4

11 97
3 1 4 1 5 9 2 6 5 3 5

Sample Output 4

9 2 6



Sample input:
3 2
1 2 3


Sample output:
2 3

Submission status: Time Limit Exceeded

Solution code:
(N,K) = map(int,input().split())
A = [int(a) for a in input().split()]

idx = list(range(N))
nx = [1]*N

for i in range(N):
  flg = True
  for j in range(i+1,N):
    if A[i]==A[j]:
      idx[i] = j
      nx[i] = 0
      flg = False
      break
  if flg:
    for j in range(i):
      if A[i]==A[j]:
        idx[i] = j
        break
#print(nx)
#print(idx)

id_cur= 0
K_cur = 0
while True:
  if K_cur + nx[id_cur] != K:
    K_cur += nx[id_cur]
    id_cur = idx[id_cur] + 1
    if id_cur==N:
      id_cur = 0
      K_cur += 1
  else:
    break

if True:
  S = []
  i = id_cur
  while True:
#    print("t")
#    print(i)
#    print(nx[i])
#    print(idx[i])
    if nx[i]==1:
      S = S + [A[i]]
      i += 1
    else:
      i = idx[i]+1
    if i >= N:
      break

  print(' '.join(map(str, S)))

Submission status: Time Limit Exceeded

Solution code:
def trial(n,deck):
    cur = [n]
    for i in range(len(deck)):
        if deck[i] in cur:
            temp = cur.index(deck[i])
            cur = cur[:temp]
        else:
            cur.append(deck[i])
    if len(cur) > 0:
        return cur
    else:
        return [0]
            
n,k = map(int,input().split())
a = list(map(int, input().split()))
d = {}
for j in [0]+list(set(a)):
    if j == 0:
        temp = trial(j,a)
        d[j] = temp[1:]
    else:
        d[j] = trial(j,a)
    if len(d[j]) == 0:
        d[j] = [0]

ii = 0
for i in range(k):
    if i == k-1:
        ans = d[ii]
    else:
        ii = d[ii][0]
if ans == [0]:
    print()
else:
    print(" ".join(list(map(str, ans))))

Submission status: Time Limit Exceeded

Solution code:
import sys

N, K = map(int, input().split())
As = list(map(int, input().split()))

# 各文字が最初に出現するインデックス
posFirst = {}
for i, A in enumerate(As):
    if A not in posFirst:
        posFirst[A] = i + N

# 次に同じ文字が出現するインデックス
nexts = [-1] * N
posLast = {}
for i in reversed(range(N)):
    A = As[i]
    if A not in posLast:
        nexts[i] = posFirst[A]
    else:
        nexts[i] = posLast[A]
    posLast[A] = i

v0s = [-1] * N
v2s = [-1] * N
for i in range(N):
    if nexts[i] < N-1:
        v2s[i] = nexts[i] + 1
        v0s[nexts[i] + 1] = i
    else:
        v2s[i] = i

v9s = [-1] * N
for i in reversed(range(N)):
    if v9s[i] == -1:
        v9s[i] = v2s[i]
    v9s[v0s[i]] = v9s[i]

numLoop = 0
vNow = 0
while True:
    vNow = nexts[vNow]
    if vNow >= N:
        numLoop += 1
    vNow %= N
    if vNow == N-1:
        break
    vNow += 1

K %= (numLoop+1)

if K == 0:
    print()
    sys.exit()

numLoop = 0
vNow = 0
while True:
    vNow = nexts[vNow] + 1
    if vNow >= N:
        numLoop += 1
    vNow %= N
    if numLoop == K-1:
        break

ss = []
for v in range(vNow, N):
    X = As[v]
    if X not in ss:
        ss.append(X)
    else:
        s = ss.pop()
        while s != X:
            s = ss.pop()

print(' '.join(map(str, ss)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
from pprint import pprint

def solve(n, k, a):
    loop = False
    indexes = []

    s = []
    s_set = set()
    for i in xrange(n * k):
        tmp_i = i % n
        if a[tmp_i] not in s_set:
            s.append(a[tmp_i])
            s_set.add(a[tmp_i])
            indexes.append(" ".join(map(str, s)))
        else:
            pos = s.index(a[tmp_i])
            s = s[:pos]
            s_set = set(s)
            indexes.append(" ".join(map(str, s)))
            if tmp_i == n - 1 and len(s) == 0:
                loop = True
                break

    if not loop:
        print " ".join(map(str, s))
        return

    last_pos = (n * k) % (len(indexes))
    print indexes[last_pos - 1]


if __name__ == '__main__':

    n, k = map(int, sys.stdin.readline().strip().split(" "))
    a = map(int, sys.stdin.readline().strip().split(" "))
    solve(n, k, a)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k = map(int,input().split())
    numlist = list(map(int,input().split()))
    s=[]
    for i in range(1,k+1):
        for j in range(0,n):
            if numlist[j] in s:
                while numlist[j] in s:
                    del s[-1]
            else:
                s.append(numlist[j])
        if len(s) == 0:
            num = (k % i)
            s=[]
            for p in range(0,num):
                for q in range(0,n):
                    if numlist[q] in s:
                        while numlist[q] in s:
                            del s[-1]
                    else:
                        s.append(numlist[q])
            return ' '.join(map(str,s))
    return ' '.join(map(str,s))
print (main())

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
k=K%(N+1)
if N == len(list(set(A))):
    for p in range(k-1,N):
        print(A[p],end=' ')
    exit()
else:
    L=[]
    c=0
    for k in range(K):
        c+=1
        for i in A:
            if len(L)!=0 and i == L[0]:
                L=[]
            elif len(L)==0:
                L=[i]
            elif i != L[0]:
                pass
        if len(L)==0:
            break
    kk=K%c
    LL=[]
    for q in range(kk):
        for j in A:
            if j in LL:
                for x in range(len(LL)):
                    if LL[-1]!=j:
                        LL.pop(-1)
                    else:
                        LL.pop(-1)
                        break
            elif j not in LL:
                LL.append(j)
    for y in LL:
        print(y,end=' ')

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
s=[]
for i in range(0,n*k):
    if s.count(A[i%n])==0:
       s.append(A[i%n])
    else:
       while s.count(A[i%n])!=0:
            del s[-1]
for j in range(0,len(s)):
    s[j]=str(s[j])
print(" ".join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
p = [[int(t) for t in r.split()] for r in  sys.stdin.read().split('\n') if r != '']
N, K = p[0]
A = p[1]
s = []
e = []
for i in range(K):
    for j in range(N):
        x = A[j]
        if len(s) == 0:
            s += [x]
            continue
        for k in range(len(s)):
            if s[k] == x:
                s = s[:k]
                break
            if k == len(s) - 1:
                s += [x]
    if s in e:
        print(' '.join(str(i) for i in e[(N * K - 1) % len(e)]))
        quit()
    e += [s.copy()]
print(' '.join(str(i) for i in e[(N * K - 1) % len(e)]))

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
a = na()

d =[]
ans = []
for q in range(k):
    g = d+a
    m = len(g)
    d = []
    i = 0
    while True:
        for j in range(i+1,m):
            if g[i] == g[j]:
                i = j+1
                break
        else:
            d.append(g[i])
            i += 1
        if i == m-1:
            d.append(g[m-1])
            break
        if i >= m:
            break
    ans.append(d)
    if len(d) == 0:
        print(' '.join([str(t) for t in ans[k%(q+1)-1]]))
        exit()

print(' '.join([str(t) for t in d]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = deque([])
while 1:
    tmp = deque()
    a = deque(A[:])
    if ans:
        a.extendleft(list(ans)[-1][::-1])
    for i in a:
        if i not in tmp:
            tmp.append(i)
        else:
            while tmp.pop() != i:
                pass
    if tmp:
        ans.append(list(tmp))
    else:
        ans.append(list(tmp))
        break
L = len(ans)
print(*ans[(K % L) - 1])

Submission status: Time Limit Exceeded

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

def add(S, A):
    for Ai in A:
        try:
            S = S[:S.index(Ai)]
        except ValueError as _:
            S.append(Ai)
    return S

ok = False
S = []
for i in range(K):
    S = add(S, A)
    if len(S) == 0:
        break
else:
    ok = True
    print(" ".join(map(str, S)))

if not ok:
    for i in range(K%(i+1)):
        S = add(S, A)
    print(" ".join(map(str, S)))

Submission status: Time Limit Exceeded

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

eachEnd = []
s = []
for j in range(K):
  for i in A:
    if i not in set(s):
      s.append(i)
#      print("app", s)
    else:
      s = s[:s.index(i)]
#      print("del", s)
  if s not in eachEnd:
    apps = s[:]
    eachEnd.append(apps)
  else:
    break
#  print("ee", eachEnd)  
loopptn = len(eachEnd)
print(*eachEnd[(K-1) % loopptn])

Submission status: Time Limit Exceeded

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

dic = {}
for i in range(N):
    a = A[i]
    v = dic.get(a, None)
    if v is None:
        dic[a] = [i]
    else:
        v.append(i)

result = []
result_dic = {}
result_dic_index = {}
count = 0
loop = 0
while count < K:
    index = 0
    if count != 0:
        if result:
            a = result[index]
            v = dic.get(a)
            next_i = v[0]
            index = next_i + 1
            result = []
    while index < N:
        a = A[index]
        v = dic.get(a)
        next_i = -1
        for i in v:
            if i > index:
                next_i = i
                break
        if next_i == -1:
            result.append(a)
            index += 1
        else:
            index = next_i + 1
    tu = tuple(result)
    t = result_dic.get(tu, None)
    if t is None:
        result_dic[tu] = count
        result_dic_index[count] = tu
    else:
        loop = count
        break
    count += 1

answers = None
if loop == 0:
    answers = tu
else:
    index = (K - 1) % loop
    answers = result_dic_index[index]

print(" ".join(map(str, answers)))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
a = [int(i) for i in input().split()]
b = []
cnt = 0

def has_duplicates(seq):
    return len(seq) != len(set(seq))

if not(has_duplicates(a)):
	cnt = len(a) + 1
	ad = k % cnt
	if ad == 0:
		print([])
	else:
		b = a[ad-1:]
		[print(i) for i in b]
elif a[0] == a[-1] and a[0] not in a[1:n-1]:
	print([])
else:
	for i in range(k):
		for j in range(n):
			if a[j] not in b:
				b.append(a[j])
			else:
				ind = b.index(a[j])
				b = b[:ind]
		cnt += 1
		if b == []:
			break

	ad = k % cnt

	for i in range(ad):
		for j in range(n):
			if a[j] not in b:
				b.append(a[j])
			else:
				ind = b.index(a[j])
				b = b[:ind]

	[print(i) for i in b]

Submission status: Time Limit Exceeded

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

gap = [0] * n
num = {}
for i in range(n):
    num.setdefault(a[i], [])
    num[a[i]].append(i)
for i in num.values():
    len_i = len(i)
    for j in range(len_i):
        gap[i[j]] = (i[(j + 1) % len_i] - i[j] - 1) % n + 1
count = 0
x = 0
while True:
    if count % n == 0 and count > 0:
        term = count // n
        break
    else:
        temp = gap[x % n] + 1
        count += temp
        x += temp
k = (k - 1) % term + 1
count2 = 0
x2 = 0
while count2 < k * n:
    temp = gap[x2 % n] + 1
    count2 += temp
    x2 += temp
else:
    count2 -= temp
    x2 -= temp
queue = []
for i in range(x2, x2 + k * n - count2):
    if a[i % n] not in set(queue):
        queue.append(a[i % n])
    else:
        while a[i % n] in set(queue):
            del queue[-1]
for i in queue:
    print(i, end = " ")

Submission status: Time Limit Exceeded

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

s_index = {}
s_list = []
    

for i in range(K):
    for a in A:
        if a in s_index:
            del_keys = [a]
            for key, val in s_index.items():
                if val > s_index[a]:
                    del_keys.append(key)

            s_list = s_list[:s_index[a]]
            for key in  del_keys :
                s_index.pop(key)
        else:
            s_index[a] = len(s_list)
            s_list.append(a)
    
print( ' '.join(list(map(str, s_list)))  )

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy

def solve(n, k, a):
    loop = False
    indexes = []

    s = []
    for i in xrange(n * k):
        tmp_i = i % n
        if a[tmp_i] not in s:
            s.append(a[tmp_i])
            indexes.append(" ".join(map(str, s)))
        else:
            pos = s.index(a[tmp_i])
            s = s[:pos]
            indexes.append(" ".join(map(str, s)))
            if tmp_i == n - 1 and len(s) == 0:
                loop = True
                break

    if not loop:
        print " ".join(map(str, s))
        return

    last_pos = (n * k) % (len(indexes))
    print indexes[last_pos - 1]


if __name__ == '__main__':

    n, k = map(int, sys.stdin.readline().strip().split(" "))
    a = map(int, sys.stdin.readline().strip().split(" "))
    solve(n, k, a)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

ans = []
f = [False] * (2 * (10 ** 5) + 1)
q = deque([])

def solve():
  N, K = map(int, input().split())
  A = [int(i) for i in input().split()]
  for i in range(K):
    for a in A:
      if not f[a]:
        q.append(a)
        f[a] = True
      else:
        while True:
          out = q.pop()
          f[out] = False
          if a == out: break
    ans.append(list(q))
    if len(q) == 0: break

  return ans[K % len(ans) - 1]

print(*solve())

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
for i in range(len(a)):
  x = a[i]
  if x in b:
    c[b.index(x)].append(i)
  else:
    b.append(x)
    c.append([i])
d = []
for i in range(len(a)):
  x = a[i]
  xc = c[b.index(x)]
  d.append(xc[(xc.index(i)+1)%len(xc)])
#print(d)
count = 0
now = 0
while now != 0 or count == 0:
  past = now
  now = ( d[past] + 1 ) % n
  if now - 1 <= past:
    count += 1
  #print(past,now,count)
limit = k % count
#print(count,limit)
count = 0
now = 0
while True:
  past = now
  now = ( d[past] + 1 ) % n
  if now - 1 <= past:
    if count == limit - 2:
      break
    count += 1
#print(d,past,a)
ans = []
for i in range(-n+past,n):
  x = a[i%n]
  #print(x)
  if x in ans:
    ans = ans[:ans.index(x)]
  else:
    ans.append(x)
#print(ans)
ans2 = list(map(str,ans))
print(' '.join(ans2))

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/agc036/tasks/agc036_b
from itertools import product
import numpy as np


def calc(N: int, K: int, A: np.ndarray) -> np.ndarray:
    loop_values, rest_iter = calc_until_find_loop(N, K, A)
    loop_len = loop_values.shape[0]
    return loop_values[rest_iter % loop_len]


def calc_until_find_loop(N: int, K: int, A: np.ndarray):  # (loop_values, rest_iter)
    results = []
    values_to_idx = {}
    values = np.array([])
    for i in range(K):
        values = one_iter(A, values)
        tup_values = tuple(values)

        if tup_values in values_to_idx:
            return np.array(results[values_to_idx[tup_values] :]), (K - i - 1)

        values_to_idx[tup_values] = len(results)
        results.append(values)

    return np.array([values]), 0


def one_iter(A: np.ndarray, prev_values: np.ndarray) -> np.ndarray:
    values = np.array(prev_values.tolist() + A.tolist())
    mask = np.ones_like(values)
    ptr = 0
    for ptr in range(len(values)):
        if mask[ptr] == 0:
            continue
        for i in range(ptr + 1, len(values)):
            if values[i] != values[ptr]:
                continue

            mask[ptr : i + 1] = 0
            break
    return values[mask.astype(bool)]


def load_input_as_int_array():
    return np.array([int(v) for v in input().split(" ")])


N, K = load_input_as_int_array()
A = load_input_as_int_array()
result = calc(N, K, A)
print(" ".join([str(v) for v in result]))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

n,k = map(int,input().split())
a = list(map(int,input().split()))

count = 0
j = 0
def f(s,j,count):
    cho = False
    for t in range(j+1,n):
        if a[s] == a[t]:
            cho == True
            if t+1==n:
                return count
            else:
                return f(t+1,t+1,count)
    if not cho:
        return f(s,-1,count+1)

lasta = []
if f(0,0,0) == 0:
    lasta = []
else:
    for i in range(0,min((k%f(0,0,0)+1),k)):
        lasta += a

def lastf(s,lista):
    cho = False
    for t in range(s+1,len(lista)):
        if lista[s] == lista[t]:
            cho = True
            lista = lista[0:s] + lista[t+1:len(lasta)]
            if len(lista)==s:
                return lista
            else:
                return lastf(s,lista)
    if not cho:
        if s < len(lista)-1:
            return lastf(s+1,lista)
        else:
            return lista


ans = lastf(0,lasta)
print(" ".join(map(str,ans)))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import bisect

d = defaultdict(list)
N,K=[int(x) for x in input().split()] 
K=N*K
A=[int(x) for x in input().split()]
for i in range(N):
    a=A[i]
    d[a].append(i)
keys=d.keys()
L={}
for k in keys:
    L[k]=len(d[k])
E=[0 for i in range(N)]
for i in range(N):
    a=A[i]
    ci=bisect.bisect_left(d[a], i)
    ni=(ci+1)%L[a]
    E[i]=(d[a][ni]-d[a][ci])%N
    if E[i]==0:
        E[i]=N
    E[i]+=1
#ss=sum(E)
ss=0
j=0
while(1):
    ss+=E[j]
    j=(j+E[j])%N
    if j==0:
        break
#print(ss)
K%=ss
j=0
#print(E)
while(K > E[j]):
    K-=E[j]
    j=(j+E[j])%N
    #print(j)
s=[0 for i in range(N)]
p=0
for k in range(K):
    if A[j] not in s[:p]:
        s[p]=A[j]
        p+=1
    else:
        p=s[:p].index(A[j])
    j=(j+1)%N
#print(s)
print(" ".join([str(x) for x in s[:p]]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = map(int, input().split())
a = list(map(int, input().split()))


def solve(remain_count):
    s = deque()
    for i in range(0, remain_count):
        for j in range(0, N):
            if a[j] in s:
                while s[-1] != a[j] and a[j] in s:
                    s.pop()
                s.pop() # pop last

                # 子数列の最後の時点で結果がカラであれば、その倍数分は省略できる実装
                if len(s) == 0 and (j == N - 1):
                    new_remain_count = K % (i + 1)
                    if new_remain_count < remain_count and ( N * new_remain_count < (N * remain_count - (i + 1) * (j + 1))):
                        return solve(new_remain_count)
            else:
                s.append(a[j])
    return s


print(' '.join(map(str,solve(K))))

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy

def solve(n, k, a):
    loop = False
    indexes = []

    s = []
    for i in xrange(n * k):
        tmp_i = i % n
        if a[tmp_i] not in s:
            s.append(a[tmp_i])
            indexes.append(copy.deepcopy(s))
        else:
            pos = s.index(a[tmp_i])
            s = s[:pos]
            indexes.append(copy.deepcopy(s))
            if tmp_i == n - 1 and len(s) == 0:
                loop = True
                break

    if not loop:
        print " ".join(map(str, s))
        return

    last_pos = (n * k) % (len(indexes))
    print " ".join(map(str, indexes[last_pos - 1]))


if __name__ == '__main__':

    n, k = map(int, sys.stdin.readline().strip().split(" "))
    a = map(int, sys.stdin.readline().strip().split(" "))
    solve(n, k, a)

Submission status: Time Limit Exceeded

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

aa = list(map(int,input().split()))

s = []
j = 0

while j < k :
  for i in aa:
    if i in s:
      p = s.index(i)
      del s[p:]
    else:
      s.append(i)
      
  if j == 0 and len(s) == 0:
    break
  elif j > 1 and len(s) == 0:
    j = k - k % j - 1
    
  else:
    j += 1

if len(s) > 0:
  print(s[0:1][0],end="")
  
  if len(s) > 1:
    for ss in s[1:]:
      print(" {}".format(ss),end="")

print()

Submission status: Time Limit Exceeded

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

dic = {}
for i in range(N):
    a = A[i]
    v = dic.get(a, None)
    if v is None:
        dic[a] = [i]
    else:
        v.append(i)

result = []
result_dic = {}
result_dic_index = {}
count = 0
loop = 0
while count < K:
    index = 0
    if count != 0:
        if result:
            a = result[index]
            v = dic.get(a)
            next_i = v[0]
            index = next_i + 1
            result = []
    while index < N:
        a = A[index]
        v = dic.get(a)
        next_i = -1
        for i in v:
            if i > index:
                next_i = i
                break
        if next_i == -1:
            result.append(a)
            index += 1
        else:
            index = next_i + 1
    tu = tuple(result)
    t = result_dic.get(tu, None)
    if t is None:
        result_dic[tu] = count
        result_dic_index[count] = tu
    else:
        loop = count
        break
    count += 1

answers = None
if loop == 0:
    answers = tu
else:
    index = (K - 1) % loop
    answers = result_dic_index[index]

for answer in answers:
    print(answer, end=" ")
print()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
As = [int(i) for i in input().split()]

s = []
loop_num = 0

for i in range(K):
    for val in As:
        if val not in s:
            s.append(val)
        elif s.index(val) == 0:
            s = []
        else:
            s = s[0: s.index(val)]

    if len(s) == 0:
        loop_num = i + 1
        break

if len(s) != 0:
    print(*s)
else:
    for i in range(K%loop_num):
        for val in As:
            if val not in s:
                s.append(val)
            elif s.index(val) == 0:
                s = []
            else:
                s = s[0:s.index(val)]
    print(*s)

Submission status: Time Limit Exceeded

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

def func(arr, i) :
    if i in arr :
        ind = arr.index(i)
        return arr[:ind]
    else :
        return arr + [i]

t = 0
b = []
while b or not t :
    for i in a :
        b = func(b, i)
    else :
        t+=1

kk = k%t
for _ in range(kk) :
    for i in a :
        b = func(b, i)

print(" ".join(map(str, b)))

Submission status: Time Limit Exceeded

Solution code:
NK = list(map(int, input().split()))
A = list(map(int, input().split()))

N = NK[0]
K = NK[1]
X = []

#print(N)
#print(K)
#print(A)


#print(X)
s= [0]
#print(s[-1])

i=0
for j in range(K):
    for i in range(N):
        if (A[i] in s):
            point=s.index(A[i])
            del s[int(point):]

        else:
            s.append(A[i])

    
del s[0]
s=[str(a) for a in s]
s=" ".join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10000)
N, K = [int(i) for i in input().split()]
A = tuple(int(i) for i in input().split())
super_flag = False
try:
    A = A * K
except BaseException:
    super_flag = True


@lru_cache(maxsize=2**20)
def add_to_list(li, num):
    if num in set(li):
        li = li[:li.index(num)]
        return li
    else:
        li = li + (num,)
        return li


@lru_cache(maxsize=2**20)
def get(li):
    if len(li) != 1:
        yoi_list_b = get(li[:-1])
        yoi_list = add_to_list(yoi_list_b, li[-1])
    else:
        yoi_list = li
    return yoi_list


if not super_flag:
    li = get(A)
else:
    li = tuple()
    count = 0
    for _ in range(K):
        li = get(li + A)
        count += 1
        if not li:
            li = tuple()
            for _ in range(K % count):
                li = get(li + A)
            break
if li:
    for i in li[:-1]:
        print(i, end=" ")
    print(li[-1])
else:
    pass

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n, k = map(int, input().split())
a = list(map(int, input().split()))

kotae = deque([])
lst = []
d = deque(a)
pattern = 0
while True:
    for kazu in d:
        if kazu in kotae:
            ind = list(kotae).index(kazu)
            kotae = deque(list(kotae)[:ind])
        else:
            kotae.append(kazu)
    kotae_lst = list(kotae)
    if kotae_lst == []:
        lst.append(kotae_lst)
        pattern += 1
        break
    else:
        lst.append(kotae_lst)
        pattern += 1

doko = k % pattern
ans = lst[doko-1]
if ans != []:
    print(' '.join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = map(int, input().split())
a = list(map(int, input().split()))


def solve(remain_count):
    s = deque()
    for i in range(0, remain_count):
        for j in range(0, N):
            if a[j] in s:
                while s[-1] != a[j] and a[j] in s:
                    s.pop()
                s.pop() # pop last

                # 子数列の最後の時点で結果がカラであれば、その倍数分は省略できる実装
                if len(s) == 0 and (j == N - 1):
                    new_remain_count = K % (i + 1)
                    if new_remain_count < remain_count and ( N * new_remain_count < (N * remain_count - i * j)):
                        return solve(new_remain_count)
            else:
                s.append(a[j])
    return s


print(' '.join(map(str,solve(K))))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import itertools

N, K = map(int, input().split()) 
A = list(map(int, input().split()))

eachEnd = deque([])
s = deque([])
apps = deque([])
for j in range(K):
  for i in A:
    if i not in s:
      s.append(i)
#      print("app", s)
    else:
#      s = s[:s.count(i)]
      s = list(itertools.islice(s, 0, len(s)))
      s = deque(s[:s.index(i)])
#      print("del", s)
  if s not in eachEnd:
#    apps = s[:]
    apps = list(itertools.islice(s, 0, len(s)))
#    print("apps", apps)
    eachEnd.append(apps)
  else:
    break
#  print("ee", eachEnd)  
loopptn = len(eachEnd)
print(*eachEnd[(K-1) % loopptn])

Submission status: Time Limit Exceeded

Solution code:
inp = input()
split_inp = inp.split()
N = int(split_inp[0])
K = int(split_inp[1])
inp = input()
a_str_list = inp.split()
a_list = []
for a in a_str_list:
    a_list.append(int(a))
if K == 1:
    for a in a_list:
        print(a, end=" ")
    print("")
    exit()


# 初めの入力に対する次の初めの辞書を作る
next_first_dictionary = {}  # 次の終わりと何回目に呼ばれるか
# 0とAの数が最初の数としてあり得る
first_number = 0  # 何もない状態
checking_number = first_number
loop_count = 0
loop_start = 0
loop_time = 0
while True:
    loop_count += 1
    if first_number in next_first_dictionary:
        # この数で始まる時の最後の数は知っている．十分
        loop_start = next_first_dictionary[first_number][1]
        loop_time = loop_count - loop_start
        break
    for a in a_list:
        if first_number == 0:
            first_number = a
        elif a == first_number:
            first_number = 0
    # 残った最初の数がわかる
    next_first_dictionary[checking_number] = [first_number, loop_count]
    checking_number = first_number
# すべての可能性のある始まりにおいての最後の数がわかっている．

# j回繰り返したときに最初にある数を求める
j = K - 1
if j != 0:
    j = j - loop_start
    j = j % loop_time  # ループの中の何番目か
    j += loop_start
after_first_number = -1
for item in next_first_dictionary.values():
    if item[1] == j:
        after_first_number = item[0]
        break
if after_first_number == -1:
    print("Error")
# after first number からはじめる．最後のn回
first_number = after_first_number
s = []
for a in a_list:
    if first_number == 0:
        first_number = a
        s.append(a)
    else:
        if a == first_number:
            s = []
            first_number = 0
        else:
            # すでに同じ数字があるときはそこまでリセットする
            if a in s:
                ind = s.index(a)
                s = s[:ind]
            else:
                s.append(a)
if not s:
    print("")
    exit()
if len(s) == 1:
    print(s[0])
    exit()
print(s[0], end="")
for ans in s[1:]:
    print(" ", end="")
    print(ans, end="")
print("")

Submission status: Time Limit Exceeded

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

def func(arr, i) :
    if i in arr :
        ind = arr.index(i)
        return arr[:ind]
    else :
        return arr + [i]

t = 0
b = []
while b or not t :
    for i in a :
        b = func(b, i)
    else :
        t+=1

kk = k%t
for _ in range(k) :
    for i in a :
        b = func(b, i)

print(" ".join(map(str, b)))

Submission status: Time Limit Exceeded

Solution code:
import math

line = input("")
N, K = list(map(int, line.strip().split(" ")))

_X = list(map(int, input("").strip().split(" ")))

X = []

for _ in range(K):
  X += _X

output_X = []
  
for i in X:
  _output_X = set(output_X)
  if i in _output_X:
    i_index = output_X.index(i)
    output_X = output_X[:i_index]
  else:
    output_X += [i]
  
out_string = ""

out_string = " ".join(list(map(str, output_X)))

print(out_string)

Submission status: Time Limit Exceeded

Solution code:
def roop(A, s):
    for v in A:
        if s.__contains__(v):
            del s[s.index(v) :]
        else:
            s.append(v)

    return


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    s = list()
    t = list()

    for i in range(K):
        roop(A, s)
        if len(s) == 0 and i == 0:
            break
        elif t.__contains__(s):
            t.append(s.copy())
            s = t[K % i]
            break

    print(*(t for t in s))

    return


if __name__ == "__main__":
    main()