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(" "))
towns = [int(i)-1 for i in input().split(" ")]
loopCount = 0
nowPos = 0
posList = [0]
while not(nowPos==0 and loopCount!=0):
    nowPos = towns[nowPos]
    posList.append(nowPos)
    for i in range(0,len(posList)+1):
        for j in range(i+1,len(posList)):
            length = j - i
            for l in range(len(posList)-length):
                if posList[l: l + length] == posList[i: j] and i != l:
                    remain = K % loopCount - i
                    nowPos = posList[j-1]
                    for k in range(remain):
                        nowPos = towns[nowPos]
                    print(nowPos + 1)
                    exit()
    loopCount = loopCount + 1
    if loopCount == K:
        print(nowPos + 1)
        exit()
    

N, K = map(int, input().split(" "))
towns = [int(i)-1 for i in input().split(" ")]
loopCount = 0
nowPos = 0
posList = [0]
while not(nowPos==0 and loopCount!=0):
    nowPos = towns[nowPos]
    posList.append(nowPos)
    for i in range(0,len(posList)+1):
        for j in range(i+1,len(posList)):
            length = j - i
            for l in range(len(posList)-length):
                if posList[l: l + length] == posList[i: j] and i != l:
                    remain = K % loopCount - i
                    nowPos = posList[j-1]
                    for k in range(remain):
                        nowPos = towns[nowPos]
                    print(nowPos + 1)
                    exit()
    loopCount = loopCount + 1
    if loopCount == K:
        print(nowPos + 1)
        exit()
print(nowPos + 1)
    

N, K = map(int, input().split())
towns = [int(i) for i in input().split()]
arrivedList = [1]
loopCount = 0
previous = 0
for i in range(len(towns)):
    idx = arrivedList[len(arrivedList) - 1] - 1
    current = towns[idx]
    if arrivedList.count(current) == 0:
        arrivedList.append(current)
    else:
        previous = len(arrivedList) - (len(arrivedList)-arrivedList.index(current))
        loopCount = len(arrivedList)-arrivedList.index(current)
        print(previous,current, loopCount, arrivedList)
        break
    if i == K - 1:
        print(arrivedList[len(arrivedList) - 1])
        exit()
remain = K % loopCount - previous
loopList = arrivedList[previous: previous + loopCount]
print(loopList[remain])


N, K = map(int, input().split())
towns = [int(i) for i in input().split()]
arrivedList = [1]
for i in range(N * 2):
    arrivedList.append(towns[arrivedList[-1] - 1])
if K <= N:
    print(arrivedList[K])
    exit()

loop_end = N
loop_start = N - 1
while (arrivedList[loop_end] != arrivedList[loop_start]):
    loop_start -= 1

loop_len = loop_end - loop_start


K = K % loop_len
while K < N:
    K += loop_len

print(arrivedList[K])


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted