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
input = sys.stdin.readline
def main():
    n,k = map(int,input().split())
    l = list(map(int,input().split()))
    position = 1

    town = ''
    stayed = [1]+[0]*n
    stayed_c = [0]*n
    for i in range(n):
        position = l[position-1]
        if not position in stayed:
            stayed[position-1] = position
            stayed_c[position-1] = i+1
        else:
            c = i+1 - stayed_c[position-1]
            g = stayed_c[position-1]
            town = position
            break
    m = (k -g) % c 
    position = town
    for i in range(m):
        position = l[position-1]
    print(position) 
    

n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        g = stayed_c[position-1]
        town = position
        break
m = abs((k % c) - g)
position = town
for i in range(m):
    position = l[position-1]
print(position) 
# print(m,g,k)




n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        town = position
        break
m = k % c
position = 1
for i in range(m):
    position = l[position-1]

print(position) 

n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        k = stayed_c[position-1]
        town = position
        break
m = (k % c) - k
position = town
for i in range(m):
    position = l[position-1]
print(position) 


import sys
input = sys.stdin.readline
def main():
    n,k = map(int,input().split())
    l = list(map(int,input().split()))
    roop = [1]
    stayed = [0]*n
    for i in range(n):
        next_city = l[roop[-1]-1]
        if stayed[next_city-1]:
            endindex = i
            for j,r in enumerate(roop):
                if r == next_city:
                    startindex = j
            break
        else:
            roop.append(next_city)
            stayed[next_city-1] = 1

    if k < startindex:
        print(roop[k])
    else:
        dis = endindex - startindex + 1
        a = (k - startindex) % dis
        print(roop[a + startindex])
main()

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted