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):
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
for i in range(N):
    town.append(i)
    flg[i] = True
    i = A[i]

if len(town) >= K:
    print(town[K])
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s
    print(town[(K - to_s) % loop + to_s])

import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
for _ in range(N):
    town.append(i)
    flg[i] = True
    i = A[i]

if len(town) >= K:
    print(town[K])
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s
    print(town[(K - to_s) % loop + to_s])

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,*a=map(int,open(0).read().split())
d=[2*k]*n
s=0
while k:k-=1;k%=d[s:=a[s]-1]-k;d[s]=k
print(s)

n,k,*a=map(int,open(0).read().split())
d=[2*k]*n
while k:n=1-a[-n];d[n]=k=~-k%(d[n]+1-k)
print(1-n)

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

otozureta = set([1])
otozureta_list = [1]


def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + (K % loop_length) - 1])


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

otozureta = set([1])
otozureta_list = [1]

import sys
sys.setrecursionlimit(1000000)

def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index


if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + ((K - loop_made_length) % loop_length)])


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 = list(map(int, input().split()))
    path = {}
    c = 1
    i = 1
    while c < k:
        if not i in path.keys():
            path[i] = c
            i = A[i - 1]
            c += 1
        elif i in path.keys():
            start = path[i]
            roop = c - path[i]
            break
    roop += start - 1
    idx = 1
    for r in range(roop):
        idx = A[idx - 1]
    print(idx)

resolve()

def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    s = []
    ord = [-1 for i in range(n + 1)]
    rep = 1
    pos = 1
    while ord[pos] == -1:
        ord[pos] = len(s)
        s.append(pos)
        pos = A[pos - 1]
        rep += 1

    rep = len(s) - ord[pos]
    
    l = ord[pos]
    if k < l:
        print(s[k])
    else:
        
        k -= l
        k %= rep
        print(s[l + k])
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):
z = 0

b = [1]
for i in range(1000000):
  j = [a[b[i]-1]]
  b.extend(j)

for i in range(n):
  if b.count(i+1)>=3:
    z = z + 1

if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)

a = list(map(int,input().split()))
z = 0
b = [1]
c = [0]*n
for i in range(n*3):
  j = [a[b[i]-1]]
  b.extend(j)
  c[a[b[i]-1]-1] = c[a[b[i]-1]-1]+1
  if c[a[b[i]-1]-1] == 2:
    z = z + 1

if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)

n,k = map(int,input().split())
a = list(map(int,input().split()))
z = 0
b = [1]
c = [0]*n
for i in range(n*3):
  j = [a[b[i]-1]]
  b.extend(j)
  c[a[b[i]-1]-1] = c[a[b[i]-1]-1]+1
  if c[a[b[i]-1]-1] == 2:
    z = z + 1
if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)

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()))
L=[1]
def f(L):
  L.append(A[l[-1]]) if A[l[-1]] in L
f(L)
print(L[k//len(L)])
  

n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
def f(L):
    if A[L[-1]-1] not in L:
        L.append(A[L[-1]-1])
        return f(L)
    else:
        return L
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
print(P)
if k+1 <= j:
    print(P[k])
else:
    print(P[j+(k-j)%l])

n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
def f(L):
    if A[L[-1]-1] not in L:
        L.append(A[L[-1]-1])
        return f(L)
    else:
        return L
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
print(P)
if k < j:
    print(P[k])
else:
    print(P[j+(k-j)%l])

n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
S = {1}

while A[L[-1]-1] not in S:
  last = L[-1]-1
  L.append(A[last])
  S |= {A[last]}

a = A[L[-1]-1]
j = L.index(a)
r = len(L)
l = r - j

if k+1 <= j:
    print(L[k])
else:
    print(L[j+(k-j)%l])


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

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

check = [0] * N
time = [-1] * N
check[0] = 1
time[0] = 0
now = 1
t = 1
ex = 0
roop = 0
for i in range(N + 1):
  now = A[now - 1]
  if check[now - 1] == 0:
    check[now - 1] = 1
    time[now - 1] = t
  else:
    ex = time[now - 1]
    roop = t - time[now - 1]
    break
  t += 1
  
if K < t:
  ans = time.index(K) + 1
  print(ans)
else:
  K -= (t - 1)
  K %= roop
  
  ans = time.index(ex + K - 1) + 1
  print(ans)
#print(check, time, roop, t, ex)  




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

check = [0] * N
time = [-1] * N
check[0] = 1
time[0] = 0
now = 1
t = 1
ex = 0
roop = 0
for i in range(N + 1):
  now = A[now - 1]
  if check[now - 1] == 0:
    check[now - 1] = 1
    time[now - 1] = t
  else:
    ex = time[now - 1]
    roop = t - time[now - 1]
    break
  t += 1
  
if K < t:
  ans = time.index(K) + 1
  print(ans)
else:
  K -= (t - 1)
  K %= roop
  if K == 0:
    K = roop
  
  ans = time.index(ex + K - 1) + 1
  print(ans)
  #print(check, time, roop, t, ex)  



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 re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

p = town.index(ans)
b = town[p-1:]
x = len(town)
y = len(b)
q = k-x+y
if y == 0:
    print(town[n-1])
    sys.exit()
if q < 0:
    print(town[k-1])
    sys.exit()
z = q%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])


import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        p = town.index(ans)
        b = town[p:]
        x = len(town)
        y = len(b)
        q = k-x+y
        if y == 0:
            print(town[n-1])
            sys.exit()
        if q < 0:
            print(town[k-1])
            sys.exit()
        z = q%y
        print(b[z-1])
        sys.exit()
        break
    town.append(a[cnt-1])
    cnt = a[cnt-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())) 

ll = [a[0]]

for i in range(1, n):
    cnt = ll[i-1]
    if cnt = a[cnt-1]:
        print(cnt)
        exit()
    if a[cnt-1] == a[0]:
        break
    else:
        ll.append(a[cnt-1])

print(ll[k%3-1])


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

done = [-1]*N
done[0] = 0

tmp = 0
done[0] = 0
for k in range(1, K + 1):
    tmp = A[tmp]
    if done[tmp] >= 0:
        for _ in range((K - done[tmp]) % (k - done[tmp])):
            tmp = A[tmp]
        print(tmp + 1)
        exit()
    else:
        done[tmp] = k
print(tmp + 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()))
a=[p-1 for p in a]
l=[0]
i=a[0]
p=-1
q=-1
for x in range(n):
    if i in set(l):break
    l.append(i)
    i=a[i]
d=-1
for x in range(n):
    if a[x]==i:
        d=x
        break
f=l[:d]
b=l[d:]
print(l,f,b,d)
if len(f)==0 or len(b)==0:
    print(l[k%(n-1)]+1)
    exit()

if k<=d:
    print(l[k]+1)
    exit()

k-=len(f)
print(b[k%len(b)]+1)

n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[False]*(n+1)
visited[1]=True
l=[1]
i=a[0]

for x in range(n):
    if visited[i]:break
    visited[i]=True
    l.append(i)
    i=a[i-1]

d=-1
for x in range(n):
    if l[x]==i:
        d=x
        break

f=l[:d]
b=l[d:]

if len(f)==0:
    print(b[k%len(b)])
    exit()
 
if len(b)==0:
    print(f[k%len(f)])
    exit()

if k<=d:
    print(l[k])
    exit()
 
k-=len(f)
print(b[k%len(b)])

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):
#!/usr/bin/env python3

n, k = map(int, input().split())
A = list(map(int, input().split()))
a = [0]*n
b = A[0]
per = []
tes = -1

if n >= k:
    for _ in range(k-1):
        b = A[b-1]
    print(b)
    exit()


for _ in range(k*2):
    #print(b)
    a[b-1] += 1
    tes += 1
    if 3 in a:
        tes -= len(per)*2
        break
    if 2 in a:
        per.append(b)
    b = A[b-1]

_len = len(per)
print(_len, tes)

ans = (k - tes) % _len

print(per[ans-1])

#!/usr/bin/env python3

n, k = map(int, input().split())
A = list(map(int, input().split()))
a = [0]*n
b = A[0]
per = []
tes = -1

if n >= k:
    for _ in range(k-1):
        b = A[b-1]
    print(b)
    exit()

for _ in range(k+1):
    a[b-1] += 1
    tes += 1
    if 3 in a:
        tes -= len(per)*2
        break
    if 2 in a:
        per.append(b)
    b = A[b-1]

_len = len(per)

ans = (k - tes) % _len

print(per[ans-1])

#!/usr/bin/env python3

N, K = map(int, input().split())
A = [int(a) - 1 for a in input().split()]

if K <= N:
    p = 0
    for _ in range(K):
        p = A[p]
    print(p + 1)
    exit()

p = 0
t = [-1] * N
t[0] = 0
for i in range(1, N):
    p = A[p]
    if t[p] != -1:
        break
    t[p] = i

d = i - t[p]
K -= i
K %= d

for i in range(K):
    p = A[p]
print(p + 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):


def main():
    N, K = map(int, input().split())
    As = list(map(int, input().split()))
    As = [0]+As
    ikisaki = As[1]
    aru = {1}
    itta = [1]
    imaitta = 1
  
    while not ikisaki in aru:
        imaitta = ikisaki
        ikisaki = As[ikisaki]
        itta.append(imaitta)
        aru.add(imaitta)
    ind = itta.index(ikisaki)
    kurikaesi = itta[ind:]
    itidokiri = itta[:ind]
    ans = itidokiri[K-1] if len(itidokiri) <= K else kurikaesi[(K-len(itidokiri))%len(kurikaesi)]


    return itta, itidokiri, kurikaesi, ans

if __name__ == '__main__':
    #start = time.time()
    print(main())
    #elapsed_time = time.time() - start
    



def main():
    N, K = map(int, input().split())
    As = list(map(int, input().split()))
    As = [0]+As
    ikisaki = As[1]
    aru = {1}
    itta = [1]
    imaitta = 1
  
    while not ikisaki in aru:
        imaitta = ikisaki
        ikisaki = As[ikisaki]
        itta.append(imaitta)
        aru.add(imaitta)
    ind = itta.index(ikisaki)
    kurikaesi = itta[ind:]
    itidokiri = itta[:ind]
    ans = itidokiri[K] if len(itidokiri) > K else kurikaesi[(K-len(itidokiri))%len(kurikaesi)]


    return ans

if __name__ == '__main__':
    #start = time.time()
    print(main())
    #elapsed_time = time.time() - start
    

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())) 
b = 0
c = [0]
if n < k:
  k = n
for i in range(k+2):
  b = a[b]-1
  if b in c:
    d = c.index(b)
    e = i-d+1
    k = k - len(c)+e
    c = c[-e:]
    print(c[k%e]+1)
    exit(0)
  else:
    c.append(b)
print(b+1)

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

n, k = map(int, input().split())
a = list(map(int, input().split())) 
b = 0
c = []
for i in range(k):
  b = a[b]-1
  if b in c:
    e = []
    e.append(b)
    d = c.index(b)
    for j in range(i-d-1):
      b = a[b]-1
      e.append(b)
    if c[-i+d:] == e:
      n = len(c[:-i+d])
      k = k-n
      print(e)
      print(e[k%len(e)-1]+1)
      exit(0) 
  else:
    c.append(b)
print(b+1)


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

n, k = map(int, input().split())
a = list(map(int, input().split())) 
b = 0
if k<=n:
  for i in range(k):
    b = a[b]-1
  print(b+1)
  exit(0)
c = [0]
for i in range(n):
  b = a[b]-1
  c.append(b)
d = c[:-1].index(c[-1])
c = c[d:-1]
k = k-d
print(c[k%len(c)]+1)



Verdicts: Wrong Answer

Runtime Error

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()))
town=[0]*n
info=[1]
now=1
while town[now-1]==0:
    town[now-1]+=1
    now=a[now-1]
    info.append(now)
loop_list=list(info[info.index(now):-1])
loop_len=len(loop_list)
before_loop_list=list(info[:info.index(now)])
print(town)
print(info)
if k<len(before_loop_list):
    print(before_loop_list[k])
else:
    print(loop_list[((k-len(before_loop_list))%loop_len)])

n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[0]*n
info=[1]
now=1
while town[now-1]==0:
    town[now-1]+=1
    now=a[now-1]
    info.append(now)
loop_list=list(info[info.index(now):-1])
loop_len=len(loop_list)
before_loop_list=list(info[:info.index(now)])
#print(town)
#print(info)
if k<len(before_loop_list):
    print(before_loop_list[k])
else:
    print(loop_list[((k-len(before_loop_list))%loop_len)])

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):
#abc167c
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

root = [1]
for i in range(10**100):
    next_num = a[root[-1]]
    root.append(next_num)

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

loop_end = n 
loop_start = n-1
while(root[loop_start]!=root[loop_end]):
    loop_start -= 1
period = loop_end - loop_start
k%=period
while k < n:
    k += period
print(root[k])





n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
root = [1]
for i in range(2*n+1000):
    next_num = a[root[-1]]
    root.append(next_num)

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

else: 
    loop_end = n 
    loop_start = n-1
    while(root[loop_start]!=root[loop_end]):
        loop_start -= 1
    period = loop_end - loop_start
    k %= period
    while k < n:
        k += period
    print(root[k])





Verdicts: Time Limit Exceeded

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 detectCycle(x, a):
    start,length = 0, 1
    p1, p2 = a[x], a[a[x]]
    while p1 != p2:
        p1, p2 = a[p1], a[a[p2]]

    p2 = x
    while p1 != p2:
        p1, p2 = a[p1], a[p2]
        start += 1

    p2 = a[p1]
    while p1 != p2:
        p2 = a[p2]
        length += 1
    
    return start,length

n, k = (int(x) for x in input().split())
a = [-1] + [int(x)-1 for x in input().split()]
start, length = detectCycle(1,a)

loc = 1
k -= start
for i in range(start):
    loc = a[loc]

k %= length
for i in range(k):
    loc = a[loc]
print(loc)

def detectCycle(a):
    start, length = 0, 1
    p1, p2 = a[1], a[a[1]]
    while p1 != p2:
        p1, p2 = a[p1], a[a[p2]]
    
    p2 = 1
    while p1 != p2:
        p1, p2 = a[p1], a[p2]
        start += 1
    
    p2 = a[p1]
    while p1 != p2:
        p2 = a[p2]
        length += 1
    
    return start, length

n, k = (int(x) for x in input().split())
a = [-1] + [int(x) for x in input().split()]
start, length = detectCycle(a)

loc = 1
for i in range(min(start,k)):
    loc = a[loc]
k = max(k-start,0)

k %= length
for i in range(k):
    loc = a[loc]
print(loc)

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

L = [0 for i in range(N+1)]
L[1] = 1
x = 1
t = 1
while K>0:
  t += 1
  K -= 1
  x = A[x]
  if L[x] != 0:
    p = t - L[x]
    K %= p
    break
  else:
    L[x] = t
    
while K>0:
  x = A[x]
  K -= 1
print(x)

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

L = [0 for i in range(N+1)]
L[1] = 1
x = 1
t = 1
while K>0:
  t += 1
  K -= 1
  x = A[x-1]
  if L[x] != 0:
    p = t - L[x]
    K %= p
    break
  else:
    L[x] = t
    
while K>0:
  x = A[x-1]
  K -= 1
