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

idx = a[0]
hist = [1]
flag = 0
for i, _ in enumerate(range(k), start=1):
    if idx in hist:
        hist.append(idx)
        flag = 1
        break
    hist.append(idx)
    idx = a[idx-1]
    
if flag:
    l_start2, l_end2 = hist.index(idx), i
    surplus = (k-l_start) % (l_end - l_start)
    print(hist[l_start+surplus])
else:
    print(idx)

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

idx = a[0]
hist = [1]
passed = [0] * n
flag = 0
for i, _ in enumerate(range(k), start=1):
    if passed[idx-1] == 1:
        hist.append(idx)
        passed[idx-1] = 1
        flag = 1
        break
    hist.append(idx)
    passed[idx-1] = 1
    idx = a[idx-1]
    
    
if flag:
    l_start, l_end = hist.index(idx), i
    surplus = (k - l_start) % (l_end - l_start)
    print(hist[l_start + surplus])
else:
    print(hist[-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()))
timestamp = [0]*n
position = 1
period = 0
start=0
for i in range(n):
  if timestamp[position-1] != 0:
    start = timestamp[position-1]
    period = i-timestamp[position-1]
    break
  timestamp[position-1] =i
  position = a[position-1]
print(period,start)


if k <=start:
  position = 1
  for i in range(k-1):
    position = a[position-1]
  print(position)
  
else:
  cycle = (k-start)%period
  for i in range(cycle):
    position = a[position-1]
  print(position) 
  

n, k = map(int, input().split())
a = list(map(int,input().split()))
timestamp = [0]*n
position = 1
period = 0
start=0
for i in range(n*2):
  if timestamp[position-1] != 0:
    start = timestamp[position-1]
    period = i-timestamp[position-1]
    break
  timestamp[position-1] =i
  position = a[position-1]
  

if k <=start:
  position = 1
  for i in range(k):
    position = a[position-1]
  print(position)
  
else:
  cycle = (k-start)%period
  for i in range(cycle):
    position = a[position-1]
  print(position) 
  

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(x) for x in input().split())
A=list(map(int, input().split()))
if K<=N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=1
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
        print(s)
    print(s)

N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=0
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
    print(s)


N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=1
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
    print(s)

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

done = [0]*N
now = 0
cnt = 1
while True:
    if done[now] != 0:
        start = done[now] - 1
        cycle = cnt - start - 1
        break
    else:
        done[now] = cnt
        now = A[now]-1
        cnt += 1
if K>=start:
    K -= start
    K %= cycle
for _ in range(K):
    now = A[now]-1
print(now+1)
print(start, cycle, now, done)

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

done = [0]*N
now = 0
cnt = 1
while True:
    if done[now] != 0:
        start = done[now] - 1
        cycle = cnt - start - 1
        break
    else:
        done[now] = cnt
        now = A[now]-1
        cnt += 1
if K>=start:
    K -= start
    K %= cycle
else:
    now = 0
for _ in range(K):
    now = A[now]-1
print(now+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,*a=map(int,open(0).read().split())
b=[1]
s_b={1}
for i in range(n):
  tmp=a[b[-1]-1]
  if tmp in s_b:
    idx=b.index(tmp)
    break
  else:
    b.append(tmp)
    s_b|={tmp}
mod=len(b[idx:])
d=len(b[:idx])
print(d,(k-d)%mod)
print(b[d+(k-d)%mod])

n,k,*a=map(int,open(0).read().split())
a=[0]+a
ans=1
while k:
  if k&1:
    ans=a[ans]
  a=[a[i] for i in a]
  k>>=1
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):
tem1=input().split()
N = int(tem1[0])
K = int(tem1[1])
tem2 = input().split()
now =1
fact = K%N
for i in range(0,fact):
    now = int(tem2[now-1])
print(now)

tem1=input().split()
N = int(tem1[0])
K = int(tem1[1])
tem2 = input().split()
jud = [0 for i in range(len(tem2))]
now =1
circle=0
for i in range(0,K):
    if jud[now-1]==0:
        jud[now-1]=i
        now = int(tem2[now - 1])
    else:
        circle=i-jud[now-1]
        fact=(K-i-1)%circle
        for j in range(0,fact+1):
            now = int(tem2[now - 1])
        break

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):
import sys
#from collections import defaultdict, deque, Counter

#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

#n = int(stdin.readline().rstrip())
n,k = map(int, stdin.readline().rstrip().split())
A = list(map(int, stdin.readline().rstrip().split()))
#numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
#word = [stdin.readline().rstrip() for _ in range(n)]
#number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]


# find loop
visited = []
visited.append(0)
next = A[0] - 1
loop = 0
for i in range(n-1):
    if next in visited:
        loop = i+1-visited.index(next)
        break
    else:
        visited.append(next)
        next = A[next] - 1

road = len(visited) - loop
print(visited, road, loop)

k = k - road
k = k%loop
print(visited[road + k] + 1)


import sys
#from collections import defaultdict, deque, Counter

#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

#n = int(stdin.readline().rstrip())
n,k = map(int, stdin.readline().rstrip().split())
A = list(map(int, stdin.readline().rstrip().split()))
#numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
#word = [stdin.readline().rstrip() for _ in range(n)]
#number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]


# find loop
visited = [0]*n
visited[0] = 1
next = A[0] - 1
loop = 0
v = 0
for i in range(n):
    if visited[next] != 0:
        loop = i-visited[next]+2
        v = i
        break
    else:
        visited[next]= i+2
        next = A[next] - 1

road =  v+1 - loop

if k <= road:
    print(visited.index(k+1) + 1)
    exit()
k = k - road
k = k%loop
print(visited.index(k+1+road) +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):
def m():
    n, k = map(int, input().split())
    a = [0] + list(map(int, input().split()))
    visited = [[] for _ in range(n + 1)]
    visited[1].append(0)
    cur = 1
    i = 1
    while True:
        cur = a[cur]
        visited[cur].append(i)
        i += 1
        if len(visited[cur]) >= 2:
            break

    if k <= visited[cur][0]:
        now = 1
        for p in range(1, k+1):
            now = a[now]
        return now
    else:
        tmp = k - visited[cur][0]
        MOD = visited[cur][1] - visited[cur][0]
        count = tmp % MOD
        now = 1
        for p in range(1, count+1+tmp):
            now = a[now]
        return now


print(m())


def m():
    n, k = map(int, input().split())
    a = [0] + list(map(int, input().split()))
    visited = [[] for _ in range(n + 1)]
    visited[1].append(0)
    cur = 1
    i = 1
    while True:
        cur = a[cur]
        visited[cur].append(i)
        i += 1
        if len(visited[cur]) >= 2:
            break

    if k <= visited[cur][0]:
        now = 1
        for p in range(1, k+1):
            now = a[now]
        return now
    else:
        tmp = k - visited[cur][0]
        MOD = visited[cur][1] - visited[cur][0]
        count = tmp % MOD
        now = 1
        for p in range(1, count+1+visited[cur][0]):
            now = a[now]
        return now


print(m())


Verdicts: Wrong Answer

Accepted
Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

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

vislist=[1]
visset={1}
loopflag=True
endflag=False
donek=0
ima=1

while loopflag:
    ima=alist[ima-1]
    if ima in visset:
        loopflag=False
        if ima == alist[ima-1]:
            endflag=True
    else:
        vislist.append(ima)
        visset.add(ima)

        donek+=1

        if donek == k:
            loopflag=False
            endflag=True

if endflag:
    print(ima)
    sys.exit()

loopstartindex=vislist.index(ima)
looplength=len(visset)-loopstartindex
if (k+1-loopstartindex)%looplength == 0:
    print(vislist[loopstartindex])
else:
    print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])



import sys
n,k=map(int,input().split())
alist=list(map(int,input().split()))

vislist=[1]
visset={1}
loopflag=True
endflag=False
donek=0
ima=1

while loopflag:
    ima=alist[ima-1]
    if ima in visset:
        loopflag=False
        if ima == alist[ima-1]:
            endflag=True
    else:
        vislist.append(ima)
        visset.add(ima)

        donek+=1

        if donek == k:
            loopflag=False
            endflag=True

if endflag:
    print(ima)
    sys.exit()

loopstartindex=vislist.index(ima)
looplength=len(visset)-loopstartindex
if (k+1-loopstartindex)%looplength == 0:
    print(vislist[-1])
else:
    print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-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):
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]

    done = [0] * N
    step = -1
    start = 0
    current = 1
    between = 0

    while True:
        if done[current - 1] > 0:
            start = done[current - 1] - 1
            between = step - done[current - 1] + 1
            break
        step += 1
        done[current - 1] = step
        current = As[current - 1]
    print(done)

    if K <= current:
        print(done.index(K) + 1)
    else:
        res = (K - start) % between
        print(done.index(res) + 1)


if __name__ == "__main__":
    main()


from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]

    done = [0] * N
    step = -1
    start = 0
    current = 1
    between = 0

    while True:
        if done[current - 1] > 0:
            start = done[current - 1] - 1
            between = step - done[current - 1] + 1
            break
        step += 1
        done[current - 1] = step
        current = As[current - 1]

    if K <= step:
        print(done.index(K) + 1)
    else:
        res = (K - start) % between
        if res == 0:
            res = between
        print(done.index(res + start) + 1)


if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Accepted
Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

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

import sys


input = sys.stdin.readline
#n = int(input())
#n,k = map(int,input().split())
#A = list(map(int,input().split()))
#s=input()
#a = input()
n,k= map(int,input().split())
A  = [0]
A += list(map(int,input().split()))
B = [-1 for _ in range(n+1)]
B[1] = 0
now =1
cnt = 0
cic = 0
for i in range(1,n+1):
    now = A[now]
    cnt +=1
    if B[now] ==-1:
        B[now] = cnt
    else:
        cic = cnt - B[now]
        road = B[now] 
        break
print(B,road,cic,cnt)
if k < road:
    print(B.index(k)+1) 
else:
    print(B.index(((k-road)%cic+road)))

n,k= map(int,input().split())
A  = [0]
A += list(map(int,input().split()))
B = [-1 for _ in range(n+1)]
B[1] = 0
now =1
cnt = 0
cic = 0
for i in range(1,n+1):
    now = A[now]
    cnt +=1
    if B[now] ==-1:
        B[now] = cnt
    else:
        cic = cnt - B[now]
        road = B[now] 
        break

if k <= road:
    print(B.index(k)) 
else:
    print(B.index(((k-road)%cic+road)))

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 numba import njit
import numpy as np


def main(N, K, A):
    d = np.zeros(N,np.int32)
    d=d-1
    p = 0
    for i in range(K):
        if d[p-1] == 0:
            d[p-1] = i
            p = A[p-1]
        else:
            p = A[p-1]
            loop = i-d[p-1]
            nokori = (K-d[p-1]) % loop
            for _ in range(nokori):
                p = A[p-1]
            return p
    return p


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


from numba import njit
import numpy as np


def main(N, K, A):
    d = np.zeros(N, np.int32)
    d = d-1
    p = 1
    for i in range(K):
        if d[p-1] == -1:
            d[p-1] = i
            p = A[p-1]
        else:
            p = A[p-1]
            loop = i-d[p-1]+1
            nokori = (K-d[p-1]) % loop
            for _ in range(nokori):
                p = A[p-1]
            return p
    return p


N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = main(N, K, A)
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):
n,k = map(int,input().split())
a = list(map(int,input().split()))
num = [a[0]]
while True:
    for i in range(len(num)-1):
        if num[i] == num[-1]:
            break
    num.append(a[num[-1]])
index = index(a[-1])
lstart = len(a[:index-1])
lroop = len(a[index])
number = (k-lstart)%lroop
for i in range(lroop-1):
    number = a[number-1]
    print(number)
print(number)

import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
list = [1]
i = 0
num = a[0]
for i in range(3*n):
    list.append(num)
    num = a[num-1]

cnt = collections.Counter(list)
onevalue = [k for k,v in cnt.items() if v == 1]
lstart = len(onevalue)
lroop = len(set(list))-lstart

if k <= lstart:
    num = a[0]
    for i in range(k-1):
        num = a[num-1]
    print(num)
    exit()
else:
    k = (k-lstart)%lroop
    num = a[0]
    if lstart > 0:
        for i in range(lstart-1):
            num = a[num-1]
    for i in range(k):
        num = a[num-1]
    print(num)
    exit()

import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
list = [1]
i = 0
num = a[0]
for i in range(3*n):
    list.append(num)
    num = a[num-1]

cnt = collections.Counter(list)
onevalue = [k for k,v in cnt.items() if v == 1]
lstart = len(onevalue)
lroop = len(set(list))-lstart

if k <= lstart:
    num = a[0]
    for i in range(k-1):
        num = a[num-1]
    print(num)
    exit()
elif lstart == 0:
    k = (k-lstart)%lroop
    num = 1
    for i in range(k):
        num = a[num-1]
    print(num)
    exit()
else:
    k = (k-lstart)%lroop
    num = a[0]
    if lstart > 0:
        for i in range(lstart-1):
            num = a[num-1]
    for i in range(k):
        num = a[num-1]
    print(num)
    exit()

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=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(len(town) <= k):
    town.append(a[town[-1]-1])
    cnt+=1
    index=len(town)-1
    if cnt != index:
        loop = cnt - index
        town.pop(-1)
        break

if k<index:
    print(town[k])
else:
    k-=index
    k%=loop
    print(town[index+k])


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

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

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

Verdicts: Runtime Error

Accepted
Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

Submission attempts (in order):
while True:
    k -= 1
    cnt += 1
    if k == 0:
        break
    dir = a[dir - 1]
    if hash_a[dir - 1] == 0 or hash_a[dir - 1] == 1 or hash_a[dir - 1] == 2:
        hash_a[dir - 1] += 1
        if hash_a[dir - 1] == 2:
            two += 1
    elif hash_a[dir - 1] == 3:
        k = (k + cnt) - (cnt - (two * 2))
        k %= two
        hash_a = [-k for _ in range(n)]
print(dir)

n, k = map(int, input().split())
a = list(map(int, input().split()))
dir = a[0]
hash_a = [0 for _ in range(n)]
hash_a[0] = 1
cnt = 0
two = 0
while True:
    k -= 1
    cnt += 1
    if k == 0:
        break
    dir = a[dir - 1]
    if hash_a[dir - 1] == 0 or hash_a[dir - 1] == 1 or hash_a[dir - 1] == 2:
        hash_a[dir - 1] += 1
        if hash_a[dir - 1] == 2:
            two += 1
    elif hash_a[dir - 1] == 3:
        k = (k + cnt) - (cnt - (two * 2))
        k %= two
        hash_a = [-k for _ in range(n)]
print(dir)

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):
# = int(input())
N, K = map(int, input().split())
A = list(map(int, input().split()))


next = 1
List = [-1]*N

for i in range(K):
    if List[next-1] == -1:
        List[next-1] = i
        next = A[next-1]

    else:
        loop = i - List[next-1]
        afterloop = (K-(i-loop+1))%loop
        print(List.index(i - loop + afterloop)+1)
        exit()

print(next)


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


next = 1
List = [-1]*N

for i in range(K):
    if List[next-1] == -1:
        List[next-1] = i
        next = A[next-1]

    else:
        loop = i - List[next-1]
        afterloop = (K-(i-loop+1))%loop
        print(List.index(i - loop + afterloop))
        exit()

print(next)


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


next = 1
List = [-1]*N

for i in range(K):
    if List[next-1] == -1:
        List[next-1] = i
        next = A[next-1]

    else:
        loop = i - List[next-1]
        afterloop = (K-(i-loop))%loop
        print(List.index((i-loop) + afterloop)+1)
        exit()

print(next)


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 solve(N,K,A):
    idx = [-1 for _ in range(len(A))]
    now = 0
    period = 0
    bias = 0
    tmp = []
    for i in range(K+1):
        tmp.append(now+1)
        if idx[now] == -1:
            idx[now] = i
        else:
            period = i - idx[now]
            cycle = tmp[-period-1:-1]
            bias = i - period
            break

    if K <= bias:
        now = 0
        for _ in range(K):
            now = A[now] - 1
        print(now + 1)
    else:
        K -= bias
        K = K % period
        print(cycle[K])

if __name__=="__main__":
    # import random
    # import numpy as np
    # for N in range(2, 10):
    #     A = list(range(1, N+1))
    
    #     for K in range(1, 10**18):
    #         solve(N, K, A)
    N,K = [int(a) for a in input().split()]
    A = [int(a) for a in input().split()]
    solve(N, K, A)

def solve(N, K, A):
    idx = [-1 for _ in range(len(A))]
    now = 0
    period = 0
    bias = 0
    tmp = []
    found = False
    i = 0
    while not found:
        tmp.append(now+1)
        if idx[now] == -1:
            idx[now] = i
        else:
            period = i - idx[now]
            cycle = tmp[-period-1:-1]
            bias = i - period
            break
        now = A[now]-1
        i += 1

    if K <= bias:
        now = 0
        for _ in range(K):
            now = A[now] - 1
        print(now + 1)
    else:
        K -= bias
        K = K % period
        print(cycle[K])

if __name__=="__main__":
    N,K = [int(a) for a in input().split()]
    A = [int(a) for a in input().split()]
    solve(N, K, A)

Verdicts: Wrong Answer

Accepted
Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

Submission attempts (in order):
N,K = map(int,input().split())
sys.setrecursionlimit(20000000)
A = list(map(int,input().split()))
adj_list=[0]*N
for i in range(N):
    adj_list[i]=(A[i])
k=0
node = 1
c=[0]*N
count=1
flag=0
ROOP=[]
roopstart=0
while k<K:
    k+=1
    c[node-1]+=1
    node = adj_list[node-1]
    if c[node-1] ==1:
        if flag ==0:
            roopstart = k
            flag=1
        ROOP.append(node)
    if c[node-1] ==2:
        break
num = (K-len(ROOP)-roopstart) % (len(ROOP))
print(ROOP[num])

N,K = map(int,input().split())
sys.setrecursionlimit(20000000)
A = list(map(int,input().split()))
adj_list=[0]*N
for i in range(N):
    adj_list[i]=(A[i])
k=0
node = 1
c=[0]*N
count=1
flag=0
ROOP=[]
roopstart=0
while k<K:
    k+=1
    c[node-1]+=1
    node = adj_list[node-1]
    if c[node-1] ==1:
        if flag ==0:
            roopstart = k
            flag=1
        ROOP.append(node)
    if c[node-1] ==2:
        break
num = (K-len(ROOP)-roopstart) % (len(ROOP))
print(ROOP[num])

N,K = map(int,input().split())
A = list(map(int,input().split()))
adj_list=[0]*N
for i in range(N):
    adj_list[i]=(A[i])
k=0
node = 1
c=[0]*N
count=1
flag=0
ROOP=[]
roopstart=0
while k<K:
    k+=1
    c[node-1]+=1
    node = adj_list[node-1]
    if c[node-1] ==1:
        if flag ==0:
            roopstart = k
            flag=1
        ROOP.append(node)
    if c[node-1] ==2:
        break
l = len(ROOP)
if l != 0:
    num = (K-l-roopstart) % (l)
    print(ROOP[num])
else:
    print(node)

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())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    K -= order.index(M[s])
    order = order[order.index(M[s]):]
    print(order[K%len(order)-1]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]



N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    if len(order) >= K:
      print(order[K-1]+1)
    else:
      K -= order.index(M[s])
      order = order[order.index(M[s]):]
      print(K, order, order[K%len(order)]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]



N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    if len(order) > K:
      print(order[K]+1)
    else:
      K -= order.index(M[s])
      order = order[order.index(M[s]):]
      print(order[(K+1)%len(order)-1]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]



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())
p=list(map(int,input().split()))
a=[0]+p
flag=0
bef=1
town=[1]
town_set=set(town)
cnt=0
while flag==0 and cnt<k:
    if not a[bef] in town_set:
        town.append(a[bef])
        town_set.add(a[bef])
        bef=a[bef]
    else:
        flag=1
        memo=a[bef]
        print(cnt)
    cnt+=1
if flag==0:
    print(bef)
    exit()
for j in range(len(town)):
    if town[j]==memo:
        num=j
x=len(town)-num
y=(k-num)%x
print(town[num+y])


n,k=map(int,input().split())
p=list(map(int,input().split()))
a=[0]+p
flag=0
bef=1
town=[1]
town_set=set(town)
cnt=0
while flag==0 and cnt<k:
    if not a[bef] in town_set:
        town.append(a[bef])
        town_set.add(a[bef])
        bef=a[bef]
    else:
        flag=1
        memo=a[bef]
        break
    cnt+=1
if cnt==k:
    print(bef)
    exit()
for j in range(len(town)):
    if town[j]==memo:
        num=j
        break
x=len(town)-num
y=(k-num)%x
print(town[num+y])


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

l = [0]*n
s = set()
x = 0

for i in range(n):
    machi = a[x]
    if machi in s:
        hajime = l.index(machi)
        owari = i-1
        repeat = l[hajime:owari+1]
        print(l)
        break
    x = machi-1
    l[i] = machi
    s.add(machi)
else:
    amari = k%n
    print(l[amari-1])
    exit()

if k < owari:
    print(l[k-1])
