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()))

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

did = set(1)
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            print(roop[(k - i) % len(roop)])
            exit()

   


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

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

did = set([1])
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            print(roop[(k - i) % len(roop)])
            exit()

   


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= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if d[nex]:
        first=c.index(nex)
        flag=1
        break
    d[nex-1]=1
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])

n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if d[nex-1]:
        first=c.index(nex)
        flag=1
        break
    d[nex-1]=1
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-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,list(input().split())))

exist = [a[0]]
ex = {}

cnt = 1
while(True):
    if a[exist[-1]-1] in ex:
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    exist.append(a[exist[-1]-1])
    ex[a[exist[-1]-1]] = 0
    
    cnt += 1
    if cnt >= k-1:
        print(exist[-1])
        exit()


#k = k-idx
#print(idx,exist,cnt)
d = (k-idx) % (cnt-idx)

print(exist[d+idx-1])


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

exist = [1]
#exist.append(a[0])
ex = {}
ex[1] = 0
#ex[a[0]] = 0
if k == 1:
    print(a[0])
    exit()

cnt = 0
while(True):
    if a[exist[-1]-1] in ex:        
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    ex[a[exist[-1]-1]] = 0
    exist.append(a[exist[-1]-1])
    
    cnt += 1
    if cnt-1 >= k-1:
        print(exist[-1])
        exit()


#k = k-idx
d = (k-idx) % (cnt-idx+1)

print(exist[d+idx])


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 = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
t = K
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:
    state ,f = test, find_f[test]
    K = K-f[0]
    f = f[1]-f[0]
    remain = K%f
    break

if t == K:
  print(test)
else:
  for i in range(remain):
   	state = move_list[state-1]
 
  print(state)
#state = 1

import sys
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:
    state ,f = test, find_f[test]
    break
try:
  K = K-f[0]
  f = f[1]-f[0]
  remain = K%f
except:
  print(test)
  sys.exit()
 
for i in range(remain):
  state = move_list[state-1]
 
  print(state)

#state = 1

N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=1000000:
    state ,f = test, find_f[test]
    break
try:
  K = K-f[0]
  f = f[1]-f[0]
  remain = K%f
  for i in range(remain):
    state = move_list[state-1]
 
  print(state)
except:
  print(test)
#state = 1

Verdicts: Wrong Answer

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 = list(map(int, input().split()))
a = list(map(int, input().split()))

init = [1]
while True:
    nxt = a[init[-1]-1]
    if nxt in init:
        idx_nxt = init[nxt]
        loop = init[idx_nxt:]
        init = init[:idx_nxt]
        break
    else:
        init.append(nxt)

idx = (k-len(init))%len(loop)
print(loop[idx])

n, k = map(int, input().split())
a = tuple(map(lambda n: int(n) - 1, input().split()))

visited = [0] * n
next_town = 0
order = []

while True:
    if visited[next_town]:
        break
    order.append(next_town)
    visited[next_town] = 1
    next_town = a[next_town]

first_loop = len(order)
offset = order.index(next_town)
second_loop = first_loop - offset

if k < first_loop:
    print(order[k] + 1)
else:
    print(order[offset + (k - first_loop) % second_loop] + 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):
def resolve():
    N,K = map(int,input().split())
    A = [int(i)-1 for i in input().split()]
    visited =[False]*N
    steps = [K for i in range(N)]

    current = 0
    while True:

        if K == 0:
			print(current+1)
            return
        if not visited[current]:
            visited[current]=True
            steps[current]=K
            current=A[current]
            K-=1
        else:
            K= K % (steps[current]-K)
            break
    
    while K != 0:

        current= A[current]
        K-=1
    print(current+1)




if __name__ == "__main__":
    resolve()


def resolve():
    N,K = map(int,input().split())
    A = [int(i)-1 for i in input().split()]
    visited =[False]*N
    steps = [K for i in range(N)]

    current = 0
    while True:

        if K == 0:
            print(current+1)
            return
        if not visited[current]:
            visited[current]=True
            steps[current]=K
            current=A[current]
            K-=1
        else:
            K= K % (steps[current]-K)
            break
    
    while K != 0:

        current= A[current]
        K-=1
    print(current+1)




if __name__ == "__main__":
    resolve()


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 = [int(i) for i in input().split()]

lst = []
visit_now = 1

while True:
    if visit_list[visit_now - 1] == 1:
        break
    visit_list[visit_now - 1] = 1
    lst.append(visit_now)
    visit_now = a[visit_now - 1]


start = lst.index(visit_now)
loop_num = n - start - 1
lst = lst[start:]

k -= start
k %= loop_num

print(lst[k])
# print(lst, loop_num, loop_list, k)


map(int, input().split())
a = [int(i) for i in input().split()]


lst = []
visit_now = 1
visit_list = [0] * n

while True:
    if visit_list[visit_now - 1] == 1:
        break
    visit_list[visit_now - 1] = 1
    lst.append(visit_now)
    visit_now = a[visit_now - 1]


start = lst.index(visit_now)
loop_num = n - start - 1

if k <= start:
    ans = lst[k]
else:
    k -= start
    k %= loop_num
    ans = lst[start + k]

print(ans)

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


lst = []
visit_now = 1
visit_list = [-1] * (n + 1)
cnt = 0

while visit_list[visit_now - 1] == -1:
    visit_list[visit_now - 1] = cnt
    lst.append(visit_now)
    visit_now = a[visit_now - 1]
    cnt += 1

start = visit_list[visit_now - 1]
loop_num = cnt - start

if k <= start:
    ans = lst[k]
else:
    k -= start
    k %= loop_num
    ans = lst[start + k]

print(ans)

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,m  = map(int,input().split())
a = [int(x) for x in input().split()]

ans1 = [1]
temp = 0
count = 0
while(True):
    ans1 += [a[temp]]
    temp = a[temp]-1
    if len(ans1) !=  len(set(ans1)):
        break
    count += 1
    if count == m:
        print(ans1[len(ans1)-1])
        exit()


num1 = ans1.index(temp+1)
ans2 = ans1[num1:len(ans1)-1]
print(ans2)
num = len(ans2)

print(ans2[(m-num1)%num])


n,m  = map(int,input().split())
a = [int(x) for x in input().split()]

ans1 = [1]
temp = 0
visit = [0]*(n+1)
visit[1] = 1
while(True):
    ans1 += [a[temp]]

    if visit[a[temp]] == 1:
        break

    visit[a[temp]] = 1
    temp = a[temp]-1


    if len(ans1) == m+1:
        print(ans1[len(ans1)-1])
        exit()


num1 = ans1.index(a[temp])
ans2 = ans1[num1:len(ans1)-1]

num = len(ans2)

print(ans2[(m-num1)%num])


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 = [0] + [int(e) for e in input().split()]

used_tele = 0
repeating_flag = False
moving_dict = dict() 
moving_dict[1]= 0
now_town = 1

while used_tele<K:
    next_town = A_list[now_town]
    used_tele += 1
    print(used_tele,":",now_town,"→",next_town)
    
    if next_town in moving_dict:
        repeating_flag = True
        break
        
    moving_dict[next_town] = used_tele
    now_town = next_town
        
if repeating_flag == False:
    print(next_town)
else:
    moving_list = list(moving_dict.keys())
    repeating_start_town_index = moving_list.index(next_town)
    
    #print(moving_list)
    #print(moving_list[0:repeating_start_town_index])
    #print(moving_list[repeating_start_town_index:])
    
    repeating_moving_list = moving_list[repeating_start_town_index:]
    K -= len(moving_list[0:repeating_start_town_index])
    
    print(repeating_moving_list[K%len(repeating_moving_list)])

N,K = map(int, input().split())
A_list = [0] + [int(e) for e in input().split()]

used_tele = 0
repeating_flag = False
moving_dict = dict() 
moving_dict[1]= 0
now_town = 1

while used_tele<K:
    next_town = A_list[now_town]
    used_tele += 1
    
    
    if next_town in moving_dict:
        repeating_flag = True
        break
        
    moving_dict[next_town] = used_tele
    now_town = next_town
        
if repeating_flag == False:
    print(next_town)
else:
    moving_list = list(moving_dict.keys())
    repeating_start_town_index = moving_list.index(next_town)
    
    #print(moving_list)
    #print(moving_list[0:repeating_start_town_index])
    #print(moving_list[repeating_start_town_index:])
    
    repeating_moving_list = moving_list[repeating_start_town_index:]
    K -= len(moving_list[0:repeating_start_town_index])
    
    print(repeating_moving_list[K%len(repeating_moving_list)])

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 defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(len(l)-first)%((len(l)-1)-first)
    k+=first
    print(l[k])

from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(k-first)%((len(l)-1)-first)
    k+=first
    print(l[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):
n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,i,j=1,0,0
root=[1]

for j in range(k):
  start=A[start-1]
  if start in root:
    i = start
    break
  root.append(start)
  
if k <= len(root):
  print(root[k-1])
else:
  newroot=root[i-1:]
  print(newroot)
  ans=(k-len(root))%len(newroot)
  print(newroot[ans])

n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,i=1,0
root=[1]

for j in range(k):
  start=A[start-1]
  if start in root:
    i = start
    break
  root.append(start)
  
if k <= len(root):
  print(root[k-1])
else:
  newroot=root[i-1:]
  print(newroot)
  ans=(k-len(root))%len(newroot)
  print(newroot[ans])

n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,steps=1,0
root=dict()

while start not in root and steps < k:
  steps+=1
  root[start]=steps
  start=A[start-1]

if steps == k:
  print(start+1)
else:
  pa = steps-root[start]
  s=root[start]
  ans=(k-steps)%pa
  root={v: k for k, v in root.items()}
  print(root[s+ans])

n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,steps=1,0
root=dict()

while start not in root and steps < k:
  root[start]=steps
  steps+=1
  start=A[start-1]

if steps == k:
  print(start)
else:
  pa = steps-root[start]
  ans= root[start] + (k-steps)%pa
  root={v: k for k, v in root.items()}
  print(root[ans])

Verdicts: Runtime Error

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):
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    cnt = [-1] * n
    cnt[0] = 0
    ary = []
    now = 1
    ary.append(now)
    if k <= n:
        for i in range(1,k+1):
            now = a[now-1]
            if i == k:
                print(now)
                return
    else:
        for i in range(1,k+1):
            now = a[now-1]
            if cnt[now-1] != -1:
                break
            else:
                cnt[now-1] = i
                ary.append(a[now-1])


        print(ary[cnt[now-1]+((k-cnt[now-1]) % (i - cnt[now-1]))+1])
        
if __name__=='__main__':
    main()


def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    cnt = [-1] * n
    cnt[0] = 0
    ary = []
    now = 0
    for i in range(n):
        a[i] -= 1

    ary.append(now)
    if k <= n:
        for i in range(1,k+1):
            now = a[now]
            if i == k:
                print(now+1)
                return
    else:
        for i in range(1,k+1):
            now = a[now]
            if cnt[now] != -1:
                break
            else:
                cnt[now] = i
                ary.append(now)
        loop_s = cnt[now]

        print(ary[cnt[now] +((k-cnt[now]) % (i - cnt[now]))] + 1)
        # print(cnt)
        # print(now)
        # print(ary)

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):
n,k = map(int, input().split())
tele = list(map(int, input().split()))
move = [-1]*n
move[0] = 0
now = 0
cnt = 0

while True:
    cnt += 1
    now = tele[now] - 1 

    if move[now] != -1:
        cycle = cnt - move[now]
        break
    move[now] = cnt


if cnt-1 >= k:
    ans = 0
    for i in range(k):
        ans = tele[ans] - 1

else:
    loop = (k-(cnt-1))%cycle
    ans = cnt - 1
    if loop == 0:
        ans = tele[ans] - 1
    for i in range(loop+1):
        ans = tele[ans] - 1
print(ans+1)
        




n,k = map(int, input().split())
tele = [0] + list(map(int, input().split()))
move = [-1]*(n+1)
move[1] = 0
now = 1
cnt = 0

while True:
    cnt += 1
    now = tele[now] 

    if move[now] != -1:
        cycle = cnt - move[now]
        break
    else:
        move[now] = cnt

notCycle = move[now]

if notCycle > k:
    now = 1
    for i in range(k):
        now = tele[now]

else:
    loop = (k-notCycle)%cycle
    for i in range(loop):
        now = tele[now]
print(now)

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 = list(map(int, input().split()))
a_list = list(map(int, input().split()))

done_list = [1]
cur_place = 1
flag = True
for i in range(K):
    if a_list[cur_place-1] not in done_list:
        cur_place = a_list[cur_place-1]
        done_list.append(cur_place)
    else:
        period = len(done_list) - done_list.index(a_list[cur_place-1])
        print(done_list[done_list.index(a_list[cur_place-1]) + (K - i + 1) % period])
        flag = False
        break

if flag:        
    print(cur_place)
    

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

done_list = [1]
cur_place = 1
flag = True
for i in range(K):
    if a_list[cur_place-1] not in done_list:
        cur_place = a_list[cur_place-1]
        done_list.append(cur_place)
    else:
        period = len(done_list) - done_list.index(a_list[cur_place-1])
        print(done_list[done_list.index(a_list[cur_place-1]) + (K - i) % period + 1])
        flag = False
        break

if flag:        
    print(cur_place)
    

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

before = 1
start = 0
end = 0

if K <= N:
    for i in range(K):
        before = a[before]
    print(before)


    
else:
    market_town = 0
    for i in range(N):
        before = a[before]
    marked_town = before
    K -= N
    counter = 1
    while marked_town != a[before]:
        before = a[before]
        counter += 1
    K = K % counter
    for i in range(K+1):
        before = a[before]
    print(before)

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())

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

city = 1
route = []
cnt = [0] * n
cnt[0] += 1
route.append(0)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)

if k <= len(route):
    print(route[k])

elif length >= 1:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

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

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

city = 1
route = []
cnt = [0] * n
cnt[0] += 1
route.append(1)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)

print(cnt)
print(route)
print(st)
print(length)

if k < len(route):
    print(route[k])

elif length >= 1:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

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

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

city = 1
route = []
cnt = [0] * n
cnt[0] += 1
route.append(1)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)


if k < len(route):
    print(route[k])

else:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

Verdicts: Runtime Error

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 = input().split()
b = [0]*n
d = {}
ind = 1
num = 0
loop = 1
while num == k:
    if b[ind-1] == 0:
        b[ind-1] = -1
        d[ind] = num
        num += 1
        ind = int(a[ind-1])
    else:
        loop = num-d[ind]
        sup = d[ind]
        break

if k-sup >=0:
    for i in range((k-sup)%loop):
        ind = int(a[ind-1])

print(ind)

n,k = map(int,input().split())
a = input().split()
b = [0]*n
d = {}
ind = 1
num = 0
while True:
    if b[ind-1] == 0:
        b[ind-1] = -1
        d[ind] = num
        num += 1
        ind = int(a[ind-1])
    else:
        loop = num-d[ind]
        sup = d[ind]
        break


for i in range((k-sup)%loop):
    ind = int(a[ind-1])
print(int(a[ind]))

n,k = map(int,input().split())
a = input().split()
b = [0]*n
d = {}
ind = 1
num = 0
loop = 1
sup =0

while num != k:
  if b[ind-1] == 0:
    b[ind-1] = -1
    d[ind] = num
    num += 1
    ind = int(a[ind-1])
  else:
    loop = num-d[ind]
    sup = d[ind]
    break

if k-sup >=0:
  for i in range((k-sup)%loop):
    ind = int(a[ind-1])

print(ind)

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(map(int, input().split()))

r = [0]*N

now = 0
r[now]=1
for i in range(N):
  now = A[now]-1
  if r[now] == 0:
    r[now] = i+2
  else:
    x1 = r[now]
    x2 = i+1
    break
print(x1, x2)
print(r)
roop_num = x2 - x1 + 1
x = (K-x1+1) % roop_num
print(x)
if K < x1:
  print(r.index(K+1)+1)
else:
  print(r.index(x1 + x)+1)


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

now = 1
r = [0]*N
r[0]=now
for i in range(N):
  if A[now-1] in r:
    x = r.index(A[now-1])
    break
  else:
    now = A[now-1]
    r[i+1]=now
r_x = r[x:]
print(r_x[(K-x)%len(r[x:])])


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

r = [0]*N

now = 0
r[now]=1
for i in range(N):
  now = A[now]-1
  if r[now] == 0:
    r[now] = i+2
  else:
    x1 = r[now]
    x2 = i+1
    break

roop_num = x2 - x1 + 1
x = (K-x1+1) % roop_num

if K < x1:
  print(r.index(K+1)+1)
else:
  print(r.index(x1 + x)+1)


Verdicts: Wrong Answer

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()))
a.insert(0, 0)

for i in range(1, n+1):
    if a.count(i)>=1:
        s_index=i
        break

cnt=0
n_index=a[s_index]
for j in range(1, n+1):
    if n_index==s_index:
        cnt+=1
        break
    else:
        n_index=a[n_index]
        cnt+=1

print(a)
print(s_index)
print(cnt)

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

t=[1]

for i in range(2*n):
    nxt=a[t[-1]]
    t.append(nxt)

if k<n+1:
    print(t[k])
    exit()

loop_end=n
loop_start=n-1

while t[loop_start]!=t[loop_end]:
    loop_start-=1

cycle=loop_end-loop_start
k%=cycle

while k<n+1:
    k+=cycle

print(t[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

n, k, *aaa = map(int, sys.stdin.buffer.read().split())
shortest = [-1] * n
v = 0
d = 0
while shortest[v] == -1:
    shortest[v] = d
    v = aaa[v] - 1
    d += 1
    if d == k:
        print(v + 1)
        exit()

first = shortest[v]
loop = d - first
remaining = (k - first) % loop
print(v, first, loop, remaining + first, shortest)
ans = shortest.index(remaining + first) + 1
print(ans)


import sys

n, k, *aaa = map(int, sys.stdin.buffer.read().split())
aaa = [0] + aaa
p = 1
while k:
    if k & 1:
        p = aaa[p]
    aaa = [aaa[a] for a in aaa]
    k >>= 1
print(p)


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()))
p = a[0]
s = a[0]
g = []
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
for i in range(len(g)):
    if g[i] == g[i] + mod
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
g.reverse()
for i in range(len(g)):
    if g[i] == g[i] + mod:
        d = i
        break
if k <= mod:
    for i in range(k%mod):
        q.append(s)
        s = a[s-1]
    q.reverse()
    q.append(1)
    print(q[0])
else:
    print(g[d+(k-d)%mod])

n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = [1]
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
g.reverse()
for i in range(len(g)):
    if g[i] == g[i + mod]:
        d = i
        break
if k <= n:
    for i in range(k):
        q.append(s)
        s = a[s-1]
    q.reverse()
    print(q[0])
else:
    print(g[d+(k-d)%mod])

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()))

tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True

for i in range(k):
  now = a[now-1]
  if now in went:
    tele = tele[tele.index(now):]
    cnt = len(tele)
    flag = False
    break
  tele.append(now)
  went.add(now)

if flag == True:
  print(tele[-1])

else:
  cnt = k % cnt
  print(tele[cnt])

print(tele)

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

tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True

for i in range(k):
  now = a[now-1]
  if i != k-1 and now in went:
    tele = tele[tele.index(now):]
    cnt = i+1
    flag = False
    break
  tele.append(now)
  went.add(now)

if flag == True:
  print(tele[-1])

else:
  k -= cnt
  cnt = k % len(tele)
  print(tele[cnt])

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,*A=map(int,open(0).read().split())
a=1
while K>0:
    if K&1:
        a=A[a-1]
        A=[A[A[i]-1] for i in range(N)]
    K>>=1
print(a)

n, k = map(int, input().split())
a = [int(i) for i in input().split()]
ordi = [0, ]
for i in range(n):
    if a[ordi[-1]] - 1 not in ordi:
        ordi.append(a[ordi[-1]] - 1)
    else:
        ordi.append(a[ordi[-1]] - 1)
        index = ordi.index(a[ordi[-1]] - 1)
        len_ = len(ordi) - index
        break
print(ordi[index + (k - index) % len_])

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
    if k & 1:
        ans = a[ans]
    a = [a[ai] for ai in a]
    k >>= 1
print(ans + 1)

Verdicts: Wrong Answer

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()))
p=1
l=[0]*n
#l[0]+=1
if k<=100000:
    for i in range(k):
        p=A[p-1]
        l[p-1]+=1
    print(p)
    exit()
else:
    for i in range(100001):
        p=A[p-1]
        l[p-1]+=1
        #print(p)
        if l[p-1]==2:
            x=i
            break
    #print("p=",str(p))
    #print(l,x)

    d=1
    for j in range(100001):
        d=A[d-1]
        if d==p:
            y=j
            break
    #print("y=",str(y))
    z=(k-x-1)%(x-y)
    for i in range(z):

