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 d():
    N, K = map(int, input().split())
    # A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[warps[-1] - 1]
        # print('city, warps = {}, {}'.format(city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()


def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[i]
        print('i, city, warps = {}, {}, {}'.format(i, city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()


def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[warps[-1] - 1]
        # print('city, warps = {}, {}'.format(city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()


def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[warps[-1] - 1]
        print('city, warps = {}, {}'.format(city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()


def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    # print('A = {}'.format(A))
    warps = [1]
    w = {}

    for i in range(K):
        city = A[warps[-1] - 1]
        # print('city, warps = {}, {}'.format(city, warps))
        if city in w:
            lsi = warps.index(city)
            Kn = (K - lsi) % (len(warps) - lsi)
            print(warps[lsi + Kn])
            return
        else:
            warps.append(city)
            w[city] = city
    print(warps[-1])


def dd():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    v = []
    w = {}
    now = 0
    for i in range(K):
        print('i, v, w, now = {}, {}, {}, {}'.format(i, v, w, now))
        p = A[now]
        now = p-1
        if p in w:
            j = v.index(p)
            df = i-j
            Kn = (K-i-1) % df
            p = v[j+Kn]
            break
        else:
            v.append(p)
            w[p] = p
    print(p)


##########
if __name__ == "__main__":
    d()


Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Accepted