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):
N,K = map(int,input().split())
A = list(map(int,input().split()))

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

AA = A+A
last_i = {}
nx = [-1]*N
for i,a in enumerate(reversed(AA)):
    i = 2*N-i-1
    if i < N:
        nx[i] = (last_i[a] - i) % N + 1
    last_i[a] = i

visited = [-1]*N
visited[0] = 0
j = i = 0
while 1:
    p = nx[i]+N if nx[i]==1 else nx[i]
    j += p
    if visited[j%N] < 0:
        visited[j%N] = j
    else:
        break
    i = j%N
cycle = (j - visited[j%N]) // N
K %= cycle
j = i = 0
while 1:
    p = nx[i]+N if nx[i]==1 else nx[i]
    if j+p > N*K:
        break
    j += p
    i = j%N

if j%N == 0:
    if K==1:
        print(*A)
    else:
        print()
else:
    ans = []
    anss = set()
    for a in A[j%N:]:
        if a in anss:
            while ans[-1] != a:
                anss.remove(ans[-1])
                ans.pop()
            anss.remove(a)
            ans.pop()
        else:
            anss.add(a)
            ans.append(a)
    print(*ans)

Solution (Python):
import bisect
from collections import defaultdict
n,k = map(int,input().split())
a = list(map(int,input().split()))
dc = defaultdict(int)
for i in range(n):
  if dc[a[i]]:
    dc[a[i]].append(i)
  else:
    dc[a[i]] = [i]
p = 0
cnt = 1
dcp = defaultdict(int)
while p != n and cnt<k:
  x = a[p]
  if p == dc[x][-1]:
    p = dc[x][0]+1
    cnt += 1
    dcp[cnt] = p
  else:
    q = bisect.bisect_right(dc[x],p)
    p = dc[x][q]+1
if cnt<k:
  k = k%cnt
ans = []
pos = dcp[k]
flg = [0 for i in range(200000+1)]
for i in range(pos,n):
  if flg[a[i]] == 0:
    ans.append(a[i])
    flg[a[i]] = 1
  else:
    while ans[-1] != a[i]:
      t = ans.pop()
      flg[t] = 0
    ans.pop()
    flg[a[i]] = 0
print(*ans)

Solution (Python):
import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
dp = [0] * (N + 1)
d = dd(list)
for i in range(N):
  d[a[i]].append(i)
for i in range(N - 1, -1, -1):
  j = bl(d[a[i]], i)
  if j == len(d[a[i]]) - 1:
    dp[i] = a[i]
  else:
    dp[i] = dp[d[a[i]][j + 1] + 1]
p = dp[0]
t = 1
l = [0, dp[0]]
if l[-1] == 0:
  t -= 1
  l.pop()
d[0].append(-1)
for _ in range(N):
  p = dp[d[p][0] + 1]
  t += 1
  if p == 0: break
  l.append(p)
p = l[(K - 1) % t]
i = d[p][0] + 1
res = []
while i < N:
  j = bl(d[a[i]], i)
  if j < len(d[a[i]]) - 1:
    i = d[a[i]][j + 1]
  else:
    res.append(a[i])
  i += 1
print(*res)

Solution (Python):
import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
dp = [0] * (N + 1)
d = dd(list)
for i in range(N): d[a[i]].append(i)
for i in range(N - 1, -1, -1):
  j = bl(d[a[i]], i)
  if len(d[a[i]]) - 1 > j: dp[i] = dp[d[a[i]][j + 1] + 1]
  else: dp[i] = a[i]
res = [0, dp[0]]
d[0].append(-1)

if dp[0] == 0: res.pop()
while dp[d[res[-1]][0] + 1] > 0:
  res.append(dp[d[res[-1]][0] + 1])
  


if K % len(res) == 0:
  print()
  exit(0)
rres = []
i = d[res[(K - 1) % len(res)]][0] + 1
while i < N:
  j = bl(d[a[i]], i)
  if len(d[a[i]]) - 1 > j: i = d[a[i]][j + 1] + 1
  else:
    rres.append(a[i])
    i += 1
print(*rres)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, A):

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

    ni = []
    for i, a in enumerate(A):
         ni.append(d[a][bisect.bisect_right(d[a], i) % len(d[a])])

    i = 0
    k = 1
    while True:
        j = ni[i]
        if j <= i:
            k +=1
        i = (j + 1) % N

        if i == 0:
            break

    error_print(k, K % k)
    s = []
    ss = set()

    i = 0
    K %= k
    k = 0
    while k < (K-1):
        j = ni[i % N]
        if j <= i:
            k += 1
        i = j + 1

    i += k * N
    s = []
    ss = set()
    for l in range(i, N*K):
        m = l % N
        a = A[m]
        if a in ss:
            while True:
                b = s.pop()
                ss.remove(b)
                if b == a:
                    break
        else:
            s.append(a)
            ss.add(a)
    return ' '.join(map(str, s))


def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))

    
    
    
    
    
    


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
from bisect import bisect_left,bisect
import sys

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

A_max=max(A)
lst=[[] for i in range(A_max+1)]
flag=[0]*(A_max+1)
for i in range(n):
    lst[A[i]].append(i)

flag=[0]*n
data=[]
i=0
while flag[i]==0:
    flag[i]=1
    a=A[i]
    b=bisect(lst[a],i)
    if b==len(lst[a]):
        data.append(i)
        i=lst[a][0]+1
    else:
        i=lst[a][b]+1
    if i==n:
        data.append(n)
        i=0


if not data:
    print()
    sys.exit()
s=len(data)
zzz=data[(K-1)%s]
i=zzz
d=[]
while i<n:
    a=A[i]
    b=bisect(lst[a],i)
    if b<len(lst[a]):
        d.append([i,lst[a][b]])
        i=lst[a][b]+1
        continue
    else:
        i+=1


if d:
    ans=A[zzz:d[0][0]]
    for j in range(len(d)-1):
        ans=list(itertools.chain(ans,A[d[j][1]+1:d[j+1][0]]))
    ans=list(itertools.chain(ans,A[d[-1][1]+1:]))
    print(*ans)
else:
    print(*A[zzz:])

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    idx=[[] for _ in range(max(A)+1)]
    for i,a in enumerate(A):
        idx[a].append(i)

    
    next=[None]*n
    for I in idx:
        if(not I): continue
        s=len(I)
        I.append(I[0]+n)
        for i in range(s):
            next[I[i]]=I[i+1]-I[i]+1

    
    M=10**18 
    nexts=[next]
    for _ in range(60):
        next=nexts[-1]
        nnext=[min(M,next[i]+next[(i+next[i])%n]) for i in range(n)]
        nexts.append(nnext)

    now=0
    for next in nexts[::-1]:
        if(now+next[now%n]<=n*k):
            now+=next[now%n]
    if(now==n*k):
        return

    
    ans=[]
    have=[0]*(max(A)+1)
    for a in A[now%n:]:
        if(not have[a]):
            ans.append(a)
            have[a]=1
        else:
            while(1):
                x=ans.pop()
                have[x]=0
                if(x==a): break

    print(*ans)
resolve()

Solution (Python):
mi = lambda: map(int, input().split())
e = enumerate
n,k = mi()
m = (n+k).bit_length()
a = list(mi())
g = [None]*n

d = dict()
for i,x in e(a):
  d[x] = i
for i,x in e(a):
  g[d[x]] = ((i+1)%n,int(d[x]>=i)+int(i+1==n))
  d[x] = i
db = [g]
for _ in range(m):
  old = db[-1]
  new = [None]*n
  for i in range(n):
    p,q = old[i]
    r,s = old[p]
    new[i] = (r,q+s)
  db.append(new)
cur = 0
st = m
cs = 0
while st>=0:
  p,q = db[st][cur]
  if cs+q < k:
    cs += q
    cur = p
  else:
    st -= 1
    
q = list()
while cur < n:
  if g[cur][1] == 0:
    cur = g[cur][0]
  elif g[cur] == (0,1):
    break
  else:
    q.append(a[cur])
    cur += 1
  
print(' '.join(map(str,q)))

