TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=1
sa=set()
d=dict()
count=0
niko=0
anss=0
bx=False
while True:
  count+=1
  if count+1==k:
    anss=a[s]
    bx=True
    print(a[s])
    count-1
    break
  if not (a[s-1] in sa):
    sa.add(a[s-1])
    d[a[s-1]]=count
    s=a[s-1]
  else:
    niko=a[s-1]
    number=count-d[a[s-1]]
    break
if bx==False:
  lf=False
  ans_list=[]
  kaisu=0
  for i,j in d.items():
    if i==niko:
      lf=True
      kaisu=j
    if lf==True:
      ans_list.append(i)
  print(ans_list[(k-kaisu)%number])
    
    
    
  

n,k=map(int,input().split())
a=list(map(int,input().split()))
s=1
sa=set()
d=dict()
count=0
niko=0
anss=0
bx=False
while True:
  count+=1
  if count==k:
    anss=a[s-1]
    bx=True
    print(a[s-1])
    count-1
    break
  if not (a[s-1] in sa):
    sa.add(a[s-1])
    d[a[s-1]]=count
    s=a[s-1]
  else:
    niko=a[s-1]
    number=count-d[a[s-1]]
    break
if bx==False:
  lf=False
  ans_list=[]
  kaisu=0
  for i,j in d.items():
    if i==niko:
      lf=True
      kaisu=j
    if lf==True:
      ans_list.append(i)
  print(ans_list[(k-kaisu)%number])
    
    
    
  

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
iran=[]
l=list(map(int,input().split()))
#n,k=6,727202214173249351
#l=[6,5,2,5,3,2]
for i in range(N):
  if l.count(i+1)==0:
    iran.append(i+1)
kiseki=[]

u=1
kiseki.append(1)
for i in l:
  u=l[u-1]
  if kiseki.count(u)==0:
    kiseki.append(u)
  else:
    kiseki.append(u)
    break
kuri=kiseki[kiseki.index(u):len(kiseki)-1]
if k<=len(kiseki)-(len(kuri)+1):
  print(kiseki[k])
else:
  K=k-(len(kiseki)-(len(kuri)+1))
  amari=K%len(kuri)
  print(kuri[amari])

n,k=map(int,input().split())
l=list(map(int,input().split()))
kiseki=[-1]*n
i=0
v=1
while kiseki[v-1]==-1:
  kiseki[v-1]=i
  v=l[v-1]
  i+=1
saisyo=kiseki[v-1]
kuri=i-saisyo

if saisyo>k:
  print(kiseki.index(k)+1)
else:
  amari=(k-saisyo)%kuri
  print(kiseki.index(amari+saisyo)+1)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
a = list(map(int,input().split()))
rep = []
rep.append(1)
i = 0
cnt = 0
if n>k:
    end =k
else:
    end =n
nof = 0
while True:
    cnt+=1
    if cnt>end:
        nof=1
        break
    go = a[i]-1
    if a[i] not in rep:
        rep.append(a[i])
        i = go
    else:
        rep.append(a[i])
        break
if nof==1:
    print(rep[k])
else:
    print('oo')
    # lr = len(rep)
    
    #     if rep[i]==rep[lr-1]:
    #         start=i
    #         rep2 = rep[i:lr-1]
    #         break
    # mod = len(rep2)
    
    # resM = k%mod
    
    #     if resM == modL[i]:
    #         print(rep2[i])

n,k=map(int,input().split())
a = list(map(int,input().split()))
visit = []
visit = [0]*n
pos=0
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:
    mod = (k-(len(move)-len(roop)))%len(roop)
    print(roop[mod]+1)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
Vector = [0]*N
j = 0
 
for i in range (0,K+1):
	if Vector(A[j]) == 1:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		Vector(A[j]) = 1
		j = A[j]-1

if A[0] ==1:
	print(1)
elif len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
Vector = [0]*N
j = 0
 
