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

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i + visited[town]) + visited[town]))
            exit()
    print(town)

if __name__ == "__main__":
    main()

import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i + visited[town]) + visited[town] ))
            exit()
    print(town)

if __name__ == "__main__":
    main()

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

    towns = [1]
    i = 0
    while a[i] not in towns:
        towns.append(a[i])
        i = a[i] - 1
    ti = towns.index(a[i]) + 1
    if ti > k:
        print(towns[ti - 1])
    else:
        print(towns[(k - ti)%(len(towns) - ti) + ti])

if __name__ == "__main__":
    main()

import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i + visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()

import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i - visited[town]) + visited[town]))
            exit()
    print(town)

if __name__ == "__main__":
    main()

import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i - visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Accepted