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

Solution (Python):
import sys
input = sys.stdin.readline
from collections import defaultdict
N,K = map(int,input().split())
A = list(map(int,input().split()))





def answer(N,K,A):
    s = []
    for i in range(N*K):
        a = A[i%N]
        if a in s:
            s = s[:s.index(a)]
        else:
            s.append(a)
    return s

def solve(N,K,A):
    B = A * 2
    NA = [-1]*N
    d = {}
    for i, b in enumerate(B):
        if b in d:
            if NA[d[b]%N] == -1:
                NA[d[b]%N] = i - d[b]+1
            d[b] = i 
        else:
            d[b] = i

    L = 57
    ds = [NA]
    for i in range(L):
        pre = ds[-1]
        temp = [pre[j] + pre[(pre[j]+j)%N] for j in range(N)]
        ds.append(temp)

    cnt = 0
    i = 0

    L -= 1
    NA = ds[L]
    while True:
        add = NA[i]
        nxt = ((i + add)%N)
        if cnt + add > N*K:
            L -= 1
            if L < 0:
                break
            NA = ds[L]
            continue
        cnt += add
        i = nxt


    s = []
    NA = ds[0]
    while cnt < N*K:
        a = A[i%N]
        add = NA[i%N]
        if add + cnt > N*K:
            s.append(a)
            i = (i+1) % N
            cnt += 1
        else:
            i = (i+add) % N
            cnt += add

    return s
print(*solve(N,K,A))

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
double_As = As*2
idx_dic = {}
idx_lis = [N+1]*(2*N)
for i in range(N*2-1, -1, -1):
    a = double_As[i]
    if a in idx_dic:
        idx_lis[i] = idx_dic[a]+1
    idx_dic[a] = i
doubling = [[0]*N for _ in range(60)]
accum = [[0]*N for _ in range(60)]
doubling[0] = [i%N for i in idx_lis[:N]]
accum[0] = [a-i for i, a in enumerate(idx_lis[:N])]
for i in range(1, 60):
    for j in range(N):
        doubling[i][j] = doubling[i-1][doubling[i-1][j]]
        accum[i][j] = accum[i-1][j] + accum[i-1][doubling[i-1][j]]
now = 0
cum = 0
cnt = 0
for i in range(59, -1, -1):
    if cum + accum[i][now]>N*K:
        continue
    else:
        cum += accum[i][now]
        now = doubling[i][now]
ans = []
while cum<N*K:
    if cum+accum[0][now]<=N*K:
        cum += accum[0][now]
        now = doubling[0][now]
    else:
        ans.append(As[now])
        now += 1
        cum += 1
print(*ans)

Solution (Python):
import sys

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


def main():
    from collections import defaultdict

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

    c = N * K
    b = 0
    while 2 ** b < c:
        b += 1

    doubling = [[0] * N for _ in range(b + 1)]

    idx = defaultdict(list)
    for i, x in enumerate(A):
        idx[x].append(i)

    for i in range(1, 200001):
        d = idx[i]
        l = len(d)
        for j in range(l):
            cur = d[j]
            nx = d[(j + 1) % l]
            if nx <= cur:
                doubling[0][cur] = N - (cur - nx) + 1
            else:
                doubling[0][cur] = nx - cur + 1

    for i in range(1, b + 1):
        for j in range(N):
            p = doubling[i - 1][j]
            doubling[i][j] = p + doubling[i - 1][(j + p) % N]

    cnt = 0
    cur = 0
    while c - cnt > 200000:
        for i in range(b, -1, -1):
            if cnt + doubling[i][cur] < c:
                cnt += doubling[i][cur]
                cur = cnt % N
                break

    rem = c - cnt
    res = []
    res_set = set()

    while rem > 0:
        num = A[cur]
        if num in res_set:
            while True:
                back = res[-1]
                res.pop()
                res_set.discard(back)
                if back == num:
                    break
        else:
            res.append(num)
            res_set.add(num)
        rem -= 1
        cur += 1
        cur %= N

    print(*res)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import *