for i in range (0,K+1):
	if Vector[A[j]] == 1:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		Vector[A[j]] = 1
		j = A[j]-1

if A[0] ==1:
	print(1)
elif len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
Vector = [0]*N
j = 0
 
for i in range (0,K):
	if Vector[A[j]-1] == 1:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		Vector[A[j]-1] = 1
		j = A[j]-1

if A[0] ==1:
	print(1)
elif len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n,k = map(int,input().split())
a_list = [0] + list(map(int,input().split()))

town_num_tmp = 1  
seen = [-1]*(n+1) 
town_hist_list = []

while seen[town_num_tmp] == -1:
    seen[town_num_tmp] = 1
    town_hist_list.append(town_num_tmp)
    
    town_num_tmp = a_list[town_num_tmp]

roop_end = len(town_hist_list)
roop_start = town_hist_list.index(town_num_tmp)
roop_range = roop_end - roop_start

if k < roop_end:
    print(town_hist_list[k])
else:
    print(town_hist_list[(k-roop_start)%roop_range+roop_start-1])

n,k = map(int,input().split())
a_list = [0] + list(map(int,input().split()))

town_num_tmp = 1  
seen = [-1]*(n+1) 
town_hist_list = []

while seen[town_num_tmp] == -1:
    seen[town_num_tmp] = 1
    town_hist_list.append(town_num_tmp)
    
    town_num_tmp = a_list[town_num_tmp]

roop_end_index = len(town_hist_list) - 1
roop_start_index = town_hist_list.index(town_num_tmp)
roop_range = roop_end_index - roop_start_index + 1

if k < roop_end_index:
    print(town_hist_list[k])
else:
    print(town_hist_list[(k-roop_start_index)%roop_range+roop_start_index])

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
counter = 1
cur = 0
mem = [0]
while True:
   if counter == k:
      ans = cur +1
      break
   cur = a[cur]
   mem.append(cur)
   counter += 1
   if cur == 0:
      k = k%counter
      ans = mem[k]
      break
print(ans)

n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
counter = 0
cur = 0
mem = [0]
fiv = [False]*n
fiv[0] = True
while True:
   if counter == k:
      ans = cur +1
      break
   cur = a[cur]
   
   #print(cur)
   
   mem.append(cur)
   counter += 1
   if fiv[cur]:
      if fiv[0] == True:
         fiv[0] = 0
      roop = counter -fiv[cur]
      k -= fiv[cur]
      k %= roop
      #print(k)
      ans = mem[k+fiv[cur]] +1
      break
   fiv[cur] = counter
   #print(counter,fiv,mem)
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
from collections import deque
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A.insert(0, 0)
    J = [-1] * (N + 1)
    T = []
    que = deque([1])
    count = 0
    l = 0
    while que:
        i = que.popleft()
        if J[i] != -1:
            l = J[i]
            break
        J[i] = count
        count += 1
        T.append(i)
        que.append(A[i])
    if K < l:
        return T[K]
    else:
        return T[(K-(l+1))%(len(T)-l)]
print(main())

from collections import deque
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A.insert(0, 0)
    J = [-1] * (N + 1)
    T = []
    que = deque([1])
    count = 0
    l = 0
    while que:
        i = que.popleft()
        if J[i] != -1:
            l = J[i]
            break
        J[i] = count
        count += 1
        T.append(i)
        que.append(A[i])
    if K < l:
        return T[K]
    else:
        return T[(K-l)%(len(T)-l) + l]
print(main())


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N,K = map(int,input().split())
list1 = list(map(int,input().split()))
visited_town = [[] for i in range(N+1)] 
visited_time = [None for i in range(2*N)] 

visited_town[1].append(0) 
visited_time[0] = 1

cycle_start = -1
cycle_end = -1
cycle_detect = False

