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):

"""
Created on Sun Jul 21 23:25:47 2019

@author: Yamazaki Kenichi
"""
import bisect

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

T = [[] for _ in range(2*10**5+1)]

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

s = A[0]
pointa = 0
counta, res = 0, [-1]
while True:
    tmp = bisect.bisect_right(T[s], pointa) % len(T[s])
    p_pointa = pointa
    pointa = T[s][tmp] + 1
    if pointa == p_pointa + 1 or pointa < p_pointa:
        counta += 1
        res.append(p_pointa)
    if pointa == N:
        counta += 1
        res.append(p_pointa)
        break
    s = A[pointa]

ans = []
if K % counta != 0:
    pointa = res[K % counta]

    while pointa < N:
        if pointa < T[A[pointa]][-1]:
            tmp = bisect.bisect_right(T[A[pointa]],pointa)
            pointa = T[A[pointa]][tmp] + 1
        else:
            ans.append(A[pointa])
            pointa += 1

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

Solution (Python):
from collections import Counter

n, k = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]
OVER_RUN = n*k

cnt = [-1] * (2 * 10**5 + 10)
transition = [[-1] * n for _ in range(58)]
transition[0] = [0 for _ in range(1,n+1)]
for i, item in enumerate(a + a):
    if cnt[item] == -1:
        cnt[item] = i
        continue
    if cnt[item] < n:
        transition[1][cnt[item]] = i+1 - cnt[item]
    cnt[item] = i

for i in range(2,58):
    for j in range(n): 
        walk = transition[i-1][j] + transition[i-1][(transition[i-1][j] + j) % n]
        if walk > OVER_RUN:
            transition[i][j] = OVER_RUN
        else:
            transition[i][j] = walk

step = 0
step_mod = 0
while True:
    itr = 57 
    while transition[itr][step_mod] + step >= OVER_RUN:
        itr -= 1
    step += transition[itr][step_mod]
    step_mod = step % n
    if itr == 0:
        break


target = a[step_mod:]
ans = []
cnt = Counter()
for item in target:
    if cnt[item] > 0:
        while True:
            p = ans.pop()
            cnt[p] -= 1
            if p == item or not p:
                break
    else:
        cnt[item] += 1
        ans.append(item)
print(" ".join([str(item) for item in ans]))

Solution (Python):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
M = [None]*(max(A)+1)
T = [None]*N
for i,a in enumerate(A):
    M[a] = i
 
for i,a in enumerate(A):
    T[M[a]]=(i+1)%N
    M[a]=i
 
ptr=0
ptr_head = [ptr]
while True:
    ptr_next = T[ptr]
    if ptr_next==0: break
    if ptr_next <= ptr+1: ptr_head.append(ptr_next)
    ptr = ptr_next
 
period = len(ptr_head)
if T[-1]==0: period+=1
 
if K % period==0:
    print()
    exit()
 
ret = []
ptr = ptr_head[K % period - 1]
while ptr<N:
    ptr_next = T[ptr]
    if ptr_next > ptr+1:
        ptr = ptr_next
    elif ptr_next==0 and ptr!=N-1:
        break
    else:
        ret.append(A[ptr])
        ptr+=1
 
print(' '.join(map(str, ret)))

Solution (Python):
N, K = map(int, input().split())
As = list(map(int, input().split()))
M = [[] for i in range(200001)]
for i, a in enumerate(As):
  M[a].append(i)
S = []
for i, a in enumerate(As):
  l = M[a]
  index = l.index(i)
  if index+1 == len(l):
    n = l[0]+N
  else:
    n = l[index+1]
  S.append(n+1-i)

p = 0
while True:
  p = p+S[p%N]
  if p%N == 0:
    break
count = p//N

K = K%count

p = 0
s = []
while p < N*K:
  q = p+S[p%N]
  if q > N*K:
    s.append(As[p%N])
    p = p+1
  else:
    p = q
print(" ".join(map(str, s)))

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

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

last = dict()
v = [-1] * N

for i, a in enumerate(A + A) :
    if a in last and last[a] < N :
        v[last[a]] = i + 1
    
    last[a] = i

dist = [-1] * N
cur = 0
cycle = 0
while dist[cur] < 0 :
    dist[cur] = cycle 
    cycle += v[cur] - cur
    cur = v[cur] % N

NK = N * K

m = NK % cycle
cur = v[cur] % N
s = 0
while m > dist[cur] > 0 :
    s = dist[cur]
    cur = v[cur] % N
m -= s

ret = []
seen = set()
for a in A[N-m:] :
    if a in seen :
        p = ret.pop()
        seen.remove(a)
        while p != a :
            seen.remove(p)
            p = ret.pop()
    else :
        ret.append(a)
        seen.add(a)
        
print(' '.join([str(i) for i in ret]))

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
from collections import defaultdict
d=defaultdict(list)
for i,a in enumerate(A):
    d[a].append(i)
x=0
k=1
from bisect import bisect
while True:
    i=bisect(d[A[x]],x)
    if i==len(d[A[x]]):
        if k==K:
            break
        i=0
        k+=1
    x=d[A[x]][i]+1
    if x==N:
        if K%k==0:
            break
        K=K%k
        k=1
        x=0
t=[]
while x<N:
    i=bisect(d[A[x]],x)
    if i==len(d[A[x]]):
        t.append(A[x])
        x+=1
    else:
        x=d[A[x]][i]+1
print(*t)

Solution (Python):
import math







n, k = map(int, input().split())
a = [int(ai) for ai in input().split()]









jumpto = [[-1] * n]
la = -1
li = -1
for (ca, ci) in sorted(zip(a, range(n))):
    if ca == la:
        jumpto[0][ci] = jumpto[0][li] + li - ci
        jumpto[0][li] = ci - li + 1
    else:
        jumpto[0][ci] = n + 1
    la = ca
    li = ci















levels = int(math.log(k, 2))
for l in range(1, levels + 1):
    jumpto.append([-1] * n)
    for i in range(n):
        f = jumpto[l - 1][i]
        s = jumpto[l - 1][(i + f) % n]
        jumpto[l][i] = f + s


ret = []
s = 0
while s < n * k:
    jumped = False
    for l in range(levels, -1, -1):
        if s + jumpto[l][s % n] <= n * k:
            s += jumpto[l][s % n]
            jumped = True
            break
    if not jumped:
        ret.append(str(a[s % n]))
        s += 1

print(' '.join(ret))

Solution (Python):
import math







n, k = map(int, input().split())
a = [int(ai) for ai in input().split()]









jumpto = [[-1] * n]
la = -1
li = -1
for (ca, ci) in sorted(zip(a, range(n))):
    if ca == la:
        jumpto[0][ci] = jumpto[0][li] + li - ci
        jumpto[0][li] = ci - li
    else:
        jumpto[0][ci] = n
    la = ca
    li = ci















levels = int(math.log(k, 2))
for l in range(1, levels + 1):
    jumpto.append([-1] * n)
    for i in range(n):
        f = jumpto[l - 1][i]
        s = jumpto[l - 1][(i + f + 1) % n]
        jumpto[l][i] = f + 1 + s


s = 0
while True:
    jumped = False
    for l in range(levels, -1, -1):
        if s + jumpto[l][s % n] < n * k:
            s += jumpto[l][s % n] + 1
            jumped = True
            break
    if not jumped:
        break


ret = []
while s < n * k:
    if s + jumpto[0][s % n] < n * k:
        s = s + jumpto[0][s % n] + 1
    else:
        ret.append(str(a[s % n]))
        s += 1

print(' '.join(ret))

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]

    n_head_pos = dict() 
    rem_head_num = [-1] * N 
    for i in reversed(range(N)):
        if A[i] not in n_head_pos:
            rem_head_num[i] = A[i]
        else:
            if n_head_pos[A[i]] == N-1: rem_head_num[i] = -1
            else: rem_head_num[i] = rem_head_num[n_head_pos[A[i]] + 1]
        n_head_pos[A[i]] = i
    
    

    start_cycle = [] 
    start = -1
    used = set()
    while start not in used:
        used |= {start}
        start_cycle.append(start)
        if start == -1: next = rem_head_num[0]
        elif n_head_pos[start] < N - 1: next = rem_head_num[n_head_pos[start] + 1]
        else: next = -1
        start = next

    

    len_cycle = len(start_cycle)
    last = (K - 1) % len_cycle 
    
    if start_cycle[last] == -1: start_index = 0
    elif n_head_pos[start_cycle[last]] < N - 1: start_index =n_head_pos[start_cycle[last]] + 1
    else: start_index == -1
    
    if start_index >= 0:
        out = []
        using = dict()
        l = 0
        for i in range(start_index, N):
            if A[i] in using:
                aindex = using[A[i]]
                l = aindex
                for a in out[aindex:]:
                    using.pop(a)
                out = out[:aindex]
            else:
                out.append(A[i])
                using[A[i]] = l
                l += 1
                
        print(" ".join(map(str, out)))
    else: print(" ")

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import bisect
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(index):
  if index == N:
    ans.append(-1)
    return
  num = a[index]
  if len(data[num]) == 1:
    ans.append(index)
    tami(index+1)
  elif index == data[num][-1]:
    ans.append(index)
    tami(data[num][0]+1)
  else:
    tmp = bisect.bisect_left(data[num],index)
    
    tami(data[num][tmp+1]+1)

N,K = IL()
a = IL()

data = [[] for i in range(200010)]
ans = []

for i in range(N):
  data[a[i]].append(i)



tami(0)





if K%len(ans) == 0:
  print()
else:
  x = ans[K%len(ans)-1]
  d = []
  d.append(a[x])
  while x != N-1:
    x += 1
    if len(data[a[x]]) == 1:
      d.append(a[x])
    elif x == data[a[x]][-1]:
      d.append(a[x])
    else:
      tmp = bisect.bisect_left(data[a[x]],x)
      x = data[a[x]][tmp+1]

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

"""
for i in range(K):
  for j in range(N):
    if a[j] in data:
      num = data.index(a[j])
      data = data[:num]
    else:
      data.append(a[j])
  print(data)

11 10
1 2 3 0 4 5 6 0 4 5 6

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

10 10
1 4 8 3 5 4 2 3 4 6
"""

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

a = list(set(A))
a.sort()
R = {}
for i in range(len(a)):
  R[a[i]] = i

n = len(a)
L = [[] for _ in range(n)]
P = [[0, 0] for _ in range(N)]
for i in range(N):
  P[i][0] = R[A[i]]
  P[i][1] = len(L[R[A[i]]])
  L[R[A[i]]].append(i)

s = [R[A[0]], 0]
D = [0]

while True:
  while s[1] < len(L[s[0]]) - 1:
    t = L[s[0]][s[1] + 1]
    if t != N - 1:
      s = P[t + 1]
    else:
      break
  else:
    if L[s[0]][0] == N - 1:
      D.append(N)
      break
    s = P[L[s[0]][0] + 1]
    D.append(L[s[0]][s[1]])
    continue
  break

M = len(D)
m = (K - 1) % M

if m == M - 1:
  print()
  exit()
ans = []
s = P[D[m]]
t = L[s[0]][s[1]]

while t != N:
  if s[1] == len(L[s[0]]) - 1:
    ans.append(A[t])
    t += 1
  else:
    t = L[s[0]][s[1] + 1] + 1
  if t == N:
    break
  s = P[t]

print(" ".join(list(map(str, ans))))

Solution (Python):
def solve():
    N, K = [int(_) for _ in raw_input().split()]
    A = [(i, int(ele)) for i, ele in enumerate(raw_input().split())]
    sortA = sorted(A, key = lambda x: x[1])
    _next = [None] * N
    save = 0

    
    for i in range(1, N):
        if sortA[i][1] == sortA[save][1]:
            _next[sortA[i-1][0]] = sortA[i][0]
        else:
            _next[sortA[i-1][0]] = sortA[save][0]
            save = i
    _next[sortA[N-1][0]] = sortA[save][0]
    

    
    start = 0
    minCntToEmpty = 0
    while start != N:
        if _next[start] <= start:
            minCntToEmpty += 1
        start = _next[start] + 1
        
    minCntToEmpty +=1
    

    
    cntDown = K % minCntToEmpty
    if cntDown == 0:
        print
        return
    
    start = 0
    while cntDown > 1:
        if _next[start] <= start:
            cntDown -= 1
        start = _next[start] + 1
        

    
    i = start
    
    while i < N:
        if _next[i] <= i:
            print A[i][1],
            i += 1
        else:
            i = _next[i] + 1

if __name__ == '__main__':
    solve()

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
hoge = [[]for i in range(200001)]
for i in range(N):
    hoge[A[i]].append(i)
NEXT = [-1]*N
for i in hoge:
    for j in range(-len(i),0):
        NEXT[i[j]] = i[j+1]
i = 1
p = 0
while i < K:
    
    if NEXT[p] <= p:
        i += 1
    if NEXT[p] == N-1:
        i += 1
    p = (NEXT[p]+1)%N
    if p == 0:
        i = ((K-1)//(i-1))*(i-1)+1
while p < N:
    if NEXT[p] <= p:
        print(A[p],end = " ")
        p += 1
    else:
        p = NEXT[p]+1
print()

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

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

X = A * 2

Nextcount = [-1] * (2 * (10**5+1))
Nextind = [-1]*N

for i,x in enumerate(X):
    if 0 <= Nextcount[x] < N:
        Nextind[Nextcount[x]] = i

    Nextcount[x] = i

Repeatcount=0
NOW = 0

Repeatcount+=Nextind[NOW]-NOW+1
NOW=(Nextind[NOW]+1)%N

while NOW!=0:
    Repeatcount+=Nextind[NOW]-NOW+1
    NOW=(Nextind[NOW]+1)%N
    

Rep=(N*K)%Repeatcount
NOW=0

while True:
    if Nextind[NOW]-NOW+1<Rep:
        Rep-=Nextind[NOW]-NOW+1
        NOW=(Nextind[NOW]+1)%N

    else:
        break
    


from collections import Counter
C=Counter()
s=[]
for i in range(Rep):
    if X[(NOW+i)%N] in C:
        while X[(NOW+i)%N] in C:
            po=s.pop()
            C[po]-=1
            if C[po]==0:
                del C[po]

    else:
        s.append(X[(NOW+i)%N])
        C[X[(NOW+i)%N]]+=1

    

print(*s)

Solution (Python):

N, K = map(int, input().split())
A = list(map(int, input().split()))
if N==1:
    if K%2==1:
        print(A[0])
    exit()
L = [-1] * (2*10**5+2)  
Plus = [0]*N  
Next = [0]*N  
for i, a in enumerate(A):
    l = L[a]
    if l==-1:
        L[a] = i
    else:
        Next[l] = i+1
        L[a] = i

if l!=-1:
    Next[l] = 0
    Plus[l] = 1


for i, a in enumerate(A):
    l = L[a]  
    if i <= l:
        Next[l] = i+1
        Plus[l] += 1
        L[a] = -1






loop = Plus[0]
i = Next[0]
while i!=0:
    i_next = Next[i]
    if i_next==N:
        loop += 1
        i_next = 0
    
    
    loop += Plus[i]

    i = i_next



TrueNext = Next.copy()
for i in range(N-1,-1,-1):
    p = Plus[i]
    if p==0:
        nn = TrueNext[i]
        if nn==N:
            nn = 0
        TrueNext[i] = TrueNext[nn]



k = K % loop
if k==0:
    exit()

start = 0
i=0
while i<k-1:
    if start==N:
        start=0
        i+=1
    if i==K-1:
        break
    start = TrueNext[start]
    i+=1


ans = []
S = [0] * (2*10**5+2)

for a in A[start:]:
    
    if S[a]==0:
        ans.append(a)
        S[a] = 1
    else:
        while S[a] != 0:
            a_ = ans.pop()
            S[a_] = 0
print(*ans)

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

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

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



res2 = res[:n]

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

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

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




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



if k != l: ans = cycle[k]
else: exit()
while res[ans] < n:
    ans = res[ans]

"""
if k != l: ans = p = cycle[k]
else: ans = p = n+1
while p <= n:
    ans = p
    p = res[p]

"""


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

Solution (Python):


def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    D = {}
    for j,a in enumerate(A):
        D[a] = D.get(a,[])+[j]

    t,k = 0,0
    T = [0]
    while True:
        k = (D[A[t]].index(t)+1)%len(D[A[t]])
        if k == 0:
            T.append(A[t])
        t = (D[A[t]][k]+1)%N
        if t == 0:
            break

    if T == [0]:
        return ''
    if K == 1 or (K-1)%len(T) == 0:
        t = 0
    else:
        t = D[T[(K-1)%len(T)]][0]+1
    S = []
    flag = False
    while t < N:
        k = (D[A[t]].index(t)+1)%len(D[A[t]])
        if k == 0:
            S.append(A[t])
            t += 1
        else:
            t = D[A[t]][k]+1

    res = ' '.join(map(str,S))

    return str(res)

if __name__ == '__main__':
    print(solve())

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n, k = rli()
    la = rli()
    MOD = len(la)
    lb = [-1 for _ in range(len(la))]
    periods = dict()
    for i, a in enumerate(la):
        periods.setdefault(a, [])
        periods[a].append(i)
    for v in periods.values():
        for i, e in enumerate(v):
            if i == len(v)-1:
                lb[e] = (v[0] + 1) % MOD
            else:
                lb[e] = (v[i+1] + 1) % MOD
    loop = [0]
    b = 0
    while True:
        nb = lb[b]
        if nb == 0:
            break
        if nb-b <= 1:
            loop.append(nb)
        b = nb
    
    period = len(loop)
    if loop[-1] == len(loop)-1:
        loop.append(None)
        period += 1

    start = loop[(k-1)%period]
    if start is None:
        print()
        return
    lc = la[start:]

    counts = dict()
    for i, a in enumerate(lc):
        counts.setdefault(a, 0)
        counts[a] += 1

    ans = []
    target = None
    for a in lc:
        if target is None:
            count = counts[a]
            if counts[a] >= 2:
                target = a
            else:
                ans.append(a)
        else:
            counts[a] -= 1
            if a == target:
                counts[a] -= 1
                target = None
    
    
    print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
A += A
d = {}
da = {}
for i in range(2*N):
    a = A[i]
    if a in da:
        d[da[a]] = i
    da[a] = i
        
for i in range(N):
    if d[i] >= N:
        d[i] %= N


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

if k == 0:
    print('')
else:
    ans = []
    while s <= N-1:
        if s < d[s]:
            s = d[s]+1
        else:
            ans.append(str(A[s]))
            s += 1
    print(' '.join(ans))

Solution (Python):


inf = 10 ** 18
P = 10 ** 9 + 7

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

d_x = {}
for idx, Ai in enumerate(A):
    if str(Ai) not in d_x.keys():
        d_x[str(Ai)] = [idx]
    else:
        d_x[str(Ai)].append(idx)

tab_jump = [None] * N
for key in d_x.keys():
    for i in range(len(d_x[key]) - 1):
        tab_jump[d_x[key][i]] = d_x[key][i + 1]
    tab_jump[d_x[key][-1]] = d_x[key][0]

ans = []
pos = 0
while True:
    
    if pos >= N * K: break
    pos_src = pos % N
    pos_dst = tab_jump[pos_src]
    if pos_src < pos_dst:
        pos_nxt = pos + (pos_dst - pos_src) + 1
    else:
        pos_nxt = pos + (pos_dst - pos_src) + N + 1
    
    if pos_nxt > N * K:
        ans.append(A[pos_src])
        pos += 1
    else:
        if pos_nxt % N == 0:
            K_iter = pos_nxt // N
            pos = N * ((K // K_iter) * K_iter)
        else:
            pos = pos_nxt

if len(ans) > 0:
    print(' '.join(map(str, ans)))

Solution (Python):
N,K = map(int, input().split())
A = list(map(int,input().split()))
MAX = 2 * 10**5+1
nxt = [-1 for i in range(MAX)]

last_index = [-1 for i in range(MAX)]
next_index = [-1 for i in range(MAX)]
first_index = [-1 for i in range(MAX)]
for i in range(N):
	a = A[i]
	last = last_index[a]
	if last >= 0:
		next_index[last] = i
	else:
		first_index[a] = i
	last_index[a] = i

start_stat = 0
while True:
	current = first_index[start_stat] + 1
	while current < N and next_index[current] != -1:
		current = next_index[current] + 1
	if current >= N:
		nxt[start_stat] = 0
		break
	else:
		stat = A[current]
		nxt[start_stat] = A[current]
		if nxt[A[current]] == -1:
			start_stat = A[current]
			continue
		else:
			break

order = [0]
in_order = [False for i in range(MAX)]
in_order[0] = True
last_stat = nxt[0]
while not in_order[last_stat]:
	order.append(last_stat)
	in_order[last_stat] = True
	last_stat = nxt[last_stat]
a,b = 0,len(order)
for x in order:
	if x == last_stat:
		break
	else:
		a += 1
		b -= 1
stat_index = K if K < a else (K-a) % b
init_status = order[stat_index]
before_stat = order[(stat_index - 1) % len(order)]
current = first_index[before_stat] + 1
if init_status == 0:
	print("")
	exit()
ans = []
while current < N:
	if next_index[current] != -1:
		current = next_index[current] + 1
	else:
		ans.append(A[current])
		current += 1
print( " ".join(list(map(str, ans))))

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

  inc=AA[index]+1
  count+=inc  
  index=(index+inc)%n
  if Chk[index]!=0 or index==0:
    if flag==False:
      dd=count-Chk[index]
      count+=(((n*k)-count)//dd)*dd
      flag=True
  Chk[index]=count

count-=inc
index=(index+n-inc)%n

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

Solution (Python):
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a_list=list(map(int,input().split()))
dp=[-1]*(n+1)
dp[n]=0
D={}
for i in range(n-1,-1,-1):
    if not a_list[i] in D:
        dp[i]=a_list[i]
        D[a_list[i]]=i+1
    else:
        dp[i]=dp[D[a_list[i]]]
        D[a_list[i]]=i+1
first={}
for i in range(n):
    if not a_list[i] in first:
        first[a_list[i]]=i
cycle=[dp[0]]
i=dp[0]
while True:
    if i==0:
        break
    ne=first[i]
    i=dp[ne+1]
    cycle.append(i)
cycle.pop()
cycle=[0]+cycle
T=len(cycle)
order=k%T
if 1<=order:
    start=cycle[order-1]
    check={}
    if start==0:
        ans=[]
        for i in range(n):
            if ans==[]:
                ans.append(a_list[i])
                check[a_list[i]]=0
            else:
                if a_list[i] in check:
                    ans=ans[:check[a_list[i]]]
                    temp={}
                    for j in check:
                        if check[j]<check[a_list[i]]:
                            temp[j]=check[j]
                    check=temp
                else:
                    ans.append(a_list[i])
                    check[a_list[i]]=len(ans)-1
    else:
        ans=[]
        x=first[start]
        for i in range(x+1,n):
            if ans==[]:
                ans.append(a_list[i])
                check[a_list[i]]=0
            else:
                if a_list[i] in check:
                    ans=ans[:check[a_list[i]]]
                    temp={}
                    for j in check:
                        if check[j]<check[a_list[i]]:
                            temp[j]=check[j]
                    check=temp
                else:
                    ans.append(a_list[i])
                    check[a_list[i]]=len(ans)-1
    for i in ans:
        print(i,end='')
        print(' ',end='')
else:
    print()

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

M = [None]*(max(A)+1)
T = [None]*N
for i,a in enumerate(A):
    M[a] = i

for i,a in enumerate(A):
    T[M[a]]=(i+1)%N
    M[a]=i

ptr=0
ptr_head = [ptr]
while True:
    ptr_next = T[ptr]
    if ptr_next==0: break
    if ptr_next <= ptr+1: ptr_head.append(ptr_next)
    ptr = ptr_next

period = len(ptr_head)
if T[-1]==0: period+=1

if K % period==0:
    print()
    exit()

ret = []
ptr = ptr_head[K % period - 1]
while ptr<N:
    ptr_next = T[ptr]
    if ptr_next > ptr+1:
        ptr = ptr_next
    elif ptr_next==0 and ptr!=N-1:
        break
    else:
        ret.append(A[ptr])
        ptr+=1

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

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

memo = {}
nxts = [None] * N
for i, x in enumerate(A + A):
  if x in memo:
    nxts[memo[x] % N] = i % N
  memo[x] = i

k = 0
visited = [-1] * N
p = 0
flag = False
ans = []
while k < K:
  if flag == False and visited[p] >= 0:
    vp = visited[p]
    k = vp + ((K-1) - vp) // (k - vp) * (k - vp)
    flag = True
  visited[p] = k
  nxt = nxts[p]
  if k == K-1 and nxt <= p:
    ans.append(A[p])
    nxt = p + 1
    if nxt >= N:
      break
  else:
    if nxt <= p:
      k += 1
    nxt = nxt + 1
    if nxt >= N:
      nxt %= N
      k += 1
  p = nxt

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

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
hoge = [[]for i in range(200001)]
for i in range(N):
    hoge[A[i]].append(i)
NEXT = [-1]*N
for i in hoge:
    for j in range(-len(i),0):
        NEXT[i[j]] = i[j+1]
i = 1
p = 0
while i < K:
    
    if NEXT[p] <= p:
        i += 1
    if NEXT[p] == N-1:
        i += 1
    p = (NEXT[p]+1)%N
    if p == 0:
        i = (K//(i-1))*(i-1)+1
while p < N:
    if NEXT[p] <= p:
        print(A[p],end = " ")
        p += 1
    else:
        p = NEXT[p]+1
print()

Solution (Python):
import bisect

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

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

loop_cnt = 0
sp = []
p = 0
fp = 1
top = A[0]
while True:
    loop_cnt += 1
    while fp < N:
        np = bisect.bisect_left(d[top], fp)
        if np < len(d[top]):
            p = d[top][np] + 1
            fp = p+1
            if p < N:
                top = A[p]
        else:
            break
    if p >= N:
        break
    top = A[p]
    sp.append(p)
    fp = 0

k = K % loop_cnt - 1
if k == -1:
    exit()
p = sp[k]
s = []
while p < N:
    top = A[p]
    while True:
        np = bisect.bisect_left(d[top], p+1)
        if np < len(d[top]):
            p = d[top][np] + 1
            if p < N:
                top = A[p]
        else:
            break
    if p < N:
        s.append(top)
        p += 1

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

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

if n == 1:
    print(*(aaa * (k % 2)))
    exit()

follow = [-1] * n
tmp = {}
fix = 0
while True:
    for i in range(n):
        a = aaa[i]
        if a in tmp:
            follow[tmp[a]] = (i + 1) % n
            tmp[a] = i
            fix += 1
        else:
            tmp[a] = i
    if fix >= n:
        break

loop = 1
i = 0
while True:
    j = follow[i]
    if j == 0:
        if i == n - 1:
            loop += 1
        break
    if i + 1 >= j:
        loop += 1
    
    i = j

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




def simulate(aaa, s, follow):
    tmp = []
    exs = set()
    for a in aaa[s:]:
        if a in exs:
            while True:
                b = tmp.pop()
                exs.remove(b)
                if a == b:
                    break
        else:
            tmp.append(a)
            exs.add(a)

    
    
    
    
    
    
    
    
    
    
    
    
    return tmp





i = 0
loop = 1
while loop <= k:
    j = follow[i]
    if i + 1 >= j:
        if loop == k:
            print(*simulate(aaa, i, follow))
            break
        loop += 1
    i = j

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

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



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




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

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



from_in_cycle=[]

i = 0
while True:
  j = next_i[i]
  if cycle_i[i]:
    from_in_cycle.append(i)
  i = j
  
  if i==0:
    break
    


if from_in_cycle:
  k = K%(len(from_in_cycle)+1)
  

  ans = []
  i = from_in_cycle[k-1]
  while i < N:
    if cycle_i[i]:
      ans.append(A[i])
      i += 1
    else:
      if next_i[i]==0:
        break
      i = next_i[i]
  print(*ans)


else:
  print('')

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

def solve(K,A):
    N = len(A)
    indices = defaultdict(list)
    for i,a in enumerate(A):
        indices[a].append(i)

    memo = [False]*N
    memo[0] = True
    trace = [0]
    cnt = [0]
    while True:
        i = trace[-1]
        c = cnt[-1]
        l = indices[A[i]]
        j = (l[bisect(l,i)%len(l)]+1)%N
        if j == 0 and i+1==N:
            c += 2
        elif j <= i+1:
            c += 1
        trace.append(j)
        cnt.append(c)
        if memo[j]:
            break
        else:
            memo[j] = True

    i = trace.index(trace[-1])

    period = cnt[-1] - cnt[i]
    if K > cnt[i]:
        K = (K-cnt[i])%period + cnt[i]
    if K == 0:
        return tuple()
    head = trace[bisect(cnt, K-1)-1]
    
    
    
    
    
    ans = []
    cur = head
    while cur < N:
        l = indices[A[cur]]
        j = bisect(l, cur)
        if j == len(l):
            ans.append(cur)
            cur += 1
        else:
            cur = l[j]+1
    return tuple(A[i] for i in ans)

def naive(K,A):
    s = {}
    l = []
    for k in range(K):
        for a in A:
            try:
                i = l.index(a)
                l = l[:i]
            except ValueError:
                l.append(a)
    return tuple(l)

















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