Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

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

b = A[0]

if A[0] == 1:
    ans = 1

else:
    for i in range(N):
        c = A[b-1]
        b = c
    ans = b
print(ans)

Submission status: Wrong Answer

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

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

count = [0]

while True:
    countCheck = copy.copy(count)
    count.append(A[count[-1]]-1)
#    print(countCheck, count)
    if(count[-1] in countCheck):
         break

#print(count)


saisyo = count.index(count[-1]) + 1

loop_kazu = len(count) - saisyo

#print(loop_kazu)
K_loop = K - saisyo

ans_index = K_loop % loop_kazu
#print(ans_index)

ans = count[saisyo + ans_index] + 1

print(ans)

#hoge = count[loop:loop_kaisuu]
#ans = hoge[(K - loop) % loop_kaisuu]

#ans = count[K % loop] + 1

#print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
visited = [False for _ in range(n)]
nx = A[0] - 1
x = []
for e, i in enumerate(range(n)):
    if visited[nx] == False:
        visited[nx] = True
        x.append(nx)
        nx = A[nx] - 1
    else:
        p = x.index(nx)
        q = x[:p]
        r = x[p:]
        break
k = (k-len(q))%len(r)
print(r[k-1]+1)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()));
a = list(map(int,input().split()));
i = 0;
count = 0;
while count<=n :
    #print(i,count)
    i = a[i]-1;
    count += 1;
    #print(i,count,1)
    if i == 0 :
        break;

amari = 0;
if count > n :
    ans = i+1;
else :
    amari = k%count;
    i = 0;
    for j in range(amari) :
        i = a[i]-1;
    ans = i+1;

print(ans);

Submission status: Wrong Answer

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

warpmap = {}

for town_num, to_warp in enumerate(A):
    
    town_num = town_num + 1
    warpmap[f"{town_num}"] = f"{to_warp}"

def check_loop(start='1', counter=0):
    past_towns = []

    for _ in range(N):
        counter += 1

        nextmap = warpmap[start]

        if nextmap in past_towns:
            return counter, counter - past_towns.index(nextmap)
        else:
            past_towns.append(start)

        start = nextmap

def getterminal(n, start):
    if n == 0:
        return start
    else:
        for _ in range(1, n + 1):
            nextmap = warpmap[start]
            start = nextmap

        return nextmap

pasttime, looptime = check_loop()

print(getterminal(n=(K - pasttime) % looptime, start=getterminal(n=pasttime, start='1')))

Submission status: Wrong Answer

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

# print(a_list)

NN = N
if N > K:
    NN = 0
else:
    NN = N + K % N

next = 1
for _ in range(NN):
    next = a_list[next-1]

print(next)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
flag = 0
position = [0]
while flag == 0:
  temp = position[-1]
  if A[temp] -1 in position:
    flag = 1
  position.append(A[temp]-1)
for i in range(len(position)):
  if position[i] == position[-1]:
    start = i
    break

x = start + (K - start)%(len(position) - start -1)

print(position[x]+1)

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count <= n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[k % n] + 1)

Submission status: Wrong Answer

Solution code:
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))
LS=lambda:list(map(str,input().split()))

def main():
    import sys
    input = sys.stdin.readline
    n,k=L()
    a=L()
    visited=[0]*(n+1)
    tmp=0
    now=0
    visited[0]=1
    loopat=0
    for i in range(k):
        #print(visited,end="")
        tmp=i+2
        now=a[now]-1
        #print(now)
        #print(tmp,now+1)
        if visited[now]==0:
            visited[now]=i+2
        else:
            loopat=now
            break
    #print(visited)
    #print(loopat,visited[loopat],tmp)
    #print(tmp,now)
    if tmp-visited[loopat]==0:
        print(a[now])
        exit()
        tmp2=k
        dist=1
        now=0
    else:
        tmp2=(k-(visited[loopat]-1))%(tmp-visited[loopat])
        dist=loopat+1
    #print(tmp2)
    
    #print(now,dist)
    #print(now+1,end=" ")
    for i in range(tmp2):
        now=a[now]-1
        dist=now+1
        #print(now,dist)
    #print()
    print(dist)
    
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np

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