else:
    amari = (k-hajime)%len(repeat)
    if hajime + amari == 0:
        print(repeat[-1])
    else:
        print(l[hajime+amari-1])


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

l = [0]*n
s = set()
x = 0

for i in range(n):
    machi = a[x]
    if machi in s:
        hajime = l.index(machi)
        repeat = l[hajime:i]
        break
    x = machi-1
    l[i] = machi
    s.add(machi)
else:
    amari = k%n
    print(l[amari-1])
    exit()

if k < hajime:
    print(l[k-1])
else:
    amari = (k-hajime)%len(repeat)
    print(repeat[amari-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):
from collections import deque

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

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0 and len(root)<k+1):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

print(root)

if len(root)==k+1:
  print(root[-1]+1)
else:
  fidx = root.index(root[-1])
  lidx = len(root)-1
  lp   = lidx-fidx

  cnt = (k-fidx)%lp

  if cnt==0:
    print(root[-1]+1)
  else:
    que.append(root[-1])
    for i in range(cnt):
      v = que.pop()
      que.append(li[v])
    print(que.pop()+1)

from collections import deque

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

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0 and len(root)<k+1):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

if len(root)==k+1:
  print(root[-1]+1)
else:
  fidx = root.index(root[-1])
  lidx = len(root)-1
  lp   = lidx-fidx

  cnt = (k-fidx)%lp

  if cnt==0:
    print(root[-1]+1)
  else:
    que.append(root[-1])
    for i in range(cnt):
      v = que.pop()
      que.append(li[v])
    print(que.pop()+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):
            
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

D=60
MAX_N=2000005
to=[[0]*MAX_N for i in range(D)]

to[0][1:N+1]=A

for i in range(D-1):
    for j in range(1,N+1):
        to[i+1][j]=to[i][to[i][j]]

v=1
for i in range(D-1,-1,-1):
    l = 1<<i
    if(l<=K):
        v=to[i][v]
        K-=l

print(v)



            
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

city_l=[0]
city_l.append(1)
city_c=[0]*(N+1)
city_c[1]+=1
n_city=A[0]
flag=True
r_start=-1
r_end=-1
while(flag):
    city_c[n_city]+=1
    city_l.append(n_city)
    if(city_c[n_city]>1):
        r_start=city_l.index(n_city)
        r_end=len(city_l)-1
        city_l.append(A[n_city-1])
        flag=False
    n_city=A[n_city-1]

if(K<=r_end):
    print(A[city_l[K]-1])


else:
    K2=K-r_end+1
    K2=K2%(r_end-r_start)
    if(K2==0):
       print(city_l[r_end])
    else:
       print(city_l[r_start+K2])


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):
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [-1]*(N+1)
path = []
at = 1
count = 0
while True:
    if visited[at] != -1:
        loop = count-visited[at]
        if K < count: 
            print(path[K]+1) 
        else:
            print(path[(K-visited[at])%loop+visited[at]]+1)
        break
    visited[at] = count
    count += 1
    path.append(at)
    at = A[at]

n, k = map(int, input().split())
q = list(map(int, input().split()))
v = [-1]*(n+1)
p = []
a = 1
c = 0
while 1:
    if v[a] != -1:
        l = c-v[a]
        if k < c:
            print(p[k])
        else:
            print(p[(k-v[a])%l+v[a]])
        break
    v[a] = c
    c += 1
    p.append(a)
    a = q[a]

n, k = map(int, input().split())
q = list(map(int, input().split()))
v = [-1]*(n+1)
p = []
a = 1
c = 0
while 1:
    if v[a] != -1:
        l = c-v[a]
        if k < c:
            print(p[k])
        else:
            print(p[(k-v[a])%l+v[a]])
        break
    v[a] = c
    c += 1
    p.append(a)
    a = q[a-1]

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

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

now = a[0]
path.append(now)

for i in range(1, k+1):
    now = a[now-1]
    if now in path:
        now = a[a.index(now) + (k-i)%(len(path) - a.index(now) - 1)]
        break
    path.append(now)

print(now)


n, k = map(int, input().split())
a = list(map(int, input().split()))
alr = [0 for i in range(n)]
now = 1
path = [now]

for i in range(k):
    now = a[now-1]
    # if now in path:
    #     now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
    #     break
    if alr[now-1] != 0:
        now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
        break
    path.append(now)
    alr[now-1] += 1

print(now)


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(" "))
routs = {}
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i + 1] = tereporter[i]
present_location = 1
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if present_location = 1:
    if n == 0 or K % n == 0:
      break
    for i in range(K % n):
      present_location = routs[present_location]
    break

print(present_location)


N, K = map(int, input().split(" "))
routs = {}
location = [0 for i in range(N + 1)]
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i] = tereporter[i] - 1
present_location = 0
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if location[present_location] == 0:
    location[present_location] = n
  else:
  loop = n - location[present_location]
  if (K - location[present_location]) % loop == 0:
    break
  else:
    for i in range((K - location[present_location]) % loop):
      present_location = routs[present_location]
	break
print(present_location + 1)



N, K = map(int, input().split())
routs = {}
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i] = tereporter[i]
present_location = 1
for i in tange(K):
  present_location = routs[present_location]
print(present_location)

from sys import exit
n, K = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
done = [-1 for _ in range(n)]
tmp = 0
done[0] = 0
for k in range(1, K + 1):
    tmp = A[tmp]
    if done[tmp] >= 0:
        for i 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

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

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

visitcount = dict()
n_point = list()

x = 1
count = 0
while(x not in set(visitcount)):
  visitcount[x] = count
  n_point.append(x)
  x = A[x-1]
  count += 1
  
period = count - visitcount[x]

if (K < visitcount[x]):
  print(n_point[K])
else:
  y = (K-visitcount[x]) % period
  print(n_point[visitcount[x] + y])
 



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

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

visitset = set()
visitcount = dict()
n_point = dict()

x = 1
count = 0
while(x not in visitset):
  visitset.add(x)
  visitcount[x] = count
  n_point[count] = x
  x = A[x-1]
  count += 1


period = count - visitcount[x]

if (K < visitcount[x]):
  print(n_point[K])
else:
  y = (K-visitcount[x]) % period
  print(n_point[visitcount[x] + y])



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

ans=[]

j=0 
while A[j] not in ans:
    ans.append(A[j])
    j=A[j]-1
ans.append(A[j])

st=ans.index(A[j])
ans=ans[st:-1]
K-=st
l=len(ans)
print(ans)
print(ans[K%l-1])

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

ans=[]
judge={}
j=0 
while judge[A[j]]!=2:
    ans.append(A[j])
    if A[j] not in judge.keys():
        judge[A[j]]=0
    judge[A[j]]+=1
    j=A[j]-1
ans.append(A[j])

st=ans.index(A[j])
ans=ans[st:-1]
K-=st
l=len(ans)
#print(ans)
print(ans[K%l-1])

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

ans=[]
judge={}
j=0 
while A[j] not in judge.keys() or judge[A[j]]!=2:
    ans.append(A[j])
    if A[j] not in judge.keys():
        judge[A[j]]=0
    judge[A[j]]+=1
    j=A[j]-1

st=ans.index(A[j])

if st<K:
    ans=ans[st:]
    K-=st
    l=len(ans)
    #print(ans)
    print(ans[K%l-1])
else:
    print(ans[K-1])

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()))
b=[1]
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in b:
        b.append(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break

e=b.index(e)
print(b,e)
b=b[e:c+1]
f=c+1-e
if 1 not in b:
    ans=(e+2+k)%f
    print(b[ans])
else:
    ans=k%f
    print(b[ans])


n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba=set()
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break

e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if 1 not in b:
    ans=(k%f+e-1)%f
    print(b[ans])
else:
    ans=k%f
    print(b[ans])


n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
bc=b
b=b[e:c+1]
f=c+1-e
if c>k:
    if c-1>=k:

        print(bc[k])
        exit()
if 1 not in b:
    # print(b,f)
    ans=(k-e)%f
    print(b[ans])
else:

    ans=k%f
    print(b[ans])

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()))
r = 1
root = [1]
for _ in range(n):
    roo = A[r-1]
    root.append(roo)
for i in range(n):
    if roo == root[i]:
        roopcount = n-i-1
        break
if k <= n:
    print(root[k-1])
else:
    s = k%n
    print(root[i])

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

f = 1
first = []
for _ in range(n):
    num = A[f-1]
    first.append(num)
    f = num

for i in range(n):
    if num == first[i]:
        roopcount = n-i-1 #3
        break

if k <= n:
    print(first[k-1])
else:
    s = (k-(n-roopcount))%roopcount
    print(first[i+s])

Verdicts: Wrong Answer

Accepted