print(x)


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()))
dp = [0] * (n+1)
B = []
f = 1
c = 0
for e,i in enumerate(A,1):
    if dp[f-1] or i == 1:
        ans = e-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(A[f-1])
        f = A[f-1]
        c+=1
k-= bal
# print(bal,ans)
B = B[bal:ans+1]
# print(dp)
k = (k%ans+1)%ans
print(B[k])

n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [-1] * (n+1)
B = []
f = 1
c = 0
ans = n
bal = 0
while dp[f-1] == -1:
    dp[f-1] =c
    B.append(f)
    f = A[f-1]
    c +=1
t = B.index(f)
if k>t:
    k -= t
    B = B[B.index(f):]
    k%=len(B)
    print(B[k])
else:
    print(B[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):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]

m = 0
b = [1]

c = 1
for i in range(n):
    c = a[c]
    if c not in b:
        b.append(c)
        if i == k-1: print(c)
        sys.exit()
    else:
        break

idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])

# print(b,idx,sn,bn)
kk = (k-sn) % bn
print(b[idx+kk])

from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]

m = 0
b = [1]
visited = [-1 for i in range(n+1)]

c = 1
visited[1] = 1
for i in range(n):
    c = a[c]
    if visited[c] != 1:
        b.append(c)
        visited[c] = 1
        if i == k-1:
            print(c)
            sys.exit()
    else:
        break

idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])

# print(b,idx,sn,bn)
kk = (k-sn) % bn
print(b[idx+kk])

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()))
a = [1] + a
def next(i):
    return a[i]

z = [0] * (n+1)

place = 1
T = 0
interval = 0
if k < n:
    for i in range(k):
        place = next(place)
    print(place);exit()
place = 0
for i in range(n):
    place = next(place)
    if z[place] != 0:
        interval = T - z[place]
        break
    z[place] = T
    T += 1
m = (k-z[place])%interval
for i in range(m):
    place = next(place)
print(place)


import sys
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9 + 7
input = sys.stdin.readline

def inp():
    return int(input())
def inpl():
    return list(map(int,input().split()))
def inpm():
    return map(int,input().split())

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

a=[1] + inpl()
b=[-1] * (n+1); b[1] = 0
place = 1
turn = 0

while b[a[place]] == -1:
    place = a[place]
    turn += 1
    b[place] = turn
r = (k - turn) % (b[place] - b[a[place]] + 1)

if k < turn:
    print(b.index(k))
else:
    for _ in range(r):
        place = a[place]
    print(place)



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()))
 
next_city = 1
visited = []
x = [-1]*n
while x[next_city-1] == -1:
    visited.append(next_city)
    next_city = A[next_city-1]
idx = visited.index(next_city)
visited2 = visited[idx:]
mod = len(visited2)
x = (k%mod-idx)%mod
print(visited2[x])

n, k = map(int, input().split())
A = list(map(int, input().split()))
visited = [0]*n
n_city = 1
while visited[n_city-1] == 0:
    visited[n_city-1] = 1
    n_city = A[n_city-1]
cnt = 1
root = n_city
n_city = A[n_city-1]
while n_city != root:
    n_city = A[n_city-1]
    cnt += 1
to_root = 0
n_city = 1
while n_city != root:
    n_city = A[n_city-1]
    to_root += 1
loop_num = k if k < to_root else (k-to_root)%cnt + to_root
cnt = 0
n_city = 1
while cnt < loop_num:
    n_city = A[n_city-1]
    cnt += 1
print(n_city)

Verdicts: Time Limit Exceeded

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 Counter
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))


visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k -= init_cnt
k %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
for _ in range(k+init_cnt):
    ans = A[ans]
print(ans+1)


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

history = [-1]*n
ans = []
cur = 0
for i in range(n):
    if history[cur] != -1:
        init_cnt = history[cur]
        loop_cnt = i - history[cur]
        break
    history[cur] = i
    ans.append(cur+1)
    cur = A[cur]

