Problem:


Score: 400 points

Problem Statement

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.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

visited_towns = [1]
for i in range(N):
    next_town  = al[visited_towns[i] - 1]
    if next_town in visited_towns:
        loop_sta = visited_towns.index((next_town)) #　visited_townsにおけるループ開始のインデックス
        loop_end = len(visited_towns) #visited_townsにおけるループの終わりのインデックス
        loop_length = loop_end - loop_sta #繰り返すループの長さ
        amari = (K - loop_sta) % loop_length
        ans = visited_towns[loop_sta + amari]
        print(ans)
        exit()
    
    visited_towns.append(next_town)

print(visited_towns[-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[0]!=2:#二回通っていたらやめる
    if visited[p]==0:#初めて通った
        first.append(p)
    else:#一回通った
        roop.append(p)
    visited[p]+=1#通った跡をつける
    p=a[p]#テレポート
if len(first)>k:#一回もループしていないのなら
    print(first[k])#最初のk番目
else:
    print(roop[(k-(len(first)-len(roop)))%len(roop)])#そうでなければkから一回目のループと二回目のループを引いて二回目のループの回数の余剰

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[0]!=2:#二回通っていたらやめる
    if visited[p]==0:#初めて通った
        first.append(p)
    else:#一回通った
        roop.append(p)
    visited[p]+=1#通った跡をつける
    p=a[p]#テレポート
if len(first)>k:#一回もループしていないのなら
    print(first[k])#最初のk番目
else:
    print(roop[(k-len(first)-len(roop))%len(roop)])#そうでなければkから一回目のループと二回目のループを引いて二回目のループの回数の余剰

Submission status: Wrong Answer

Solution code:
import sys

N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))

visited = [False] * (N+1)
s = 1

while not visited[s]:
    visited[s] = True
    s = A[s]

a = 1

while a != s:
    K -= 1
    if K == 0: 
        print(a)
        sys.exit()
    a = A[a]

visited = [False] * (N+1)
length = 0
while not visited[s]:
    visited[s] = True
    s = A[s]
    length += 1
    
k = K % length
while k:
    s = A[s]
    k -= 1
    
print(s)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

visited_towns = [1]
for i in range(N):
    next_town  = al[visited_towns[i] - 1]
    if next_town in visited_towns:
        loop_sta = visited_towns.index((next_town)) #　visited_townsにおけるループ開始のインデックス
        loop_end = len(visited_towns) #visited_townsにおけるループの終わりのインデックス
        loop_length = loop_end - loop_sta #繰り返すループの長さ
        amari = (K - loop_sta) % loop_length
        ans = visited_towns[loop_sta + amari]
        print(ans)
        exit()
    else:
        visited_towns.append(next_town)

print(visited_towns[-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
ed=[False]*n
go=0
c=0
cc=0
while not ed[go]:#既に通っていたら終了
    ed[go]=True
    go=a[go]
    c+=1
ed=[False]*n
while not ed[go]:#二回目のループは違う可能性があるため6,5,2,5,3,2の場合、6,2,5,3,2,5,3,2,5,...となる
    ed[go]=True
    go=a[go]
    cc+=1#別変数で保存
for i in range(k if c>k else (k-c-cc)%cc):
    go=a[go]
print(go+1)

Submission status: Wrong Answer

Solution code:
import sys

N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))

visited = [False] * (N+1)
s = 1

while not visited[s]:
    visited[s] = True
    s = A[s]

a = 1

while a != s:
    K -= 1
    if K == 0: 
        print('exit')
        print(a)
        sys.exit()
    a = A[a]

visited = [False] * (N+1)
length = 0
while not visited[s]:
    visited[s] = True
    s = A[s]
    length += 1
    
k = K % length
while k:
    s = A[s]
    k -= 1
    
print(s)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
ed=[False]*n
go=0
c=0
cc=0
while not ed[go]:#既に通っていたら終了
    ed[go]=True
    go=a[go]
    c+=1
ed=[False]*n
while not ed[go]:#二回目のループは違う可能性があるため6,5,2,5,3,2の場合、6,2,5,3,2,5,3,2,5,...となる
    ed[go]=True
    go=a[go]
    cc+=1#別変数で保存
for i in range((k-c-cc)%cc):
    go=a[go]
print(go+1)

Submission status: Wrong Answer

Solution code:
N,K=[int(x) for x in input().rstrip().split()]
A=[int(x) for x in input().rstrip().split()]

stack=[]
done=[0 for i in range(N)]
done[0]=0
now=1
cnt=0

for i in range(2*(10**5)):
  done[now-1]+=1
  if done[now-1]==2:
    cnt+=1

  if done[now-1]==3:
    break
  
  stack.append(now)
  now=A[now-1]


ans=K%cnt
print(stack[ans])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

loop_end, loop_start, loop_length = 0,0,0

visited = [1]
for i in range(N):
    next_town = al[visited[i] - 1]
    if visited.count(next_town) == 1 :
        loop_start = visited.index(next_town)
        loop_end = len(visited)
        loop_length = loop_end - loop_start
        break
    else:
        visited.append(next_town)

if K >= loop_end:
    amari = (K - loop_start) % loop_length
    ans = visited[loop_start + amari]
    # print(route)
    print(ans)
    exit()
else:
    ans = visited[-1]
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
done=[0 for _ in range(N)]
done[0]=1
W=[0]

init=0
loop=N

for i in range(N):
    if done[A[i]-1]==2:
        init=W.index(A[i]-1)
        loop=i-init
        break
    elif W[-1]==A[W[-1]]-1:
        init=i
        loop=1
        break
    done[W[-1]]+=1
    W.append(A[W[-1]]-1)

if K>=init:
    print(W[(K-init)%loop+init]+1)
else:
    print(W[K]+1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    visited = [0] * (N+1)
    seq = []

    step = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)):
        #print(visited)
        seq.append(current)
        step += 1
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            break
        current = next_pos

    #print(n1, n2)
    #print(n1-1, ((K-(n1-1))%n2))
    #print(seq)
    if n1+n2<=K:
        print(seq[n1-1+((K-(n1-1))%n2)])
    else:
        print(seq[K-1])

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, K = map(int, input().split())
h = {}
for i, v in enumerate(input().split()):
    h[i+1] = int(v)

s = 1
r = []
k = defaultdict(int)
while True:
    r.append(s)
    k[s] += 1
    if k[s] > 1:
        break
    s = h[s]

for i, v in enumerate(r):
    if r[-1] == v:
        break

S = len(r) - i - 1
s = r[-1-i]
for i in range(K % S):
    s = h[s]

print(s)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
iti = 1
ex_A = []

for k in range(K):
    ex_A.append(iti)
    iti = A[iti-1]
    if iti in ex_A:
        break

nokori = K % len(ex_A)

for i in range(nokori):
    iti = A[iti-1]

print(iti)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

route = [1]
for i in range(N):
    next_town = al[route[i]-1]
    if route.count(next_town) > 1:
        loop_start = route.index(next_town)
        loop_end = len(route)
        loop_length = loop_end - loop_start
        amari = (K - loop_start)%loop_length
        ans = route[loop_start + amari]
        #print(route)
        print(ans)
        exit()
    else:
        route.append(next_town)

ans = route[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [i-1 for i in list(map(int, input().split()))]

l = []
used = [0 for _ in range(n)]

c = 0
flag = True
while 1:
    if not flag:
        l.append(c)
    if used[c] == 1 and l:
        break
    if not flag:
        used[c] = 1
    c = a[c]
    flag = False

p = 0
length = len(l)
b = []
for i in range(length):
    if l[i] == l[-1]:
        b = l[:i]
        l = l[i:-1]
        p = i
        break


if len(b) >= k:
    print(b[k - 1])
else:
    print(l[(k - p - 1) % len(l)] + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k <= n:
    print(visited[k]+1)
else:
    p = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p-1)%loop
    print(p, loop, x)
    print(visited[p+x+1]+1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    next = a[0]
    b.append(next)
    
    s = 0
    e = 0
    for i in range(n-1):
        next = a[next-1]
        if next in b:
            e = i
            s = b.index(next)
            break
        b.append(next)
    ord = b[s:]
    if k > s:
        if e - s != 0:
            ans = (k - s)%(e-s)
        else:
            ans = 0
        print(ord[ans])
    else:
        print(b[k])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k <= n:
    print(visited[-1])
else:
    p = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p-1)%loop
    print(visited[p+x+1]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a] # 入力値を添字に合わせる
path = [] # 通った道を記録。通過済みのところに到達したら記録中止
now = 0
visited = [False] * n

while not visited[now]:
    path.append(now)
    visited[now] = True
    now = a[now]

# sはループの開始地点の添字
# whileループが終わった時点のnowには、堂々巡りの開始地点の添字が入っている
s = path.index(now)
length_loop = len(path) - s

if k < s:
    print(k)
else:
    print(path[(k-s)%length_loop+s]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n + 1):
  x = num[t[-1] - 1]
  if(i == 0):
    t.remove(t[0])
  if(x not in t):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]
    break

list_num = list(set(t) - set(t_test))
if(len(t_test) >= 1):
  print(t_test[(k - len(list_num)) % len(t_test) - 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

route = [1]
for i in range(N):
    next_town = al[route[i]-1]
    if len(route) != len(set(route)):
        loop_start = route.index(next_town)
        loop_end = len(route)
        loop_length = loop_end - loop_start
        amari = (K - loop_start)%loop_length
        ans = route[loop_start + amari]
        #print(route)
        print(ans)
        exit()
    else:
        route.append(next_town)

ans = route[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

route = [1]
for i in range(N):
    next_town = al[route[i]-1]
    if next_town in route:
        loop_start = route.index(next_town)
        loop_end = len(route)
        loop_length = loop_end - loop_start
        if K >= loop_end:
            amari = (K - loop_start)%loop_length
            ans = route[loop_start + amari]
            #print(route)
            print(ans)
            exit()
        else:
            print(next_town)
            break
    else:
        route.append(next_town)

ans = route[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k <= n:
    print(visited[-1])
else:
    p = visited.index(visited[-1])+1
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p)%loop
    print(visited[p+x]+1)

Submission status: Wrong Answer

Solution code:
import sys
import os

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

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

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    pre_d=0
    i=0
    while True:
        i+=1
        pre_d=d
        d=A[d-1]
        # print("i:"+str(i)+" "+str(pre_d)+"->"+str(d))
        if li[d-1]!=-1:
            # print("detect_roop:"+str(i)+"~"+str(li[d-1]))
            break
        else:
            li[d-1]=i

    # print(pre_d)
    # print(d)
    # roop=pre_d-d
    roop=i-li[d-1]
    k=1
    for j in range((K-li[d-1]-1)%roop+li[d-1]+1):
        k=A[k-1]
    print(k)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
al = list(map(int, input().split()))

route = [1]
for i in range(K):
    next_town = al[route[i]-1]
    if next_town in route:
        loop_start = route.index(next_town)
        loop_end = len(route)
        loop_length = loop_end - loop_start
        if K >= loop_end:
            amari = (K - loop_start)%loop_length
            ans = route[loop_start + amari]
            #print(route)
            print(ans)
            exit()
        else:
            ans = route[-1]
            print(ans)
            exit()
    else:
        route.append(next_town)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

# from fractions import gcd
from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]

index = 0
dict_root = { i:0 for i in range(N)}
rootnum = 1

ho = 0

for i in range(K):
    dict_root[index] = rootnum

    if dict_root[A[index]-1] != 0:
        ho = dict_root[A[index]-1] - 1
        break
    rootnum += 1
    index = A[index] - 1

nokori = (K-ho) % (rootnum-ho) + 1

for key, value in dict_root.items():
    if value == nokori+ho:
        print(key+1)
        break

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))

Town = []
#まだ訪れていない町は-1
rec = [-1] * (n+1)
idx = 1
#すべての町を訪れるまで
while (rec[idx] == -1):
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

Exce = rec[idx]
Cycl = len(Town) - Exce

if Cycl > k:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[k+Exce])

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n-1):
    m=0
    for j in range(i+1,n):
        if a[i] == a[j]:
            st =i
            end =j
            m =1
            break
    if m == 1:
        break

if m == 0:
    print(a[k%n+1])
else:
    ans = a[st +(k-st)%n]
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = 1
m = []

for i in range(200000):
    if a[l - 1] in m: #ループが始まったかどうか
        for j in range(len(m)):
            if m[j] == a[l - 1]:
                cnt = j #ループ開始までの端数
                loop = len(m) - cnt #ループの長さ
                break
    else:
        m.append(a[l - 1])
        l = a[l - 1]
        continue
    break


if (k - cnt)%loop == 0:
    l = loop + cnt - 1
else:
    l = (k - cnt)%loop - 1 + cnt

print(m[l])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))

ma = [0]*N
pr = [0]*N
flag = 0
i = 0
ne = A[i]
#print(1)
ma[i] = 1
#print(ne)

while(flag == 0):
  #map判定
  if(ma[ne-1] == 0):
    ma[ne-1] = 1
    pr[ne-1] = i+1
  else:
    #print('flag!')
    flag = 1
    break
  
  #Kに到達(ループなし)
  if(i == K-1):
    #print('fin!')
    flag = 2
    break
  
  #更新
  ne = A[ne-1]
  i+=1
  #print(ne)