N,K=map(int,input().split())
A=list(map(int,input().split()))
siz=200814
Y=[[] for i in range(siz)]
p=[]
for i in range(N):
  Y[A[i]].append(i)
  p.append(i)
for i in range(N):
  Y[A[i]].append(N+i)
  p.append(i)
def f(x):
  S=set()
  r=[]
  for i in range(x,N):
    if A[i] in S:
      while True:
        S.discard(r[-1])
        if r[-1]==A[i]:
          del r[-1]
          break
        del r[-1]
    else:
      r.append(A[i])
      S.add(A[i])
  return r

Z=-1
R=0
V=0
while True:
  Z+=1
  V=Y[A[p[Z]]][bisect_right(Y[A[p[Z]]],Z)]
  if V>=N and R==K-1:
    print(*f(Z))
    exit()
  if V>=N:
    R+=1
    V=p[V]
  Z=V
  if Z==N-1:
    K%=(R+1)
    Z=-1
    R=0
    break
if K==0:
  print()
  exit()
while True:
  Z+=1
  V=Y[A[p[Z]]][bisect_right(Y[A[p[Z]]],Z)]
  if V>=N and R==K-1:
    print(*f(Z))
    exit()
  if V>=N:
    R+=1
    V=p[V]
  Z=V

Solution (Python):
import sys
input = sys.stdin.buffer.readline




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


import bisect

def main():
	n,k=MI()
	A=LI()
	B=[[] for _ in range(2*10**5+10)]

	for i,v in enumerate(A):
		B[v-1].append(i)

	for li in B:
		if li:
			li.append(li[0])

	
	

	C=[]

	s=0
	t=-1

	while t<n-1:
		a=A[s]-1
		ti=bisect.bisect_left(B[a][:-1],s)+1
		t=B[a][ti]
		
		if s>=t:
			C.append(s)
		s=t+1
	

	k%=len(C)+1
	

	if k==0:
		print()
		exit()

	s=C[k-1]
	

	ans=[]

	while s<n:
		a=A[s]-1
		si=bisect.bisect_left(B[a][:-1],s)
		if si+1==len(B[a][:-1]):
			ans.append(a+1)
			s+=1
		else:
			s=B[a][si+1]+1
	print(*ans)



if __name__ == "__main__":
	main()

Solution (Python):
import sys
input = sys.stdin.buffer.readline




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


import bisect

def main():
	n,k=MI()
	A=LI()
	B=[[] for _ in range(2*10**5+10)]

	for i,v in enumerate(A):
		B[v-1].append(i)

	for li in B:
		if li:
			li.append(li[0])

	
	

	C=[]

	s=0
	t=-1

	while t<n-1:
		a=A[s]-1
		ti=bisect.bisect_left(B[a][:-1],s)+1
		t=B[a][ti]
		
		if s>=t:
			C.append(s)
		s=t+1
	

	k%=len(C)+1
	

	if k==0:
		print()
		exit()

	s=C[k-1]
	

	ans=[]

	while s<n:
		a=A[s]-1
		si=bisect.bisect_left(B[a][:-1],s)
		if si+1==len(B[a][:-1]):
			ans.append(a+1)
			s+=1
		else:
			s=B[a][si+1]+1
	print(*ans)



if __name__ == "__main__":
	main()

Solution (Python):
def examA():
    N = DI()/dec(7)
    ans = N
    print(N)
    return

def examB():
    N, K = LI()
    A = LI()
    maxA = max(A)

    def function(s):
        stack = deque()
        num = [0]*(maxA+1)
        for i in range(s,N):
            a = A[i]
            if num[a]==0:
                stack.append(a)
                num[a] += 1
            else:
                while(num[a]>0):
                    num[stack.pop()] -= 1

        return stack

    B = [-1 for _ in range(N)]
    que = {}
    for i in range(N):
        a = A[i]
        if a not in que:
            que[a] = i
        else:
            prev = que.pop(a)
            B[prev] = i - prev
            que[a] = i
    i = 0
    while(que):
        a = A[i]
        if a in que:
            prev = que.pop(a)
            B[prev] = N + i - prev
        i += 1
    
    locat_doubling = [[-1]*N for _ in range(51)]

    for i in range(N):
        locat_doubling[0][i] = 0
        locat_doubling[1][i] = B[i] + 1

    for k in range(2,51):
        for i in range(N):
            locat_doubling[k][i] = locat_doubling[k-1][i] + locat_doubling[k-1][(i+locat_doubling[k-1][i])%N]
            if locat_doubling[k][i]>inf:
                locat_doubling[k][i] = inf
    
    now = 0
    rest = K*N
    for i in range(51)[::-1]:
        if locat_doubling[i][now]<=rest:
            rest -= locat_doubling[i][now]
            now = (locat_doubling[i][now]+now)%N
    

    ans = function(now)
    print(" ".join(map(str,ans)))
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
aa=a+a
lp=[-1]*n
dn={}
for i in range(2*n):
    x=aa[i]
    if x in dn:
        dn[x].append(i)
    else:
        dn[x]=[i]
from bisect import bisect_right,bisect_left
now_idx=0
ka=set(())
cnt=1
while True:
    v=a[now_idx]
    tmp=bisect_right(dn[v],now_idx)
    next_idx=dn[v][tmp]
    if next_idx>=n:
        cnt+=1
        next_idx%=n
    if next_idx in ka:
        break
    ka.add(next_idx)
    lp[next_idx]=cnt
    now_idx=next_idx+1
    now_idx%=n
ml=max(lp)
k%=ml
ans=[]

c=set(())
for i in range(n):
    if lp[-1-i]==-1 or lp[-1-i]%ml!=k:
        ans.append(str(a[-1-i]))
    else:
        break
ans.reverse()
dnn={}
m=len(ans)
for i in range(m):
    if ans[i] in dnn:
        dnn[ans[i]].append(i)
    else:
        dnn[ans[i]]=[i]
aans=[]
i=0
while i<m:
    tmp=bisect_right(dnn[ans[i]],i)
    if tmp==len(dnn[ans[i]]):
        aans.append(ans[i])
        i+=1
    else:
        i=dnn[ans[i]][tmp]+1
print(' '.join(aans))

Solution (Python):
import bisect
N, K, *A = [int(_) for _ in open(0).read().split()]
K -= 1
L = [[] for _ in range(2 * 10**5 + 1)]
for i, a in enumerate(A * 2):
    L[a] += [i]
before_next = [L[a][bisect.bisect(L[a], i)] for i, a in enumerate(A)]
first_cycle = [None] * N
now = 0
idx = 0
cycle = 0
while True:
    if first_cycle[now] is not None:
        break
    first_cycle[now] = cycle
    now = before_next[now] + 1
    dcycle, now = divmod(now, N)
    cycle += dcycle
cycle2 = cycle
cycle1 = first_cycle[now]

K2 = cycle1 + (K - cycle1) % (cycle2 - cycle1)
if K2:
    f1 = f2 = 0
    for i in range(N - 1, -1, -1):
        if first_cycle[i] == K2 - 1:
            f1 = 1
            break
    for j in range(N - 1, -1, -1):
        if first_cycle[j] == K2:
            f2 = 1
            break
    if f2:
        B = A[j:]
    elif f1:
        B = A[i:] + A
else:
    B = A
ans = []
cnt = [0] * (2 * 10**5 + 1)
for a in B:
    if cnt[a] > 0:
        while True:
            cnt[ans.pop()] -= 1
            if cnt[a] == 0:
                break
    else:
        ans += [a]
        cnt[a] += 1
print(' '.join(map(str, ans)))

Solution (Python):

printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7


def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()


prev = [-1]*200001
nxt = [-1]*(n+1)
for i in range(2*n):
    v = a[i%n]
    if 0<=prev[v]<n:
        nxt[prev[v]] = (i+1)%n
    prev[v] = i




last = [-1]*(2*n)
cur = 0
cyc = 0
dolast = False
for i in range(2*n):
    last[cur] = cyc
    nx = nxt[cur]
    if nx<cur+2:
        if cyc==k-1:
            dolast = True
            break
        cyc += 1 + (1 if cur==n-1 and nx==0 else 0)
    cur = nx
    if last[nx]>=0:
        break


if not dolast:
    
    d = cyc-last[nx]
    cyc += ((k-1-cyc)//d)*d
    

    
    while cyc<k:
        nx = nxt[cur]
        if nx<cur+2:
            if cyc==k-1:
                break
            cyc += 1 + (1 if cur==n-1 and nx==0 else 0)
        cur = nx



last = [-1]*200001
b = [] 

if cyc<k:
  for i in range(cur,n):
    j = last[a[i]]
    if j>=0:
        while b[-1]!=a[i]:
            last[b[-1]] = -1
            b.pop()
        last[b[-1]] = -1
        b.pop()
    else:
        b.append(a[i])
        last[a[i]] = i


pad = ''
for x in b:
    printn(pad+str(x))
    pad = ' '
print('')

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi
from operator import mul
from functools import reduce
from pprint import pprint
from copy import deepcopy


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


n, k = LI()
A = LI()
D = defaultdict(list)
for i in range(n):
    D[A[i]] += [i]


nxt_idx = [0] * n
for idx_list in D.values():
    for j in range(len(idx_list)):
        nxt_idx[idx_list[j]] = idx_list[(j + 1) % len(idx_list)]



ret = 0
now = 0
L = []
visited = set()
while now not in visited:
    visited.add(now)
    nxt = nxt_idx[now]
    diff = (nxt - now) % n if nxt != now else n
    now = (nxt + 1) % n
    ret += diff + 1
    L += [[ret - 1, nxt]]


ans = []
if (n * k - 1) < ret:
    ss = set()
    for i in range(n * k):
        a = A[i % n]
        if a in ss:
            while a in ss:
                ss.remove(ans.pop())
        else:
            ans += [a]
            ss.add(a)
    print(*ans)
    exit()


ll = (n * k - 1) % ret
bb = bisect_right(L, [ll, INF]) - 1
cc, last_idx = L[bb]
ss = set()
for i in range(ll - cc):
    a = A[(last_idx + i + 1) % n]
    if a in ss:
        while a in ss:
            ss.remove(ans.pop())
    else:
        ans += [a]
        ss.add(a)



print(*ans)

Solution (Python):
import collections

n,k=map(int,input().split())
arr=list(map(int,input().split()))
pos=[[] for _ in range(max(arr)+1)]
for i in range(2*n):
  pos[arr[i%n]].append(i)
edge=[[] for _ in range(n)]
cnt=collections.defaultdict(int)
for i in range(n):
  s=pos[arr[i]][cnt[arr[i]]]
  g=pos[arr[i]][cnt[arr[i]]+1]
  edge[s].append((g+1)%n)
  edge[s].append((g+1)//n)
  cnt[arr[i]]+=1
start=0
loop=0
for _ in range(n):
  loop+=edge[start][1]
  start=edge[start][0]
  if start==0:
    break
k%=loop
if k==0:
  print('')
else:
  k-=1
  start=0
  loop=0
  for _ in range(n):
    if loop==k:
      break
    loop+=edge[start][1]
    start=edge[start][0]
  s=set()
  q=collections.deque()
  for i in range(start,n):
    if arr[i] not in s:
      s.add(arr[i])
      q.append(arr[i])
    else:
      while 1:
        tmp=q.pop()
        s.discard(tmp)
        if tmp==arr[i]:
          break
  print(*list(q))

Solution (Python):
from collections import defaultdict
from bisect import bisect_right
import sys
input=sys.stdin.readline


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

    dd = defaultdict(list)
    cc = defaultdict(list)

    for i, j in enumerate(A):
        dd[j].append(i)

    pos, loop = 0, 1
    ans = []

    while pos < N:
        num = A[pos]
        pp = bisect_right(dd[num], pos)
        if pp < len(dd[num]):
            pos = dd[num][pp]+1
        else:
            pos = dd[num][0]+1            
            loop += 1
        cc[loop].append(pos)

    pos = cc[K%loop][0] if cc[K%loop] else 0
    while pos < N:
        num = A[pos]
        pp = bisect_right(dd[num], pos)
        if pp < len(dd[num]):
            pos = dd[num][pp]+1
        else:
            ans.append(num)
            pos += 1
    print(*ans)


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import defaultdict
from bisect import bisect_right
import sys
input=sys.stdin.readline


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

    dd = defaultdict(list)
    cc = defaultdict(list)

    for i, j in enumerate(A):
        dd[j].append(i)

    pos, loop = 0, 1
    ans = []

    while pos < N:
        num = A[pos]
        pp = bisect_right(dd[num], pos)
        if pp < len(dd[num]):
            pos = dd[num][pp]+1
        else:
            pos = dd[num][0]+1            
            loop += 1
        cc[loop].append(pos)

    pos = cc[K%loop][0] if cc[K%loop] else 0
    while pos < N:
        num = A[pos]
        pp = bisect_right(dd[num], pos)
        if pp < len(dd[num]):
            pos = dd[num][pp]+1
        else:
            ans.append(num)
            pos += 1
    print(*ans)


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import defaultdict
from bisect import bisect_right

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

dd = defaultdict(list)
cc = defaultdict(list)

for i, j in enumerate(A):
    dd[j].append(i)

pos, loop = 0, 1
ans = []

while pos < N:
    num = A[pos]
    pp = bisect_right(dd[num], pos)
    if pp < len(dd[num]):
        pos = dd[num][pp]+1
    else:
        pos = dd[num][0]+1            
        loop += 1
    cc[loop].append(pos)

pos = cc[K%loop][0] if cc[K%loop] else 0
while pos < N:
    num = A[pos]
    pp = bisect_right(dd[num], pos)
    if pp < len(dd[num]):
        pos = dd[num][pp]+1
    else:
        ans.append(num)
        pos += 1
print(*ans)

Solution (Python):
from collections import defaultdict
from bisect import bisect_right

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

dd = defaultdict(list)
for i, j in enumerate(A):
    dd[j].append(i)

pos = 0
loop = 1

cic = defaultdict(list)

while pos < N:
    num = A[pos]
    
    pp = bisect_right(dd[num], pos)
    if pp < len(dd[num]):
        pos = dd[num][pp]+1
    else:
        pos = dd[num][0]+1            
        loop += 1
    cic[loop].append(pos)

pos = cic[K%loop][0] if cic[K%loop] else 0
ans = []
while pos < N:
    num = A[pos]
    pp = bisect_right(dd[num], pos)
    if pp < len(dd[num]):
        pos = dd[num][pp]+1
    else:
        ans.append(num)
        pos += 1
print(*ans)

Solution (Python):
from collections import defaultdict, Counter
from bisect import bisect_right, bisect_left

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

c = Counter(A)
target = []
d = defaultdict(list)

dd = defaultdict(list)
for i, j in enumerate(A):
    dd[j].append(i)

pos = 0
loop = 1
l = []

while pos < N:
    num = A[pos]
    
    pp = bisect_right(dd[num], pos)
    if pp < len(dd[num]):
        pos = dd[num][pp]+1
    else:
        pos = dd[num][0]+1            
        loop += 1
    l.append((num, pos, loop))

for v, pos, p in l:
    if (K-p)%loop==0:
        break

pos = pos if pos<N else 0
ans = []
while pos < N:
    num = A[pos]
    pp = bisect_right(dd[num], pos)
    if pp < len(dd[num]):
        pos = dd[num][pp]+1
    else:
        ans.append(num)
        pos += 1
print(*ans)

Solution (Python):
from bisect import bisect_right
from collections import defaultdict

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

indices_of_elem = defaultdict(list)
for i, A in enumerate(As):
    indices_of_elem[A].append(i)

empty_index = [0]
while True:
    cur_index = empty_index[-1]
    look_for_elem = As[cur_index%N]
    index_of_indices = bisect_right(indices_of_elem[look_for_elem], cur_index%N)
    if len(indices_of_elem[look_for_elem]) ==  index_of_indices:
        pair_end_index = (cur_index // N) * N + indices_of_elem[look_for_elem][0] + N
    else:
        pair_end_index = (cur_index // N) * N + indices_of_elem[look_for_elem][index_of_indices]

    new_index = pair_end_index + 1
    empty_index.append(new_index)
    if new_index % N == 0:
        break
loop = new_index // N



K %= loop

if K == 0:
    exit()
else:
    start = empty_index[bisect_right(empty_index, K * N) - 1] % N
    tmp_ans = As[start:]



index_for_tmp_ans = dict()
split = []
for i, elem in enumerate(tmp_ans):
    if elem in index_for_tmp_ans:
        split.append((index_for_tmp_ans[elem], i))
        while True:
            k, v = index_for_tmp_ans.popitem()
            if k == elem:
                break
    else:
        index_for_tmp_ans[elem] = i

for start, end in split:
    tmp_ans[start:end+1] = [-1] * (end - start + 1)

for ans in tmp_ans:
    if ans != -1:
        print(ans, end = ' ')

Solution (Python):
N, K, *A = map(int, open(0).read().split())

nxt = [-1] * N
prev = {}
for i, a in enumerate(A + A):
    if a in prev:
        nxt[prev[a]] = i % N
    prev[a] = i % N

cur = 0
k = 0
while k < K - 1:
    if cur >= nxt[cur]:
        k += 1
    cur = (1 + nxt[cur]) % N
    if cur == 0:
        k = K - (K % (k + 1))

ans = []
while cur < N:
    if cur >= nxt[cur]:
        ans.append(A[cur])
        cur += 1
    else:
        cur = 1 + nxt[cur]

print(*ans)

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,k = map(int,ipt().split())
    a = list(map(int,ipt().split()))



    nxt = [-1]*(n)
    pa = [-1]*(2*10**5+1)
    for i,ai in enumerate(a*2):
        pai = pa[ai]
        if pai == -2:
            continue
        if pai == -1:
            pa[ai] = i
        else:
            nxt[pai] = i-pai+1
            if i <= n-1:
                pa[ai] = i
            else:
                pa[ai] = -2
    dbl = [nxt]
    ni = 1
    while ni <= n*k:
        nnxt = [0]*n
        pnxt = dbl[-1]
        for i in range(n):
            nnxt[i] = pnxt[i]+pnxt[(i+pnxt[i])%n]
        dbl.append(nnxt)
        if min(nnxt) > n*k:
            break
        ni *= 2
    np = 0
    ld = len(dbl)
    ni = len(dbl)-1

    for db in dbl[::-1]:
        while True:
            if db[np%n] < n*k-1-np:
                np += db[np%n]
            else:
                break

    ans = []
    np %= n
    while np < n:
        if nxt[np]+np > n:
            ans.append(a[np])
            np += 1
        elif nxt[np]+np == n:
            np = n
        else:
            np = nxt[np]+np
    print(" ".join(map(str,ans)))



    return

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))


import bisect

n,k = readints()

a = readints()

place = [[] for i in range(max(a)+1)]

for i,x in enumerate(a):
    place[x].append(i)

now = 0
i = 1
while k>1:
    x = a[now]
    if now >= place[x][-1]:
        i+=1
        k-=1
        now = place[x][0]+1
    else:
        now = place[x][bisect.bisect_right(place[x],now)] + 1
    if now == n:
        now = 0
        k-=1
        k%=i
        if k<1:
            k+=i

ans = []
while now <= n-1:
    x = a[now]
    if now >= place[x][-1]:
        ans.append(x)
        now += 1
    else:
        now = place[x][bisect.bisect_right(place[x],now)] + 1

printline(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    A=LI()
    if N==1:
        if K%2==0:
            ans=[]
            print(' '.join(map(str, ans)))
        else:
            print(A[0])
    else:
        
        
        
        
        M=2*(10**5)
        L=[[]for _ in range(M+1)]
        
        A=A+A+A
        for i in range(N*3):
            L[A[i]].append(i)
            
        nxt=[0]*(N+1)
        
        import bisect
        for i in range(N-1,-1,-1):
            num=bisect.bisect_right(L[A[i]],i)
            nxt_st=L[A[i]][num]
            if nxt_st>=N:
                if nxt_st==2*N-1:
                    nxt[i]=N
                else:
                    nxt[i]=nxt_st+1-N
            else:
                if nxt_st==N-1:
                    nxt[i]=N
                nxt[i]=nxt[(nxt_st+1)%N]
            
            
            
            
            
            
            
        
        N2=50
        dub=[[0]*(N+1) for _ in range(N2)]
        for i in range(N):
            dub[0][i]=i
            dub[1][i]=nxt[i]

        
        for j in range(1,N2-1):
            for i in range(N+1):
                dub[j+1][i]=dub[j][dub[j][i]]
                
        
        
        
        st=0
        K-=1
        for j in range(1,N2):
            if K&1:
                st=dub[j][st]
            K=K>>1
            
        if st==N:
            ans=[]
            print(' '.join(map(str, ans)))
        else:

            
            ans=[]
            from collections import defaultdict
            dd = defaultdict(int)
            
            for i in range(st,N):
                if dd[A[i]]:
                    x=-1
                    while x!=A[i]:
                        x=ans.pop()
                        dd[x]=0
                else:
                    ans.append(A[i])
                    dd[A[i]]=1
                    
            print(' '.join(map(str, ans)))
        
    
    
    

main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

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

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,k = LI()
    a = LI()

    x = collections.defaultdict(int)
    t = [0] * n
    for i in range(n-1,-1,-1):
        t[i] = x[a[i]]
        x[a[i]] = i

    for i in range(n-1,-1,-1):
        t[i] = x[a[i]]
        x[a[i]] = i

    e = n * k
    ei = 0
    i = 0
    while ei < e - n*2:
        j = t[i]
        if j <= i:
            ei += j - i + n + 1
        else:
            ei += j - i + 1
        i = (j + 1) % n
        if i % n == 0:
            ei = e // ei * ei

    r = [0] * n
    ri = 0
    b = {}
    while ei < e:
        c = a[ei%n]
        ei += 1
        if c in b and b[c] < ri and r[b[c]] == c:
            ri = b[c]
            del b[c]
        else:
            r[ri] = c
            b[c] = ri
            ri += 1


    return JA(r[:ri], " ")

print(main())

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    f = {}
    p = {}
    l = [-1] * N
    for i, v in enumerate(A):
        if v not in f:
            f[v] = i
            p[v] = i
        else:
            l[p[v]] = i
            p[v] = i
    t = 0
    r = 0
    while r < K - 1:
        if l[t] == -1:
            r += 1
            t = f[A[t]] + 1
        else:
            t = l[t] + 1
        if t == N:
            r += 1
            r = K - (K % r)
            t = 0
    a = []
    while t < N:
        if l[t] == -1:
            a.append(A[t])
            t += 1
        else:
            t = l[t] + 1
    print(' '.join(str(i) for i in a))
main()