loop = k if k <= init_cnt else init_cnt+((k-init_cnt) % loop_cnt)
print(ans[loop])

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())
As = list(map(int, input().split()))
As = [A-1 for A in As]
Vs = [-1]*N
p = 0
p0 = -1
Rs = [p]
Vs[p] = p
for i in range(N):
  n = As[p]
  Rs.append(n)
  if not Vs[n] < 0:
    p0 = Vs[n]
    break
  p = n
  Vs[p] = p
#print(Rs, p0)
if K <= p0:
  r = Rs[K]
else:
  r = Rs[p0+(K-p0)%(len(Rs)-p0+1)]
print(r+1)


N, K = map(int, input().split())
As = list(map(int, input().split()))
As = [A-1 for A in As]
Vs = [False]*N
p = 0
Rs = [p]
Vs[p] = True
for i in range(N+3):
  n = As[p]
  Rs.append(n)
  if Vs[n]:
    break
  p = n
  Vs[p] = True
p0 = Rs.index(Rs[-1])
#print(Rs, p0)
if K <= p0:
  r = Rs[K]
else:
  r = Rs[p0+(K-p0)%(len(Rs)-p0-1)]
print(r+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()))
from collections import deque

ans = deque()
ans.appendleft(1)
count = 1
s = -1

while True:
    if a[count-1] in a[:count-1]:
        s = a[count-1]
        ans = a[:count-1]
        break
    else:
        count = a[count-1]

if s == -1:
    print(ans[k-1])
else:
    t = ans.index(s)
    if t >= k:
        print(ans[k-1])
    else:
        ans = ans[t-1:]
        print(ans[(k-t)%len(ans)])

n, k = map(int,input().split())
a = list(map(int,input().split()))
from collections import deque

ans = deque()
ans.appendleft(1)
count = 1
s = -1
zeros = [0]*n

while True:
    if zeros[a[count-1]] == 1:
        s = a[count-1]
        break
    else:
        ans.append(a[count-1])
        zeros[count-1]=1
        count = a[count-1]

ans = list(ans)
if s == -1:
    print(ans[k])
else:
    t = ans.index(s)
    if t >= k:
        print(ans[k])
    else:
        ans = ans[t:]
        print(ans[(k-t)%len(ans)])


n, k = map(int,input().split())
a = list(map(int,input().split()))
from collections import deque

ans = deque()
ans.appendleft(1)
count = 1
s = -1

while True:
    if a[count-1] in a[:count-1]:
        s = a[count-1]
        ans = a[:count-1]
        break
    else:
        count = a[count-1]

if s == -1:
    print(ans[k-1])
else:
    t = ans.index(s)
    if t >= k:
        print(ans[k-1])
    else:
        ans = ans[t:]
        print(ans[(k-t)%len(ans)])

n, k = map(int,input().split())
a = list(map(int,input().split()))
from collections import deque

ans = deque()
ans.appendleft(1)
count = 1
s = -1
zeros = [0]*n

while True:
    if zeros[a[count-1]-1] == 1:
        s = a[count-1]
        break
    else:
        ans.append(a[count-1])
        zeros[count-1]=1
        count = a[count-1]

ans = list(ans)
if s == -1:
    print(ans[k])
else:
    t = ans.index(s)
    if t >= k:
        print(ans[k])
    else:
        ans = ans[t:]
        print(ans[(k-t)%len(ans)])