n,k=map(int,input().split())
A=list(map(int,input().split()))
p=1
l=[0]*n
#l[0]+=1
if k<=200000:
    for i in range(k):
        p=A[p-1]
        l[p-1]+=1
    print(p)
    exit()
else:
    for i in range(200001):
        p=A[p-1]
        l[p-1]+=1
        #print(p)
        if l[p-1]==2:
            x=i
            break
    #print("p=",str(p))
    #print(l,x)

    d=1
    for j in range(200001):
        d=A[d-1]
        if d==p:
            y=j
            break
    #print("y=",str(y))
    z=(k-x-1)%(x-y)
    #print(z)
    for i in range(z):
        p=A[p-1]
        #print(p)
    print(p)

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().strip().split(' ') ]
A = [ int(i) for i in input().strip().split(' ') ]

current = 1
N_counter = [0] * N 
N_counter_idx = [0] * N 
N_counter[current-1] += 1

k_reserved = -1
for i in range(1, N+1):
    current = A[current-1]
    N_counter[current-1] += 1
    if i == K:
        k_reserved = current
    if N_counter[current-1] == 2:
        span = i
        break
    N_counter_idx[current-1] = i

if K < i:
    print(k_reserved)
else:
    #print(N_counter)
    #print(N_counter_idx)
    pre_loop_length = N_counter_idx[current-1] - 1
    loop_length = max(N_counter_idx) - pre_loop_length
    K_new = (K - pre_loop_length) % loop_length + pre_loop_length
    
    
    current = 1
    for k in range(K_new):
        current = A[current-1]
    print(current)

N, K = [ int(i) for i in input().strip().split(' ') ]
A = [ int(i) for i in input().strip().split(' ') ]

current = 1
N_counter = [0] * N 
N_counter_idx = [0] * N 
N_counter[current-1] += 1
N_counter_idx[current-1] = 1

k_reserved = -1
for i in range(1, N+1):
    current = A[current-1]
    N_counter[current-1] += 1
    if i == K:
        k_reserved = current
    if N_counter[current-1] == 2:
        break
    N_counter_idx[current-1] = i+1

if K < i:
    print(k_reserved)
else:
    #print(N_counter)
    #print(N_counter_idx)
    pre_loop_length = N_counter_idx[current-1] - 1
    loop_length = max(N_counter_idx) - pre_loop_length
    #print(pre_loop_length)
    #print(loop_length)
    K_new = (K - pre_loop_length) % loop_length + pre_loop_length
    
    
    current = 1
    for k in range(K_new):
        current = A[current-1]
    print(current)

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 numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
def B(i):
  if i==0:
    return 1
  return A[B(i-1)-1]
C=[1]
for i in range(1,N+1):
  if C.count(B(i))==0:
    C.append(B(i))
  else:
    l=C.index(B(i))+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-1])
print(C)

import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
C=[1]
for i in range(1,N+1):
  if C.count(A[C[i]-1])==0:
    C.append(A[C[i]-1])
  else:
    l=C.index(A[C[i]-1])+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-1+(K+1-l)%(len(C)-l+1)])
print(C)

import numpy as np
import sys
sys.setrecursionlimit(200000)
N,K=map(int,input().split())
A=list(map(int,input().split()))
def B(i):
  if i==0:
    return 1
  return A[B(i-1)-1]
C=[1]
for i in range(1,N+1):
  if C.count(B(i))==0:
    C.append(B(i))
  else:
    l=C.index(B(i))+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-2+(K+2-l)%(len(C)-l+1)])
print(C)

import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
C=[1]
s=set([1])
for i in range(N):
  if A[C[i]-1] in s:
    l=C.index(A[C[i]-1])+1
    break
  else:
    C.append(A[C[i]-1])
    s.add(A[C[i]-1])
if K+1<=l:
  print(C[K])
else:
  print(C[l-1+(K+1-l)%(len(C)-l+1)])

Verdicts: Runtime Error

Runtime Error

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(lambda x: int(x)-1, input().split()))

visited = [1] + [0]*(n-1)
idx = [0] + [-1]*(n-1)
route = [0]
while True:
    now = a[route[-1]]
    if visited[now]:
        break
    visited[now] = 1
    idx[now] = len(route)
    route.append(now)

border = idx[a[route[-1]]]
non_loop = route[:border]
loop = route[border:]

if a[route[-1]] == 0:
    print(route[k%len(route)])