#print('---')
if(flag == 1):
  #calc
  start = pr[ne-1]
  #print('start = '+str(start))
  dist = i+1 - start
  #print('dist = '+str(dist))
  tmp = (K-start)%dist + start
  #print('tmp = '+str(tmp))
  
  ne = A[tmp]
  #for x in range(N):
  #  if(pr[x] == tmp):
  #    ne = A[x]

print(ne)
#print(pr)
#print(ma)

Submission status: Wrong Answer

Solution code:
#解説
n, k = map(int, input().split())
A = list(map(int, input().split()))

Town = []
#まだ訪れていない町は-1
rec = [-1] * (n+1)
idx = 1

#すべての町を訪れるまで
while rec[idx] == -1:
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

Cycl = 1
Cycl = len(Town) - rec[idx]
Exce = rec[idx]

if k < Cycl:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[Exce+k])

Submission status: Wrong Answer

Solution code:
import sys
import os

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

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

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    pre_d=0
    i=0
    while True:
        i+=1
        pre_d=d
        d=A[d-1]
        if li[d-1]!=-1:
            break
        else:
            li[d-1]=i

    # print(pre_d)
    # print(d)
    # roop=pre_d-d
    roop=i-li[d-1]
    print(A[(K-li[d-1]-1)%roop+li[d-1]+1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import os

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

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

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    pre_d=0
    i=0
    while True:
        i+=1
        pre_d=d
        d=A[d-1]
        if li[d-1]!=-1:
            break
        else:
            li[d-1]=i

    # print(pre_d)
    # print(d)
    # roop=pre_d-d
    roop=i-li[d-1]
    print(A[(K-li[d-1])%roop+li[d-1]])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))

Town = []
#まだ訪れていない町は-1
rec = [-1] * (n+1)
idx = 1

#すべての町を訪れるまで
while rec[idx] == -1:
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

Cycl = len(Town) - rec[idx]
Exce = rec[idx]

if k < Cycl:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[Exce+k])

Submission status: Wrong Answer

Solution code:
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))

def has_duplicates(seq):
    return len(seq) != len(set(seq))

visited = [0]
flag = False
loopStart = 0
loopCycle = 0

for i in range(n):
    visited.append(a[visited[-1]]-1)
    if has_duplicates(visited):
        flag = True
        loopStart = visited.index(visited[-1])
        loopCycle = len(visited) - 1 - loopStart
        break
    if i == k - 1:
        print(visited[i+1]+1)
        break

if flag:
    print(visited[k % loopCycle]+1)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))

def has_duplicates(seq):
    return len(seq) != len(set(seq))

visited = [0]
flag = False
loopStart = 0
loopCycle = 0

for i in range(n):
    visited.append(a[visited[-1]]-1)
    if has_duplicates(visited):
        flag = True
        loopStart = visited.index([x for x in set(visited) if visited.count(x) > 1][0])
        loopCycle = len(visited) - 1 - loopStart
        break
    if i == k - 1:
        print(i+1)
        break

if flag:
    print(visited[loopStart + (k - loopStart) % loopCycle]+1)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    data = s.split(" ")
    n = int(data[0])
    k = int(data[1])

    s = input()
    data = s.split(" ")
    ts = [int(i) for i in data]

    step_set = set()
    step = []
    nt = ts[0]
    for i in range(k-1):
        nt = ts[nt-1]
        if nt not in step_set:
            step_set.add(nt)
            step.append(nt)
        else:
            break;

    if k % len(step) == len(step) - 1:
        print(step[0])
    else:
        print(step[k%len(step) + 1])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=[0]*n
l1[0]+=1
c=0 #ループに入る前までの移動回数
now=0#現在位置
ne=a[now]-1 #次の移動位置
while True:
    if c==k:
        print(a.index(a[ne])+1)
        exit()
    if l1[ne]==0:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        c+=1
    else:
        now=ne
        ne=a[now]-1
        c+=1
        break

roop_c=0
while True:
    if c+roop_c==k:
        print(a.index(a[now])+1)
        exit()
    if l1[ne]==1:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        roop_c+=1
    else:break

