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



Sample input:
3 2
1 2 3


Sample output:
2 3

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_right
from collections import defaultdict
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, K = inpl()
A = inpl()
places = defaultdict(list)
nextplace = [-1]*N
newloop = [-1]*N
for i in range(N):
    places[A[i]].append(i)
for i in range(N):
    pl = places[A[i]]
    k = bisect_right(pl,i)
    if k >= len(pl):
        nextplace[i] = (pl[0]+1)%N
        if nextplace[i] == 0:
            newloop[i] = 2
        else:
            newloop[i] = 1
    else:
        nextplace[i] = (pl[k]+1)%N
        if nextplace[i] == 0:
            newloop[i] = 1
        else:
            newloop[i] = 0

Nloop = 0
prev_loop = [-1]*N
j = 0
before_jump = True
while Nloop+newloop[j] < K or (Nloop+newloop[j]==K and nextplace[j]==0):
    if before_jump and prev_loop[j] >= 0:
        period = Nloop - prev_loop[j]
        K -= ((K - Nloop)//period) * period
        before_jump = False
    else:
        prev_loop[j] = Nloop
        Nloop += newloop[j]
        j = nextplace[j]
if Nloop == K:
    exit()

S = [0]*N
lenS = 0
XinS = defaultdict(lambda: -1)
for i in range(j,N):
    if XinS[A[i]] < 0:
        S[lenS] = A[i]
        XinS[A[i]] = lenS
        lenS += 1
    else:
        lenS = XinS[A[i]]
        XinS[A[i]] = -1
if lenS > 0:
    print(' '.join(map(str,S[:lenS])))

Submission status: Wrong Answer

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

s = []
tmp = []
for i in range(K):
        for n in A:
                if n in s:
                        while n in s:
                                s.pop(-1)
                else:
                        s.append(n)
        if s in tmp:
                break
        else:
                tmp.append(s)

# print(tmp)
tmp = list(tmp[K % len(tmp) - 1])
for ans in tmp:
        print(ans)

Submission status: Wrong Answer

Solution code:
def solve(array):
    ans = []
    memo1 = {}
    for i in range(len(array)):
        if array[i] not in memo1:
            memo1[array[i]] = 1
        else:
            memo1[array[i]] += 1
    i = 0
    while i < len(array):
        if memo1[array[i]] == 1:
            ans.append(array[i])
            i += 1
        else:
            tmp = array[i]
            memo1[array[i]] -= 1
            i += 1
            while True:
                memo1[array[i]] -= 1
                if tmp == array[i]:
                    i += 1
                    break
                else:
                    i += 1
    return ans   

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

start = {}
for i in range(len(a))[::-1]:
    start[a[i]] = i
flag = False
li = []
tmp = []
while True:
    memo = {}
    if not tmp:
        start_num = 0
    else:
        start_num = start[tmp[-1]] + 1
    tmp = solve(a[start_num:n])
    #print(tmp)
    if li and li[-1] == tmp:
        flag = True
        break
    li.append(tmp)
    if not tmp:
        break

t = 1
for i in range(len(li)):
    t += len(li[i])
if flag:
    t -= (len(li) - 1)
#print(li)
#print(t)
ans = k % t
if ans == 0:
    print()
else:
    for i in range(len(li)):
        if ans <= len(li[i]):
            print(" ".join(map(str, li[i][ans-1:])))
            break
        ans -= len(li[i])

Submission status: Wrong Answer

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

s_index = {}
s_list = []
    
loop_i = 1
for i in range(K):
    for a in A:
        if a in s_index:
            del_keys = [a]
            for key, val in s_index.items():
                if val > s_index[a]:
                    del_keys.append(key)

            s_list = s_list[:s_index[a]]
            for key in  del_keys :
                s_index.pop(key)
        else:
            s_index[a] = len(s_list)
            s_list.append(a)
        
        #print(s_list)
    if len(s_list) == 0: 
        #print(s_list, i)
        loop_i = i+1
        break
        
s_index = {}
s_list = []
for i in range(K % loop_i):
    for a in A:
        if a in s_index:
            del_keys = [a]
            for key, val in s_index.items():
                if val > s_index[a]:
                    del_keys.append(key)

            s_list = s_list[:s_index[a]]
            for key in  del_keys :
                s_index.pop(key)
        else:
            s_index[a] = len(s_list)
            s_list.append(a)
        
        #print(s_list)
    if len(s_list) == 0: 
        #print(s_list, i)
        loop_i = i+1
        break        
        
    
print( ' '.join(list(map(str, s_list)))  )

Submission status: Wrong Answer

Solution code:
try:
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    L = [-1] * (2*10**5+2)  # aを入れてindexを返す
    Plus = [0]*N  # indexを入れて1か0
    Next = [0]*N  # indexを入れてindex
    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
    #print(Next)

    for i, a in enumerate(A):
        l = L[a]  # あなたはどこから来たの
        if i <= l:
            Next[l] = i+1
            Plus[l] += 1
            L[a] = -1


    #
    # print(Next)
    # print(Plus)

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

        i = i_next
    #
    # print(loop)

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

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

    start = 0
    for i in range(k-1):
        start = TrueNext[start]
    # print(start)

    ans = []#[A[start]]
    S = [0] * (2*10**5+2)
    #S[ans[0]] = 1
    for a in A[start:]:
        #print(a, ans)
        if S[a]==0:
            ans.append(a)
            S[a] = 1
        else:
            while S[a] != 0:
                a_ = ans.pop()
                S[a_] = 0
    print(*ans)
except:
    exit()

Submission status: Wrong Answer

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

s = []

if n > 1:
    k = k%(n-1)
for _ in range(k):
    for i in range(n):
        if a[i] in s:
            while a[i] in s:
                del s[-1]
        else:
            s.append(a[i])


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

Submission status: Wrong Answer

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


if K >= 2:
    K = 2
X = [As[i%N] for i in range(N*K)]

s = []
Nmax = 200160
dp = [0] * Nmax #最初に入ってあるiの数

set_x = set()
for i,x in enumerate(X):
    seq = dp[x]
    if seq == 0:
        dp[x] = i+1
        set_x.add(x)    
    else:
        set_tmp = set_x.copy()
        for s in set_tmp:
            if seq - dp[s] <= 0:
                dp[s] = 0
                set_x.remove(s)

ans1 = sorted([(dp[s],s) for s in set_x])
ans = [i for x,i in ans1]
print(*ans)

Submission status: Wrong Answer

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

s = []
result = []
for i in range(K):
    for a in A:
        if a in s:
            s = s[0:s.index(a)]
        else:
            s.append(a)

    result.append(s.copy())
    if len(s) == 0:
        break
        

#print("K={} result={}".format(K, result))
ii = K % len(result)
print(" ".join([str(i) for i in result[ii]]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

t = set()
tt = []
ttt = defaultdict(int)

tttt = defaultdict(int)
for i in range(n-1, -1, -1):
  tttt[a[i]] = i

s = []

for i in range(n):
  if ttt[a[i%n]]==0:
    s.append(a[i%n])
    ttt[a[i%n]] += 1
  else:
    while ttt[a[i%n]]>0:
      ttt[s.pop()] -= 1
  if i%n==n-1:
    #print(s)
    if tuple(s) in t:
      want = tuple(s)
      break
    t.add(tuple(s))
    tt.append(tuple(s))


for i in range(k-1):
  if len(s)==0:
    want = tt[0]
    break
  if len(s):
    s = a[tttt[s[0]]+1:]
    ss = []
    ttttt = defaultdict(lambda: -1)
    for j in range(len(s)):
      if ttttt[s[j]]!=-1:
        ss.extend(s[:ttttt[s[j]]])
        ss.extend(s[j+1:])
        ttttt[s[j]] = -1
      else:
        ttttt[s[j]] = j
    s = ss
  #print(s)
  if tuple(s) in t:
    want = tuple(s)
    break
  t.add(tuple(s))
  tt.append(tuple(s))

#print(t)
#print(tt)
#print(want)

if len(tt)==k:
  print(*tt[-1])
else:
  idx = 0
  for i in range(len(tt)):
    if tt[i]==want:
      idx = i
  #print(want, idx)
  print(*tt[k%(len(tt)-idx)-1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import collections



def solve(N: int, K: int, A: "List[int]"):
    last = {}
    dp = {}
    for i,a in enumerate(A):
        if a in last:
            dp[(a,last[a])] = i
        else:
            dp[(a,-1)] = i
        last[a] = i


    s = []
    i = 0
    while i < K:
        j = 0
        if len(s) > 0:
            j = dp[(s[0],-1)] + 1
            s = []
        while j < N:
            if (A[j],j) in dp:
                j = dp[(A[j],j)] + 1
            else:
                s.append(A[j])
                j += 1

        if len(s) == 0:
            if i > 0:
                i = (K // i * i)-1
            else:
                break
        else:
            i += 1

    print(" ".join([str(c) for c in s]))
                


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N-1-0+1) ]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    numlist = list(map(int,input().split()))
    s=[]
    for i in range(1,k+1):
        for j in range(0,n):
            if numlist[j] in s:
                while numlist[j] in s:
                    del s[-1]
            else:
                s.append(numlist[j])
        if len(s) == 0:
            num = (k % i)
            s=[]
            for p in range(0,num):
                for q in range(0,n):
                    if numlist[q] in s:
                        while numlist[q] in s:
                            del s[-1]
                    else:
                        s.append(numlist[q])
            return ' '.join(map(str,s))
print (main())

Submission status: Wrong Answer

Solution code:
def solve(array):
    ans = []
    memo1 = {}
    for i in range(len(array)):
        if array[i] not in memo1:
            memo1[array[i]] = 1
        else:
            memo1[array[i]] += 1
    i = 0
    while i < len(array):
        if memo1[array[i]] == 1:
            ans.append(array[i])
            i += 1
        else:
            tmp = array[i]
            memo1[array[i]] -= 1
            i += 1
            while True:
                memo1[array[i]] -= 1
                if tmp == array[i]:
                    i += 1
                    break
                else:
                    i += 1
    return ans   

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

start = {}
for i in range(len(a))[::-1]:
    start[a[i]] = i
flag = False
li = []
tmp = []
while True:
    memo = {}
    if not tmp:
        start_num = 0
    else:
        start_num = start[tmp[-1]] + 1
    tmp = solve(a[start_num:n])
    #print(tmp)
    if li and li[-1] == tmp:
        flag = True
        break
    li.append(tmp)
    if not tmp:
        break

t = 1
for i in range(len(li)):
    t += len(li[i])
if flag:
    t -= 1
#print(li)
#print(t)
ans = k % t
if ans == 0:
    print()
else:
    for i in range(len(li)):
        if ans <= len(li[i]):
            print(" ".join(map(str, li[i][ans-1:])))
            break
        ans -= len(li[i])

Submission status: Wrong Answer

Solution code:
def solve(array):
    ans = []
    memo1 = {}
    for i in range(len(array)):
        if array[i] not in memo1:
            memo1[array[i]] = 1
        else:
            memo1[array[i]] += 1
    i = 0
    while i < len(array):
        if memo1[array[i]] == 1:
            ans.append(array[i])
            i += 1
        else:
            tmp = array[i]
            memo1[array[i]] -= 1
            i += 1
            while True:
                memo1[array[i]] -= 1
                if tmp == array[i]:
                    i += 1
                    break
                else:
                    i += 1
    return ans   

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

start = {}
for i in range(len(a))[::-1]:
    start[a[i]] = i
flag = False
li = []
tmp = []
while True:
    memo = {}
    if not tmp:
        start_num = 0
    else:
        start_num = start[tmp[-1]] + 1
    tmp = solve(a[start_num:n])
    #print(tmp)
    if li and li[-1] == tmp:
        flag = True
        break
    li.append(tmp)
    if not tmp:
        break

t = 1
for i in range(len(li)):
    t += len(li[i])
if flag:
    t -= (len(li[i]) - 1)
#print(li)
#print(t)
ans = k % t
if ans == 0:
    print()
else:
    for i in range(len(li)):
        if ans <= len(li[i]):
            print(" ".join(map(str, li[i][ans-1:])))
            break
        ans -= len(li[i])

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
s = np.zeros(0)

for i in range(10):
    for j in range(N):
        if np.any(s == A[j]) == False:
            s = np.append(s, A[j])
            
        else:
            k = np.where(s == A[j])[0][-1]
            s = s[:k]
            
print(*s.astype(np.int32))

Submission status: Wrong Answer

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


if K >= 2:
    K = 2
X = [As[i%N] for i in range(N*K)]

s = []
Nmax = 200160
dp = [0] * Nmax #最初に入ってあるiの数

set_x = set()
for i,x in enumerate(X):
    seq = dp[x]
    if seq == 0:
        dp[x] = i+1
        set_x.add(x)    
    else:
        set_tmp = set_x.copy()
        for s in set_tmp:
            if seq - dp[s] <= 0:
                dp[s] = 0
                set_x.remove(s)
ans1 = sorted([(x,i) for i,x in enumerate(dp) if x != 0])
ans = [i for x,i in ans1]
print(*ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
s = np.zeros(0)

for i in range(N):
    for j in range(N):
        if np.any(s == A[j]) == False:
            s = np.append(s, A[j])
            
        else:
            k = np.where(s == A[j])[0][-1]
            s = s[:k]
            
print(*s.astype(np.int32))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
s=[]
b=0
c=0
for n in range(N*K):
    j=n-b*N
    if j>=N:
        b+=1
        j=n-b*N
    if (a[j] in s) :
        while True:
            if s.pop()==a[j]:
                break
    else:
        s.append(a[j])
    j=0

print(s)

Submission status: Wrong Answer

Solution code:
import sys

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

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

    #print(numpos)
    #print(wordlist)

    rem = [] #残る数列の変遷をどうにかして求める
    startindex = 0
    appeared = set()
    appeared_index = dict()
    rep_start = 0
    for i in range(len(A)):
        remtop = wordlist[startindex]
        
        if remtop == -1: 
            rem.append(remtop)
            break
        elif remtop in appeared:
            rep_start = appeared_index[remtop] 
            break
        else: 
            appeared |= {remtop}
            appeared_index[remtop] = i
            rem.append(remtop)
            startindex = (numpos[remtop] + 1) % N
        

    #print(rem)

    rep = len(rem) - rep_start
    ans = (K - rep_start) % rep
    ans -= 1
    #print(ans)
    if ans == -1: print("")
    else:
        if ans == 0: startindex = (0 if rep_start == 0 else remtop[rep_start - 1])
        else: startindex = numpos[rem[rep_start + ans - 1]] + 1
        out = []
        using = dict()
        l = 0
        for i in range(startindex, N):
            if A[i] in using and using[A[i]] <= l:
                out = out[:using[A[i]]]
                l = using[A[i]]
                using.pop(A[i])
            else:
                out.append(A[i])
                using[A[i]] = l
                l += 1
            #print(out, using, l)
        print(" ".join(map(str, out)))


    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    a = list(map(int, input().split()))

    ans = []
    tmp = []
    check = [False] * N
    check[0] = True
    end = False
    for i in range(N*K):
        t = i%N
        # print("now:a[{}]={}".format(t, a[t]))
        c = str(a[t])
        in_ = False
        for j in range(len(tmp)):
            if tmp[j] == c:#入ってたらスライス
                in_ = True
                tmp = tmp[:j]
                n = (t+1) % N
                # print(a[n], n)
                if n == 0 and len(tmp) == 0:
                    end = True
                break
        if not in_:
            tmp.append(c)
        s = "".join(tmp)
        ans.append("".join(tmp))
        if end:
            break
    # print("ans{}:".format(len(ans)), ans)
    print(" ".join(ans[(N*K-1)%len(ans)]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
for i in range(len(a)):
  x = a[i]
  if x in b:
    c[b.index(x)].append(i)
  else:
    b.append(x)
    c.append([i])
d = []
for i in range(len(a)):
  x = a[i]
  xc = c[b.index(x)]
  d.append(xc[(xc.index(i)+1)%len(xc)])
#print(d)
count = 0
now = 0
while now != 0 or count == 0:
  past = now
  now = ( d[past] + 1 ) % n
  if now - 1 <= past:
    count += 1
  #print(past,now,count)
print(count)
limit = k % count
#print(count,limit)
count = 0
now = 0
while True:
  past = now
  now = ( d[past] + 1 ) % n
  if now - 1 <= past:
    if count == limit - 2:
      break
    count += 1
#print(d,past,a)
ans = []
for i in range(-n+past,n):
  x = a[i%n]
  #print(x)
  if x in ans:
    ans = ans[:ans.index(x)]
  else:
    ans.append(x)
#print(ans)
ans2 = list(map(str,ans))
print(' '.join(ans2))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
 
X = list(map(str,input().split()))
 
mylist = X*2
 
s = []
for i in mylist:
    if i not in s:
        s += [i]
    else:
        s = s[:s.index(i)]
    #print(i, s)
        


s = ' '.join(s)
print(s)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

ends = []
e = []
k = 0
while True:
    for a in A:
        if a in e:
            e = e[:e.index(a)]
        else:
            e.append(a)
    ends.append(deepcopy(e))
    k += 1
    if len(e) == 0 or k >= K:
        break

if k >= K:
    ans = ends[-1]
else:
    ans = ends[N % k]

s = ""
for k in ans:
    s += str(k)
    s += " "
print(s[:-1])

Submission status: Wrong Answer

Solution code:
import sys
n,k = map(int,input().split())
numlist = list(map(int,input().split()))
s=[]
for i in range(1,k+1):
    for j in range(0,n):
        if numlist[j] in s:
            while numlist[j] in s:
                del s[-1]
        else:
            s.append(numlist[j])
    if len(s) == 0:
        num = (k % i)
        s=[]
        for p in range(0,num):
            for q in range(0,n):
                if numlist[q] in s:
                    while numlist[q] in s:
                        del s[-1]
                else:
                    s.append(numlist[q])
        print(' '.join(map(str,s)))
        sys.exit()

Submission status: Wrong Answer

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

s = []
i = 0
while i < n * k:
    r = i % n
    if r == 0 and s == [] and i != 0:
        i = n * (k - 1)
    x = a[r]
    j = 0
    try:
        seq = list(a)
        if i // n < k - 1:
            seq.extend(a)
        j = seq[r+1:].index(x)
        i = i + j + 2
    except:
        s.append(a[r])
        i += 1
print(' '.join(str(y) for y in s))

Submission status: Wrong Answer

Solution code:
import sys

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

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

    #print(numpos)
    #print(wordlist)

    rem = [] #残る数列の変遷をどうにかして求める
    startindex = 0
    appeared = set()
    appeared_index = dict()
    rep_start = 0
    for i in range(len(A)):
        remtop = wordlist[startindex]
        
        if remtop == -1: 
            rem.append(remtop)
            break
        elif remtop in appeared:
            rep_start = appeared_index[remtop] -1
            break
        else: 
            appeared |= {remtop}
            appeared_index[remtop] = i
            rem.append(remtop)
            startindex = (numpos[remtop] + 1) % N
        

    #print(rem)

    rep = len(rem) - rep_start
    ans = (K - rep_start) % rep
    ans -= 1
    #print(ans)
    if ans == -1: print("")
    else:
        if ans == 0: startindex = rep_start
        else: startindex = numpos[rem[rep_start + ans-1]] + 1
        out = []
        using = dict()
        l = 0
        for i in range(startindex, N):
            if A[i] in using and using[A[i]] <= l:
                out = out[:using[A[i]]]
                l = using[A[i]]
                using.pop(A[i])
            else:
                out.append(A[i])
                using[A[i]] = l
                l += 1
            #print(out, using, l)
        print(" ".join(map(str, out)))


    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

cnt = 0
s = ''
tmp = []
for i in range(K):
        for n in A:
                if n in s:
                        while n in s:
                                s = s[:-1]
                else:
                        s = s + n
        if s in tmp:
                break
        else:
                tmp.append(s)


tmp = list(tmp[K % len(tmp) - 1])
for ans in tmp:
        print(ans)

Submission status: Wrong Answer

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


if K >= 2:
    K = 2
X = [As[i%N] for i in range(N*K)]

s = []
Nmax = 200160
dp = [0] * Nmax #最初に入ってあるiの数

for i,x in enumerate(X):
    seq = dp[x]
    if seq == 0:
        dp[x] = i+1
    else:
        dp = [0 if seq-x1<=0 else x1 for x1 in dp]

ans1 = sorted([(x,i) for i,x in enumerate(dp) if x != 0])
ans = [i for x,i in ans1]
print(*ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

gap = [0] * n
num = {}
for i in range(n):
    num.setdefault(a[i], [])
    num[a[i]].append(i)
for i in num.values():
    for j in range(len(i)):
        gap[i[j]] = (i[(j + 1) % len(i)] - i[j] - 1) % n + 1
count = 0
x = 0
x_list = [0]
while True:
    if count % n == 0 and count > 0:
        term = count // n
        break
    else:
        count += (gap[x % n] + 1)
        x += (gap[x % n] + 1)
        x_list.append(x)
k = (k - 1) % term + 1
x2 = x_list[bisect.bisect(x_list, k * n) - 1]
queue = []
for i in range(-x2 % n):
    if a[(x2 + i) % n] not in set(queue):
        queue.append(a[(x2 + i) % n])
    else:
        while a[(x2 + i) % n] in set(queue):
            del queue[-1]
for i in queue:
    print(i, end = " ")

Submission status: Wrong Answer

Solution code:
import sys

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

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

    #print(numpos)
    #print(wordlist)

    rem = [] #残る数列の変遷をどうにかして求める
    startindex = 0
    appeared = set()
    appeared_index = dict()
    rep_start = 0
    for i in range(len(A)):
        remtop = wordlist[startindex]
        
        if remtop == -1: 
            rem.append(remtop)
            break
        elif remtop in appeared:
            rep_start = appeared_index[remtop]
            break
        else: 
            appeared |= {remtop}
            appeared_index[remtop] = i
            rem.append(remtop)
            startindex = (numpos[remtop] + 1) % N
        

    #print(rem)

    rep = len(rem) - rep_start
    ans = (K - rep_start) % rep
    ans -= 1
    #print(ans)
    if ans == -1: print("")
    else:
        if ans == 0: startindex = rep_start
        else: startindex = numpos[rem[rep_start + ans-1]] + 1
        out = []
        using = dict()
        l = 0
        for i in range(startindex, N):
            if A[i] in using and using[A[i]] <= l:
                out = out[:using[A[i]]]
                l = using[A[i]]
                using.pop(A[i])
            else:
                out.append(A[i])
                using[A[i]] = l
                l += 1
            #print(out, using, l)
        print(" ".join(map(str, out)))


    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
N , K = map(int, input().split())
a = list(input().split())
#print(a)
a = [int(s) for s in a]
if a[0] == a[-1] and (not a[0] in a[1:-1]):
        print()
        sys.exit()
b = a
q = 0
while True:
        check = True
        if b == []: break
        for i in range(q+1,len(b)):
                if b[q] == b[i]:
                        b = b[:q] + b[i+1:]
                        check = False
                        break
        if check:
                q += 1
                if q >= len(b): break
#print(b)
if b == []:
        print()
        sys.exit()
#print(a)
x = b
for i in range(K-1):
        x += a
p = 0

while True:
#       print(x)
        check = True
        if x == []: break
        for i in range(p+1,len(x)):
                if x[p] == x[i]:
                        x = x[:p] + x[i+1:]
                        check = False
                        break
        if check:
                p+=1
                if p >= len(x): break


for i in range(len(x)-1):
        print("{} ".format(x[i]),end="")
if len(x): print("{}".format(x[-1]))
else: print()

Submission status: Wrong Answer

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

cnt = 0
s = ''
tmp = []
for i in range(K):
        for n in A:
                if n in s:
                        while n in s:
                                s = s[:-1]
                else:
                        s = s + n
        if s in tmp:
                break
        else:
                tmp.append(s)
else:
        for ans in tmp[-1]:
                print(ans)
        else:
                exit()


tmp = list(tmp[K % len(tmp) - 1])
for ans in tmp:
        print(ans)