tele = list(input().split())
tele = [int(tele[i]) for i in range(N)]
tele = np.array(tele)
seen = set([])
now = 1
w_num = 0


while now not in seen and w_num < K:
    seen.add(now)
    now = tele[now - 1]
    w_num += 1
    
if w_num == K:
    print(now)
else:
    seen = list(seen)
    roop_in_index = seen.index(now)
    roop = list(seen)[roop_in_index:]
    roop_len = len(roop)
    last = (K - (w_num - 1)) % roop_len
    print(roop[last - 1])

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
a=list(map(int, input().split()))
l=[]
t=1
for i in range(n):
    l.append(t)
    k-=1
    if a[t-1] in l:
        break
    else:
        t=a[t-1]
i=l.index(a[t-1])
del l[0:i]
k=k%len(l)
print(l[k])

Submission status: Wrong Answer

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

h = set([])

c = 0
for i in range(2 * n):
  c = a[c] - 1
  if n < i:
    print("{} {}".format(i, c))
    if c in h:
      break
    else:
      h.add(c)


c = 0
d = 1
for i in range(n):
  c = a[c] - 1
  print("{} {}".format(i, c))
  if c in h:
    break
  else:
    d += 1

c = 0
if d >= len(a):
  for i in range(a):
    c = a[c] - 1
else:
  for i in range(d + (k % len(h))):
    print("{} {}".format(c, a[c]))
    c = a[c] - 1

print(c + 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())

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

loop = deque([0])
teleported = [False] * N

loop_start = 0
loop_end = []

while True:
    i = loop[len(loop) - 1]
    if teleported[i]:
        loop_end = [i, A[i] - 1]        
        break
    teleported[i] = True
    loop.append(A[i] - 1)

cnt = [0] * (N)

for i in range(len(loop)):
    cnt[loop[i]] += 1
    if cnt[loop[i]] == 2:
        loop_end[0] = i
        cnt[0] = loop[i]

loop_start = loop.index(cnt[0])
loop_end = loop_end[0]
loop = list(loop)
loop = loop[loop_start:loop_end]

if K < loop_start:
    print(K)
else:
    ans = loop[(K - loop_start) % len(loop)] + 1
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
li = [1]
c = 1
for i in range(200001):
    cnt += 1
    c = a[c-1]
    if c in li:
        break
    li.append(c)
b = li[li.index(c):]
e = (K-cnt)%len(b)
print(b[e])

Submission status: Wrong Answer

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i+1
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis.index(Next))
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()

nokori=(K-startat) % loopcount

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
town = [0] * n
move = []
next1 = a[0] - 1
for i in range(n):
    town[next1] += 1
    move += [next1]
    if town[next1] > 1:
        break
    next1 = a[next1] - 1
first = move.index(move[-1])
roop = move[first:-1]
print(roop[(k - first)%len(roop)-1]+1)

Submission status: Wrong Answer

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

B = [1]
for i in range(N):
    s = B[i]
    if A[s-1] != 1:
        B.append(A[s-1])
    else:
        break
    
l = len(B)
print(B[(K%l)])

Submission status: Wrong Answer

Solution code:
# # for i in range(m):
# s = input()
# t = input()
# n = len(s)
# m = len(t)
# # print(s,t,n,m)
# if n+1==m:
# 	for i in range(n):
# 		# print(s[i], t[i])
# 		if s[i]!=t[i]:
# 			print("No")
# 			break
# 	else:
# 		print("Yes")
# else:
# 	print("No")
	# l, r = [int(j)-1 for j in input().split()]
	################
# def fun(a,b,c,k):
# 	ans =0 
# 	if k<=a:
# 		return k
# 	else:
# 		ans = a
# 		k-=a
# 	if k<=b:
# 		return ans
# 	else:
# 		k-=b
# 	# if k<=c:
# 	# print(ans, c)
# 	return ans-min(c,k)
# a,b,c,k = [int(j) for j in input().split()]
# # l = [a,b,c]
# # j = 0
# print(fun(a,b,c,k))
################################