for i in range(1,min([K,2*N])):
  next_town = list1[visited_time[i-1]-1]
  visited_town[next_town].append(i)
  visited_time[i] = next_town
  if len(visited_town[next_town]) == 2:
    cycle_start = visited_town[next_town][0]
    cycle_end   = visited_town[next_town][1]
    cycle_detect = True
    break

print(visited_time)

if (!cycle_detect)
  ans = visited_time[K]
  print(ans)
else :   
  syuuki = cycle_end-cycle_start
  cycle = visited_time[cycle_start:cycle_end]
  ans = cycle[(K-cycle_start) % syuuki]
  print(ans)


N,K = map(int,input().split())
list1 = list(map(int,input().split()))
visited_town = [[] for i in range(N+1)] 
visited_time = [None for i in range(2*N)] 

visited_town[1].append(0) 
visited_time[0] = 1

cycle_start = -1
cycle_end = -1

for i in range(1,2*N):
  next_town = list1[visited_time[i-1]-1]
  visited_town[next_town].append(i)
  visited_time[i] = next_town
  if len(visited_town[next_town]) == 2:
    cycle_start = visited_town[next_town][0]
    cycle_end   = visited_town[next_town][1]
    break

if K < cycle_start:
  ans = visited_time[K]
  print(ans)
else :   
  syuuki = cycle_end-cycle_start
  cycle = visited_time[cycle_start:cycle_end]
  ans = cycle[(K-cycle_start) % syuuki]
  print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import numpy as np

def check_num_of_remaining_teleport(num_of_teleport, i):
    if num_of_teleport == 0:
        print(i)
        exit()

num_of_town, num_of_teleport = map(int, input().split())
town = np.array(list(map(int, input().split())))


i = 1

fragranttown_index = 1
fragrants = [0] * num_of_town
fragrants[0] = 1
for _ in range(num_of_town):
    print(num_of_teleport)
    i = town[i - 1]
    num_of_teleport = num_of_teleport - 1

    
    check_num_of_remaining_teleport(num_of_teleport, i)

    
    if fragrants[i - 1] != 1:
        fragrants[i - 1] = 1
    else:
        fragranttown_index = i
        break



revisit = 0
j = fragranttown_index
for _ in range(num_of_town):
    print(num_of_teleport)
    j = town[j - 1]
    revisit = revisit + 1
    num_of_teleport = num_of_teleport - 1

    
    check_num_of_remaining_teleport(num_of_teleport, j)

    
    if fragranttown_index == j:
        break


num_of_last_teleport = num_of_teleport % revisit
k = fragranttown_index
lasttown_index = fragranttown_index

for _ in range(num_of_last_teleport):
    lasttown_index = k
    k = town[k - 1]
    
print(lasttown_index)


import numpy as np

def check_num_of_remaining_teleport(num_of_teleport, i):
    if num_of_teleport == 0:
        print(i)
        exit()

num_of_town, num_of_teleport = map(int, input().split())
town = np.array(list(map(int, input().split())))


i = 1

fragranttown_index = 1
fragrants = [0] * num_of_town
fragrants[0] = 1
for _ in range(num_of_town):
    i = town[i - 1]
    num_of_teleport = num_of_teleport - 1

    
    check_num_of_remaining_teleport(num_of_teleport, i)

    
    if fragrants[i - 1] != 1:
        fragrants[i - 1] = 1
    else:
        fragranttown_index = i
        break



revisit = 0
j = fragranttown_index
for _ in range(num_of_town):
    j = town[j - 1]
    revisit = revisit + 1
    num_of_teleport = num_of_teleport - 1

    
    check_num_of_remaining_teleport(num_of_teleport, j)

    
    if fragranttown_index == j:
        break


num_of_last_teleport = num_of_teleport % revisit
k = fragranttown_index

for _ in range(num_of_last_teleport):
    k = town[k - 1]
    
print(k)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N, K = map(int, readline().split())
A = list(map(lambda x: x - 1, map(int, readline().split())))
visited = [False for i in range(N)]