Verdicts: Wrong Answer

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  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [0]
#load = []
res = 0
flag = 0
for i in range(k-1):
    #print(nxt)
    if not(nxt in seen):
        seen.append(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        print(root)
        print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

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

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [0]*n
seen[0] = 1
#load = []
res = 0
flag = 0
for i in range(k-1):
    
    #print(nxt)
    if not(nxt in seen and i > n):
        seen[i+1] = nxt
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

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

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [1]
load = set()
load.add(1)
res = 0
flag = 0
for i in range(k-1):
    
    #print(nxt)
    if not(nxt in load):
        seen.append(nxt)
        load.add(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

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

s = [0] * N
p = 1
h = []
while True:
    h.append(p)
    s[p-1] = 1
    p = A[p-1]

    if s[p-1] != 0:
        break
print(h, p)
last = h.index(p)
leng = len(h) - last
rem = (K - last) % leng
print(h[last:][rem])


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

seen = [False] * (n+1)
visited = []
nxt = 1
cnt = 0
while not seen[nxt] and cnt != k:
    seen[nxt] = True
    visited.append(nxt)
    nxt = a[nxt]
    cnt += 1

if cnt == k:
    print(nxt)
    exit()
    
idx = visited.index(nxt)
print(visited[idx:][(k-idx)%(len(visited)-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 = list(map(int,input().split()))

t = 1
tl = [1]
ts = {1}
for i in range(len(a)):

    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]

print(tl)
print(tl2)

if k < len(tl):
    ans = tl[k]
else:
    k = k - (len(tl) - len(tl2))
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

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

t = 1
tl = [1]
ts = {1}
for i in range(len(a)):

    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]


if k < len(tl):
    ans = tl[k]
else:
    k = k - (len(tl) - len(tl2))
    j = k % len(tl2)
    ans = tl2[j]

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):
# import sys
# import math #sqrt,gcd,pi



# import heapq # priolity-queue

# from itertools import product,permutations,\
#     combinations,combinations_with_replacement


# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

def main():
    n,k = readInt()
    a = readInt()
    pos = 0
    visit = [0] * n
    move = []
    roop = []
    while visit[pos]!=2:
        if visit[pos]==0:
            move.append(pos)
        else:
            roop.append(pos)
        visit[pos] += 1
        pos = a[pos] - 1
    
    roop.reverse()
    num = 0
    if len(move)>=k:
        print(move[k]+1)
    else:
        num = (k - (len(move)-len(roop))) % len(roop)
        print(roop[num]+1)
    return

if __name__=='__main__':
    main()


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

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

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 sys

sys.setrecursionlimit(10000000)
N,K = [int(v) for v in input().split()]
A = [int(v) for v in input().split()]
print(N,K)


loop = []

def dfs(now):
    loop.append(now)
    if A[now-1] == 1:
        return
    else:
        dfs(A[now-1])
dfs(1)

print(loop[K%len(loop)])


import sys

sys.setrecursionlimit(10000000)
N,K = [int(v) for v in input().split()]
A = [int(v) for v in input().split()]

loop = []
pre = []
visited = set()

def dfs(index,now):
    global pre
    global loop
    loop.append(now)
    if now in visited:
        for left in range(len(loop)):
            if now == loop[left]:
                break
        pre = loop[:left]
        loop = loop[left:index]
        return
    else:
        visited.add(now)
        dfs(index+1,A[now-1])
dfs(0,1)

if len(pre) > K:
    print(pre[K])
else:
    K-=len(pre)
    print(loop[K%len(loop)])


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] * (n + 1)
now = 1
check = 2 * n
loop_start = 1
while check > 0:
    if visited[now - 1] == 1:
        loop_start = now
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1

visited = [0] * (n + 1)
now = loop_start
check = n
loop = 0
while check > 0:
    if visited[now - 1] == 1:
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1
        loop += 1

now = 1
while k > 0:
    if now == loop_start:
        k %= loop
        if k <= 0:
            break
        else:
            now = a[now - 1]
            k = min(k - 1, 0)
    else:
        now = a[now - 1]
        k -= 1

ans = now
print(ans)


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

visited = [0] * (n + 1)
now = 1
check = 2 * n
loop_start = 1
while check > 0:
    if visited[now - 1] == 1:
        loop_start = now
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1

visited = [0] * (n + 1)
now = loop_start
check = n
loop = 0
while check > 0:
    if visited[now - 1] == 1:
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1
        loop += 1

now = 1
while k > 0:
    if now == loop_start:
        k %= loop
        if k == 0:
            break
    now = a[now - 1]
    k -= 1

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):
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1:
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1:
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) > K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted