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: Runtime Error

Solution code:
# D
N, K = map(int, input().split())
A = list(map(int, input().split()))
dct = {i+1:a for i,a in enumerate(A)}
visited = [0] * N
visited[0] = 1
if (K <= 2 * 10**5):
    nex = 1
    for _ in range(K):
        nex = dct[nex]
    print(nex)
else:
    cnt = 0
    nex = 1
    for _ in range(N):
        cnt += 1
        nex = dct[nex]
        visited[nex-1] += 1
        if visited[nex-1] > 1:
            break
    cnt1 = 0
    nex1 = 1
    for _ in range(N):
        cnt1 += 1
        nex1 = dct[nex1]
        if nex1 == nex:
            break
    l = (K - cnt1) % (cnt - cnt1)
    for i in range(l):
        nex = dct[nex]
print(nex)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
town = [[] for _ in range(N)]
visited = [0]

for i in range(N):
  town[i] = [i,A[i] - 1]

def teleport(i,con):
  goal = town[i][1]
  if goal in visited or con == N:
    return goal + 1
  else:
    visited.append(goal)
    return teleport(goal,con + 1)

l = teleport(0,0)
visited = [i + 1for i in visited]
cs = visited.index(l)
cycle = visited[cs:]

if K <= len(visited):
  print(visited[K])
else:
  K = K - len(visited) + len(cycle)
  print(cycle[K % len(cycle)])

Submission status: Runtime Error

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

node = [0] * (N + 1)
index = 1
loop_flag = False

for i in range(1, K + 1):
    tmp = A[index - 1]
    if not node[tmp]:
        index = tmp
        node[index] += i
    else:
        pre = node[tmp]
        loop = index - node[tmp]
        if loop > 1:
            answer = node.index((K - pre) % loop + pre)
        else:
            answer = tmp
        loop_flag = True
        print(answer)
        break

if loop_flag == False:
    print(index)

Submission status: Runtime Error

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
def last():
  k = (K - start_loop) % (count - start_loop)
  print(position_list[start_loop+k])

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

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*1000
tmp = 0
t = 1
index = 0

for i in range(1000):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2.append(tmp)
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        exit

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-1])

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current+1)
else:
    isReturn = [0]*(N+1)
    step = 1
    current = 1
    isReturn[current] = step
    for i in range(K):
        current = A[current-1]
        step += 1
        if isReturn[current] == 0:
            isReturn[current] = step
        else:
            r = step - isReturn[current]
            b = isReturn[current] - 1
            break
    
    t = (K-step) % N
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*1000
tmp = 0
t = 1
index = 0

for i i range(1000):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2.append(tmp)
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        exit

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-1])

Submission status: Runtime Error

Solution code:
# A_1からスタートして、同じA_iに戻ってくるまでの[移動回数][到着場所]をリスト化

import sys
readline = sys.stdin.readline

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

route = [0]
visited = set()
while True:
  nex = A[route[-1]] - 1
  if nex in visited:
    loopstart = route.index(nex)
    break
  else:
    visited.add(nex)
    route += [nex]

before_loop,one_loop = route[:loopstart],route[loopstart:]

if K <= len(before_loop):
  print(before_loop(K) + 1)
else:
  K -= len(before_loop)
  K %= len(one_loop)
  print(one_loop[K] + 1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
an = list(map(int, input().split()))
copyan = an[:]
i = copyan[0]
copyan[0]=10**6
movecount = 1
roopstart = 0
if i != 1:
    while True:
        memo = i
        i = copyan[i-1]
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        copyan[memo-1]=10**6 + movecount
        movecount += 1
        roopstart = i-1
else:
    print(1)
roopnum = movecount - yokei
x = (k-yokei)%roopnum #移動する回数

ans = copyan.index(10**6+yokei+x) +1
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
loop = []
loop.append(1)
n = A[0]
i = 0
while i <= K:
    i += 1
    if n in loop:
        idx = loop.index(n)
        flag = 1
        f = idx
        loop = loop[idx:].copy()
        break
    loop.append(n)
    n = A[n-1]
K -= idx
r = K % len(loop)
print(loop[r])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
f = 1
c = 0
for e,i in enumerate(A,1):
    if dp[f-1] or i == 1:
        ans = e-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(A[f-1])
        f = A[f-1]
        c+=1
k-= bal
# print(bal,ans)
B = B[bal:ans+1]
# print(dp)
k = (k%ans+1)%ans
print(B[k])

Submission status: Runtime Error

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

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0
t=0
rep = False

for i in range(n):
    li.append(A[next])
    li2[next] = 1
    next = A[next]-1

    if li2[next] == 1:
        t = li.index(A[next])
        rep = True
        break

if rep == True:
    tmp = (k%len(li[t:])) - len(li[0:t])
    g=li[t:]
    print(g[tmp])
else:
    print(li[k-1])

Submission status: Runtime Error

Solution code:
import sys
inp = input().split(" ")
n = int(inp[0])
k = int(inp[1])

pos=0
go=input().split(" ")
for i in range(len(go)):
  go[i]=int(go[i])-1
dict={'k0':0}
for i in range(k):
  pos=go[pos]
  if pos in dict.values():
    list=list(dict.values())
    roopind=list.index(pos)
    rps=list[roopind:]
    rest=((k-1-i)%len(rps))
    print(rps[rest]+1)
    sys.exit()
  dict['k{}'.format(i+1)]=pos
print(list[-1]+1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

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

tele = [1,A[0]]
B = [0]
z = 0
s = 0
for i in range(N):
    if z in B and len(B) != 1:
        s = B.index(z)
        if s != 0: s+=1
        break
    tele.append(A[tele[-1]-1])
    B.append(tele[-2]-1)
    z = tele[-1]-1

if K <= s:
    print(tele[K])
else:
    K -= s
    amari = K%N
    if s == 0:
        tele = tele[1:len(tele)-1] + tele[:len(tele)-1]
    else:
        tele = tele[s:]*2
    print(tele[amari])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
while True:  
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]
first = l.index(last)
loop = len(l)-first

if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
while True:  
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]
first = l.index(last)
loop = len(l)-first

if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])
print(l.index(last))

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
s = {1}
while True:
  now = town[via[-1]-1-
  if not now in s:
    via.append(now)
    s.add(now)
  else:
    start = via.index(now)
    roop = len(via) - start
    break


if len(via) == 1:
    print(1)
    exit()
 
elif len(via) > k:
    print(via[k])
    exit()
 
else:
    roop_remainder = (k + 1 - start) % roop
    if roop_remainder == 0:
        print(via[-1])
    else:
        print(via[start + roop_remainder -1])

Submission status: Runtime Error

Solution code:
n, k = [int(x) for x in input().strip().split()]
a = [int(x)-1 for x in input().strip().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
way = [0]
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
else:
  print(a[current]+1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 10 21:41:44 2020

@author: Kei
"""

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

A = [0]*N
town = [0]*N

A = input().split()

A = [int(s) for s in A]

temp = 1

for i in range(0,K+1):
    town[temp-1] += 1 
    if town[temp-1] == 2:
        roop = i
        break
    temp = A[temp-1]

roop_len = roop

temp2 = 1
i=0
while temp != A[temp2-1]:
#    print(str(temp)+"=="+str(A[temp2-1]))
    temp2 = A[temp2-1]
    roop_len -=1
    i += 1

ini = roop-roop_len

ans = (K+1-ini)%roop_len

goal = 0
for i in range(0,ini+roop_len+ans):
    goal = A[goal-1]

print(goal)

Submission status: Runtime Error

Solution code:
NK = list(map(int, input().split())) 
A = list(map(int, input().split())) 
e = 0
N = NK[0]
K = NK[1]
town= 1
list = []
for i in range(N+1):
  town = A[town-1]
  list.append(town)
  if i > 0 and town == list[0]:
    e += 1
    break
if e == 1:
  print(list[-1])
else:
  d = K % len(list[:-1])
  print(list1[d-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
res = [A[0]-1]
for i in range(K):
  a = A[res[-1]-1]
  res.append(A[a])
print(res[-1])

Submission status: Runtime Error

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    prev_p = 1
    g = []
    while True:
        if g[:len(g)//2]==g[len(g)//2:] and len(g) > 0:
            break
        g.append(A[prev_p])
        prev_p = g[-1]  

    K = K % len(g)
    print(g[K-1])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

# print(A)

counts = []

for i in range(n):
  counts.append(0)

count = 0
steps = [0]

for i in range(n):
  if i == 0:
    n_p = A[0] - 1
    steps.append(n_p)
    counts[0] += 1
    counts[n_p] += 1
    count += 1
    # print(counts)
  else:
    n_p = A[n_p] - 1
    steps.append(n_p)
    counts[n_p] += 1
    # print(counts)
    count += 1
    if counts[n_p] == 2:
      loop_p = n_p
      loop_c = count
      break

count_2 = 0      
      
for i in range(count):
  if i == 0:
    n_p = A[0] - 1
    count_2 += 1
    # print(n_p)
    if n_p == loop_p:
      loop_s = count_2
      break
  else:
    n_p = A[n_p] - 1
    count_2 += 1
    # print(n_p)
    if n_p == loop_p:
      loop_s = count_2
      break

if k <= loop_c:
  for i in range(k):
    if i == 0:
      n_p = A[0] - 1
    else:
      n_p = A[n_p] - 1
  print(n_p)
  
else:
  step = ((k - loop_s) % (loop_c - loop_s + 1))
  #  print(step)
  print(steps[loop_p + step] + 1)
  
# print(steps) 
# print("loop_c:", loop_c)
# print("loop_p:", loop_p)
# print("loop_s:", loop_s)

Submission status: Runtime Error

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

G = {}
for i in range(1, n + 1):
    G.update({i: a[i - 1]})

seen = [False] * (n + 1)
path = []

def dfs(graph, start):
    global return_ans
    seen[start] = True
    path.append(start)
    
    if seen[graph[start]]:
        return graph[start]
    else:
        dfs(graph, graph[start])


point = dfs(G, 1)
m = path.index(point)

if m > k:
    print(path[k])
else:
    k -= m
    path = path[m:]
    k %= len(path)
    print(path[k])

Submission status: Runtime Error

Solution code:
import sys
inp = input().split(" ")
n = int(inp[0])
k = int(inp[1])

pos=0
go=input().split(" ")
for i in range(len(go)):
  go[i]=int(go[i])-1
dict={'k0':0}
for i in range(k):
  pos=go[pos]
  if pos in dict.values():
    list=list(dict.values())
    print(list)
    roopind=list.index(pos)
    rps=list[roopind:]
    rest=((k-1-i)%len(rps))
    print(rps[rest]+1)
    sys.exit()
  dict['k{}'.format(i+1)]=pos
print(list[-1]+1)

Submission status: Runtime Error

Solution code:
import sys

n,k = (int(i) for i in input().split())

a = [int(i)-1 for i in input().split()]

hantei = [True]*(n+1)
otozure = [None]*(n+1)
mokuteki = a[0]
moto = k
kazoe = 0
otozure[0] = 0
while k>0:
        
    if hantei[mokuteki] == False: #二週目突入
       # print("break\nmokuteki= {}\nk = {}".format(mokuteki,k))
        otozure[kazoe+1] = mokuteki
        saigo = otozure
        break

    else:
        hantei[mokuteki]= False
        otozure[kazoe+1] = mokuteki#今いるところを記憶
       # print(otozure[kazoe])
        mokuteki = a[mokuteki]#次に行くところ
        kazoe+=1
    k-=1
"""else:
    print(otozure[kazoe-1]+1)
    sys.exit()"""
i = 0
while True:
    if otozure[kazoe+1] == otozure[kazoe-i]:
        break
    i+=1


nokori = k%(i+1)
#print(nokori)
i=0
while otozure[i] != mokuteki:
    i+=1

if a[0]==0:
    print("1")
else:
    print(otozure[i-1+nokori]+1)

Submission status: Runtime Error

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

rute = list()
visited = dict()
city = A[0]
i = 1
while city not in rute:
    rute.append(city)
    visited[city] = i  #
    city = A[city]
    i += 1

# loopstart = rute.index(city)
loopstart = visited[city]
rute_nonloop = rute[0:loopstart]
rute_loop = rute[loopstart:]

if K < len(rute_nonloop):
    print(rute[K-1]+1)
else:
    K -= len(rute_nonloop)
    K %= len(rute_loop)
    print(rute_loop[K-1]+1)

Submission status: Runtime Error

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

a = [0] * (K+1)
a[0] = 1

for i in range(K):
    a[i+1] = A_list[a[i]-1]
    
print(a[K])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

B = B*1000000000000000000000
print(B[k-1])

Submission status: Runtime Error

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

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

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

Submission status: Runtime Error

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
#print(path)
c = Counter(path)
roop1 = 0
for key, v in c.items():
    if v == 2:
        roop1 = key
if roop1 == 0:
    roop1 = 1
roopidx = path.index(roop1)
roopidx2 = path.index(roop1, roopidx+1)
#print(roopidx,roopidx2)
k_mod = (k-roopidx) % (roopidx2-roopidx)
print(path[roopidx+k_mod])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
move=[1]
now=1
while li[now-1] not in move:
    move.append(li[now-1])
    now=li[now-1]  
move.append(li[now-1])    
now=li[now-1]
start=move.index(now)
roope=move[start:-1]
move.pop(-1)
Q=len(move)-len(roope)
check=k-Q
if len(move)>=k:
    print(move[k])
else:
    print(roope[check%len(roope)])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(A) == len(B):
    ans = 0
B = B*1000000000
print(B[k-1])

Submission status: Runtime Error

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

a = [0] * (K+1)
a[0] = 1

for i in range(K):
    print('i=' ,i)
    a[i+1] = A_list[a[i]-1]
    print(a[i+1])
    
print(a[K])

Submission status: Runtime Error

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

seen = [-1 for _ in range(N)]
seen[0] = 0
next = 0

# たかだか N 回回ればループに入る
for i in range(min(K, N)+1):
    if seen[next] == -1:
        last = i
        break
    else:
        next = A[next]-1
        seen[i+1] = next

# loop なしの場合
if A[next]-1 not in seen[:min(K, N)]:
    print(A[next])
    exit()

# それ以外
next_ind = seen[:last+1].index(next)
loop = seen[next_ind:last+1]
#print(loop)

mod = (K - next_ind) % len(loop)
ans = loop[mod]+1

print(ans)

Submission status: Runtime Error

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

a = [0] * (K+1)
a[0] = 1
print(a)

for i in range(K):
    print('i=' ,i)
    a[i+1] = A_list[a[i]-1]
    print(a[i+1])
    
print(a[K])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[0]*(n+1)
l[1]=1
q=[]
cnt1=0
cnt2=0
def g(r,n):
  if n==k:
    print(r)
    exit()
  q.append(r)
  global cnt2
  cnt2+=1
  l[a[r-1]]+=1
  if l[a[r-1]]==3:
    return
  g(a[r-1],n+1)

def f(r,n):
  if n==k:
    print(r)
    exit()
  global cnt1
  cnt1+=1
  l[a[r-1]]+=1
  if l[a[r-1]]==2:
    g(a[r-1],n+1)
    return
  f(a[r-1],n+1)

f(1,0)
intr=cnt1-cnt2
p=(k-intr)%cnt2

print(q[p])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(A) == len(B):
    ans = 0

print(B[k])

Submission status: Runtime Error

Solution code:
n,k = list(map(int,input().split()))
A = list(map(int,input().split()))
tour=[0]
visited=[0]*n
visited[0]=1
next_town = 0
flag=False
for i in range(n):
    next_town=A[next_town]-1
    if i==k:
        flag = True
        ans = next_town
    if next_town==0:
        first_roop_idx = 0
        break
    if visited[next_town]>0:
        first_roop_idx = visited[next_town]
        break
    tour.append(next_town)
    visited[next_town]=i+1
#print(tour)
#print(visited)
#print(first_roop_idx)
if not flag:
    if k<=len(tour):
        ans = tour[k]
    else:
        k-=first_roop_idx
        #print("k=",k)
        k=k%(len(tour)-first_roop_idx)
        #print("k=",k)
        ans = tour[first_roop_idx+k]
print(ans+1)

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
A = list(map(int,input().split()))
memo = [0 for _ in range(n+1)]
dict = {i:0 for i in range(1,n+1)}
memo[0] = 1
dict[1] = 1
i = 1
j = 1
loopnum = 0
ans = 0
while(j <= k+1):
    if dict[A[i-1]] == 1:
        loopnum = A[i-1]
        break
    a = A[i-1]
    memo[j] = a
    dict[a] = 1
    ans = A[j-1]
    i = a
    j += 1
if loopnum != 0:
    left = 0
    if loopnum != 1:
        left = bisect.bisect_left(memo,loopnum,hi=j+1)+1
    f = j - left
    diff = max(left,0)
    index = (k - diff) % f + diff
    ans = memo[index]

print(ans)

Submission status: Runtime Error

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

visited = []
t = None
k = 0

def rec(A, i):
  global t, k
  if i in visited:
    t = i
    return
  elif k>K:
    return
  visited.append(i)
  k += 1
  rec(A, A[i-1])

rec(A, 1)

if t==None:
  ans = visited[-1]
  print(ans)
else:
  visited = np.array(visited)
  idx = np.where(visited==t)
  f1 = idx[0][0]
  f2 = len(visited) - f1

  roop = (K - f1)//f2

  rest = K - (f1 + f2*roop)
  ans = visited[f1 + rest]

  print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1
kaisuu=1
C=[-1]*(N+1)
C[0]=0
roo=0
rooa=0
while kaisuu>=0:
 B[kaisuu]=A[B[kaisuu-1]-1]
 if C[B[kaisuu]-1]>=0:
  roo=kaisuu-C[B[kaisuu]-1]
  rooa=C[B[kaisuu]-1]
  break
 else:
  C[B[kaisuu]-1]=kaisuu
  kaisuu+=1
if K<=rooa:
 print(lst[K])
else:
 d= (K-rooa)%roo
 print(B[rooa+d])

Submission status: Runtime Error

Solution code:
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    now = 0
    hoge= []
    bit = 0
    piyo = 0
    huga = []
    for i in range(k):
        hoge.append(now)
        now = a[now]-1
        if now in hoge:
            huga = hoge[hoge.index(now):len(hoge)]
            piyo = hoge.index(now)
            bit = 1
            break
    ans = huga[(k-piyo)%len(huga)]
    print(ans+1)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

node = [0] * (N + 1)
index = 1
loop_flag = False

for i in range(1, K + 1):
    tmp = A[index - 1]
    if not node[tmp]:
        index = tmp
        node[index] += i
    else:
        pre = node[tmp]
        loop = index - node[tmp]
        if loop > 1:
            answer = node.index((K - pre) % loop + pre)
        else:
            answer = tmp
        loop_flag = True
        print(answer)
        break

if loop_flag:
    print(index)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(B) == len(A):

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[0]*(n+1)
l[1]=1
q=[]
cnt1=0
cnt2=0
def g(r,n):
  print(r)
  if n==k:
    print(r)
    exit()
  q.append(r)
  global cnt2
  cnt2+=1
  l[a[r-1]]+=1
  if l[a[r-1]]==3:
    return
  g(a[r-1],n+1)

def f(r,n):
  print(r)
  if n==k:
    print(r)
    exit()
  global cnt1
  cnt1+=1
  l[a[r-1]]+=1
  if l[a[r-1]]==2:
    g(a[r-1],n+1)
    return
  f(a[r-1],n+1)

f(1,0)
intr=cnt1-cnt2
p=(k-intr)%cnt2
print(q)
print(p)
print(q[p])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
an = list(map(int, input().split()))
copyan = an[:]
i = copyan[0]
copyan[0]=10**6
movecount = 1
roopstart = 0
if i != 1:
    while True:
        memo = i
        i = copyan[i-1]
        copyan[memo-1]=10**6 + movecount
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        movecount += 1
        roopstart = i-1
else:
    print()
roopnum = movecount - yokei
x = (k-yokei)%roopnum #移動する回数

count = 0
iti = roopstart

while True:
    
    if x == 0:
        print(roopstart+1)
        exit()
    else:
        ans = iti
        iti = an[iti]-1
        
        count += 1
        if count == x:
            print(iti+1)
            exit()

Submission status: Runtime Error

Solution code:
# D
N, K = map(int, input().split())
A = list(map(int, input().split()))
dct = {i+1:a for i,a in enumerate(A)}
visited = [0] * N
visited[0] = 1
cnt = 0
if (K <= 2 * 10**5):
    nex = 1
    for _ in range(K):
        nex = dct[nex]
else:
    nex = 1
    for _ in range(N):
        cnt += 1
        nex = dct[nex]
        visited[nex-1] += 1
        if visited[nex-1] > 1:
            break
    cnt1 = 0
    nex1 = 1
    for _ in A:
        cnt1 += 1
        nex1 = dct[nex1]
        if nex1 == nex:
            break
    l = (K - cnt1) % (cnt - cnt1)
    for i in range(l):
        nex = dct[nex]
print(nex)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
now=a[0]
his=[now]
for i in range(20):
  now=a[now-1]
#  print(now)
  if i==k-1:
    quit()
  if now in his:
    first=his.index(now)
    last=i
    break
  his.append(now)
print(his[first+(k-first)%(last-first+1)-1])

Submission status: Runtime Error

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

node = [0] * (N + 1)
index = 1
loop_flag = False

for i in range(1, N + 1):
    tmp = A[index - 1]
    if not node[tmp]:
        index = tmp
        node[index] += i
    else:
        pre = node[tmp]
        loop = index - node[tmp]
        if loop > 1:
            answer = node.index((K - pre) % loop + pre)
        else:
            answer = tmp
        loop_flag = True
        print(answer)
        break

if loop_flag:
    print(index)

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
towns = [int(i) for i in input().split()]
ind_ = 1
ts = []
roop_s = None
for i in range(k):
    ind_ = towns[ind_-1]
    if ind_ in ts:
        roop_s = ts.index(ind_)
        break
    else:
        ts.append(ind_)
init = len(ts[:roop_s])
roop_l = len(ts) - init
roop = ts[roop_s:]
print(roop[((k-init)%roop_l)-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
for i in range(10**18) 
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]

loop = len(l[l.index(last):-1])
first = l.index(last)
if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

Submission status: Runtime Error

Solution code:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

visited = [0]
x = 0
T = -1
while T < 0:
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  else:
    visited.append(x)
  break
    
mod = (k-first) % T
print(a[first+mod]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
    
print(B[k-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
aa=[]
res=0
ans=0
q=0
for i in range(1,k+1):
    aa.append(res)
    res=a[res]-1
    if res in aa:
        ia=aa.index(res)
        m=res+1
        q=i
        break
k=k-i
q=q-ia
if q !=0:
    k=k%q
for i in range(1,k+1):
    res=a[res]-1
print(res+1)

Submission status: Runtime Error

Solution code:
n,k=list(map(int,input().split()))
A=[i for i in range(1,n+1)]
B=list(map(int,input().split())) 
road=dict(zip(A,B))

#残りi回移動する、移動前に自分がいるところをj
def walk(i,j):
    if i==1:
        return road[j]
    else:
        return walk(i-1,road[j])

print(walk(k,1))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
ans=0
for i in range(k):
  ans=a[ans]-1
  if ans in b:
    d=len(b)-b.index(ans)
    e=b.index(ans)
    break
  else: b.append(ans)

f=(k-e)%d

print(b[e+f])

Submission status: Runtime Error

Solution code:
L = []

def Search(pos):
  if pos - 1 not in L:
    L.append(pos - 1)
    Search(A[pos-1])
  else:
    L.append(pos - 1)
    return pos
  
N, K = map(int, input().split())
A = list(map(int, input().split()))
Search(1)
#print(L)
if K < L.index(L[-1]):
  print(L[K])
else:
  t = L.index(L[-1])
  k = len(L) - t - 1
  K -= t
  #print(t, k, K % k)
  print(L[t + (K % k)] + 1)

Submission status: Runtime Error

Solution code:
N,K =map(int, input().split())
A=list(map(int, input().split()))
B = A.copy()
now = 0
nex =1
f=[]
for i in range(N):
    if B[nex-1]>=0:
        now=nex
        f.append(now)
        nex = B[now-1]
        B[now-1] = -1
    else:
        s = f.index(A[i])
        l = len(f)  -  s
        break
print(f[(K-s)%l+s])

Submission status: Runtime Error

Solution code:

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

tele = tuple(map(int, input().split()))

former = 1
has_come = [0] * n
flag = [1] + [0] * n


def fun():
    global former
    for i in range(k):
        next_town = tele[former - 1]
        if flag[next_town - 1] == 1:
            return i
        flag[next_town - 1] = 1
        has_come[i] = next_town
        former = next_town
    else:
        print(former)
        return None

rtn = fun()
if rtn is not None:
    loop_start = tele[former - 1]
    loop_start_idx = has_come.index(loop_start)

    last_tele = k - rtn
    loop = has_come[loop_start_idx:rtn]
    print(loop[last_tele % len(loop) - 1])

Submission status: Runtime Error

Solution code:
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    now = 0
    print(now)
    hoge= []
    bit = 0
    piyo = 0
    huga = []
    for i in range(k):
        hoge.append(now)
        now = a[now]-1
        if now in hoge:
            huga = hoge[hoge.index(now):len(hoge)]
            piyo = hoge.index(now)
            bit = 1
            break
    ans = huga[(k-piyo)%len(huga)]
    print(ans+1)
    return 0

if __name__ == "__main__":
    solve()