# while(k>0):
# 	a = min(k, l[j])
# 	k-=a

#######################

n,k = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
s = dict({1:0})
l = [1]
temp = 1
ct = 0
rep = None
# k_copy = 
while(k>0):
	ct+=1
	k-=1
	
	temp = a[temp-1]
	# print(temp)
	if temp in s:
		
		rep = s[temp]
		l = l[rep:]
		break
	else:
		s[temp]=ct
		l.append(temp)
if k==0:
	print(l[-1])
else:
	n = len(l)
	# print(l, k, n)


	print(l[k%(n)])
# print(l[s[a[ct]]-1+(k-s[a[ct]]+1)%(-1*s[a[ct]]+ct+1)])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
P = [-1] * N 
cur = 0
P[cur] = 0
route = [1]

for step in range(K):
  cur = A[cur]
  route.append(cur + 1)
  if P[cur] >= 0:
    l_start = P[cur]
    l_end = step + 1
    l_len = l_end - l_start
    break
  else:
    P[cur] = step + 1
else:
  print(cur)
  quit()
  
print(route[l_start + ((K - l_start) % l_len)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input().split()
go=[]
fin=0
now=1
loop=0

while len(go)==len(set(go)):
  go.append(a[now-1])
  now=int(a[now-1])
  
fin=go.index(go[len(go)-1])
del(go[len(go)-1])
loop=len(go)-fin
k-=fin
k-=1

print(str(int(go[k%loop+fin])))

Submission status: Wrong Answer

Solution code:

n, k = map(int, raw_input().split(' '))
adj = [-1] + map(int, raw_input().split(' '))

seen = {}
h = {}
cur = 1
count = 0

while(k):
	
	if cur not in seen:
		seen[cur] = count
		h[count] = cur
		count += 1
	nex = adj[cur]
	if nex not in seen:
		k -= 1
		cur = nex
	elif nex in seen:
		if seen[nex] > seen[cur]:
			k -= 1
			c = seen[cur] + k
			cur = h[c] 
			k = 0
			#cur = nex

		elif seen[nex] < seen[cur]:
			k %= seen[cur] - seen[nex] + 1
			if k:
				k-=1
				cur = nex
			

print cur

Submission status: Wrong Answer

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

citys = [False] * n
now = 0
citys[0] = True
costt = 0
while True:
    now = a[now] - 1
    if citys[now]:
        costt += 1
        break
    else:
        citys[now] = True
        costt += 1

if costt >= k:
    i = 0
    while i != k:
        now = a[now] - 1
        i += 1
    print(now+1)
    quit()

citys = [False] * n
citys[now] = True
check = [0] * n
check[now] = 1
cost = 0
while True:
    now = a[now] - 1
    if citys[now]:
        cost += 1
        break
    else:
        citys[now] = True
        cost += 1
        check[now] = cost + 1

costt -= cost
d = (k - costt) % cost + 1
ans = check.index(d)
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[0]*n
town=0
Town=0
visited[0]=0
lst=[]
if a[0]==1:
  print(1)
  sys.exit()
for i in range(n):
  town=a[town]-1
  if visited[town]!=0 or town==0:
    lst.append([town,visited[town],i+1])
    break
  else:
    visited[town]=i+1
Lst=[]
town=lst[0][0]
Lst.append(town)
if lst[0][2]-lst[0][1]-1==0:
  if k<lst[0][1]:
    print(visited.index(k)+1)
    sys.exit()
  else:
    print(lst[0][0])
    sys.exit()
for i in range(lst[0][2]-lst[0][1]-1):
  town=a[town]-1
  Lst.append(town)
if k<=lst[0][1]:
  print(visited.index(k)+1)
else:
  k=k-lst[0][1]
  l=len(Lst)
  k=k%l
  print(Lst[k]+1)

Submission status: Wrong Answer

Solution code:
NUMBERX,KABALIs=map(int,input().split())
LISTX=list(map(int,input().split()))
indexi=1
STRS=[1]
COUNTS=0
while(1):
	STRS.append(LISTX[indexi-1])
	indexi=STRS[-1]
	COUNTS+=1
	if len(set(STRS))!=len(STRS):
		break
	if COUNTS-1==KABALIs:
		print(STRS[-1])
		exit()
for indexi in range(len(STRS)):
	if STRS[-1]==STRS[indexi]:
		KABALIs=KABALIs-indexi
		KABALIs=KABALIs%(len(STRS)-1-indexi)
		print(STRS[KABALIs+indexi])
		exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))

lst = [1]
pos = 1
cnt = 0
while True:
	if A[pos] in lst:
		cnt += 1
		lst.append(A[pos])
		break
	pos = A[pos]
	cnt += 1
	lst.append(pos)

idx = lst.index(lst[-1])
loop = cnt - idx # 循環
r = idx + 1 # 最初の端数

kk = (k - r) % loop

print(lst[r + kk])

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N, K = map(int, input().split())
    A = list(map(lambda x: int(x) - 1, input().split()))

    visited = [False] * N
    route = []
    
    cnt = 0
    v = 0
    while visited[v] == False and cnt < K:
        visited[v] = True
        route.append(v)
        v = A[v]
        cnt += 1

    if cnt >= K:
        print(route[len(route)-1] + 1)
    else:
        start = route.index(v)
        loop = cnt - start
        offset = (K - start) % loop
        res = route[start + offset] + 1
        print(res)
        # print(cnt)
        # print(offset)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

now = 1
first_loop = 0
l = [0 for _ in range(N)]
while max(l) < 2:
    l[now - 1] += 1
    now = A[now - 1]
    first_loop += 1

loop_start = 0
for i in range(N):
    if l[i] == 2:
        loop_start = i + 1
        break

now = loop_start
loop_node = []
l2 = [0 for _ in range(N)]
while True:
    l2[now - 1] += 1
    if max(l2) >= 2:
        break
    else:
        loop_node.append(now)
        now = A[now - 1]
loop_num = len(loop_node)

print(loop_node[((K + 1) - (first_loop - loop_num)) % loop_num])

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split(' ')))
n = l[0]
k = l[1]
l = list(map(int, input().split(' ')))
index = l[0]-1
e = l[index]-1
lindex = [0]
while True: 
    lindex += [index]
    if lindex.index(index)!=len(lindex)-1-lindex[::-1].index(index):
        break
    index = e
    e = l[index]-1
tloopsize = len(lindex)-lindex[::-1].index(index)-1-lindex.index(index)
k = k%tloopsize


print(l[lindex[k-1]])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))
ans = 1
n = 1
check = 1
city = [-1 for i in range(N+1)]
city[1] = 0
roop = 1
i = 0
while(check == 1):
  if i == K:
    check = 0
  else:
    n = A[n]
    i = i + 1 
    if city[n] == -1:
      city[n] = i
    else:
      roop = i - city[n] + 1
      check = 0
roop_num = (K - i)%roop
for i in range(roop_num+1):
  n = A[n]
print(n)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]

l = [0]*n
x = [1]
w = 0
for i in range(k):
  l[x[i]-1] += 1
  w = a[x[i]-1]
  x.append(w)
  if l[w-1] == 1:
    break

c = 0
for i in range(k):
  if x[-1] == x[i]:
    c = i
    break

ans = (k-c)%(len(x)-1-c)    

if k > len(x):
  print(x[i+ans])
else:
  print(x[k-1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

n, k = map(int, input().split())
*a, = map(int, input().split())
a = [a[i] - 1 for i in range(n)]
ans = 0

if k <= n:
    for i in range(k):
        ans = a[i]

else:
    # 周期の場所をチェック
    start_period = 1
    period_i = -1
    d = defaultdict(int)
    d[0] = 1
    i = 0
    count = 1
    while(1):
        if a[i] in d.keys():
            period_i = count
            start_period = d[a[i]]
            break

        d[a[i]] = count + 1
        i = a[i]
        count += 1
        
    # 逆引き
    d_r = {i:k for k, i in d.items()}
    ans = d_r[start_period + ((k - (start_period - 1)) % (period_i - start_period + 1))] + 1
    
    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
while True:
    if i in check:
        break
    else:
        check.append(i)
        i = A[i]
        c += 1
    
X = check[check.index(i):]
print(1+X[(K-c)%len(X)])

Submission status: Wrong Answer

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

visited = [0]*N
i = 0
rootin = []
rootin.append(1)
while  visited.count(2) == 0:
  visited[i] += 1
  if visited[i] == 2:
    break
  rootin.append(A[i])
  i = A[i]-1

start,end = 0,-1
for i in range(len(rootin)):
  if rootin[i] == rootin[-1]:
    start = i
    break
od = len(rootin[:start])
root = len(rootin[start:end])

K -= od
ans = rootin[start+K%root]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

s = 0  # カウンター
p = 0  # 初期位置

for i in range(n):
    s += 1
    p = maps[p]
    if (p == 0):
        break
d = k % s
ans = 0
for i in range(d):
    ans = maps[ans]

print(ans + 1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
state=[1]
for _ in range(n+1):
    state.append(A[state[-1]-1])
count={}
for i in range(n+1):
    if state[i] in count:
        if count[state[i]]==0:
            start=0
            end=i
            break
        elif i>0 and state[i-1]==state[count[state[i-1]]]:
            start=count[state[i]]
            end=i
            break
    else:
        count[state[i]]=i
k -= start
k%=(end-start)
print(state[k+start])

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools



sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

N, K = map(int,input().split())
A = [int(i) for i in input().split()]
Temp = [0]*N
imp = [0]*3
ans = [0]

def rec(next, kore):
    #print(kore)
    #print(next)
    if(kore==K):
        ans[0] = next
        return
    if(Temp[next-1] == 0):
        Temp[next-1] = kore
        rec(A[next-1], kore+1)
    else:
        imp[0] = kore-Temp[next-1]
        imp[1] = kore
        imp[2] = next
    return

rec(Temp[0],1)
#print(imp)
#print("fin")
for i in range(N):
    Temp[i] = 0

if(ans[0] != 0):
    print(ans[0])
else:
    K -= imp[1]
    K = K%imp[0]
    #print(K)
    if(K==0):
        print(imp[2])
    else:
        rec(imp[2], 1)
        print(ans[0])

Submission status: Wrong Answer

Solution code:
n,k=list(map(int, input().split()))
a=list(map(int, input().split()))
b=[0]*(n+1)
for i in range (n):
    b[i+1]=a[i]
ctr=[0]*(n+1)
x=1
c=0
s=0
while c==0:
    y=b[x]
    s+=1
    if ctr[y]==0 and y!=1:
        ctr[y]+=s
        x=y
    else:
        ll=s-ctr[y]
        c+=1
        f=ctr[y]

if k<f:
    ans=ctr.index(k)
else:
    zz=(k-f)%ll
    ans=ctr.index(zz+f)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
stack = deque([])
passed = set()
town = 0
while town not in passed:
    passed.add(town)
    stack.append(town)
    town = A[town] - 1

if len(stack) > K:
    print(stack[K])
    import sys
    sys.exit()

tail = 0
while stack[0] != town:
    _ = stack.popleft()
    tail += 1

K -= tail
K += 1
tmp = K % len(stack)
if tmp == 0:
    tmp = len(stack)
print(stack[tmp - 1] + 1)

Submission status: Wrong Answer

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

count = 0
i = 1
position = 1
position_list = [1]
position_history = [0] * N
start_loop = 0
while i > 0:
  position = A[position-1]
  position_history[position-1] += 1
  count += 1
  if position_history[position-1] > 1:
    start_loop = position_list.index(position)
    position_list.append(position)
    break
  else:
    position_list.append(position)
  	
k = (K - start_loop) % (count - start_loop)
print(position_list[start_loop+k])

Submission status: Wrong Answer

Solution code:
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))
LS=lambda:list(map(str,input().split()))

def main():
    import sys
    input = sys.stdin.readline
    n,k=L()
    a=L()
    visited=[0]*(n+1)
    tmp=0
    now=0
    visited[0]=1
    loopat=0
    for i in range(k):
        #print(visited,end="")
        tmp=i+2
        now=a[now]-1
        #print(now)
        #print(tmp,now+1)
        if visited[now]==0:
            visited[now]=i+2
        else:
            loopat=now
            break
    #print(visited)
    #print(loopat,visited[loopat],tmp)
    #print(tmp,now)
    if tmp-visited[loopat]==0:
        print(now+1)
        exit()
        tmp2=k
        dist=1
        now=0
    else:
        tmp2=(k-(visited[loopat]-1))%(tmp-visited[loopat])
        dist=loopat+1
    #print(tmp2)
    
    #print(now,dist)
    #print(now+1,end=" ")
    for i in range(tmp2):
        now=a[now]-1
        dist=now+1
        #print(now,dist)
    #print()
    print(dist)
    
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
P = [-1] * N 
cur = 0
P[cur] = 0
route = [1]

for step in range(K):
  cur = A[cur]
  route.append(cur + 1)
  if P[cur] >= 0:
    l_start = P[cur]
    l_end = step + 1
    l_len = l_end - l_start
    break
  else:
    P[cur] = step + 1
else:
  print(cur)
  quit()
  

print(route,l_start,l_end)
print(route[l_start + ((K - l_start) % l_len)])

Submission status: Wrong Answer

Solution code:
import sys
from math import sqrt
from collections import Counter, defaultdict, deque

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


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    al = LI()

    now = 1
    if n >= k:
        for _ in range(k):
            now = al[now]
    else:
        al.insert(0, 0)
        passed = [-1] * (n + 1)
        i = 0
        while passed[now] == -1:
            passed[now] = i
            now = al[now]
            i += 1

        loop = i - passed[now]

        for _ in range((k - passed[now]) % loop):
            now = al[now]

    print(now)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
ed=[False]*n
go=0
c=0
while not ed[go]:
    ed[go]=True
    go=a[go]
    c+=1
go=0
for i in range(k%c):
    go=a[go]
print(go+1)

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


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


def INT(): return int(input())


def MAP(): return map(int, input().split())


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()
    A = LIST()
    po = 1
    lis = [1]
    ct = defaultdict(int)
    ct[1] += 1
    for i in range(N-1):
        po = A[po-1]
        ct[po] += 1
        if ct[po] > 1:
            break
        lis.append(po)
    else:
        ind = lis.index(po)
        lis = lis[po:]
        print(lis)
        print(ind)

        K -= ind
        K %= len(lis)

        print(lis[K])

    K %= len(lis)
    print(lis[K])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

loc = 1
loclog=[1]
flag = 0

for i in range(1,K+1):
    loc = A[loc-1]
    if loc in loclog:
        roopstart = loclog.index(loc)
        roopend = len(loclog)
        flag = 1
        break
    loclog.append(loc)

if flag == 0:
    ans = loc
else:
    roopidx = (K+1-roopstart)%(roopend-roopstart)
    ans = loclog[roopstart+roopidx-1]
print(ans)

Submission status: Wrong Answer

Solution code:
# N個の町, K個のテレポート
N, K = map(int, input().split())

# 町
array = list(map(int, input().split()))

while True:
  K = K/N
  if K < N:
    print(array[array[int(K)]])
    break

Submission status: Wrong Answer

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

T = 1
cnt = 0
for k in range(K):
    T = A[T-1]
    cnt += 1
    if T == 1:
        break
print(K%cnt)
for k in range(K%cnt):
    T = A[T-1]
print(T)

Submission status: Wrong Answer

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


N, K = map(int, readline().split())
A = list(map(int, readline().split()))
passed = [0] * N
P = []
c = 0
for _ in range(N):
    c = A[c] - 1
    if passed[c] == 1:
        s = c
        break
    P.append(c)
    passed[c] = 1
    
if K <= len(P):
    print(P[K-1])
else:
    idx = P.index(s)
    m = (K - idx) % (len(P) - idx)

    print(P[idx:][m-1] + 1)

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count < n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count < n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[k % n] + 1)