else:
    print(loop[(k-len(non_loop))%len(loop)]+1)


n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))

visited = [1] + [0]*(n-1)
idx = [0] + [-1]*(n-1)
route = [0]
while True:
    now = a[route[-1]]
    if visited[now]:
        break
    visited[now] = 1
    idx[now] = len(route)
    route.append(now)

border = idx[a[route[-1]]]
non_loop = route[:border]
loop = route[border:]

print(non_loop[k]+1) if k < len(non_loop) else print(loop[(k-len(non_loop))%len(loop)]+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()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  chk=(k-start+1)%loopcntac
  if chk==0:
    chk=loopcnt
  print(keys[(start-1)+chk-1])


n,k=map(int,input().split())
a=list(map(int,input().split()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  chk=(k-start+1)%loopcnt
  if chk==0:
    chk=loopcnt
  print(keys[(start-1)+chk-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()))

num = 1
l = []
l.append(num)
i = 0
while i < K-1:
  num = A[num-1]
  if num not in l:
    l.append(num)
    i += 1
  else:
    i = K - (K-1-i) % (i+1-(l.index(num)))
    break;
    
while i < K-1:
  num = A[num-1]
  i += 1
  
print(num)

N, K = map(int, input().split())
A = list(map(int, input().split()))
was = []
tf = [False] * N
place = 1
cnt = 0

while tf[place-1] == False:
  if cnt < K:
    tf[place-1] = True
    was.append(place)
    place = A[place-1]
    cnt += 1
  else:
    print(place)
    break;
else:
  loop = cnt - was.index(place)
  num = (K-cnt) % loop + was.index(place)
  print(was[num])

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()))
p = 1
p_hist = []
while p not in p_hist:
    p_hist.append(p)
    p = A[p-1]
end_loop = p_hist[-1]

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

N,K = map(int, input().split())
A = list(map(int, input().split()))
p = 1
p_hist = []
p_hist_set = set()
p_hist_index = [-1] * (N+1)
while p not in p_hist_set:
    p_hist_index[p] = len(p_hist)
    p_hist.append(p)
    p_hist_set.add(p)
    p = A[p-1]
end_loop = p_hist[-1]
end_loop_idx = p_hist_index[end_loop]
start_loop = A[end_loop - 1]
start_loop_idx = p_hist_index[start_loop]
if K < start_loop_idx:
    print(p_hist[K])
else:
    print(p_hist[(K-start_loop_idx)%(end_loop_idx-start_loop_idx+1)+start_loop_idx])


Verdicts: Wrong Answer

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,*a=map(int,open(0).read().split());a=[0]+a;p=1
while k:p=k%2*p and a[p];a=[a[b]for b in a];k//=2
print(p)

_,k,*a=map(int,open(0).read().split());a=[0]+a;p=1
while k:p=~-k%2*p or a[p];a=[a[b]for b in a];k//=2
print(p)

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):
max = int(data[1])
limit = max
times = 0
pos = 1
experience = [1]
while max > 0:
    pos = telepo[pos - 1]
    max -= 1
    times += 1
    #print(pos)
    if pos in experience:
        length = len(experience) - experience.index(pos)
        max = max % length
        if max == 0:
            break
        while max > 0:
            pos = telepo[pos - 1]
            max -= 1
            times += 1
            #print(pos)
        break
    experience.append(pos)
print(pos)

data = input().rstrip().split(" ")
tele = input().rstrip().split(" ")
telepo = [int(i) for i in tele]

max = int(data[1])
limit = max
times = 0
pos = 1
experience = [1]
check = {1}
before = 0
while max > 0:
    pos = telepo[pos - 1]
    max -= 1
    times += 1
    before = len(check)
    check.add(pos)
    if before == len(check):
        length = len(experience) - experience.index(pos)
        max = max % length
        if max == 0:
            break
        while max > 0:
            pos = telepo[pos - 1]
            max -= 1
            times += 1
            #print(pos)
        break
    experience.append(pos)
print(pos)

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()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k <= n:
    print(visited[k]+1)
else:
    p = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p-1)%loop
    print(p, loop, x)
    print(visited[p+x+1]+1)


n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k < n:
    print(visited[k]+1)
else:
    p = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p-1)%loop
    print(visited[p+x+1]+1)


Verdicts: Wrong Answer

Accepted