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(i) for i in input().split()]

cpA=A[:]

head=[]
tail=[]



cnt=0
arc=[]
while True:
    head=[]
    
    while True:
        #print(A)
        head=[]
        for i in A:
            if i in head:
                p=A.index(i)
                q=A[p+1:].index(i)+p+1
                break
            else:
                head.append(i)  
        else:
            break
        A=A[:p]+A[q+1:]
    cnt+=1
    arc.append(A[:])
    #print(A)
    if A == []:
        #print(cnt)
        break
    else:
        A.extend(cpA)
        
        

lenA=len(A)

k=(k-1)%(cnt)

print(" ".join([str(i) for i in arc[k]]))

Submission status: Time Limit Exceeded

Solution code:
#!venv/bin/python
#from collections import defaultdict 
from itertools import combinations
from collections import defaultdict
from pprint import pprint
import math
import datetime
import sys
N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

memo = defaultdict(dict)

head = None
cycle_start = cycle_end = None
for global_cnt in range(N * K):
    i = global_cnt % N
    if head is None:
        head = A[i]
        if not memo[i].get(A[i]) is None:
            cycle_start = memo[i].get(A[i])
            cycle_end = global_cnt
            break
        memo[i][A[i]] = global_cnt
        continue
    if head != A[i]:
        continue
    else:
        head = None

starting = 0
if cycle_end:
    cycle_term = cycle_end - cycle_start
    repeat = (N * K - cycle_start) // cycle_term
    starting = cycle_start + repeat * cycle_term

head = None
i = starting % N
last_empty_point = starting
for global_cnt in range(starting, N * K):
    i = global_cnt % N
    if head is None:
        head = A[i]
        last_empty_point = global_cnt
        continue
    if head != A[i]:
        continue
    else:
        head = None

nums = []
for global_cnt in range(last_empty_point, N * K):
    i = global_cnt % N
    if A[i] in nums:
        idx = nums.index(A[i])
        nums = nums[0:idx]
        continue
    nums.append(A[i])
print(" ".join([str(s) for s in nums]))

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
As = list(map(int, input().split()))
S = []
As2 = As + As
for i in range(N):
  a = As[i]
  j = As2.index(a, i+1, 2*N)+1
  S.append((j//N, j%N))
#print(S)
count = 0
p = 0
while True:
  c, p = S[p]
  count += c
  if p == 0:
    break
#print(count)
K = K%count
#print(K)
s = []
for k in range(K):
  for a in As:
    i = -1
    for j in range(len(s)):
      if s[j] == a:
        i = j
    if i < 0:
      s.append(a)
    else:
      s = s[0:i]
print(" ".join(map(str, s)))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import os
import copy


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

    s_set = []
    count = 0
    s = []
    for i in range(K):
        for j in range(N):
            if A[j] not in s:
                s.append(A[j])
            else:
                index = s.index(A[j])
                s = s[:index]
                # for k in range(len(s))[::-1]:
                # val = s.pop()
                # if val == A[j]:
                # break
            # print(' '.join(map(str, s)))
        if s in s_set:
            # print('braek')
            # print(s_set)
            # print(s)
            break
        # print('-------- count: %d ------' % count)
        # print(' '.join(map(str, s)))
        s2 = copy.deepcopy(s)
        s_set.append(s2)
        count += 1

    result_set = s_set[K % count-1]
    print(' '.join(map(str, result_set)))


solve()

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(input().split())
ans = A[:]
i=0
k_num = 1
roop_point = 0
Ans_list = [[]]

while 1:
    while i < len(ans)-1:
        head = ans[i]
        if head in ans[i+1:]:
            index_head = ans[i+1:].index(head) + i+1
            ans = ans[:i] + ans[index_head+1:]
        else:
            i+=1

    if ans not in Ans_list:
        Ans_list.append(ans)
        if k_num < K:
            ans = ans + A
            i=0
            k_num+=1
        elif k_num == K:
            print(*ans)
    else:
        roop_point = Ans_list.index(ans)
        break
#print(Ans_list)
print(*Ans_list[(K-roop_point)%len(Ans_list)])

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [0] * N
dic = {}
for i in range(N):
    a = A[i]
    if dic.get(a, None) is None:
        dic[a] = 1
    else:
        dic[a] += 1

count = 0
for i in range(N):
    a = A[i]
    v = dic.get(a)
    if v % 2 == 1:
        B[count] = a
        count += 1


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.clear()
    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:
# -*- coding: utf-8 -*-
import copy

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

ptn = [[]]
tmp = []
cnt = 0
while(cnt<K):
    for a in A:
        dup = [i for i, x in enumerate(tmp) if x == a]
        if len(dup) == 0:
            tmp.append(a)
        else:
            tmp = tmp[:dup[0]]

    if tmp == []: break
    ptn.append(copy.copy(tmp))
    cnt += 1

s = list(map(str, ptn[K%len(ptn)]))
print(" ".join(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 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 = map(int,input().split())
A = list(input().split())
l = []



tmp = []

T = 100000000000000000000000
h = 0
for i in range(K):
    if i!=0 and len(l[i-1]) !=0:
        h = A.index(l[i-1][0])
    for j in range(h,N):
        if A[j] not in tmp :
            tmp.append(A[j])

        else:
            index = len(tmp) - tmp[::-1].index(A[j]) -1
            del tmp[index:]

    tmp_copy = tmp.copy()
    l.append(tmp_copy)

    if l[i] in l[:i]:
        T = i
        break


ans = l[(K-1)%T]
print(' '.join(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n,k = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]

memo = dict()
res = [-1]*(2*n)
for i,c in enumerate(a*2):
    if c in memo: res[memo[c]] = i+1
    memo[c] = i


res2 = res[:n]

"""
def dfs(i):
    if res2[i] < n: res2[i] = dfs(res2[i])
    return res2[i]

for i in range(n): dfs(i)
"""

for i in range(n-1,-1,-1):
    if res2[i] < n: res2[i] = res2[res2[i]]

cycle = [0]
offset = 0
now = res2[0]-n

while now != 0:
    if now == n:
        offset += 1
        break
    else:
        cycle.append(now)
        now = res2[now]-n


#print(cycle,offset)

l = len(cycle)
k-=1
k %= (l+offset)

#print("k =",k)

if k != l: ans = p = cycle[k]
else: ans = p = n+1
while p <= n:
    ans = p
    p = res[p]
#    print(ans,p)

if ans >= n: exit()
else:
    s = []
    for i in range(ans,n):
        if a[i] not in s:
            s.append(a[i])
        else:
            while a[i] in s: s.pop()
    
print(" ".join(map(str,s)))

Submission status: Time Limit Exceeded

Solution code:
import math

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

    dp = {}
    memo = set()
    # arrays = []
    # if len(set(A)) == len(A):
    #     if K % 2 == 0:
    #         print()
    #     else:
    #         print(*A, sep=" ")
    #     return
    
    L = []
    arrays = {}
    values = set()
    # K = min(K, K % 10 + 10)
    for k in range(K):
        for a in A:
            # print(L, memo)
            if a in memo:
                i = dp[a]
                L = L[:i]
                memo = set(L)
            else:
                i = len(L)
                L.append(a)
                dp[a] = i
                memo.add(a)
        if len(L) == 0:
            s = " "
        else:
            s = " ".join(map(str,L))
        if s in values:
            loop = len(arrays)
            # print(loop)
            # print(arrays)
            print(arrays[(K-1) % loop])
            return
        else:
            arrays[k] = s
            values.add(s)



    print(*L, sep=" ")

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy

n,k=list(map(int,input().split()))
li=list(map(int,input().split()))
first_list=copy.deepcopy(li)

remain_list=[]
pre_list=[[]]

for j in range(k):
  for i in range(len(li)):
    if li[i] not in remain_list:
      remain_list.append(li[i])
    else:
      length=len(remain_list)
      while(remain_list[length-1] != li[i]):
        length-=1
      remain_list=remain_list[:length-1]
  if remain_list not in pre_list:
    pre_list.append(remain_list)
    li=remain_list+first_list
    remain_list=[]
  else:
    pre_list.append(remain_list)
    break

ans_count=0
while(pre_list[-1]!=pre_list[ans_count]):
  ans_count+=1

answer=ans_count
pre_list=pre_list[ans_count:]
ans_count=len(pre_list)-ans_count
k-=answer

L=[str(a) for a in pre_list[k%(ans_count-1)]]
L=" ".join(L)
print(L)

Submission status: Time Limit Exceeded

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

if N==1:
  if K%2:
    print(A[0])
  else:
    print('')
  exit()
  

#print(*range(10000))

def func(L):
  s = []
  for l in L:
    if l in s:
      del s[s.index(l):]
      #s_set.remove(l)
    else:
      s.append(l)
      #s_set.add(l)
  return s




index_dict={}
for i,a in enumerate(A):
  index_dict.setdefault(a,[]).append(i)

next_i = [0]*N
cycle_i = [0]*N
for k,v in index_dict.items():
  for i in range(len(v)):
    next_i[v[i]] = (v[(i+1)%len(v)]+1)%N
    cycle_i[v[i]] = (i+1)//len(v)

from_in_cycle=[]

i = 0
while True:
  j = next_i[i]
  if cycle_i[i]:
    from_in_cycle.append(i)
  i = j
  # 循環の評価
  if i==0:
    break
    
#print(from_in_cycle)

if from_in_cycle:
  k = K%(len(from_in_cycle)+1)
  print(*func(A[from_in_cycle[k-1]:]))
else:
  print('')

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
a_list = list(map(int, input().split()))

break_point = 0
state_list = []
x_list = []
for i in range(K):
    for a in a_list:
        if a in x_list:
            x_list = x_list[:x_list.index(a)]
        else:
            x_list.append(a)
    if x_list in state_list:
        break
    else:
        state_list.append(x_list.copy())
        if x_list == []:
            break
if len(state_list) == 0:
    print(" ".join(map(str, state_list[0])))
else:
    print(" ".join(map(str, state_list[(K - 1) % len(state_list)])))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
a_list = list(map(int, input().split()))

break_point = 0
state_list = []
x_list = []
for i in range(K):
    for a in a_list:
        if a in x_list:
            x_list = x_list[:x_list.index(a)]
        else:
            x_list.append(a)
    if x_list in state_list:
        break
    else:
        state_list.append(x_list.copy())
if len(state_list) == 0:
    print(" ".join(map(str, state_list[0])))
else:
    print(" ".join(map(str, state_list[(K - 1) % len(state_list)])))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
AA=[0]*n
B=[2*n+1]*(2*(10**5)+1)
for i in reversed(range(n)):
  B[A[i]]=n+i+1
for i in reversed(range(n)):
  AA[i]=B[A[i]]-(i+1)
  B[A[i]]=i+1
count,index,inc=0,0,0
while count<n*k:
  index=(index+inc)%n
  inc=AA[index]+1
  count+=inc  
  if count%n==0:
    count*=(n*k)//count

count-=inc
d=n*k-count
Ans1=A+A
Ans2=AA+AA
Ans=[]
i=index
while i<d+index:
  if i+Ans2[i]>=d+index:
    Ans.append(Ans1[i])
    i+=1
  else:
    i+=Ans2[i]+1
print(*Ans)

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.clear()
    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(" "))
a = list(map(int, input().split(" ")))


s = []
ss = {}
i = 0
while i < n * k:
    if i != 0 and s == [] and i % n == 0:
        i = (n * k // i) * i
        if i == n * k:
            break
    e = a[i % n]
    if e not in ss:
        s.append(e)
        ss[e] = len(s) - 1
        # print("Add", e)
    else:
        s = s[:ss[e]]
        ss = {k: s.index(k) for k in set(s)}
    i += 1
print(' '.join(str(x) for x in s))

Submission status: Time Limit Exceeded

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

t = set()
tt = []
ttt = set()

s = []
for i in range(n*k):
  if a[i%n] not in ttt:
    s.append(a[i%n])
    ttt.add(a[i%n])
  else:
    while a[i%n] in ttt:
      ttt.remove(s.pop())
  if i%n==n-1:
    #print(s)
    if tuple(s) in t:
      want = tuple(s)
      break
    t.add(tuple(s))
    tt.append(tuple(s))

#print(t)
#print(tt)

if len(tt)==k:
  print(*tt[-1])
else:
  idx = 0
  for i in range(len(tt)):
    if tt[i]==want:
      idx = i
  #print(want, idx)
  print(*tt[k%(len(tt)-idx)-1])

Submission status: Time Limit Exceeded

Solution code:
import math

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

    dp = {}
    memo = set()
    # arrays = []
    # if len(set(A)) == len(A):
    #     if K % 2 == 0:
    #         print()
    #     else:
    #         print(*A, sep=" ")
    #     return
    
    L = []
    arrays = {}
    values = set()
    # K = min(K, K % 10 + 10)
    for k in range(K):
        for a in A:
            # print(L, memo)
            if a not in memo:
                i = len(L)
                L.append(a)
                dp[a] = i
                memo.add(a)
            else:
                i = dp[a]
                L = L[:i]
                memo = set(L)
                dp = {x: i for i,x in enumerate(L)}
        if len(L) == 0:
            s = " "
        else:
            s = " ".join(map(str,L))
        if s in values:
            loop = len(arrays)
            # print(loop)
            # print(arrays)
            print(arrays[(K-1) % loop])
            return
        else:
            arrays[k] = s
            values.add(s)



    print(*L, sep=" ")

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

N , K = map(int, input().split())
a = list(input().split())
#print(a)
a = [int(s) for s in a]
b = a
q = 0
while True:
        check = True
        if b == []: break
        for i in range(q+1,len(b)):
                if b[q] == b[i]:
                        b = b[:q] + b[i+1:]
                        check = False
                        break
        if check:
                q += 1
                if q >= len(b): break
#print(b)
if b == []:
        print()
        sys.exit()
#print(a)
x = b
for i in range(K-1):
        x += a
p = 0

while True:
#       print(x)
        check = True
        if x == []: break
        for i in range(p+1,len(x)):
                if x[p] == x[i]:
                        x = x[:p] + x[i+1:]
                        check = False
                        break
        if check:
                p+=1
                if p >= len(x): break


for i in range(len(x)-1):
        print("{} ".format(x[i]),end="")
if len(x): print("{}".format(x[-1]))
else: print()

Submission status: Time Limit Exceeded

Solution code:
import copy
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
pattern = []
l = []
for i in range(K):
    for a in A:
        l.append(a)
        idx = l.index(a)
        if idx < len(l)-1:
            l = l[:idx]
    if len(pattern) > 0 and pattern[0] == l:
        break
    else:
        pattern.append(copy.deepcopy(l))
ans = pattern[(K-1)%len(pattern)]
print(" ".join(list(map(str, ans))))

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
s=[]
results={-1:[]}

for i in range(K):
    for x in A:
        if x not in s:
            s.append(x)
        else:
            while x in s:
                s.pop()
    if s in results.values():
        m = K % (i + 1)
        if m == 0:
            print(*s)
        else:
            print(*results[m-1])
        break
    else:
        results[i]=s[:]
else:
    print(*s)

Submission status: Time Limit Exceeded

Solution code:
import sys

N , K = map(int, input().split())
a = list(input().split())
#print(a)
a = [int(s) for s in a]
b = a
q = 0
while True:
        check = True
        if b == []: break
        for i in range(q+1,len(b)):
                if b[q] == b[i]:
                        b = b[:q] + b[i+1:]
                        check = False
                        break
        if check:
                q += 1
                if q == len(b): break
#print(b)
if b == []:
        print()
        sys.exit()
#print(a)
x = b
for i in range(K-1):
        x += a
p = 0

while True:
#       print(x)
        check = True
        if x == []: break
        for i in range(p+1,len(x)):
                if x[p] == x[i]:
                        x = x[:p] + x[i+1:]
                        check = False
                        break
        if check:
                p+=1
                if p == len(x): break


for i in range(len(x)-1):
        print("{} ".format(x[i]),end="")
if len(x): print("{}".format(x[-1]))
else: print()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = map(int, input().split())
a = list(map(int, input().split()))
s = deque()
for i in range(0, K):
    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
        else:
            s.append(a[j])

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

Submission status: Time Limit Exceeded

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



tmp = []

T = 100000000000000000000000
for i in range(K):
    for j in range(N):
        if A[j] not in tmp :
            tmp.append(A[j])

        else:
            index = len(tmp) - tmp[::-1].index(A[j]) -1
            del tmp[index:]

    tmp_copy = tmp.copy()
    l.append(tmp_copy)

    if l[i] in l[:i]:
        T = i
        break


ans = l[(K-1)%T]
print(' '.join(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

N , K = map(int, input().split())
a = list(input().split())
#print(a)
a = [int(s) for s in a]
b = a
q = 0
while True:
        check = True
        if b == []: break
        for i in range(q+1,len(b)):
                if b[q] == b[i]:
                        b = b[:q] + b[i+1:]
                        check = False
                        break
        if check:
                q += 1
                if q == len(b): break
#print(b)
if b == []:
        print()
        sys.exit()
#print(a)
x = b
for i in range(K-1):
        x += a
p = 0

while True:
#       print(x)
        check = True
        for i in range(p+1,len(x)):
                if x[p] == x[i]:
                        x = x[:p] + x[i+1:]
                        check = False
                        break
        if check:
                p+=1
                if p == len(x): break


for i in range(len(x)-1):
        print("{} ".format(x[i]),end="")
if len(x): print("{}".format(x[-1]))
else: print()

Submission status: Time Limit Exceeded

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

if N==1:
  if K%2:
    print(A[0])
  else:
    print('')
  exit()
  

#print(*range(10000))

def func(L):
  s = []
  for l in L:
    if l in s:
      del s[s.index(l):]
      #s_set.remove(l)
    else:
      s.append(l)
      #s_set.add(l)
  return s

index_dict={}

for i,a in enumerate(A):
  index_dict.setdefault(a,[]).append(i)

#print(index_dict)

from_in_cycle=[]

i = 0
while True:
  a = A[i]
  l = len(index_dict[a])
  j = index_dict[a].index(i)
  if j==l-1:
    from_in_cycle.append(i)
    i = (index_dict[a][0]+1)%N
  else:
    i = (index_dict[a][j+1]+1)%N
  # 循環の評価
  if i==0:
    break
    
#print(from_in_cycle)

if from_in_cycle:
  k = K%(len(from_in_cycle)+1)
  print(*func(A[from_in_cycle[k-1]:]))
else:
  print('')

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
As = list(map(int, input().split()))
S = []
As2 = As + As
for i in range(N):
  a = As[i]
  for j in range(i+1, 2*N):
    if As2[j] == a:
      S.append(j)
      break
#print(S)
count = 0
p = 0
while True:
  n = S[p]+1
  count += n // N
  p = n % N
  if p == 0:
    break
#print(count)
if count < 0:
  print(" ".join(map(str, s)))
else:
  K = K%count
  s = []
  for k in range(K):
    for a in As:
      i = -1
      for j in range(len(s)):
        if s[j] == a:
          i = j
      if i < 0:
        s.append(a)
      else:
        s = s[0:i]
  print(" ".join(map(str, s)))

Submission status: Time Limit Exceeded

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

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

aa = [ ai for i in range(k) for ai in a]

s = []
for i in aa:
  try:
    p = s.index(i)
    del s[p:]
  except ValueError:
    s.append(i)

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:
import copy
n, k = [ int(v) for v in input().split() ]

num_list = [ int(v) for v in input().split() ]

que = []
que_list = [[]]
f_flg = 0
for i in range(k):
    for j in num_list:
        if j not in que:
            que.append(j)
        else:
            que = que[:que.index(j)]

    if que == []:
        f = i+1
        f_flg = 1
        break
    
    que_list.append(copy.deepcopy(que))

    
if f_flg == 0:
    print(*que)
else:
    mod = k % f
    print(*que_list[mod])

Submission status: Time Limit Exceeded

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

t = set()
tt = []

s = []
for i in range(n*k):
  if a[i%n] not in s:
    s.append(a[i%n])
  else:
    while a[i%n] in s:
      s.pop()
  if i%n==n-1:
    #print(s)
    if tuple(s) in t:
      want = tuple(s)
      break
    t.add(tuple(s))
    tt.append(tuple(s))

#print(t)
#print(tt)

if len(tt)==k:
  print(*tt[-1])
else:
  idx = 0
  for i in range(len(tt)):
    if tt[i]==want:
      idx = i
  #print(want, idx)
  print(*tt[k%(len(tt)-idx)-1])

Submission status: Time Limit Exceeded

Solution code:
#from collections import deque

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)

'''
for i in range(K):
    X += A
'''

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

i=0
loop=0
for j in range(K):
    for i in range(N):
        while (A[i] in s):
            del s[-1]
            loop = 1
        if loop == 0:
            s.append(A[i])
        loop=0

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

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(N, 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(N: int, A: np.ndarray, prev_values: np.ndarray) -> np.ndarray:
    values = prev_values.tolist()
    values_to_idx = {v: i for i, v in enumerate(values)}
    for j in range(N):
        if A[j] not in values_to_idx:
            values_to_idx[A[j]] = len(values)
            values.append(A[j])
        else:
            removing = values[values_to_idx[A[j]] :]
            values = values[: values_to_idx[A[j]]]
            for rm in removing:
                if rm not in values_to_idx:
                    continue
                del values_to_idx[rm]
    return np.array(values)


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:
# -*- coding: utf-8 -*-
import sys
import os
import copy


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

    s_set = []
    count = 0
    s = []
    for i in range(K):
        for j in range(N):
            if A[j] not in s:
                s.append(A[j])
            else:
                for k in range(len(s))[::-1]:
                    val = s.pop()
                    if val == A[j]:
                        break
            # print(' '.join(map(str, s)))
        if s in s_set:
            # print('braek')
            # print(s_set)
            # print(s)
            break
        # print('-------- count: %d ------' % count)
        # print(' '.join(map(str, s)))
        s2 = copy.deepcopy(s)
        s_set.append(s2)
        count += 1

    result_set = s_set[K % count-1]
    print(' '.join(map(str, result_set)))


solve()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    idx = [[] for _ in range(int(2e5+1))]
    for i in range(N):
        idx[A[i]].append(i)
    pos = 0
    empty = []
    visited = set()
    while pos == 0 or pos % N != 0:
        empty.append(pos)
        visited.add(pos % N)
        num = A[pos % N]
        nxt = bisect_right(idx[num], pos % N) % len(idx[num])
        npos = idx[num][nxt]
        while npos <= pos:
            npos += N
        pos = npos + 1
    goal = N * K % pos
    cur = empty[max(0, bisect_right(empty, goal) - 1)]
    #print(cur, goal)
    ans = []
    seen = set()
    while cur < goal:
        a = A[cur % N]
        if a not in seen:
            ans += [a]
            seen.add(a)
        else:
            seen.remove(a)
            i = len(ans) - 1
            while i > 0 and ans[i] != a:
                seen.remove(ans[i])
                i -= 1
            ans = ans[:i]
        cur += 1
    print(*ans)


if __name__ == "__main__":
    main()