Problem:
 Score : 700 points    Problem Statement We have a sequence of N \times K integers: X=(X_0,X_1,\cdots,X_{N \times K-1}) .
Its elements are represented by another sequence of N integers: A=(A_0,A_1,\cdots,A_{N-1}) . For each pair i, j ( 0 \leq i \leq K-1,\ 0 \leq j \leq N-1 ), X_{i \times N + j}=A_j holds.  Snuke has an integer sequence s , which is initially empty.
For each i=0,1,2,\cdots,N \times K-1 , in this order, he will perform the following operation:   If s does not contain X_i : add X_i to the end of s .  If s does contain X_i : repeatedly delete the element at the end of s until s no longer contains X_i . Note that, in this case, we do not add X_i to the end of s .   Find the elements of s after Snuke finished the operations.      Constraints  1 \leq N \leq 2 \times 10^5  1 \leq K \leq 10^{12}  1 \leq A_i \leq 2 \times 10^5  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  K  A_0  A_1  \cdots  A_{N-1}       Output Print the elements of s after Snuke finished the operations, in order from beginning to end, with spaces in between.        Sample Input 1 3 2
1 2 3      Sample Output 1 2 3  In this case, X=(1,2,3,1,2,3) .
We will perform the operations as follows:   i=0 : s does not contain 1 , so we add 1 to the end of s , resulting in s=(1) .  i=1 : s does not contain 2 , so we add 2 to the end of s , resulting in s=(1,2) .  i=2 : s does not contain 3 , so we add 3 to the end of s , resulting in s=(1,2,3) .  i=3 : s does contain 1 , so we repeatedly delete the element at the end of s as long as s contains 1 , which causes the following changes to s : (1,2,3)→(1,2)→(1)→() .  i=4 : s does not contain 2 , so we add 2 to the end of s , resulting in s=(2) .  i=5 : s does not contain 3 , so we add 3 to the end of s , resulting in s=(2,3) .        Sample Input 2 5 10
1 2 3 2 3      Sample Output 2 3       Sample Input 3 6 1000000000000
1 1 2 2 3 3      Sample Output 3  s may be empty in the end.       Sample Input 4 11 97
3 1 4 1 5 9 2 6 5 3 5      Sample Output 4 9 2 6

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import defaultdict
from bisect import bisect_left

n, k = li()
a = list(li())

a = a + a


jump = defaultdict(lambda: -1)
buf = defaultdict(lambda: -1)
for i, ai in enumerate(a):
    if buf[ai] != -1:
        jump[buf[ai]] = i - buf[ai]

    buf[ai] = i


checkpoint = [0]
cur = 0

while cur == 0 or cur % n > 0:
    cur += jump[cur % n]
    cur += 1
    checkpoint.append(cur)

cycle = cur // n
mod = k % cycle


if mod == 0:
    print()

else:
    last_idx = n * mod

    
    start_idx = checkpoint[bisect_left(checkpoint, last_idx) - 1]
    s = []

    idx = start_idx
    while idx < last_idx:
        if idx + jump[idx % n] < last_idx:
            idx += jump[idx % n] + 1
        else:
            s.append(a[idx % n])
            idx += 1

    print(*s)

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)

Solution (Python):
import bisect
n, k = map(int, input().split())
alist = list(map(int, input().split()))
positions = [[] for i in range(200002)]
for i in range(n):
    positions[alist[i]].append(i)
visited = [0] * n
lastList = []
nextP = 0
times =0
while True:
    num = alist[nextP]
    if len(positions[num]) == 1:
        lastList.append(nextP)
        visited[nextP] = times
        times += 1
        nextP += 1
        if nextP == n:
            break
    else:
        nextIn = bisect.bisect_right(positions[num], nextP)
        if nextIn == len(positions[num]):
            lastList.append(nextP)
            visited[nextP] = times
            times += 1
            nextP = positions[num][0] + 1
        else:
            nextP = positions[num][nextIn] + 1
            if nextP == n:
                break
if times != 0:
    loopIn = k % (times + 1)
    if loopIn != 0:
        loopIn -= 1
        lastIn = lastList[loopIn]
        result = []
        while True:
            num = alist[lastIn]
            nextIn = bisect.bisect_right(positions[num], lastIn)
            if nextIn == len(positions[num]):
                result.append(num)
                lastIn += 1
            else:
                lastIn = positions[num][nextIn] + 1
            if lastIn == n:
                break
        if len(result) > 0:
            print(' '.join(map(str, result)))

Solution (Python):
from bisect import bisect_left, bisect_right


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


if __name__ == "__main__":
    main()

