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: Time Limit Exceeded

Solution code:
import collections
N, K = [int(_) for _ in input().split()]
A1 = [int(_) for _ in input().split()]
A = A1 * 2
a_i = collections.defaultdict(lambda: [])
for i, a in enumerate(A):
    a_i[a] += [i]
i_delta = [0] * (2 * N)
for a in a_i:
    array = a_i[a]
    for i in range(len(array) - 1):
        i_delta[array[i]] = array[i + 1] - array[i] + 1
first = [-1] * N
second = [-1] * N
i = 0
while second[i % N] == -1:
    if first[i % N] == -1:
        first[i % N] = i
    else:
        second[i % N] = i
        break
    i += i_delta[i % N]
i %= N
start = first[i] + (second[i] - first[i]) * (
    (N * K - first[i]) // (second[i] - first[i]))
end = N * K
while True:
    s2 = start + i_delta[start % N]
    if s2 >= end:
        break
    start = s2
B = A1[N - (end - start) % N:] + A1 * ((end - start) // N)
dp = [{}]
for i, b in enumerate(B):
    if b in dp[-1]:
        dp = dp[:dp[-1][b]]
    else:
        s = dp[-1].copy()
        s[b] = len(dp)
        dp += [s]
print(*list(a for a, _ in sorted(dp[-1].items(), key=lambda xs: xs[1])))

Submission status: Time Limit Exceeded

Solution code:
import math

# input
#
# n = 5
# k = 10
# a = [1, 2, 3, 2, 3]

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

# make jumpto
#
# 0 -> 6 (6)
# 1 -> 4 (3)
# 2 -> 5 (3)
# 3 -> 7 (4)
# 4 -> 8 (4)

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

# doubling jumpto
#
# 0 -> 6 ->  9 (9)
# 1 -> 4 ->  8 (7)
# 2 -> 5 -> 11 (9)
# 3 -> 7 -> 10 (7)
# 4 -> 8 -> 12 (8)
#
# 0 -> 6 ->  9 -> 13 -> 17 (17)
# 1 -> 4 ->  8 -> 12 -> 15 (14)
# 2 -> 5 -> 11 -> 14 -> 18 (16)
# 3 -> 7 -> 10 -> 16 -> 19 (16)
# 4 -> 8 -> 12 -> 15 -> 21 (17)

levels = int(math.log(n * 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

# step1 and step2
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))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

b = []
for i in range(0,n):
    p = a[i]
    chou = False
    for j in range(i+1,n):
        if a[i] == a[j]:
            chou = True
            b.append(j)
            break
    if not chou:
        for j in range(0,i+1):
            if a[i] == a[j]:
                chou = True
                b.append(j)
                break
def f(s,count):
    if s < b[s]:
        if b[s]==n-1:
            return count
        else:
            return f(b[s]+1,count)
    else:
        count += 1
        if b[s]==n-1:
            return count
        else:
            return f(b[s]+1,count)

lasta = []
loop = f(0,0)
leng = min(k%(loop+1),k)
if loop == 0:
    lasta = []
else:
    for i in range(0,leng):
        lasta += a
lenlasta = n*leng
ans = []
#listaからansをつくる．
t = 0
count = 0
while lenlasta>0:
    if b[t]<=t:
        count=count+1
        j = b[t]+count*n
        if j<lenlasta-1:
            t = (b[t]+1)%n
        elif j == lenlasta-1:
            break
        else:
            ans.append(lasta[t])
            count = count -1
            if t+count*n==lenlasta-1:
                break
            else:
                t = (t + 1)%n
    else:
        j = b[t]+count*n
        if j<lenlasta-1:
            t = (b[t]+1)%n
        elif j == lenlasta-1:
            break
    


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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

b = []
for i in range(0,n):
    p = a[i]
    chou = False
    for j in range(i+1,n):
        if a[i] == a[j]:
            chou = True
            b.append(j)
            break
    if not chou:
        for j in range(0,i+1):
            if a[i] == a[j]:
                chou = True
                b.append(j)
                break
def f(s,count):
    if s < b[s]:
        if b[s]==n-1:
            return count
        else:
            return f(b[s]+1,count)
    else:
        count += 1
        if b[s]==n-1:
            return count
        else:
            return f(b[s]+1,count)

lasta = []
loop = f(0,0)
if loop == 0:
    lasta = []
else:
    for i in range(0,min(k%(loop+1),k)):
        lasta += a

ans = []
#listaからansをつくる．
t = 0
count = 0
while len(lasta)>0:
    if b[t]<=t:
        count=count+1
        if b[t]+count*n<len(lasta)-1:
            t = (b[t]+1)%n
        elif b[t]+count*n == len(lasta)-1:
            break
        else:
            ans.append(lasta[t])
            count = count -1
            if t+count*n==len(lasta)-1:
                break
            else:
                t = (t + 1)%n
    else:
        if b[t]+count*n<len(lasta)-1:
            t = (b[t]+1)%n
        elif b[t]+count*n == len(lasta)-1:
            break
    


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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

#A
def A():
    n = I()
    if n <= 1000000000:
        print(0,0,1,0,0,n)
    else:
        b = 1000000000
        a = 1
        y = -n%1000000000
        x = (n+a*y)//b
        print(0,0,x,y,a,b)
        return

    return

#B
def B():
    n,k = LI()
    a = LI()
    for i in range(n):
        a.append(a[i])
    MA = 200000
    f = [n+1]*n
    l = [-1]*(MA+1)
    for i in range(n):
        if l[a[i]] < 0:
            l[a[i]] = i
        else:
            if f[l[a[i]]] > n:
                f[l[a[i]]] = i+1-l[a[i]]
                l[a[i]] = i
    for i in range(n,2*n):
        if f[l[a[i]]] > n:
            f[l[a[i]]] = i+1-l[a[i]]
            l[a[i]] = -1
    b = 57
    g = [[f[i]] for i in range(n)]
    m = n*k
    for i in range(b):
        for j in range(n):
            d = g[j][i]+g[(j+g[j][i])%n][i]
            if d >= m:break
            g[j].append(g[j][i]+g[(j+g[j][i])%n][i])
        else:
            continue
        break
    i = 0
    while 1:
        j = bisect.bisect_left(g[i],m)
        if j == 0:
            break
        m -= g[i][j-1]
        i = (i+g[i][j-1])%n
    s = []
    f = [1]*(MA+1)
    for j in range(i,i+m):
        k = j%n
        if f[a[k]]:
            s.append(a[k])
            f[a[k]] = 0
        else:
            while 1:
                x = s.pop(-1)
                f[x] = 1
                if x == a[k]:
                    break
    print(*s)
    return

#C
mod = 998244353
def C():
    def comb(a,b):
        if a < b:
            return 1
        return fact[a]*inv[b]*inv[a-b]%mod
    n,m = LI()
    MA = 3*m+max(n,2*m)-1
    fact = [1]*(MA+1)
    for i in range(MA):
        fact[i+1] = fact[i]*(i+1)%mod
    inv = [1]*(MA+1)
    inv[MA] = pow(fact[MA],mod-2,mod)
    for i in range(MA)[::-1]:
        inv[i] = inv[i+1]*(i+1)%mod
    ans = 0
    for k in range(min(m,n)+1):
        if (3*m-k)%2 == 0:
            ans += comb(n,k)*comb((3*m-k)//2+n-1,n-1) #奇数の数がk個かつ総和が3mとなるパターンの総数(n個のうちk個に1を割り振る→総和が3m-kとなるパターンの総数を求める)
            ans %= mod
    ans -= n*comb(3*m-(2*m+1)+n-1,n-1)
    ans %= mod
    print(ans)

    return

#D
def D():
    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

#A
def A():
    n = I()
    if n <= 1000000000:
        print(0,0,1,0,0,n)
    else:
        b = 1000000000
        a = 1
        y = -n%1000000000
        x = (n+a*y)//b
        print(0,0,x,y,a,b)
        return

    return

#B
def B():
    n,k = LI()
    a = LI()
    for i in range(n):
        a.append(a[i])
    MA = 200000
    f = [n+1]*n
    l = [-1]*(MA+1)
    for i in range(n):
        if l[a[i]] < 0:
            l[a[i]] = i
        else:
            if f[l[a[i]]] > n:
                f[l[a[i]]] = i+1-l[a[i]]
                l[a[i]] = i
    for i in range(n,2*n):
        if f[l[a[i]]] > n:
            f[l[a[i]]] = i+1-l[a[i]]
            l[a[i]] = -1
    b = 57
    g = [[f[i]] for i in range(n)]
    for i in range(b):
        for j in range(n):
            g[j].append(g[j][i]+g[(j+g[j][i])%n][i])
    m = n*k
    i = 0
    while 1:
        j = bisect.bisect_left(g[i],m)
        if j == 0:
            break
        m -= g[i][j-1]
        i = (i+g[i][j-1])%n
    s = []
    f = [1]*(MA+1)
    for j in range(i,i+m):
        k = j%n
        if f[a[k]]:
            s.append(a[k])
            f[a[k]] = 0
        else:
            while 1:
                x = s.pop(-1)
                f[x] = 1
                if x == a[k]:
                    break
    print(*s)
    return

#C
mod = 998244353
def C():
    def comb(a,b):
        if a < b:
            return 1
        return fact[a]*inv[b]*inv[a-b]%mod
    n,m = LI()
    MA = 3*m+max(n,2*m)-1
    fact = [1]*(MA+1)
    for i in range(MA):
        fact[i+1] = fact[i]*(i+1)%mod
    inv = [1]*(MA+1)
    inv[MA] = pow(fact[MA],mod-2,mod)
    for i in range(MA)[::-1]:
        inv[i] = inv[i+1]*(i+1)%mod
    ans = 0
    for k in range(min(m,n)+1):
        if (3*m-k)%2 == 0:
            ans += comb(n,k)*comb((3*m-k)//2+n-1,n-1) #奇数の数がk個かつ総和が3mとなるパターンの総数(n個のうちk個に1を割り振る→総和が3m-kとなるパターンの総数を求める)
            ans %= mod
    ans -= n*comb(3*m-(2*m+1)+n-1,n-1)
    ans %= mod
    print(ans)

    return

#D
def D():
    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
tmp = a[0]
i = 0
j = 1
while i < N:
	i += 1
	if i == N:
		i = 0
		j += 1
	if a[i] == tmp:
		i += 1
		if i < N:
			tmp = a[i]
K %= j
if K == 0:
	print()
else:
	M = N * K
	b = a * K
	c = []
	tmp =  b[0]
	k = 0
	l = 0
	while k < M:
		while l < M:
			l += 1
			if l == M:
				c.append(tmp)
				k += 1
				l = k
				if k < M:
					tmp = b[k]
			else:
				if b[l] == tmp:
					l += 1
					k = l
					if k < M:
						tmp = b[k]
	C = [str(x) for x in c]
	print(' '.join(C))

Submission status: Time Limit Exceeded

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

num=0
Kuri=0
s= [0]
i=0
for j in range(K):
    for i in range(N):
        if (A[i] in s):
            point=s.index(A[i])
            del s[int(point):]
        else:
            s.append(A[i])
    if s == [0]:
        num=(j+1)
        break

if (num != 0):
    Kuri=K%num#the number of cycle
    for j in range(Kuri):
        for i in range(N):
            if (A[i] in s):
                point=s.index(A[i])
                del s[int(point):]
            else:
                s.append(A[i])
    
if len(s) != 1:
    del s[0]
    s=[str(a) for a in s]
    s=" ".join(s)
    print(s)
else:
    print("")

Submission status: Time Limit Exceeded

Solution code:

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

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

dic = {}


for i in range(len(a)):

    if a[i] not in dic:
        dic[a[i]] = [i]

    else:
        dic[a[i]].append(i)

#print (dic)
left = a[0]
poi = 0
cnt = 1
now = 0

while True:

    poi += 1

    if poi >= len(dic[left]):
        cnt += 1
        poi = 0

    if dic[left][poi]+1 < N:
        nleft = a[dic[left][poi]+1]
        now = dic[left][poi]+1
    else:
        nleft = a[0]
        now = 0
        break

    #print (dic[nleft],now)
    npoi = dic[nleft].index(now)

    left = nleft
    poi = npoi

#print (left,poi,cnt)
    

if K >= cnt:
    st = K % cnt
else:
    st = K


#print (st)



left = a[0]
poi = 0
cnt = 1
now = 0

while cnt < st:

    poi += 1

    if poi >= len(dic[left]):
        cnt += 1
        poi = 0

    if dic[left][poi]+1 < N:
        nleft = a[dic[left][poi]+1]
        now = dic[left][poi]+1
    else:
        nleft = a[0]
        now = 0

    #print (dic[nleft],now)
    npoi = dic[nleft].index(now)

    left = nleft
    poi = npoi

#print (left,poi,cnt)

ndic = {}
nlis = []


now = dic[left][poi]

for j in range(N-now):

    j += now

    if a[j] not in ndic or ndic[a[j]] == "x":
        ndic[a[j]] = len(nlis)
        nlis.append(a[j])
    
    else:

        nlis = nlis[:ndic[a[j]]]

        fi = ndic[a[j]]
        for k in ndic:
            if ndic[k] != "x" and fi <= ndic[k]:
                ndic[k] = "x"

        #print ("ij",i,j,":"," ".join(map(str,nlis)),ndic)

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

Submission status: Time Limit Exceeded

Solution code:

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

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

dic = {}


for i in range(len(a)):

    if a[i] not in dic:
        dic[a[i]] = [i]

    else:
        dic[a[i]].append(i)

#print (dic)
left = a[0]
poi = 0
cnt = 1
now = 0

while True:

    poi += 1

    if poi >= len(dic[left]):
        cnt += 1
        poi = 0

    if dic[left][poi]+1 < N:
        nleft = a[dic[left][poi]+1]
        now = dic[left][poi]+1
    else:
        nleft = a[0]
        now = 0
        break

    #print (dic[nleft],now)
    npoi = dic[nleft].index(now)

    left = nleft
    poi = npoi

#print (left,poi,cnt)
    

if K >= cnt:
    st = K % cnt
else:
    st = K


#print (st)

ndic = {}
nlis = []

for i in range(st):

    for j in range(len(a)):

        if a[j] not in ndic or ndic[a[j]] == "x":
            ndic[a[j]] = len(nlis)
            nlis.append(a[j])

        else:

            nlis = nlis[:ndic[a[j]]]

            fi = ndic[a[j]]
            for k in ndic:
                if ndic[k] != "x" and fi <= ndic[k]:
                    ndic[k] = "x"

        #print ("ij",i,j,":"," ".join(map(str,nlis)),ndic)


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

Submission status: Time Limit Exceeded

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

def add(S, e, A):
    for Ai in A:
      we = e
      S[e] = Ai
      e = S.index(Ai, 0, e+1)
      if we == e:
        e = e + 1
    return S, e

ok = False
S = [0] * (2 * N)
e = 0
for i in range(K):
    S, e = add(S, e, A)
    if e == 0:
        break
else:
    ok = True
    print(" ".join(map(str, S[:e])))

if not ok:
    for i in range(K%(i+1)):
        S, e = add(S, e, A)
    print(" ".join(map(str, S[:e])))

Submission status: Time Limit Exceeded

Solution code:
n , k = map(int, input().split())
a = list(map(int, input().split()))
ans = []
memo = []
for i in range(k):
    for j in range(n):
        c = a[j]
        if c in ans:
            ans = ans[:ans.index(c)]
        else:
            ans.append(c)
    memo.append(ans[:])
    if len(ans) == 0:
        break
print(" ".join([str(item) for item in memo[k % len(memo) - 1]]))

Submission status: Time Limit Exceeded

Solution code:
def trial(n,deck):
    cur = [n]
    for i in range(len(deck)):
        if deck[i] in cur:
            temp = cur.index(deck[i])
            cur = cur[:temp]
        else:
            cur.append(deck[i])
    if len(cur) > 0:
        return cur
    else:
        return [0]
            
n,k = map(int,input().split())
a = list(map(int, input().split()))
d = {}
for j in [0]+list(set(a)):
    if j == 0:
        temp = trial(j,a)
        d[j] = temp[1:]
    else:
        d[j] = trial(j,a)
    if len(d[j]) == 0:
        d[j] = [0]

ii = 0
cond = True
sd = []
while cond:
    if d[ii][0] != 0:
        sd.append(d[ii])
        ii = d[ii][0]
    else:
        sd.append(d[ii])
        cond = False

ans = sd[(k-1)%len(sd)]
print(" ".join(list(map(str, ans)))) if ans != [0] else print()

Submission status: Time Limit Exceeded

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

N = NK[0]
K = NK[1]
X = []

num=0
Kuri=0

#print(N)
#print(K)
#print(A)
#print(X)
s= [0]

i=0
for j in range(K):
    for i in range(N):
        if (A[i] in s):
            point=s.index(A[i])
            del s[int(point):]
        else:
            s.append(A[i])
    if s == [0]:
        num=(j+1)
        break

if (num != 0):
    Kuri=K%num#実際に繰り返す回数
    for j in range(Kuri):
        for i in range(N):
            if (A[i] in s):
                point=s.index(A[i])
                del s[int(point):]
            else:
                s.append(A[i])

    
if len(s) != 1:
    del s[0]
    s=[str(a) for a in s]
    s=" ".join(s)
    print(s)
else:
    print("")

Submission status: Time Limit Exceeded

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

num_list = []
index_list = []
for i in range(len(A)):
    if(not A[i] in num_list):
        num_list.append(A[i])
        index_list.append([i])
    else:
        index_list[num_list.index(A[i])].append(i)

T = 0
remain = []
while True:
    N_remain = len(remain)
    B = remain + A
    i = 0
    remain = []
    while(i < len(B)):
        temp_list = index_list[num_list.index(B[i])]
        ind_list = []
        for j in range(len(temp_list)):
            if(temp_list[j] + N_remain > i):
                ind_list.append(temp_list[j] + N_remain)
        
        if(len(ind_list) >= 1):
            i = ind_list[0] + 1
        else:
            remain.append(B[i])
            i += 1
    T += 1
    if(remain == []):
        break

M = K % T
for _ in range(M):
    N_remain = len(remain)
    B = remain + A
    i = 0
    remain = []
    while(i < len(B)):
        temp_list = index_list[num_list.index(B[i])]
        ind_list = []
        for j in range(len(temp_list)):
            if(temp_list[j] + N_remain > i):
                ind_list.append(temp_list[j] + N_remain)
        
        if(len(ind_list) >= 1):
            i = ind_list[0] + 1
        else:
            remain.append(B[i])
            i += 1


ans = ''
for i in range(len(remain)):
    ans += str(remain[i]) + ' '
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(v) for v in input().split()]
al = [int(v) for v in input().split()]

pre_ind = {}
first_ind = {}
skip_table = {}
for i, a in enumerate(al):
    if a in pre_ind:
        skip_table[pre_ind[a]] = i
    pre_ind[a] = i
    if a not in first_ind:
        first_ind[a] = i

# print(first_ind, skip_table)

state = []
states = [[]]
ls = 0
period = 0
while True:
    period += 1

    if len(state) > 0:
        pa = first_ind[state[0]]+1
    else:
        pa = 0
    while pa in skip_table:
        pa = skip_table[pa] + 1
    state = al[pa:]

    si = 1
    while len(state) > si:
        for i, a in enumerate(al[pa+1:]):
            if state[si] == a:
                pa = i + pa + 1
                while pa in skip_table:
                    pa = skip_table[pa] + 1
                break
        state = state[:si] + al[pa:]
        si += 1
    
    if len(state) == 0:
        break
    states.append(state.copy())


# print(period, states)
print(*states[k%period])

Submission status: Time Limit Exceeded

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

N = NK[0]
K = NK[1]
X = []

num=0
Kuri=0

#print(N)
#print(K)
#print(A)

for i in range(K):
    X += A

#print(X)
s= [0]

i=0
for j in range(K):
    for i in range(N):
        if (A[i] in s):
            point=s.index(A[i])
            del s[int(point):]
        else:
            s.append(A[i])
    if s == [0]:
        num=(j+1)
        break

if (num != 0):
    Kuri=K%num#実際に繰り返す回数
    for j in range(Kuri):
        for i in range(N):
            if (A[i] in s):
                point=s.index(A[i])
                del s[int(point):]
            else:
                s.append(A[i])

    
del s[0]
s=[str(a) for a in s]
s=" ".join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, K = (int(_) for _ in input().split())
A = np.array(list((int(_) for _ in input().split())))
c = []
flag = 1
count = 0

while flag ==  1:
    if A[count%N] in c:
        c = c[:c.index(A[count%N])]
        count += 1
    else:
        c.append(A[count%N])
        count += 1
    if c ==[] and count % N == 0:
        flag = 0
    #print(c)
loop = count / N
k = K%loop

count = 0
for i in range(N*int(k)):
    if A[count%N] in c:
        c = c[:c.index(A[count%N])]
    else:
        c.append(A[count%N])
    count += 1
        
print(' '.join(map(str, c)))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
pos=[[] for _ in range(2*10**5+1)]
pos[0].append(-1)
arrpos=[0]*n
for i in range(n):
  pos[arr[i]].append(i)
  arrpos[i]=len(pos[arr[i]])
ans=[]
tmp=[0]
cnt=0
while len(tmp)!=0:
  p=pos[tmp[0]][0]+1
  ntmp=[]
  flag=True
  while p<n:
    if arrpos[p]<len(pos[arr[p]]):
      p=pos[arr[p]][arrpos[p]]+1
    else:
      ntmp.append(arr[p])
      if len(pos[arr[p]])!=1:
        flag=False
      p+=1
  ans.append(ntmp)
  tmp=ntmp
  cnt+=1
  if flag==True:
    break
for i in range(1,len(tmp)+1):
  ans.append(tmp[i:])
print(*ans[k%len(ans)-1])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
ans = []
idx = 0 

while True:

  x = idx
  for j in range(idx, n):
    if a[j] == a[idx] and j != idx:
      x = j
      break
  if x != idx:
    idx = x + 1
    if idx == n:
      break
  else:
    ans.append(a[idx:])
  #  print("check", idx)
    for j in range(idx):
      if a[j] == a[idx] and j != idx:
        idx = j + 1 
        break
    else:
      idx = idx + 1
      if idx == n:
        break
count =len(ans) + 1
k %= count
#print(ans, k)
if k != 0:
  L = ans[k-1]
  idx = 1
  m = len(L)
  if m > 3:
    while True:
      for i in range(idx+1, m):
        if L[i] == L[idx]:
          L = L[0:idx] + L[i+1:m]
          break
      if m == len(L):
        idx += 1
      else:
        m = len(L)
      if m <= idx:
        break
  print(" ".join(map(str, L)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N, K = map(int, input().split())
As = [int(a) for a in input().split()]

prev = (As[0], 0)
flag = False

dq = deque()
for j in range(K):
    for i, a in enumerate(As, 1):
        if a in dq:
            while a != dq.pop():
                pass
        else:
            dq.append(a)
    if not dq:
        flag = True
        break
if flag:
    for j in range(K % (j + 1)):
        for i, a in enumerate(As, 1):
            if a in dq:
                while a != dq.pop():
                    pass
            else:
                dq.append(a)
        if not dq:
            flag = True
            break

print(*dq)

Submission status: Time Limit Exceeded

Solution code:
def roop(A, s):
    for v in A:
        if s.__contains__(v):
            s.reverse()
            i = s.index(v) + 1
            s.reverse()
            del s[-i:]
        else:
            s.append(v)

    return


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    s = list()
    t = list()

    for i in range(K):
        roop(A, s)
        if len(s) == 0 and i == 0:
            break
        elif t.__contains__(s):
            t.append(s.copy())
            s = t[K % i]
            break

    print(*(t for t in s))

    return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

# 各文字が最初に出現するインデックス
posFirst = {}
for i, A in enumerate(As):
    if A not in posFirst:
        posFirst[A] = i + N

# 次に同じ文字が出現するインデックス
nexts = [-1] * N
posLast = {}
for i in reversed(range(N)):
    A = As[i]
    if A not in posLast:
        nexts[i] = posFirst[A]
    else:
        nexts[i] = posLast[A]
    posLast[A] = i

v0s = [-1] * N
v2s = [-1] * N
for i in range(N):
    if nexts[i] < N-1:
        v2s[i] = nexts[i] + 1
        v0s[nexts[i] + 1] = i
    else:
        v2s[i] = i

v9s = [-1] * N
for i in reversed(range(N)):
    if v9s[i] == -1:
        v9s[i] = v2s[i]
    v9s[v0s[i]] = v9s[i]

numLoop = 0
vNow = 0
while True:
    vNow = nexts[vNow]
    if vNow >= N:
        numLoop += 1
    vNow %= N
    if vNow == N-1:
        break
    vNow += 1

K %= (numLoop+1)

if K == 0:
    print()
    sys.exit()

numLoop = 0
vNow = 0
while True:
    vNow = nexts[vNow] + 1
    if vNow >= N:
        numLoop += 1
    vNow %= N
    if numLoop == K-1:
        break

ss = []
s2s = set()
for v in range(vNow, N):
    X = As[v]
    if X not in s2s:
        ss.append(X)
        s2s.add(X)
    else:
        s = ss.pop()
        s2s.discard(s)
        while s != X:
            s = ss.pop()
            s2s.discard(s)

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

Submission status: Time Limit Exceeded

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

def add(S, e, A):
    for Ai in A:
        try:
            e = S.index(Ai, 0, e)
        except ValueError as _:
            S[e] = Ai
            e = e + 1
    return S, e

ok = False
S = [0] * (2 * N)
e = 0
for i in range(K):
    S, e = add(S, e, A)
    if e == 0:
        break
else:
    ok = True
    print(" ".join(map(str, S[:e])))

if not ok:
    for i in range(K%(i+1)):
        S, e = add(S, e, A)
    print(" ".join(map(str, S[:e])))

Submission status: Time Limit Exceeded

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+1) * (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: Time Limit Exceeded

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
if loop_i != -1:
    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)
        
    
print( ' '.join(list(map(str, s_list)))  )