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):
from collections import defaultdict

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

D = defaultdict(list)

for i in range(N):
    D[A[i]].append(i)

next = [None for _ in range(N)]
dist = [None for _ in range(N)]

for v in D.values():
    l = len(v)
    for i in range(l):
        next[v[i]] = (v[(i + 1) % l] + 1) % N
        dist[v[i]] = (v[(i + 1) % l] - v[i] + N - 1) % N + 2

period = 0
node = 0

while node or not period:
    period += dist[node]
    node = next[node]

rem = N * K % period
count = 0
node = 0

while count + dist[node] <= rem:
    count += dist[node]
    node = next[node]

s = []
used = [0 for _ in range(10**6)]

for i in range(rem - count):
    j = (node + i) % N
    if not used[A[j] - 1]:
        used[A[j] - 1] = 1
        s.append(A[j])
    else:
        while True:
            p = s.pop()
            used[p - 1] = 0
            if p == A[j]:
                break

print(*s)

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

main()

Solution (Python):
import bisect

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

ind = [[] for _ in range(2*10**5 + 1)]
for i,num in enumerate(a):
    ind[num].append(i)

for i in range(1,2*10**5 + 1):
    if(ind[i]):
        ind[i].append(ind[i][0] + n)

before = []
cnt = 0
now = a[0]
while(True):
    right = bisect.bisect_right(ind[now], cnt%n)
    cnt += ind[now][right] - ind[now][right-1] + 1
    if(ind[now][right] >= n):
        before.append(ind[now][right-1])
    now = a[cnt%n]
    if(cnt%n == 0):
        break

if(cnt//n==1):
    print('')
    exit()

cyc = k%(cnt//n)

cnt = before[cyc-1]
ans = []
while(cnt < n):
    now = a[cnt]
    right = bisect.bisect_right(ind[now],cnt)
    if(ind[now][right] >= n):
        ans.append(now)
        cnt += 1
    else:
        cnt = ind[now][right] + 1

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

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

N, K = map(int, input().split())
A = list(map(int, input().split()))
first_idx = {}
for i in range(N)[::-1]:
    first_idx[A[i]] = i
next_idx = {}
dist = [[0] * N for _ in range(41)]
for i in range(N)[::-1]:
    if A[i] in next_idx:
        dist[0][i] = next_idx[A[i]] - i + 1
    else:
        dist[0][i] = N - i + first_idx[A[i]] + 1
    next_idx[A[i]] = i
for j in range(40):
    for i in range(N):
        dist[j+1][i] = dist[j][i] + dist[j][(i + dist[j][i]) % N]
s = []
i = 0
while i < N * K:
    for j in range(41)[::-1]:
        if i + dist[j][i % N] <= N * K:
            i += dist[j][i % N]
            break
    else:
        s.append(A[i % N])
        i += 1
print(*s)

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))

last=[-1]*(2*10**5+1)
nex=[-1]*n
prog=[0]*n

for i,a in enumerate(A*2):
    l=last[a]
    last[a]=i%n
    if nex[l]==-1 and l!=-1:
        nex[l]=(i+1)%n
        if i>=n or i+1==n:
            prog[l]=1
if nex[-1]==0:
    prog[-1]+=1

flag=False 
stage=0
now=0
roop=[-1]*n
while True:
    if roop[now]!=-1 and flag==False:
        flag=True
        rr=stage-roop[now]
        stage=k-1-(k-1-stage)%rr
    if stage+prog[now]>=k:
        break
    roop[now]=stage
    stage+=prog[now]
    now=nex[now]

A=A[now:]+A if k-1-stage else A[now:]
check=[0]*(2*10**5+1)
stack=[]
for a in A:
    if check[a]:
        while check[a]:
            check[stack.pop()]=0
        continue
    stack.append(a)
    check[a]=1

print(*stack)

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))

last=[-1]*(2*10**5+1)
nex=[-1]*n
prog=[0]*n

for i,a in enumerate(A*2):
    l=last[a]
    last[a]=i%n
    if nex[l]==-1 and l!=-1:
        nex[l]=(i+1)%n
        if i>=n or i+1==n:
            prog[l]=1
if nex[-1]==0:
    prog[-1]+=1

flag=False 
stage=0
now=0
roop=[-1]*n
while True:
    if roop[now]!=-1 and flag==False:
        flag=True
        rr=stage-roop[now]
        stage=k-1-(k-1-stage)%rr
    if stage+prog[now]>=k:
        break
    roop[now]=stage
    stage+=prog[now]
    now=nex[now]

A=A[now:]+A if k-1-stage else A[now:]
check=[0]*(2*10**5+1)
stack=[]
for a in A:
    if check[a]:
        while check[a]:
            check[stack.pop()]=0
        continue
    stack.append(a)
    check[a]=1

print(*stack)

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

dic = {}
for i in range(n):
    if a[i] in dic:
        dic[a[i]].append(i)
    else:
        dic[a[i]] = [i]

zero = [-1 for i in range(n)]
for key, value in dic.items():
    num = len(value)
    for i in range(num-1):
        zero[value[i]] = value[i+1] - value[i] + 1
    zero[value[-1]] = n - value[-1] + value[0] + 1

lst = [zero]
while lst[-1][0] <+ n*k - 1:
    before = lst[-1]
    after = []
    for i in range(n):
        after.append(before[i] + before[(before[i]+i)%n])
    lst.append(after)
lst.pop()

i = 0
while lst != []:
    lsti = lst.pop()
    while True:
        if i + lsti[i%n] <= n*k - 1:
            i = i + lsti[i%n]
        else:
            break

i = i % n
nokori = a[i:]
lst = []
dic = {}
ko = 0
for i in range(len(nokori)):
    if nokori[i] not in dic:
        lst.append(nokori[i])
        dic[nokori[i]] = ko
        ko += 1
    else:
        lst = lst[:dic[nokori[i]]]
        dic = {}
        for j in range(len(lst)):
            dic[lst[j]] = j
        ko = len(lst)
print(*lst)

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

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

dp = [[-1]*N for _ in range(log_size)]
idx = defaultdict(int)

for i in range(2*N):
    if A[i%N] in idx and idx[A[i%N]]<N:
        dp[0][idx[A[i%N]]] = i+1-idx[A[i%N]]
    
    idx[A[i%N]] = i

for i in range(1, log_size):
    for j in range(N):
        
        dp[i][j] = min(10**18, dp[i-1][j]+dp[i-1][(j+dp[i-1][j])%N])

now = 0
NK = N*K

for i in range(log_size-1, -1, -1):
    if NK-dp[i][now]>=0:
        NK -= dp[i][now]
        now = (now+dp[i][now])%N

if NK==0:
    print()
    exit()
    
q = deque([])
cnt = defaultdict(int)

for i in range(now, N):
    if cnt[A[i]]>0:
        while cnt[A[i]]>0:
            rem = q.pop()
            cnt[rem] -= 1
    else:
        cnt[A[i]] += 1
        q.append(A[i])

print(*q)

Solution (Python):
from bisect import bisect_left
from collections import defaultdict
N, K, *A = map(int, open(0).read().split())
dic = defaultdict(list)
for i, a in enumerate(A):
  dic[a] += [i]
ls = []
ps = 0
m = A[0]
cycle = 0
while True:
  ls.append(ps+N*cycle+1)
  i = bisect_left(dic[m],ps+1)
  ps = dic[m][i%len(dic[m])]
  if i>=len(dic[m]):
    cycle += 1
  ps += 1
  ps %= N
  m = A[ps]
  if ps==0:
    cycle += 1
    break
k = K%cycle
j = bisect_left(ls,k*N)
if ls[j]==k*N:
  c = (ls[j]-1)%N
  print(A[c])
elif ls[j]==k*N+1:
  pass
else:
  c = (ls[j-1]-1)%N
  M = k*N-ls[j-1]
  ans = [A[c]]
  log = set(ans)
  ind = {}
  ind[A[c]] = 0
  cnt = 0
  while M:
    c += 1
    c %= N
    cnt += 1
    cnt %= N
    M -= 1
    if not A[c] in log:
      log.add(A[c])
      ind[A[c]] = cnt
      ans += [A[c]]
    else:
      log.remove(A[c])
      ans = ans[:ind[A[c]]]
      log = set(ans)
      cnt = ind[A[c]]-1
  print(*ans)

Solution (Python):
from collections import defaultdict
from bisect import bisect_right
N,K,*A = map(int, open(0).read().split())
a = A[0]
p = 0
ls = []
dic = defaultdict(list)
for i,c in enumerate(A):
  dic[c].append(i)
while True:
  ls.append(p)
  ind = bisect_right(dic[a],p%N)
  if ind==len(dic[a]):
    ind = 0
    p += N
  p = (p//N)*N+dic[a][ind]+1
  if p%N==0:
    M = p//N
    break
  a = A[p%N]
K %= M
X = N*K
I = bisect_right(ls,X)-1
X -= ls[I]
p = ls[I]%N
S = set()
cnt = 0
ans = []
while X:
  if A[p] not in S:
    S.add(A[p])
    ans.append(A[p])
    cnt += 1
  else:
    i = ans.index(A[p])
    ans = ans[:i]
    S = set(ans)
    cnt = i
  X -= 1
  p += 1
  p %= N
print(' '.join(map(str,ans)))

Solution (Python):
from bisect import bisect_right
n, k = map(int, input().split())
a = [int(x) for x in input().split()]

d = {}
for i in range(n):
    if a[i] in d:
        d[a[i]].append(i)
    else:
        d[a[i]] = [i]


def simulate(k):
    if k == 0:
        return 0, []
    lap = 1
    res = [0]
    while True:
        s = res.pop()
        nxt = d[a[s]][bisect_right(d[a[s]], s) % len(d[a[s]])] + 1
        if nxt == n:
            if lap == k and s == n-1:
                res.append(s)
            if s == n-1:
                lap += 1
            return lap, res
        elif nxt <= s+1:
            if lap == k:
                res.append(s)
                if s == n-1:
                    return lap, res
                res.append(s+1)
                if s+1 == n-1:
                    return lap, res
            else:
                res.append(nxt)
                lap += 1
        else:  
            s = nxt
            res.append(s)
    return 0


period, ans = simulate(k)

if period != k:
    lap, ans = simulate(k % period)

print(*[a[x] for x in ans])

Solution (Python):
import bisect

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

dic={}
for i in range(N):
    if A[i] not in dic:
        dic[A[i]]=[]
    dic[A[i]].append(i)

check={}
for ai in dic:
    check[ai]=0
    check[ai]=max(dic[ai])


front=[-1]
pos=0
visit=set([-1])
while True:
    val=A[pos]
    if pos>=check[val]:
        front.append(pos)
        if pos in visit:
            break
        else:
            visit.add(pos)
        pos=dic[val][0]+1
        if pos==N:
            front.append(-1)
            break
    else:
        index=bisect.bisect_right(dic[val],pos)
        pos=dic[val][index]+1
        if pos==N:
            front.append(-1)
            break
last=front[-1]
i=front.index(last)
period=len(front)-i-1
const=i
K-=const
K-=1
r=K%period
fff=front[r]
if fff!=-1:
    import heapq
    que=[]
    check={A[i]:0 for i in range(N)}
    index=0
    flag=True
    while N>index:
        if A[index]!=A[fff] and flag:
            index+=1
        elif A[index]==A[fff] and flag:
            index+=1
            flag=False
        else:
            if check[A[index]]==0:
                heapq.heappush(que,(-index,A[index]))
                check[A[index]]=1
                index+=1
            else:
                while True:
                    test,a=heapq.heappop(que)
                    check[a]=0
                    if a==A[index]:
                        break
                check[A[index]]=0
                index+=1
    que.sort(reverse=True)
    que=[que[i][1] for i in range(len(que))]
    print(*que)
else:
    import heapq
    que=[]
    check={A[i]:0 for i in range(N)}
    index=0
    while N>index:
        if check[A[index]]==0:
            heapq.heappush(que,(-index,A[index]))
            check[A[index]]=0
            check[A[index]]=1
            index+=1
        else:
            while True:
                test,a=heapq.heappop(que)
                check[a]=0
                if a==A[index]:
                    break
            check[A[index]]=0
            index+=1
    que.sort(reverse=True)
    que=[que[i][1] for i in range(len(que))]
    print(*que)

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






first = [0] * (N+1)
follow_idx = [-1] * (N+1)

d = {}
for i, a in enumerate(A[::-1]):
  j = N - 1 - i
  
  if a in d:
    idx = d[a][-1] + 1 
    d[a].append(j)
    if first[idx] < 0: 
      first[j] = first[idx] 
      follow_idx[j] = follow_idx[idx] 
    else: 
      first[j] = -first[idx] 
      follow_idx[j] = idx 
  
  
  else:
    first[j] = a 
    d[a] = [j]
    follow_idx[j] = j + 1 



indices = [-1] 
s0 = first[0] 
if s0 >= 0: 
  indices.append(0) 
else:
  s0 = -s0 
  indices.append(follow_idx[0]) 

if s0 == 0: 
  print('')
  quit()




first_a = {0:0, s0:1}
for k in range(2, K+1):
  idx = d[s0][-1] + 1 
  s0 = first[idx] 
  if s0 >= 0: 
    indices.append(idx) 
  else: 
    s0 = -s0 
    indices.append(follow_idx[idx]) 
  if s0 in first_a: 
    break
  first_a[s0] = k 

if len(indices) < K: 
  
  
  
  
  K = first_a[s0] + (K - first_a[s0]) % (k - first_a[s0])

if K == 0: 
  print('')
  quit()

ans = []
idx = indices[K] 
while idx < N: 
  s = first[idx]
  if s > 0: 
    ans.append(s)
  idx = follow_idx[idx]

print(*ans)

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

first = [0] * (N+1)
follow_idx = [-1] * (N+1)
d = {}
for i, a in enumerate(A[::-1]):
  j = N - 1 - i
  if a in d:
    idx = d[a][-1]
    d[a].append(j)
    if first[idx + 1] < 0:
      first[j] = first[idx + 1]
      follow_idx[j] = follow_idx[idx + 1]
    else:
      first[j] = -first[idx + 1]
      follow_idx[j] = idx + 1
  else:
    first[j] = a
    d[a] = [j]
    follow_idx[j] = j + 1


indices = [-1]
s0 = first[0]
if s0 >= 0:
  indices.append(0)
else:
  s0 = -s0
  indices.append(follow_idx[0])
    
if s0 == 0:
  print('')
  quit()

d2 = {0:0, s0:1}
for k in range(2, K+1):
  idx = d[s0][-1] + 1
  s0 = first[idx]
  if s0 >= 0:
    indices.append(idx)
  else:
    s0 = -s0
    indices.append(follow_idx[idx])
  if s0 in d2:
    break
  d2[s0] = k

if len(indices) < K:
  K = d2[s0] + (K - d2[s0]) % (k - d2[s0])

if K == 0:
  print('')
  quit()

ans = []
idx = indices[K]
while idx < N:
  s = first[idx]
  if s > 0:
    ans.append(s)
  idx = follow_idx[idx]

print(*ans)

Solution (Python):
from bisect import bisect
from collections import defaultdict
def main():
	n, k = map(int, input().split())
	*a, = map(int, input().split())
	idx = defaultdict(list)
	for i, x in enumerate(a):
		idx[x].append(i)
	m = (k * n).bit_length()
	dub = [[-1] * n for _ in range(m)]
	for i, x in enumerate(a):
		if i == idx[x][-1]:
			dub[0][i] = n + idx[x][0] - i
		else:
			dub[0][i] = idx[x][bisect(idx[x], i)] - i
	
	for i in range(m - 1):
		for j in range(n):
			if dub[i][j]<n*k and dub[i][j]!=-1:
				dub[i + 1][j] = dub[i][j] + 1 + dub[i][(j + dub[i][j] + 1) % n]
	
	now = -1
	for i in range(m - 1, -1, -1):
		if dub[i][(now+1)%n]!=-1 and now + 1 + dub[i][(now + 1) % n] < n * k:
			now += 1 + dub[i][(now + 1) % n]
	
	if now == n * k:
		print("")
		exit()
	b = a[(now + 1) % n:]
	
	inQ = {x: False for x in b}
	Q = []
	for x in b:
		if inQ[x]:
			while Q[-1] != x:
				inQ[Q.pop()] = False
			Q.pop()
			inQ[x] = False
		else:
			inQ[x] = True
			Q.append(x)
	print(" ".join(map(str, Q)))

if __name__ == '__main__':
	main()

Solution (Python):

import sys
from collections import defaultdict
from bisect import bisect_right

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


N, K = lr()
A = lr()
next_index = [-1] * N
dict = defaultdict(list)
for i in range(N):
    dict[A[i]].append(i)

for i in range(N):
    a = A[i]
    length = len(dict[a])
    j = bisect_right(dict[a], i)
    next_index[i] = dict[a][j%length]

cur = 0
time = 1
while cur != N:
    ne = next_index[cur]
    if ne <= cur:
        time += 1
    cur = ne + 1

K %= time
time = 0
cur = 0
while time < K:
    ne = next_index[cur]
    if ne <= cur:
        time += 1
    if time == K:
        break
    cur = ne + 1

answer = []
while cur < N:
    ne = next_index[cur]
    if ne <= cur:
        answer.append(A[cur])
        cur += 1
    else:
        cur = ne + 1

print(*answer)

Solution (Python):
from collections import deque


def solve(i):
    if i in memo:
        return memo[i]
    q = deque([i])
    while True:
        nxt_i = next_ind[i]
        if nxt_i >= n:
            break
        q.append(nxt_i)
        i = nxt_i
    while q:
        memo[q.pop()] = nxt_i % n
    return memo[i]



n, k = map(int, input().split())
a = list(map(int, input().split())) + [-1, -1]
k_time = k
n += 2
aa = a + a
next_ind = [-1] * (2 * n)
tmp_memo = {}
for i in range(2 * n)[::-1]:
    if aa[i] not in tmp_memo:
        tmp_memo[aa[i]] = i + 1
    else:
        next_ind[i] = tmp_memo[aa[i]]
        tmp_memo[aa[i]] = i + 1

log_size = k.bit_length() + 2
double = [[-1] * n for i in range(log_size)]


memo = {}
for i in range(n):
    double[0][i] = solve(i)


for k in range(1, log_size):
    for i in range(n):
        double[k][i] = double[k - 1][double[k - 1][i]]

pos = 0
k_time -= 1
for k in range(log_size):
    if (k_time >> k) & 1:
        pos = double[k][pos]
q = deque([])
set_ = set()
for i in range(pos, n):
    if a[i] not in set_:
        set_.add(a[i])
        q.append(a[i])
    else:
        while True:
            tmp = q.pop()
            set_.remove(tmp)
            if tmp == a[i]:
                break
print(*q)

Solution (Python):
"""
iの直前で空のときに、Xi=Xj(i<j)であるようなjまで進むと、空になる
よってnext[i] = j
iの直前に空のときに、次にiの直前で空が回ってくるまでのidxの変化
jump[i] = ? * N or inf
もしjump[i] != infならできるだけこれを回したあとに、愚直にやるだけでよい
もしjump[i] == infならnext[i]に移動して、これを試す
どこかで周期ができるはずなので鳩ノ巣原理よりO(N)で見つかる
"""












import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    A=LI()
    A *= 2 
    next_ = [-1]*2*N 
    last = defaultdict(lambda : -1) 
    for i,a in enumerate(A):
        if last[a] != -1:
            next_[last[a]] = i
        last[a] = i
    
    now = 0 
    accumulate_step = 0 
    step_size = 0 
    last_step = [-1]*2*N 
    
    while accumulate_step + next_[now] - now  < N*K: 
        if last_step[now]!=-1: 
            step_size = accumulate_step - last_step[now]
            remain = N*K - accumulate_step
            accumulate_step += remain//step_size * step_size
            now = accumulate_step%N
            if remain//step_size: 
                continue
        
        
        last_step[now] = accumulate_step
        before = now 
        now = next_[before] + 1
        accumulate_step += now - before 
        now %= N
    
    
    remain = N*K-accumulate_step
    ans = []
    cnt = defaultdict(int)
    for a in A[2*N-remain:]:
        if not cnt[a]:
            ans.append(a)
            cnt[a] += 1
            continue
        while cnt[a]:
            cnt[ans.pop()] -= 1
    print(*ans)
if __name__ == '__main__':
    main()

Solution (Python):
"""
iの直前で空のときに、Xi=Xj(i<j)であるようなjまで進むと、空になる
よってnext[i] = j
iの直前に空のときに、次にiの直前で空が回ってくるまでのidxの変化
jump[i] = ? * N or inf
もしjump[i] != infならできるだけこれを回したあとに、愚直にやるだけでよい
もしjump[i] == infならnext[i]に移動して、これを試す
どこかで周期ができるはずなので鳩ノ巣原理よりO(N)で見つかる
"""












import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    A=LI()
    A *= 2 
    next_ = [-1]*2*N
    last = defaultdict(lambda : -1)
    for i,a in enumerate(A):
        if last[a] != -1:
            next_[last[a]] = i
        last[a] = i
    
    now = 0
    accumulate_step = 0
    step_size = 0
    last_step = [-1]*2*N
    while accumulate_step + next_[now] - now  < N*K:
        if last_step[now]!=-1:
            step_size = accumulate_step - last_step[now]
            remain = N*K - accumulate_step
            accumulate_step += remain//step_size * step_size
            now = accumulate_step%N
            if remain//step_size:
                continue
        
        last_step[now] = accumulate_step
        before = now 
        now = next_[before] + 1
        accumulate_step += now - before 
        now %= N
    
    remain = N*K-accumulate_step
    ans = []
    cnt = defaultdict(int)
    for a in A[2*N-remain:]:
        if not cnt[a]:
            ans.append(a)
            cnt[a] += 1
            continue
        while cnt[a]:
            cnt[ans.pop()] -= 1
    print(*ans)

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    

    
    
    
    
    
    


    
if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a + a
to = [-1] * n
num = [-1] * (2 * 10 ** 5 + 1)
for i, x in enumerate(b):
    if 0 <= num[x] < n:
        to[num[x]] = i + 1
    num[x] = i

c, now = 1, 0
check = 0
while now != n:
    v = to[now]
    if v > n:
        c += 1
        v -= n

    now = v



k %= c
now = 0
while k > 1:
    v = to[now]
    if v > n:
        k -= 1
        v -= n

    now = v

ans = []

while now < n:
    if to[now] < n:
        now = to[now]
    elif to[now] == n:
        break
    else:
        ans.append(a[now])
        now += 1

print(*ans)

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

N, K = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
L = [0]*N
P = [-1]*(2*10**5+1)
for i in range(2*N):
    j = i%N
    a = A[j]
    if P[a] == -1:
        P[a] = i
    else:
        L[P[a]] = j
        P[a] = j
s = 0
start = 0
v = [True]*N
h = [(0, 0)]
h_app = h.append
while v[start]:
    v[start] = False
    if start < L[start]:
        s += (L[start] - start +1)
    else:
        s += (L[start] - start +N +1)
    start = (L[start]+1)%N
    h_app((s, start))

r = (N*K)%s
import bisect
if r == 0:
    print("")
else:
    res = bisect.bisect(h, (r, 10**10))-1
    s, start = h[res]
    s = r-s
    ans = []
    P = [-1]*(2*10**5+1)
    if s:
        cou = 0
        for i in range(s):
            if P[A[(start+i)%N]] == -1:
                P[A[(start+i)%N]] = cou
                ans.append(str(A[(start+i)%N]))
            else:
                sr = P[A[(start+i)%N]]
                P[A[(start+i)%N]] = -1
                for j in range(cou-sr):
                    a = ans.pop()
                    P[int(a)] = -1
                cou = sr-1
            cou += 1
    print(" ".join(ans))

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



from bisect import bisect_right

N,K = LI()
A = LI()


d = defaultdict(list)
for i in range(N):
    d[A[i]].append(i)


T = [0]*(N+1)
for i in range(N)[::-1]:
    p = bisect_right(d[A[i]],i)
    if p < len(d[A[i]]):
        T[i] = T[d[A[i]][p]+1]
    else:
        T[i] = A[i]


S = defaultdict(list)
index = 0
roop_flag = False
empty_flag = False
for i in range(1,N+2):
    S[index].append(i)
    if len(S[index]) > 1:
        start = S[index][0]
        end = S[index][1]
        roop_flag = True
        break
    n = T[index]
    if n == 0:
        num = i
        empty_flag = True
        break
    index = d[n][0]+1

def request_s(index):
    ret = []
    while index <= N-1:
        p = bisect_right(d[A[index]],index)
        if p < len(d[A[index]]):
            index = d[A[index]][p]+1
        else:
            ret.append(A[index])
            index += 1
    return ret

if roop_flag:
    if K <= start:
        index = 0
        for i in range(K-1):
            index = d[T[index]][0]+1
        ans = request_s(index)
    else:
        index = 0
        for i in range(start-1):
            index = d[T[index]][0]+1
        left = (K-(start-1)) % (end-start+1)
        if left == 0:
            left = end-start+1
        for i in range(left-1):
            index = d[T[index]][0]+1
        ans = request_s(index)
else:
    K = K%num
    index = 0
    if K == 0:
        ans = []
    else:
        for i in range(K-1):
            index = d[T[index]][0]+1
        ans = request_s(index)

print(*ans)

Solution (Python):
from bisect import bisect_left
from collections import defaultdict
N, K, *A = map(int, open(0).read().split())
dic = defaultdict(list)
for i, a in enumerate(A):
  dic[a] += [i]
ls = []
ps = 0
m = A[0]
cycle = 0
while True:
  ls.append(ps+N*cycle+1)
  i = bisect_left(dic[m],ps+1)
  ps = dic[m][i%len(dic[m])]
  if i>=len(dic[m]):
    cycle += 1
  ps += 1
  ps %= N
  m = A[ps]
  if ps==0:
    cycle += 1
    break
k = K%cycle
j = bisect_left(ls,k*N)
if ls[j]==k*N:
  c = (ls[j]-1)%N
  print(A[c])
elif ls[j]==k*N+1:
  pass
else:
  c = (ls[j-1]-1)%N
  M = k*N-ls[j-1]
  ans = [A[c]]
  log = set(ans)
  ind = {}
  ind[A[c]] = 0
  cnt = 0
  while M:
    c += 1
    c %= N
    cnt += 1
    cnt %= N
    M -= 1
    if not A[c] in log:
      log.add(A[c])
      ind[A[c]] = cnt
      ans += [A[c]]
    else:
      log.remove(A[c])
      ans = ans[:ind[A[c]]]
      log = set(ans)
      cnt = ind[A[c]]-1
  print(*ans)

Solution (Python):

n,k = map(int,input().split())
a = list(map(lambda x: int(x)-1, input().split()))
mx = max(a) + 1
num = [[] for _ in range(mx)]
leg = [0 for _ in range(mx)]
for i in range(n):
  num[a[i]].append(i)
  leg[a[i]] += 1

nex = [0 for _ in range(n)]
for j in range(mx):
  nu = num[j]
  le = leg[j]
  for li in range(le-1):
    nex[nu[li]] = nu[li+1]+1
  if nu:
    nex[nu[-1]] = nu[0]+n+1

unit = 0
now = 0
while True:
  tmp = nex[now]
  unit += tmp // n
  now = tmp % n
  
  if now == 0:
    break

k %= unit
k *= n

if k == 0:
  exit()
while True:
  tmp = nex[now]
  if k - (tmp-now) > 0:
    k -= tmp - now
    now = tmp % n
  else:
    ans = a[-k:]
    break
  
l = len(ans)
seen = [0 for _ in range(mx)]
ans2 = []
for li in range(l):
  if seen[ans[li]] == 1:
    while True:
      p = ans2.pop()
      seen[p] = 0
      if p == ans[li]:
        break
  else:
    ans2.append(ans[li])
    seen[ans[li]] = 1
print(' '.join(map(lambda x:str(x+1),ans2)))

Solution (Python):
from bisect import bisect_right
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))
a_to_i = [[] for _ in range(max(A)+1)]
for i in range(N):
    a_to_i[A[i]].append(i)
next_index = [-1]*N
for i in range(N):
    a = A[i]
    j = bisect_right(a_to_i[a], i)
    if j == len(a_to_i[a]):
        next_index[i] = a_to_i[a][0]+N
    else: next_index[i] = a_to_i[a][j]
now = 0
cnt = 0
while True:
    i = next_index[now]
    if i>=N:
        i%=N
        cnt+=1
    else: now = i
    i+=1
    if i>=N:
        cnt+=1
        i%=N
    now = i
    if now == 0:
        break
K%=cnt
if K==0:
    print()
    sys.exit()
ans = [A[0]]
K*=N
K-=1
now = 0
while now<K:
    ni = next_index[now%N]+1
    if(now//N)*N+ni-1 == K:
        ans.pop()
        break
    if (now//N)*N+ni>K:
        now+=1
        ans.append(A[now%N])
    else:
        now = (now//N)*N+ni
        ans[-1] = A[now%N]




print(*ans, sep = ' ')