Solution (Python):
def main():
    import bisect
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    d=dict()
    for j,i in enumerate(x):
        if i in d.keys():
            d[i].append(j)
        else:
            d[i]=[j]
    first_word=[""]
    
    def operation(temp_operated,temp_operator):
        l=[i for i in temp_operated]
        s=set(temp_operated)
        for i in temp_operator:
            if i in s:
                while True:
                    if l[-1]!=i:
                        l.pop()
                    else:
                        l.pop()
                        s=set(l)
                        break
            else:
                l.append(i)
                s.add(i)
        return l
    
    if operation("",x)!=[]:
        first_word.append(operation("",x)[0])
    flag=0

    while True:
        w=first_word[-1]
        if w=="":
            break
        t=d[w][0]
        if t==n-1:
            break
        while True:
            ww=x[t+1]
            temp=bisect.bisect_right(d[ww],t+1)
            if temp>=len(d[ww]):
                first_word.append(ww)
                break
            else:
                t=d[ww][temp]
                if t==n-1:
                    flag=1
                    break
        if flag==1:
            break
    
    l=len(first_word)
    k=k%l
    if len(first_word)==1:
        print("")
    elif k-1==0:
        print(*operation("",x))
    else:
        print(*operation("",x[d[first_word[k-1]][0]+1:]))

main()

Solution (Python):






N, K = map(int, input().split())
A = [int(x) for x in input().split()]





kioku = {}
saisyo = {}
idou = {}

for i in range(N):
    if A[i] in kioku:
        idou[kioku.pop(A[i])] = i
    kioku[A[i]] = i
    if A[i] not in saisyo:
        saisyo[A[i]] = i

    























i = 0
ret = []
while True:
    if i in idou:
        i = idou[i]
    else:
        ret.append(i)
        i = saisyo[A[i]]
    if i == N - 1:
        i = 0
        break
    else:
        i += 1





























loop = len(ret) + 1
if K % loop != 0:


    i = ret[K % loop - 1]
    ans = []
    while i <= N - 1:
        if i in idou:
            i = idou[i]
        else:
            ans.append(A[i])
        i += 1
    print(' '.join(map(str, ans)))

Solution (Python):
n,k,*a=map(int,open(0).read().split())
l,r,*b=[[]for _ in range(10**6)]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while 1:
  j=l[i]%len(b[s])
  if(c>k-2)>j:
    r+=s,
    i+=1
  else:i=b[s][j]+1
  c+=j<1
  if i==n:
    if c>k-2:break
    k%=c+1
    i=c=0
  s=a[i]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
l,r,*b=[[]for _ in range(10**6)]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while i<n:
  j=l[i]%len(b[s])
  c+=j<1
  i=b[s][j]+1
  s=a[i%n]
k%=c+1
i=c=0
while i<n:
  j=l[i]%len(b[s])
  if(c>k-2)>j:
    r+=s,
    i+=1
  else:i=b[s][j]+1
  c+=j<1
  s=a[i%n]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
l,r,*b=[[]for _ in range(200003)]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while i<n:
  j=l[i]%len(b[s])
  c+=j<1
  i=b[s][j]+1
  s=a[i%n]
k%=c+1
i=c=0
while i<n:
  j=l[i]%len(b[s])
  if c>k-2and j<1:
    r+=s,
    i+=1
  else:i=b[s][j]+1
  c+=j<1
  s=a[i%n]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
l,*b=[[]for _ in range(200002)]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while i<n:
  j=l[i]%len(b[s])
  c+=j<1
  i=b[s][j]+1
  s=a[i%n]
k%=c+1
i=c=0
r=[]
while i<n:
  j=l[i]%len(b[s])
  if c>k-2and j<1:
    r+=s,
    i+=1
  else:i=b[s][j]+1
  c+=j<1
  s=a[i%n]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
b=[[]for _ in range(max(a)+1)]
l=[]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while i<n:
  m=len(b[s])
  j=l[i]
  c+=j==m
  i=b[s][j%m]+1
  s=a[i%n]
k%=c+1
i=c=0
r=[]
while i<n:
  m=len(b[s])
  j=l[i]
  if c>k-2and j==m:
    r+=s,
    i+=1
  else:i=b[s][j%m]+1
  c+=j==m
  s=a[i%n]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
b=[[]for _ in range(max(a)+1)]
l=[]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while 1:
  t=b[s]
  m=len(t)
  j=l[i]
  c+=j==m
  i=t[j%m]+1
  if i==n:break
  s=a[i]
k%=c+1
s=a[0]
i=c=0
r=[]
while 1:
  t=b[s]
  m=len(t)
  j=l[i]
  f=j==m
  if c>k-2and f:
    r+=s,
    i+=1
  else:i=t[j%m]+1
  c+=f
  if i==n:break
  s=a[i]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
b=[[]for _ in range(max(a)+1)]
l=[]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while 1:
  t=b[s]
  m=len(t)
  j=l[i]
  c+=j==m
  i=t[j%m]+1
  if i==n:break
  s=a[i]
k%=c+1
s=a[0]
i=c=0
r=[]
while c<k-1or i<n:
  t=b[s]
  m=len(t)
  j=l[i]
  f=j==m
  if c>k-2and f:
    r+=s,
    i+=1
  else:i=t[j%m]+1
  c+=f
  s=a[i%n]
print(*r)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
b=[[]for _ in range(max(a)+1)]
l=[]
for i,v in enumerate(a):
  b[v]+=i,
  l+=len(b[v]),
s=a[0]
i=c=0
while 1:
  t=b[s]
  m=len(t)
  j=l[i]
  c+=j==m
  i=t[j%m]+1
  if i==n:break
  s=a[i]
k%=c+1
s=a[0]
i=c=0
while c<k-1:
  t=b[s]
  m=len(t)
  j=l[i]
  c+=j==m
  i=t[j%m]+1
  s=a[i]
r=[]
while 1:
  t=b[s]
  m=len(t)
  j=l[i]
  if j==m:
    r+=s,
    i+=1
  else:i=t[j]+1
  if i==n:break
  s=a[i]
print(*r)

Solution (Python):
from bisect import*
from collections import*
n,k,*a=map(int,open(0).read().split())
b=defaultdict(list)
for i,v in enumerate(a):b[v]+=i,
s=a[0]
i=c=0
while 1:
  t=b[s]
  m=len(t)
  j=bisect(t,i)
  c+=j==m
  i=t[j%m]+1
  if i==n:break
  s=a[i]
k%=c+1
s=a[0]
i=c=0
while c<k-1:
  t=b[s]
  m=len(t)
  j=bisect(t,i)
  c+=j==m
  i=t[j%m]+1
  s=a[i]
r=[]
while 1:
  t=b[s]
  m=len(t)
  j=bisect(t,i)
  if j==m:
    r+=s,
    i+=1
  else:
    i=t[j]+1
  if i==n:
    break
  s=a[i]
print(*r)

Solution (Python):
from bisect import*
from collections import*
n,k,*a=map(int,open(0).read().split())
b=defaultdict(list)
for i,v in enumerate(a):b[v]+=i,
s=a[0]
i=c=0
while 1:
  t=b[s]
  m=len(t)
  j=bisect(t,i)
  c+=j==m
  i=t[j%m]+1
  if i==n:break
  s=a[i]
k%=c+1
s=a[0]
i=c=0
while c<k-1:
  t=b[s]
  m=len(t)
  j=bisect(t,i)
  c+=j==m
  i=t[j%m]+1
  s=a[i]
r=[]
while 1:
  t=b[s]
  m=len(t)
  j=bisect(t,i)
  if j==m:
    r+=s,
    i+=1
  else:
    i=t[j]+1
  if i==n:
    break
  s=a[i]
print(*r)

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

def getDistNexts(As):
    lenA = len(As)
    poss = dict()
    for i, A in enumerate(As):
        if A not in poss:
            poss[A] = i + lenA

    distNexts = [0]*(lenA)
    for i in reversed(range(lenA)):
        distNexts[i] = poss[As[i]] - i
        poss[As[i]] = i
    return distNexts

distNexts = getDistNexts(As)

NK = N*K

iNow = 0
dist = 0
hists = [-1] * N
hists[0] = 0
isSkipped = False
while dist + distNexts[iNow] + 1 < NK:
    dist += distNexts[iNow] + 1
    iNow = dist % N
    if not isSkipped and hists[iNow] != -1:
        period = dist - hists[iNow]
        dist += (NK-1-dist) // period * period
        isSkipped = True
    hists[iNow] = dist

Ss = []
setS = set()
for _ in range(NK-dist):
    A = As[iNow]
    if A in setS:
        S = ''
        while S != A:
            S = Ss.pop()
            setS.remove(S)
    else:
        Ss.append(A)
        setS.add(A)
    iNow = (iNow+1)%N

if Ss:
    print(' '.join(map(str, Ss)))

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

def getDistNexts(As):
    lenA = len(As)
    poss = dict()
    for i, A in enumerate(As):
        if A not in poss:
            poss[A] = i + lenA

    distNexts = [0]*(lenA)
    for i in reversed(range(lenA)):
        distNexts[i] = poss[As[i]] - i
        poss[As[i]] = i
    return distNexts

distNexts = getDistNexts(As)

NK = N*K

iNow = 0
dist = 0
hists = [-1] * N
hists[0] = 0
isSkipped = False
while dist + distNexts[iNow] + 1 < NK:
    dist += distNexts[iNow] + 1
    iNow = dist % N
    if not isSkipped and hists[iNow] != -1:
        period = dist - hists[iNow]
        dist += (NK-1-dist) // period * period
        isSkipped = True
    hists[iNow] = dist

Ss = []
setS = set()
for A in As[iNow:]:
    if A in setS:
        S = ''
        while S != A:
            S = Ss.pop()
            setS.remove(S)
    else:
        Ss.append(A)
        setS.add(A)

if Ss:
    print(' '.join(map(str, Ss)))

Solution (Python):
def solve():
    N, K = map(int, input().split())
    As = list(map(int, input().split()))

    def getDistNexts(As):
        lenA = len(As)
        poss = dict()
        for i, A in enumerate(As):
            if A not in poss:
                poss[A] = i + lenA

        distNexts = [0]*(lenA)
        for i in reversed(range(lenA)):
            distNexts[i] = poss[As[i]] - i
            poss[As[i]] = i
        return distNexts

    distNexts = getDistNexts(As)

    NK = N*K
    maxD = (NK).bit_length() - 1

    dp = [[0]*(N) for _ in range(maxD+1)]
    for i in range(N):
        dp[0][i] = distNexts[i] + 1
    for d in range(1, maxD+1):
        for i in range(N):
            i2 = i + dp[d-1][i]
            dp[d][i] = i2 + dp[d-1][i2%N] - i

    iNow = 0
    dist = 0
    for d in reversed(range(maxD+1)):
        if dist+dp[d][iNow] < NK:
            dist += dp[d][iNow]
            iNow = dist%N

    Ss = []
    setS = set()
    for A in As[iNow:]:
        if A in setS:
            S = ''
            while S != A:
                S = Ss.pop()
                setS.remove(S)
        else:
            Ss.append(A)
            setS.add(A)

    if Ss:
        print(' '.join(map(str, Ss)))


solve()

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

from bisect import bisect_left

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

indexes = {}
for i, a in enumerate(A):
    if not a in indexes.keys():
        indexes[a] = [i]
    else:
        indexes[a].append(i)

L = [N]
ind = N
while True:
    ind = (ind+1)%(N+1)
    if ind == N: break
    a = A[ind]
    if indexes[a][-1] == ind:
        ind = indexes[a][0]
        L.append(ind)
    else:
        loc = bisect_left(indexes[a], ind)
        ind = indexes[a][loc+1]
    

start_ind = L[(K-1)%len(L)] + 1
if start_ind == N+1: start_ind = 0
in_list = {}
for a in A:
    in_list[a] = 0
ans = []
for i in range(start_ind, N):
    a = A[i]
    if in_list[a] == 0:
        ans.append(a)
        in_list[a] = 1
    else:
        while True:
            b = ans.pop()
            in_list[b] -= 1
            if a == b:
                break

for a in ans:
    print(a, end=' ')
print()

Solution (Python):
from collections import deque
N, K = map(int, input().split())
A = list(map(int, input().split()))
next_step = [0] * N
memo = [-1] * (2*10**5)
cnt = 0
for i, a in enumerate(A+A):
    if memo[a-1] >= 0:
        if next_step[memo[a-1]%N]:
            continue
        next_step[memo[a-1]%N] = i - memo[a-1] + 1
        if next_step[memo[a-1]%N] == 0:
            pass
        cnt += 1
    if cnt == N:
        break
    memo[a-1] = i

