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

Submission attempts (in order):
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline

from numba import njit


@njit
def make_my_route(route,A,N):
    pre = 1
    for i in range(N):
        now = A[pre-1]
        temp = np.sum(route == now)
        if temp == 1:
            break
        route = np.append(route,now)
        pre = now
    return (route,now)


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    A = np.array(A)

    route = np.array([1])

    route,now = make_my_route(route,A,N)
    start = int(np.where(route == now)[0])

    if K < start:
        res = route[K]
    elif start+1 != N:
        K -= start
        loop = route[start:]
        
        M = len(loop)
        K %= M
        res = loop[K]
    else:
        res = route[K]

    print(res)



main()


import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline

from numba import njit


@njit
def make_my_route(route,A,N):
    pre = 1
    for i in range(N):
        now = A[pre-1]
        route = np.append(route,now)
        temp = np.sum(route == now)
        if temp == 2:
            break
        pre = now
    return route


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    A = np.array(A)

    route = np.array([1])

    route = make_my_route(route,A,N)
    now = route[-1]
    start = int(np.where(route == now)[0][0])

    if K < start:
        res = route[K]
    elif start+1 != N:
        K -= start
        loop = route[start:-1]
        
        M = len(loop)
        K %= M
        res = loop[K]
    else:
        res = route[K]

    print(res)



main()


import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline

from numba import njit


@njit
def make_my_route(route,A,N):
    pre = 1
    for i in range(N):
        now = A[pre-1]
        temp = np.sum(route == now)
        if temp == 1:
            break
        route = np.append(route,now)
        pre = now
    return (route,now)


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    A = np.array(A)

    route = np.array([1])

    route,now = make_my_route(route,A,N)
    start = int(np.where(route == now)[0])

    if start+1 != N:
        K -= start
        loop = route[start:]
        
        M = len(loop)
        K %= M
        res = loop[K]
    else:
        res = route[K]

    print(res)



main()


# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline

# from numba import njit


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

    s = []
    ord = [-1] * (N+1)
    v = 1

    while ord[v] == -1:
        ord[v] = len(s)
        s.append(v)
        v = A[v-1]

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

    if K < l:
        res = s[K]
    else:
        K -= l
        K %= c
        res = s[l+K]

    print(res)



main()


Verdicts: Time Limit Exceeded

Time Limit Exceeded

Wrong Answer

Accepted