cur = 0
cycle = []
start = 0
while True:
    if visited[cur]:
        start = cur + 1
        break

    visited[cur] = True
    cycle.append(cur + 1)
    cur = A[cur]

print(cycle)
cycle_start_idx = 0
for i in range(len(cycle)):
    if cycle[i] == start:
        cycle_start_idx = i
        break

if K <= cycle_start_idx:
    print(cycle[K])
    exit()


cycle_len = len(cycle) - cycle_start_idx
idx = (K - cycle_start_idx) % cycle_len
print(cycle[idx + cycle_start_idx])

import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N, K = map(int, readline().split())
A = list(map(lambda x: x - 1, map(int, readline().split())))
visited = [False for i in range(N)]

cur = 0
cycle = []
start = 0
while True:
    if visited[cur]:
        start = cur + 1
        break

    visited[cur] = True
    cycle.append(cur + 1)
    cur = A[cur]

# print(cycle)
cycle_start_idx = 0
for i in range(len(cycle)):
    if cycle[i] == start:
        cycle_start_idx = i
        break

if K <= cycle_start_idx:
    print(cycle[K])
    exit()


cycle_len = len(cycle) - cycle_start_idx
idx = (K - cycle_start_idx) % cycle_len
print(cycle[idx + cycle_start_idx])

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n, k = map(int,input().split())
a_l = list(map(int,input().split()))

town = 1
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

k -= count
if k = 0:
    print(town)
    exit()
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

remain = k%count
count = 0
while count < remain:
    town = a_l[town-1]
    count += 1
   
print(town)

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

town = 1
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

k -= count
if k == 0:
    print(town)
    exit()
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

remain = k%count
count = 0
while count < remain:
    town = a_l[town-1]
    count += 1
   
print(town)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int,input().split())
A = list(map(int,input().split()))
X = [-1 for j in range(N)]
X[0] = 0
Y = [0]
i = 0
Num = 0
MM = 0
for k in range(1,N):
  i = A[i]-1
  if k == K:
    MM = 1
    P = i
  elif X[i] != -1:
    Num = X[i]
    break
  else:
    X[i] = k
    Y.append(i)
Y = Y[Num:]
B = (K-Num)%len(Y)
if MM = 1:
  print(P+1)
else:
  print(Y[B]+1)

N, K = map(int,input().split())
A = list(map(int,input().split()))
X = [-1 for j in range(N)]
X[0] = 0
Y = [0]
i = 0
Num = 0
MM = 0
for k in range(1,N):
  i = A[i]-1
  if k = K:
    MM = 1
    P = i
  elif X[i] != -1:
    Num = X[i]
    break
  else:
    X[i] = k
    Y.append(i)
Y = Y[Num:]
B = (K-Num)%len(Y)
if MM = 1:
  print(P+1)
else:
  print(Y[B]+1)

N, K = map(int,input().split())
A = list(map(int,input().split()))
X = [-1 for j in range(N)]
X[0] = 0
Y = [0]
i = 0
Num = 0
MM = 0
for k in range(1,N):
  i = A[i]-1
  if k == K:
    MM = 1
    P = i
    break
  elif X[i] != -1:
    Num = X[i]
    break
  else:
    X[i] = k
    Y.append(i)
Y = Y[Num:]
B = (K-Num)%len(Y)
if MM == 1:
  print(P+1)
else:
  print(Y[B]+1)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
la = list(map(int, input().split()))

lc = [1]
lcnt = [1]
lcnt.extend([0]*(N-1))
print(lcnt)
i = 1
while lcnt[lc[i-1]-1] == 1:
    if i == N:
        break
    else:
        lc.append(la[lc[i-1]-1])
        lcnt[la[lc[i-1]-1]-1] += 1
        i += 1
print("lcnt =", lcnt)
print("lc =", lc)

lpst = lc.index(lc[-1])
lpnum = len(lc) - lpst - 1
if K <= lpst:
    loclst = K
elif lpnum == 0:
    loclst = lpst
else:
    loclst = (K - lpst) % lpnum + lpst
print(lpst, lpnum)
print(lc[loclst])

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

lc = [1]
lcnt = [1]
lcnt.extend([0]*(N-1))

i = 1
while lcnt[lc[i-1]-1] == 1:
    if i == N:
        break
    else:
        lc.append(la[lc[i-1]-1])
        lcnt[la[lc[i-1]-1]-1] += 1
        i += 1

lpst = lc.index(lc[-1])
lpnum = len(lc) - lpst - 1
if K <= lpst:
    loclst = K
elif lpnum == 0:
    loclst = lpst
else:
    loclst = (K - lpst) % lpnum + lpst

print(lc[loclst])

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
a=list(input().split())
p[0]=int(a[0])-1
f=0
for i in range(k-1):
    p[i+1]=int(a[p])-1
    for j in range(i+1):
        if p[i+1] == p[j]:
            r=j
            q=j-i-1
            f=1
            break
    if f==1:
        break
k-=r
k%=q
s=p[r+k-1]
if f==0:    
    print(p[k-1]+1)
else:
    print(s)

n,k=map(int,input().split())
a=list(input().split())
p = [0]*200000
q = [0]*200000
p[0]=1
p[1]=int(a[0])
q[0]=1
q[p[1]-1]=2
f=0
for i in range(k-1):
    p[i+2]=int(a[p[i+1]-1])
    if q[p[i+2]]>0:
        r=q[p[i+2]]
        q=i+2-r
        f=1
        break
    if f==1:
        break
    q[p[i+2]]=i+3
l=k-r
l%=q
s=p[r+l]
if f==0:    
    print(p[k])
else:
    print(s)

n,k=map(int,input().split())
a=list(input().split())
p = [0]*200000
q = [0]*200000
p[0]=1
p[1]=int(a[0])
q[0]=1
q[p[1]-1]=2
f=0
for i in range(k-1):
    p[i+2]=int(a[p[i+1]-1])
    if q[p[i+2]-1]>0:
        r=q[p[i+2]-1]-1
        q=i+2-r
        f=1
        l=k-r
        l%=q
        s=p[r+l]
        break
    if f==1:
        break
    q[p[i+2]-1]=i+3
if f==0:    
    print(p[k])
else:
    print(s)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import math

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

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        print(chklist)
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True

    for i in range(chklist[state - 1] + chkmod):
        state = A[state - 1]

    print(state)

if __name__ == "__main__":
    solve()

import math

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

    state = 0
    chklist = [0] * N
    chklist_flag = [False] * N
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
        state = A[state]
        print(chklist_flag)

    for i in range(chklist[state - 1] + chkmod):
        chklist[state - 1] = i 
        state = A[state- 1]

    print(state)

if __name__ == "__main__":
    solve()

import math

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

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
 
    chkmod = 0
    chkrem = K

    chklist_flag[0] = True

    for i in range(1, K + 1):
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            chkrem = chklist[state - 1]
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
    
    state = 1

    for i in range(chkrem + chkmod):
        state = A[state - 1]
    
    print(state)

if __name__ == "__main__":
    solve()

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k -= len(know)
    ans = loop[k%len(loop)]

print(ans)


n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k - = len(know)
    ans = know[k%len(loop)]


print(ans)


n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k - = len(know)
    ans = know[k%len(loop)]


print(ans)


n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k -= len(know)
    ans = loop[k%len(loop)]

print(ans)


Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
# -*- coding: utf-8 -*-
"""
Created on Sun May 10 20:50:56 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

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

city = [False for i in range(N)]
city[1-1] =True
now = 1

counta1 = 0
while True:
    counta1 += 1
    prev = now
    now = A[now-1]
    print(prev,now,counta1)
    if city[now-1]:
        break
    elif counta1 == K:
        print(now)
        sys.exit()
    else:
        city[now-1] = True

counta = 1
rout = []
rout.append(now)
while now != prev:
    now = A[now-1]
    rout.append(now)
    counta +=1
    print(now,rout,counta)

ans = rout[(K-counta1) % counta]
print(ans)


# -*- coding: utf-8 -*-
"""
Created on Sun May 10 20:50:56 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

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

city = [False for i in range(N)]
city[1-1] =True
now = 1

counta1 = 0
while True:
    counta1 += 1
    prev = now
    now = A[now-1]
#    print(prev,now,counta1)
    if city[now-1]:
        break
    elif counta1 == K:
        print(now)
        sys.exit()
    else:
        city[now-1] = True

counta = 1
rout = []
rout.append(now)
while now != prev:
    now = A[now-1]
    rout.append(now)
    counta +=1
#    print(now,rout,counta)

ans = rout[(K-counta1) % counta]
print(ans)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = [int(i) for i in input().split(' ')]
A = [0]
A.extend([int(a) for a in input().split(' ')])
check_list = [0] * (N + 1)

town = 1
check_point = 0
initial = -1
interval = -1

print(A)

for k in range(K):
    if check_list[town] > 0:
        check_point = town
        initial = check_list[town]
        interval = k - initial
        break
    check_list[town] = k
    print(check_list)
    town = A[town]

remain = K - k

rem = remain % interval

print(rem, remain, interval, initial)

check_list_ = list(map(lambda x: x - initial, check_list))

print(check_list_)

for i, c in enumerate(check_list_):
    if c == rem:
        print(i)
        exit(0)

N, K = [int(i) for i in input().split(' ')]
A = [0]
A.extend([int(a) for a in input().split(' ')])
check_list = [0] * (N + 1)

town = 1
check_point = 0
initial = -1
interval = -1

for k in range(K):
    if check_list[town] > 0:
        check_point = town
        initial = check_list[town]
        interval = k - initial
        break
    check_list[town] = k
    town = A[town]

if interval == -1:
    print(town)
    exit(0)

remain = K - k

rem = remain % interval

check_list_ = list(map(lambda x: x - initial, check_list))

for i, c in enumerate(check_list_):
    if c == rem:
        print(i)
        exit(0)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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)

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]
      check = 0
roop_num = (K - i)%roop
for i in range(roop_num):
  n = A[n]
print(n)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
def main():
    n,k = map(int, input().split())
    a =  list(map(int, input().split()))

    t = 1
    ans = [1]
    for i in range(n):
        if a[t-1] in ans :
            start = ans.index(a[t-1])
            break
        ans.append(a[t-1])
        t = a[t-1]

    index = k % (len(ans)-start)
    t = ans[index]
    return

if __name__ == '__main__':
    main()


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

    path = [1]
    for _ in range(n-1):
        path.append(a[path[-1]-1])

    if k > n:
        start = path.index(path[-1])
        index = (k - start) % (len(path) - start - 1) + start
        t = path[index]
    else:
        t = path[k]
    return print(t)

if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
Town = []
C = 0
N, K = map(int, input().split())
A = list(map(int, input().split()))
def dfs(a, i, town, c):
    if i-1 in town:
        L = len(town) - len(town[:town.index(i-1)])
        c += (K - c) - (K%L)
        town = []
        town.append(i-1)
    else:
        town.append(i-1)
        c += 1
    if c == K:
        return print(a[i-1])
    nx = a[i-1]
    dfs(a, nx, town, c)

dfs(A, 1, Town, C)

Town = []
N, K = map(int, input().split())
A = list(map(int, input().split()))
vis = [0]*N
p = 1
while vis[p-1]==0:
    Town.append(p)
    vis[p-1] = 1
    p = A[p-1]

l = Town.index[p]

if K < len(Town):
    print(Town[K])
else:
    print(Town[l + (K-l)%(len(Town[l:]))])

N, K = map(int, input().split())
A = list(map(int, input().split()))
vis = [0]*N
p = 1
Town = []

while vis[p-1] == 0:
    Town.append(p)
    vis[p-1] = 1
    p = A[p-1]

l = Town.index(p)

if K < len(Town):
    print(Town[K])
else:
    print(Town[l + (K-l)%(len(Town[l:]))])

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main(n, k, a):
    ck=[-1]*n
    tak=0
    c=0
    ck[0]=c
    for i in range(n):
        c+=1
        tak=a[tak]-1
        if ck[tak]!=-1:
            loop=c-ck[tak]
            lps=ck[tak]
            break
        ck[tak]=c
    print(ck, loop, lps)

    tak=0
    for i in range((k-lps)%(loop)+lps):
        tak=a[tak]-1

    print(tak+1)


if __name__ == '__main__':
    n,k=map(int, input().split())
    a=list(map(int, input().split()))
    main(n, k, a)


#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main(n, k, a):
    ck=[-1]*n
    tak=0
    c=0
    ck[0]=c
    for i in range(n):
        c+=1
        tak=a[tak]-1
        if ck[tak]!=-1:
            loop=c-ck[tak]
            lps=ck[tak]
            break
        ck[tak]=c

    tak=0
    for i in range(min(k, (k-lps)%(loop)+lps)):
        tak=a[tak]-1

    print(tak+1)


if __name__ == '__main__':
    n,k=map(int, input().split())
    a=list(map(int, input().split()))
    main(n, k, a)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import sys
n, k = map(int, input().split())
l = list(map(int, input().split()))
count = 0
li = [0 for _ in range(n)]
b = 1
for i in range(1,n + 1):
    print(b)
    if li[b-1] != 0:
        break
    li[b-1] = i
    b = l[b-1]
    count += 1
#     print('count',count)


d = count - li[b-1] + 1
intercep = li[b-1] -1
print('d', d, 'intercep', intercep)
if k <= intercep:
    b = 1
    for i in range(k):
        b = l[b-1]
    print(b)
    sys.exit()
    
mod = (k - intercep) % d
b = 1
for i in range(intercep + mod):
    print('b',b)
    b = l[b-1]
print(b)

import sys
n, k = map(int, input().split())
l = list(map(int, input().split()))
count = 0
li = [0 for _ in range(n)]
b = 1
for i in range(1,n + 1):
#     print(b)
    if li[b-1] != 0:
        break
    li[b-1] = i
    b = l[b-1]
    count += 1
#     print('count',count)


d = count - li[b-1] + 1
intercep = li[b-1] -1
# print('d', d, 'intercep', intercep)
if k <= intercep:
    b = 1
    for i in range(k):
        b = l[b-1]
    print(b)
    sys.exit()
    
mod = (k - intercep) % d
b = 1
for i in range(intercep + mod):
#     print('b',b)
    b = l[b-1]
print(b)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import sys
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
vis={0:0}
now=0
for i in range(1,k+1):
  now=a[now]-1
  if now in vis:
    bgn=vis.index(now)
    print(vis[bgn+((k-bgn)%(len(vis)-bgn))]+1)
    sys.exit()
  else:
    vis[i]=now
print(vis[-1]+1)


import sys
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
vis_ti=[-1]*n
vis_at=[0]
now=0
for i in range(1,k+1):
  now=a[now]-1
  if vis_ti[now]>=0:
    bgn=vis_ti[now]
    print(vis_at[bgn+(k-bgn)%(i-bgn)]+1)
    sys.exit()
  vis_ti[now]=i
  vis_at.append(now)
print(vis_at[-1]+1)



Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N,K=map(int,input().split())
A=list(map(int,input().split()))

genzaichi=1
count=0
for i in range(K):
    genzaichi=A[genzaichi-1]
    count += 1
    if genzaichi == 1:
        break

K=K%count
print(count)

for i in range(K):
    genzaichi=A[genzaichi-1]


print(genzaichi)

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

genzaichi=1
rireki=[]
houmon=[0]*(N+1)
flag=0

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.append(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        flag=1
        break

print("now")
print(x)
print(roop)
print(roop_from)
print(flag)

genzaichi=1
if flag==1:
    K=K-(((K-x)//roop)-1)*roop
    print(K)

for i in range(K):
    genzaichi=A[genzaichi-1]

print(genzaichi)

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

genzaichi=1
rireki=set()
houmon=[0]*(N+1)
flag=0

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.append(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        flag=1
        break

if flag==0:
    print(genzaichi)
else:
    genzaichi=roop_from
    K=(K-x)-((K-x)//roop)*roop
    for i in range(K):
        genzaichi=A[genzaichi-1]
    print(genzaichi)

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

genzaichi=1
rireki=set()
houmon=[0]*(N+1)
flag=0

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.add(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        flag=1
        break

if flag==0:
    print(genzaichi)
else:
    genzaichi=roop_from
    K=(K-x)-((K-x)//roop)*roop
    for i in range(K):
        genzaichi=A[genzaichi-1]
    print(genzaichi)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
from sys import stdin
import math


def main():
    n, k = list(map(int, input().split()))
    A = list(map(int, stdin.readline().split()))
    roots = []
    roots.append(A)
    maxLoop = math.ceil(math.log(k)/math.log(2))
    for _ in range(1, maxLoop):
        roots_tmp = [None] * n
        for j in range(n):
            roots_tmp[j] = roots[-1][roots[-1][j]-1]
        roots.append(roots_tmp)

    position = 1
    for i in range(maxLoop+1):
        if k & (1 << (maxLoop-i)):
            position = roots[-1-i][position-1]

    print(position)

if __name__ == '__main__':
    main()


from sys import stdin


def main():
    n, k = list(map(int, input().split()))
    A = list(map(int, stdin.readline().split()))
    roots = [1]
    position = 0
    for _ in range(n):
        position = A[roots[-1]-1]
        A[roots[-1]-1] = - position
        if position < 0:
            break
        roots.append(position)

    x = roots.index(-position)
    y = len(roots) - x

    if k <= x:
        print(roots[k])
    elif k > x:
        print(roots[x + ((k-x) % y)])


if __name__ == '__main__':
    main()


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
6 727202214173249351
6 5 2 5 3 2
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = []
o = [-1] * (n + 1)  
c, l = 1, 0  

v = 1
while o[v] == -1:
    o[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - o[v]
l = o[v]

if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l + k])


n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = []
o = [-1] * (n + 1)  
c, l = 1, 0  

v = 1
while o[v] == -1:
    o[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - o[v]
l = o[v]

if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l + k])


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int,input().split())
ls_pass = list(map(int,input().split()))
ls_pass.insert(0,0)
a=1
b=0
ls_past=[1]
for i in range(1,N+1):
  b=a
  a=ls_pass[a]
  if b==a:
    print(a)
    break
  elif a in ls_past:
    c=ls_past.index(a)
    ls_cicle=ls_past[c:i]
    print(ls_cicle[(K-i)%(i-c)])
    break
  elif i=K:
    print(a)
    break
  else:
    ls_past.append(a)

N, K = map(int,input().split())
ls_pass = [0]+list(map(int,input().split()))
a=1
b=0

ls=["ンゴ"]+[0]+["ンゴ"]*(N-1)
ls_past=[1]
for i in range(1,N*3):
  b=a
  a=ls_pass[a]
  if ls[a]!="ンゴ":
    ls_cicle=ls_past[ls[a]:i]
    print(ls_cicle[(K-i)%len(ls_cicle)])
    break
  elif i==K:
    print(a)
    break  
  else:
    ls_past.append(a)
    ls[a]=i

Verdicts: Runtime Error

Accepted