re=(k-c)%roop_c
if re==0:print(a.index(a[now])+1)
else:
    for i in range(re):
        if i==re-1:print(a.index(a[now])+1)
        ne+=-ne+a[ne]-1

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x) for x in stdin.readline().rstrip().split()]
    
    if A[0] == 1:
        print(1)
        exit()
        
    X=1
    Log = [1]
    
    if N >= K:
        for i in range(K):
            X = A[X-1]
            print(X)
    else:
        for i in range(K):
            X = A[X-1]
            if (X in Log) ==True:
                break
            Log += [X]
            #print(Log)
        #print(K - Log.index(X))
        #print(i - Log.index(X)+1)
        K = (K - Log.index(X))%(i - Log.index(X)+1)
        #print(K)
        print(Log[Log.index(X)+K])
            
main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    data = s.split(" ")
    n = int(data[0])
    k = int(data[1])

    s = input()
    data = s.split(" ")
    ts = [int(i) for i in data]

    step = []
    nt = ts[0]
    for i in range(k-1):
        nt = ts[nt-1]
        if nt not in step:
            step.append(nt)
        else:
            break;

    if k % len(step) == len(step) - 1:
        print(step[0])
    else:
        print(step[k%len(step)])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))

citys = [False] * n
visited = []
now = 0
citys[now] = True
visited.append(now)
loop = 0
while True:
    now = a[now] - 1
    if citys[now]:
        loop += 1
        break
    else:
        citys[now] = True
        loop += 1
        visited.append(now)

citys = [False] * n
cycle_list = []
citys[now] = True
cycle_list.append(now)
while True:
    now = a[now] - 1
    if citys[now]:
        break
    else:
        citys[now] = True
        cycle_list.append(now)

cycle = len(cycle_list)
d = loop - cycle
if k <= d:
    print(visited[k])
else:
    k -= d
    k %= cycle
    print(cycle_list[k]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = [-1] * n
f[0] = 0
s1, s2, g1, g2 = 0, 0, 0, 0
j = a[0] - 1
jpre = 0
c = 1
while True:
    if f[j] != -1:
        s1, s2 = f[jpre], jpre
        g1, g2 = f[j], j
        break
    else:
        f[j] = c
        c += 1
        jpre = j
        j = a[j] - 1
ans = (k - g1) % (s1 - g1 + 1) + f[g2]
for i in range(n):
    if f[i] == ans:
        print(i + 1)
        break

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = [-1] * n
f[0] = 0
s1, s2, g1, g2 = 0, 0, 0, 0
j = a[0] - 1
jpre = 0
c = 1
i = 0
while True:
    if i >= n:
        break
    if f[j] != -1:
        s1, s2 = f[jpre], jpre
        g1, g2 = f[j], j
        break
    else:
        f[j] = c
        c += 1
        jpre = j
        j = a[j] - 1
    i += 1
ans = (k - g1) % (s1 - g1 + 1) + f[g2]
for i in range(n):
    if f[i] == ans:
        print(i + 1)
        break

Submission status: Wrong Answer

Solution code:
from sys import stdin


def get_result(data):
    N, K = data[0]
    A = data[1]
    trans_num = [0] * N
    curr_pos = 1
    arival_pos = set()
    for i in range(N):
        trans_num[curr_pos-1] = i
        arival_pos.add(curr_pos)

        # see next
        next_pos = A[curr_pos-1]
        if next_pos in arival_pos:
            loop_start = trans_num[next_pos-1]
            loop_trans_num = i + 1 - loop_start
            break

        # update
        curr_pos = next_pos

    loop = loop_start + (K - loop_start) % loop_trans_num
    curr_pos = 1
    for _ in range(loop):
        next_pos = A[curr_pos-1]
        curr_pos = next_pos
    return next_pos


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))

citys = [False] * n
visited = []
now = 0
citys[now] = True
visited.append(now)
loop = 0
while True:
    now = a[now] - 1
    if citys[now]:
        loop += 1
        break
    else:
        citys[now] = True
        loop += 1
        visited.append(now)

citys = [False] * n
cycle_list = []
citys[now] = True
cycle_list.append(now)
while True:
    now = a[now] - 1
    if citys[now]:
        break
    else:
        citys[now] = True
        cycle_list.append(now)

cycle = len(cycle_list)
d = loop - cycle
if k < d:
    print(visited[k])
else:
    k -= d
    k %= cycle
    print(cycle_list[k]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
Cyc = [0]
i = 0
while Cyc.count(A[i]) == 0:
	Cyc.append(A[i])
	i = A[i]-1

place = Cyc.index(A[i])
p2 = place-1
ans = (K-p2) % (len(Cyc)-place) 
print(Cyc[p2+ans])

Submission status: Wrong Answer

Solution code:
from sys import stdin


def get_result(data):
    N, K = data[0]
    A = data[1]
    trans_num = [0] * N
    loop_start = 0
    loop_num = 0
    curr_pos = 1
    ariival_pos = set()
    for i in range(N):
        trans_num[curr_pos-1] = i
        ariival_pos.add(curr_pos)

        # see next
        next_pos = A[curr_pos-1]
        if next_pos in ariival_pos:
            loop_start = trans_num[next_pos-1]
            loop_num = i + 1 - loop_start
            break

        # update
        curr_pos = next_pos

    loop_mod = (K - loop_start) % loop_num
    loop = loop_start + loop_mod
    curr_pos = 1
    for _ in range(loop):
        next_pos = A[curr_pos-1]
        curr_pos = next_pos
    return next_pos


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = []
now = 0
lim = 2 * 10**5 + 1
for _ in range(n+1000):
    visited.append(now)
    now = a[visited[-1]]-1
    if now in visited:
        break
visited.append(now)

ln1 = len(visited)-1
idx = visited.index(now)
ln2 = len(visited[idx:-1])
ln = (k-ln1)%ln2

print(visited[idx+ln]+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = [*map(lambda x:int(x)-1, input().split())]
index = [-1]*N
path = []
pre = 0
while index[pre] < 0:
    index[pre] = len(path)
    path += [pre]
    pre = A[pre]
one = index[pre]
loops = len(path) - one
if K < one: print(path[K])
else: print(path[(K-one) % loops + one]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
Cyc = [0]
i = 0
while True:
	if Cyc.count(A[i]) > 0:
		place = Cyc.index(A[i])
		break
	Cyc.append(A[i])
	i = A[i]-1

p2 = place-1
ans = (K-p2) % (len(Cyc)-place) 
print(Cyc[p2+ans])

Submission status: Wrong Answer

Solution code:
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
list = [1]
i = 0
num = a[0]
for i in range(3*n):
    list.append(num)
    num = a[num-1]

cnt = collections.Counter(list)
onevalue = [k for k,v in cnt.items() if v == 1]
lstart = len(onevalue)
lroop = len(set(list))-lstart

if k <= lstart:
    num = a[0]
    for i in range(k-1):
        num = a[num-1]
    print(num)
    exit()
else:
    k = (k-lstart)%lroop
    num = a[0]
    if lstart != 0:
        for i in range(lstart-1):
            num = a[num-1]
    if k == 0:
        for i in range(lroop-1):
            num = a[num-1]
        print(num)
        exit()
    for i in range(k-1):
        num = a[num-1]
    print(num)
    exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int, input().split()))
s=1
t=0

lists=[]
listt=[]
for i in range(10**6):
    s=A[s-1]
    listt=lists[:]
    lists.append(s)
    t+=1
    if len(set(lists))==len(set(listt)):
        break
    else:
        continue
k=0
for i in range(1,t):
    if lists[t-1]==lists[t-1-i]:
        k=i
if K<=t-1-k:
  	print (lists[K])
else:
  	print (lists[(K-t)%k+(t-1-k)])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = [*map(lambda x:int(x)-1, input().split())]

index = [-1]*N
pre, path_cnt = 0, 0
for i in range(N):
    if index[pre] >= 0: break
    else:
        index[pre] = i
        path_cnt += 1
    pre = A[pre]

loops = path_cnt - index[pre]
one = index[pre]
k = max(0, K-one) % loops

for _ in range(k): pre = A[pre]
print(pre + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = {}
for i in range(1, N+1):
    visited[i] = False

i2p = {}
p2i = {}
i = 0
cur = 1
while True:
    i2p[i] = cur
    p2i[cur] = i
    visited[cur] = True
    if visited[A[cur]]:
        break
    cur = A[cur]
    i += 1

start_loop = p2i[A[cur]]
end_loop = len(i2p)
num_loop = end_loop - start_loop

if K < num_loop:
    print(i2p[K])
else:
    print(i2p[(K-start_loop)%num_loop+start_loop])

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))

B = [1]
i = 0
count = 0

while True:
  if count == K:
    print(i+1)
    break
  elif A[i] in B:
    x = B.index(A[i])
    a = (K-x) % (len(B)-x)
    print(B[a+x],2)
    break
  else:
    B.append(A[i])
    i = A[i]-1
    count += 1