Solution (Python):


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    
    B = [None]*N
    firstPos = {}
    lastPos = {}

    for p, a in enumerate(A):
        if a in lastPos:
            B[lastPos[a]] = p
            B[p] = firstPos[a]
        else:
            firstPos[a] = p
            B[p] = p

        lastPos[a] = p

    C = [None]*N
    for p, b in enumerate(B):
        c = (b - p)%N
        if c == 0:
            c = N

        C[p] = c + 1

    cycle = C[0]
    nextPos = cycle%N

    while nextPos != 0:
        cycle += C[nextPos]
        nextPos = (nextPos + C[nextPos])%N

    r = N*K%cycle
    ans = []

    while r > 0:
        while r >= C[(-r)%N]:
            r -= C[(-r)%N]

        if r > 0:
            ans.append(A[(-r)%N])
            r -= 1
        
    print(' '.join(map(str, ans)))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
if n == 1:
	if k%2 == 1:
		print(a[0])
	else:
		print("")
else:
	aa = a + a
	skip = [0 for _ in range(n)]
	ind = dict()
	for i in range(2*n-1, -1, -1):
		if aa[i] in ind and i < n:
			skip[i] = ind[aa[i]]-i+1
		ind[aa[i]] = i
	
	if k == 1:
		ans = []
		cnt = 0
		while cnt < n:
			if cnt + skip[cnt%n] > n:
				ans.append(a[cnt])
				cnt += 1
			elif cnt + skip[cnt%n] <= n:
				cnt += skip[cnt%n]
		print(*ans)
		sys.exit()
	cnt = 0
	while cnt == 0 or (cnt + skip[cnt%n] <= n*k and cnt%n != 0):
		cnt += skip[cnt%n]
	if cnt%n == 0:
		l = cnt//n
		
		k %= l
		if k == 0:
			print("")
			sys.exit()
		cnt = 0
		while cnt == 0 or cnt + skip[cnt%n] <= n*k:
			cnt += skip[cnt%n]
	ans = []
	cnt = cnt%n
	while cnt < n:
		if cnt + skip[cnt%n] > n:
			ans.append(a[cnt])
			cnt += 1
		elif cnt + skip[cnt%n] <= n:
			cnt += skip[cnt%n]
	print(*ans)

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

dict = {}
A_set = set(A)
for a in A_set:
    dict[a] = []
for i in range(N):
    dict[A[i]].append(i)

trans = {}
for a_list in dict.values():
    for i in range(len(a_list)):
        trans[a_list[i - 1]] = a_list[i]

k = 1
pos = 0
print_ans = 0
div_flag = 0
while True:
    new_pos = trans[pos]
    if new_pos <= pos:
        if k + 1 <= K:
            k += 1
            if new_pos + 1 < N:
                pos = new_pos + 1
            else:
                if div_flag == 0:
                    pos = 0
                    K = K % k
                    k = 1
                    div_flag = 1
                else:
                    print('')
                    break
        else:
            print_ans = 1
            break
    else:
        if new_pos + 1 < N:
            pos = new_pos + 1
        else:
            if div_flag == 0:
                pos = 0
                K = K % k
                k = 1
                div_flag = 1
            else:
                print('')
                break

if print_ans:
    ans = []
    i = pos
    while i < N:
        if trans[i] <= i:
            ans.append(A[i])
            i += 1
        else:
            i = trans[i] + 1
    print(' '.join(map(str, ans)))

Solution (Python):
from sys import exit, setrecursionlimit, stdin, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *
from heapq import *
import operator
import math
from fractions import gcd
from random import randint

setrecursionlimit(10**8)

def input():
  return stdin.readline().strip()

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

N, K = reads()
A = reads()

skip = [-1] * (N + 1)
first_occ = {}
for i in range(N-1, -1, -1):
  a = A[i]
  if a in first_occ:
    if first_occ[a] < N-1:
      skip[i] = skip[first_occ[a] + 1]
    else:
      skip[i] = first_occ[a] + 1
  else:
    skip[i] = i
  first_occ[a] = i
skip[N] = N

back = [first_occ[A[i]] + 1 for i in range(N)] + [0]
next_state = [skip[back[q]] for q in range(N+1)]


q = skip[0]

rev = {}
memo = {}
i = 0
while True:
  if q in rev:
    j = rev[q]
    break
  rev[q] = i
  memo[i] = q
  q = next_state[q]
  i += 1

m = i - j
q = memo[j + (K - 1 - j) % m]

s = []
while q < N:
  if skip[q] == q:
    s.append(A[q])
    q += 1
  else:
    q = skip[q]

print(*s)

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

MAX = 10**5*2+3

N,K = inpl()
aa = inpl()
cnts = defaultdict(int)
inds = defaultdict(list)
info = [[0,0] for _ in range(N)]
ed = set(aa)

for i,a in enumerate(aa):
    cnts[a] += 1
    inds[a].append(i)     
    info[i] = [a,cnts[a]] 


nexts = [-1]*MAX
ed.add(0)
for e in ed:
    tmp = e
    if e == 0:
        i = 0
    else:
        i = inds[tmp][0] + 1

    while True:
        
        if i == N:
            nexts[e] = 0
            break
        tmp,n = info[i]
        if cnts[tmp] == n:
            nexts[e] = tmp
            break
        else: 
            nextind = inds[tmp][n+1-1] 
            nextind += 1 
            i = nextind


visited = [False]*MAX
kero = []
x = 0
L = 0
while True:
    if visited[x]:
        break
    else:
        kero.append(x)
        visited[x] = True
        x = nexts[x]
        L += 1

tmp = kero[K%L-1]



ans = []
if tmp == 0:
    i = 0
else:
    i = inds[tmp][0] + 1

while i < N:
    tmp,n = info[i]
    if cnts[tmp] == n:
        ans.append(tmp)
        i += 1
    else:
        nextind = inds[tmp][n+1-1] 
        nextind += 1 
        i = nextind

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
from collections import defaultdict

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

AA = A + A 
next_same = [None] * (2*N)
last_pos = defaultdict(lambda: None)

for i,a in enumerate(AA):
    j = last_pos[a]
    if j is None:
        last_pos[a] = i
    else:
        next_same[j] = i
        last_pos[a] = i



NK = N * K
visited = [None] * N
x = 0
visited[0] = 0
while True:
    y = next_same[x % N] + (x//N) * N + 1
    if y >= NK:
        break
    if visited[y % N] is not None:
        
        P = y - visited[y % N]
        y += (NK - y) // P * P
        x = y
        break
    else:
        visited[y % N] = y
        x = y

answer = []
while x < NK:
    y = next_same[x % N] + (x//N) * N + 1
    if y > NK:
        answer.append(A[x%N])
        x += 1
        continue
    x = y

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

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

C = Counter()
table = [None]*N
for i in range(N-1, -1, -1):
    a = A[i]
    C[a] = i
for i in range(N-1, -1, -1):
    a = A[i]
    table[i] = C[a]
    C[a] = i
cost = [1 + table[i] - i if table[i] > i else 1+ table[i] - i + N for i in range(N)] 
table = [(1+t)%N for t in table]
loop = 0
st = 0
en = None
while en != 0:
    en = table[st]
    loop += cost[st]
    st = en
K = (N*K) % loop
st = 0
while K - cost[st] >= 0:
    K -= cost[st]
    st = table[st]
sS = set()
S = []
for i in range(K):
    a = A[(st+i)%N]
    if a in sS:
        sS.remove(a)
        while S[-1] != a:
            sS.remove(S.pop())
        S.pop()
    else:
        S.append(a)
        sS.add(a)
print(*S)

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

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


counts = [0] * N
D = {}
for i, a in enumerate(A + A):
    if a not in D:
        D[a] = i
    else:
        if D[a] < N:
            counts[D[a]] = i - D[a]
            D[a] = i

circle = counts[0] + 1
i = (counts[0] + 1) % N
while i != 0:
    circle += counts[i] + 1
    i = (i + counts[i] + 1) % N
rem = N * K % circle

ans = []

i = 0
while rem > 0:
    while rem - counts[i] > 0:
        rem -= counts[i] + 1
        i = (i + counts[i] + 1) % N
    if rem:
        ans.append(A[i])
        rem -= 1
        i = (i + 1) % N

if ans:
    print(*ans)

Solution (Python):
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
M = 200100
P = [-1 for i in range(M)]
NE = [-1 for a in range(N)]
for i in range(N):
    if P[A[i]] >= 0 and NE[P[A[i]]] < 0:
        NE[P[A[i]]] = i
    P[A[i]] = i
for i in range(N):
    if P[A[i]] >= 0 and NE[P[A[i]]] < 0:
        NE[P[A[i]]] = i
    P[A[i]] = i

i = 0
c = 1
while True:
    if NE[i] <= i: c += 1
    i = (NE[i] + 1) % N
    if i == 0:
        break
K %= c
ANS = []
i = 0
while K:
    while NE[i] > i:
        i = (NE[i] + 1) % N
    if K >= 2:
        i = (NE[i] + 1) % N
        K -= 1
    else:
        break
while i < N:
    if NE[i] > i:
        i = NE[i] + 1
    else:
        ANS.append(A[i])
        i += 1
print(*ANS)