j = 0
while True:
    if j + next_step[j%N] < N*K:
        j += next_step[j%N]
        if j%N == 0:
            j = max(j, N*K//j*j)
    else:
        break

q = deque()
memo = [-1] * (2*10**5)
cnt = 0
for k in range(j, N*K):
    a = A[k%N]
    if memo[a-1] < 0:
        q.append(a)
        memo[a-1] = cnt
        cnt += 1
    else:
        while True:
            b = q.pop()
            memo[b-1] = -1
            if b == a:
                break

print(*q)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    na = [-1 for _ in range(2*n)]
    nk = n * k

    prevai = {}
    for i, _a in enumerate(a+a):
        if _a in prevai:
            na[prevai[_a]] = (i-prevai[_a])%n + 1
            if na[prevai[_a]] == 1:
                na[prevai[_a]] = n+1
        prevai[_a] = i

    doubling = [[0 for _ in range(n)] for _ in range(61)]
    for i in range(n):
        doubling[0][i] = na[i]
    for i in range(1, 61):
        for j in range(n):
            doubling[i][j] = min(doubling[i-1][j] + doubling[i-1][(j+doubling[i-1][j])%n], nk+5)

    tmp = 0
    for i in range(60, -1, -1):
        if nk - tmp >= doubling[i][tmp%n]:
            tmp += doubling[i][tmp%n]

    if tmp == nk:
        return
    
    ans  = []
    adict = {}
    for i in range(tmp%n, n):
        if a[i] in adict and adict[a[i]] > 0:
            while True:
                _a = ans.pop()
                adict[_a] = 0
                if _a == a[i]:
                    break
        else:
            ans.append(a[i])
            adict[a[i]] = 1
    print(*ans)


if __name__ == '__main__':
    main()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()


B = [[] for i in range(max(A)+1)]
for i, a in enumerate(A+A):
    B[a].append(i)

C = [0] * N
for li in B:
    for i in range(len(li)-1):
        if li[i] >= N:
            break
        C[li[i]] = li[i+1] - li[i]


nxt = list2d(61, N, 0)

for i in range(N):
    
    nxt[0][i] = C[i] + 1

for k in range(1, 61):
    for i in range(N):
        nxt[k][i] = nxt[k-1][i] + nxt[k-1][(i+nxt[k-1][i])%N]
    if min(nxt[k]) > N*K:
        break


def get_cur():
    i = cur = 0
    while True:
        for k in range(61):
            if cur + nxt[k][i] > N * K:
                if k != 0:
                    cur += nxt[k-1][i]
                    i = cur % N
                    break
                else:
                    return cur
cur = get_cur()


ans = []
s = set()
for i in range(cur, N*K):
    a = A[i%N]
    
    if a not in s:
        ans.append(a)
        s.add(a)
    
    else:
        
        while True:
            key = ans.pop()
            s.remove(key)
            if key == a:
                break
print(*ans)

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

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

jump_table = [0] * N
for val in temp.values():
    for i in range(len(val)):
        if i == len(val)-1:
            jump_table[val[i]] = val[0]+N-val[i]+1
        else:
            jump_table[val[i]] = val[i+1]-val[i]+1

a = 0
f = 0
while True:
    a += jump_table[a % N]
    if a % N == 0:
        f = a//N
        break

K %= f
a = 0
stack = []
while a < N*K:
    if a + jump_table[a % N] <= N*K:
        a += jump_table[a % N]
    else:
        stack.append(A[a % N])
        a += 1


print(*stack)

Solution (Python):
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
nxt = [0]*(2*n)
first = [-1]*n
prev = {}
for i in range(2*n):
    x = a[i%n]
    if x in prev:
        nxt[prev[x]] = i
    prev[x] = i

ddprint(nxt)




loopfound = False
i=0
first[0] = 0
while True:
    i0 = i%n
    j = nxt[i0]+1
    nxtj = j + i - i0
    ddprint("  i {} j {} nxtj {} aj0 {}".format(i,j,nxtj,a[j%n]))
    if nxtj >= n*k:
        break
    j0 = j%n
    if first[j0] != -1:
        loopfound = True
        break
    first[j0] = nxtj
    i = nxtj




ddprint(first)


if loopfound:
    dif = nxtj - first[j0]
    if dif<=0:  
        print("foo")
        exit()
    i = nxtj + ((n*k - nxtj) // dif) * dif
    ddprint("nxt {} j0 {} dif {} i {}".format(nxtj,j0,dif,i))
    while True:
        i0 = i%n
        j = nxt[i0] + 1
        nxtj = j+i-i0
        if nxtj >= n*k:
            break
        i = nxtj

ddprint(i)

done = [False] * 200001
stk = []
for j in range(i,n*k):
    ddprint(j)
    ddprint(stk)
    ddprint(done[:11])
    j0 = j%n
    if done[a[j0]]:
        ddprint(a[j0])
        m = len(stk)
        for k in range(m-1,-1,-1):
            x = stk[k]
            ddprint(x)
            stk.pop()
            done[x] = False
            if x==a[j0]:
                break
    else:
        stk.append(a[j0])
        done[a[j0]] = True

for i in stk:
    printn(str(i) + ' ')
print("")

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

table = {}
init_table = {}
move = {}

for i in range(N):
    if A[i] not in table:
        table[A[i]] = i
        init_table[A[i]] = i
    else:        
        move[table[A[i]]], move[i] = i+1, init_table[A[i]]+1
        table[A[i]] = i

T = K
idx = 0
remain = [0]
for i in range(1, K+1):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    remain.append(idx)
    if(idx == N and i != K):
        T = K % i
        break
    if idx in move:
        idx = move[idx]
    else:
        idx += 1

if(T == 0):
    print()
    exit()
idx = remain[T]
ret = []
while idx < len(A):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    if(idx < len(A)):
        ret.append(A[idx])
    idx += 1

print(*ret)

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

table = {}
init_table = {}
move = {}

for i in range(N):
    if A[i] in table:
        move[table[A[i]]], move[i] = i+1, init_table[A[i]]+1
    else:
        if A[i] not in init_table:
            init_table[A[i]] = i
    table[A[i]] = i

T = K
idx = 0
remain = [0]
for i in range(1, K+1):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    remain.append(idx)
    if(idx == N and i != K):
        T = K % i
        break
    if idx in move:
        idx = move[idx]
    else:
        idx += 1

if(T == 0):
    print()
    exit()
idx = remain[T]
ret = []
while idx < len(A):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    if(idx >= len(A)):
        break
    ret.append(A[idx])
    idx += 1

print(*ret)

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

table = {}
init_table = {}
move = {}

for i in range(N):
    if A[i] in table:
        move[table[A[i]]], move[i] = i+1, init_table[A[i]]+1
    else:
        if A[i] not in init_table:
            init_table[A[i]] = i
    table[A[i]] = i

T = K
idx = 0
remain = [0]
for i in range(1, K+1):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    remain.append(idx)
    if(idx == N and i != K):
        T = K % i
        break
    if idx in move:
        idx = move[idx]
    else:
        idx += 1

if(T == 0):
    print()
    exit()
idx = remain[T]
ret = []
while idx < len(A):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    if(idx < len(A)):
        ret.append(A[idx])
    idx += 1

print(*ret)

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

table = {}
init_table = {}
move = {}

for i in range(N):
    if A[i] not in table:
        table[A[i]] = i
        init_table[A[i]] = i
    else:        
        move[table[A[i]]], move[i] = i+1, init_table[A[i]]+1
        table[A[i]] = i

T = K
idx = 0
remain = [0]
for i in range(1, K+1):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    remain.append(idx)
    if(idx == N and i != K):
        T = K % i
        break
    if idx in move:
        idx = move[idx]
    else:
        idx += 1

if(T == 0):
    print()
    exit()
idx = remain[T]
ret = []
while idx < len(A):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    if(idx < len(A)):
        ret.append(A[idx])
    idx += 1

print(*ret)

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

if n == 1:
    if k % 2:
        print(*arr)
    else:
        print(*[])
    exit()

table = {}
original_table = {}
move = {}

for i, a in enumerate(arr):
    if a not in table:
        table[a] = i
        original_table[a] = i
    else:
        move[table[a]], move[i] = i + 1, original_table[a] + 1
        table[a] = i

res = {}
idx = 0

for i in range(1, k + 1):
    while idx in move and move[idx] > idx:
        idx = move[idx]

    res[i] = idx
    if idx == len(arr):
        k %= len(res)
        if not k:
            k = len(res)
        break

    if idx in move:
        idx = move[idx]
    else:
        idx += 1

idx = res[k]
response = []

while idx < len(arr):
    while idx in move and move[idx] > idx:
        idx = move[idx]
    if idx < len(arr):
        response.append(arr[idx])
    idx += 1

print(*response)