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_in=map(int, input().split())
a_l=list(map(int, input().split()))

def f(s):
    a = s
    b = s
    i=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        i+=1

    j=1
    a = a_l[0]
    while True:
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        j+=1

    k=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        k+=1

    return j, k

lam, myu = f(a_l[0])
# print(lam,myu)
pos=[]
pos.append(a_l[0])
for i in range(n):
    pos.append(a_l[pos[i]-1])

if k_in<=lam:
    print(pos[lam])
else:
    k_in=(k_in-lam)%myu
    print(pos[lam+k_in-1])

Submission status: Wrong Answer

Solution code:

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

flag = [0 for _ in range(N)]

tmp = 1
i = 1
flag[0] = 1
while(True):
    tmp = A[tmp-1]
    i += 1
    if (flag[tmp-1] != 0):
        break
    flag[tmp-1] = i

pre = flag[tmp-1] - 1
cyc = i - flag[tmp-1]
if (K >= pre):
    index = (K - pre) % cyc
    print(A[flag.index(pre+index)])
else:
    print(A[flag.index(K)])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)
n, k, *a = map(int, open(0).read().split())
F = [0 for _ in range(n)]  # 来た？
T = [None for _ in range(n)]  # いつ来た？
def main(now, time, K):
    time += 1
    if F[now-1]:
        P = T[now-1]
        print(T.index(P + (K - time + 1) % (time - P)) + 1)
        exit()
    F[now-1] = 1
    T[now-1] = time
    main(a[now-1], time, K)
    if time == K:
        print(now)
        exit()

main(1, 0, k)

Submission status: Wrong Answer

Solution code:
# D

N, K = list(map(int, input().split()))
As = list(map(int, input().split()))

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i

if K < loop_end:
    compute(1, K)

div = (K - loop_start) // (loop_end - loop_start)
compute(city, K - loop_start - div * (loop_end - loop_start))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
i_to_a = {i+1: int(a) for i, a in enumerate(input().split())}

seen_d = {i: 0 for i in range(1, n+1)}
seen = []

next_v = 1
loop_start_index = 0
while True:    
    if seen_d[next_v]:
        break
    seen_d[next_v] = 1
    seen.append(next_v)
    next_v = i_to_a[next_v]
loop_start_index = seen.index(next_v)
to_loop = seen[:loop_start_index]
additional = seen[:loop_start_index]
loop = seen[loop_start_index:]

additional_l = len(additional)
loop_l = len(loop)

if k > additional_l:
    s = (k - additional_l) % loop_l
    print(seen[additional_l + s])
else:
    s = k % additional_l
    print(seen[s])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
k += 1
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be,r)
if k<= len(be):
  print(be[k])
else:
  w = k-len(be)-1
  u = (w%len(r))-1
  print(r[u])

Submission status: Wrong Answer

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

isPassed = [False] * N
town = 0
#2度同じところに行きつくまで繰り返す
while True:
    if isPassed[town]==False:
        isPassed[town] = True
        town = A[town]-1
    else:
        loopPoint = town
        break

#ループまでの長さを測る
wayLen = 0
town = 0
while town!=loopPoint:
    town = A[town]-1
    wayLen += 1

#ループ自体の長さを測る
loopLen = 1
town = A[loopPoint] - 1
while town!=loopPoint:
    town = A[town]-1
    loopLen += 1

if K < wayLen:
    town = 0
    for i in range(K):
        town = A[town]-1
    print(town)
else:
    loopMod = (K-wayLen) % loopLen

    for i in range(loopMod):
        town = A[town]-1
    print(town+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
L = list(map(int, input().split()))
loop = [1]
town = 1
for i in range(N):
    if not L[town - 1] in loop:
        loop.append(L[town - 1])
        town = L[town - 1]
    else:
        break
st = loop.index(L[town - 1])
tk = (K - st) % (len(loop) - st)
print(loop[st + tk])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
ls = list(map(int, input().split()))
mls = []

u = 0
t = 1
for i in range(k):
    if i == k - 1:
        print(ls[t - 1])
        break
    else:
        if t not in mls:
            mls.append(t)
            t = ls[t - 1]
        else:
            l = mls.index(t)
            u = 1
            break

t = 1
if u == 1:
    len = len(mls)
    roop = len - l
    honsitu = k % roop
    honsitu += l + 1
    for j in range(honsitu):
        if j == honsitu - 1:
            print(ls[t - 1])
            break
        else:
            t = ls[t - 1]

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = li = list(map(int,input().split()))
s = 1
b = [1]
c = 0
d = 0
e = []

while d == 0:
    s = a[s-1]
    d = b.count(s)
    b.append(s)
    c = c + 1

if d == 0:
    s = s
else:
    for i in range(len(b)):
        if b[i] == s:
            e.append(i)

    f = (k - e[0]) % (e[1]-e[0])

    for j in range (f):
        s = a[s-1]

print(s)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
p=k%n
o=a[0]-1
for i in range(p):
    o=a[o]
print(o)

Submission status: Wrong Answer

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

a = list(map(int, input().split()))
done = [0]*n
stay = 1

for i in range(1, k+1):
    if done[stay-1] != 0:
        break
    done[stay-1] = i
    stay = a[stay-1]
    #print(stay)

nokori = k + 1 - i
loop = i - done[stay-1]

for i in range(nokori%loop):
    stay = a[stay-1]

print(stay)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
count = 2
li2 = [1]
li2.append(li[0])
for i in range(n):
    li2.append(li[li2[i+1]-1])
    count += 1
    if len(set(li2)) != count:
        break
if count == n + 2:
    print(li2[-2])
else:
    print(li2)
    s = li2.index(li2[-1])
    del li2[0:s]
    del li2[-1]
    print(li2[(k-s) % len(li2)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
cnt = 1
loop = 0
cnts = [False] * n
cnts[now - 1] = cnt
li = [now]
while True:
    now = a[now - 1]
    cnt += 1
    li.append(now)
    if cnts[now - 1]:
        loop = (cnt - cnts[now - 1])
        print(li[(cnts[now - 1] - 1) + ((k - (cnts[now - 1] - 1)) % loop)])
        exit()
    cnts[now - 1] = cnt

Submission status: Wrong Answer

Solution code:
inputLine=input()
N=int(inputLine.split(" ")[0])
K=int(inputLine.split(" ")[1])

inputLine=input()
A=inputLine.split(" ")

for i in range(len(A)):
    A[i] = int(A[i])

nowPoint=0
transferHistory = []
while True:
    transferHistory.append(nowPoint)
    nowPoint = A[nowPoint] - 1
    if nowPoint in transferHistory:
        break

for i in range(len(transferHistory)):
    if transferHistory[i] == nowPoint:
        loopLength = len(transferHistory) - i
        break

#print(loopLength)

NumberOfTransfers = K % loopLength
nowPoint=0

for i in range(NumberOfTransfers):
    nowPoint = A[nowPoint] - 1

nowPoint = nowPoint + 1

print(nowPoint)

Submission status: Wrong Answer

Solution code:
import bisect
n, k = map(int, input().split())
a = list(map(int, input().split()))
seen = []
seen_sort = [1]
start = 1

while True:
    tmp = a[start - 1]
    seen.append(start)
    index = bisect.bisect_left(seen_sort, tmp)
    if index >= len(seen_sort) or seen_sort[index] != tmp:
        seen_sort.insert(index, tmp)
    else:
        repeat_index = seen.index(tmp)
        repeat_len = len(seen) - repeat_index
        break
    start = tmp

print(seen[repeat_index + ((k - repeat_index) % repeat_len)])

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
import itertools
import fractions
#import numpy as np
#from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

# ###########################################

N,K=MI()
A=LI()

loopStartPoint=0
current=A[0]
keiyu=[]
keiyu.append(current)
for i in range(N):
    current=A[current-1]
    if current in keiyu:
        loopStartPoint=current
        break
    keiyu.append(current)

loopCount=len(keiyu)-keiyu.index(loopStartPoint)
loopStart=keiyu.index(loopStartPoint)

if K-1<=loopStart:
    print(A[K-1])
else:
    iti = (K-loopStart)%loopCount
    print(keiyu[iti-1+loopStart])

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]

place = (0,)
now = 0
length = 1
while True:
    length += 1
    now = A[now]
    place = place + (now,)
    place_set = set(place)
    if length != len(place_set):
        break
    

t = place.index(place[length-1])
if K <= t:
    print(place[K-1]+1)
    exit()
K -= t
K %= (length-t-1)
print(place[K+t]+1)

Submission status: Wrong Answer

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

if a[0] == 1:
    print(1)
    exit()

new_a = []
tele = 1
city = 1
count = 0
ipponmichi_last = 1
while True:

    # if a[city - 1] == 1:
    #     break
    if city in new_a:
        ipponmichi_last  = city
        break
    else:
        new_a.append(city)
        count += 1
        city = a[city - 1]

lastcitynum  = 0
for i, city in enumerate(new_a):
    if city == ipponmichi_last:
        lastcitynum = i
        break

print(new_a[lastcitynum:][(k-lastcitynum) % (len(new_a) - lastcitynum)])

Submission status: Wrong Answer

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

print(loopcount)
if loopcount==0:
    print(tmpPoint)
    exit()

nokori=(K-startat) % loopcount

print(startat)
print(nokori)
print(loopcount)

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Wrong Answer

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

l = [0]*n
x = [1]
w = 0
for i in range(k):
  l[x[i]-1] += 1
  w = a[x[i]-1]
  x.append(w)
  if l[w-1] == 1:
    break

for i in range(k):
  if x[-1] == x[i]:
    break

x.pop(-1)
    
ans = (k-i)%(len(x)-i)    

print(x[i+ans])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
ans = a[0]
t = 0 
for i in range(k):
    t = a[ans-1]
    ans = a[t-1]
    if ans in s:
        break
    else:
        s.append(ans)
r = k % len(s)
print(s[r-1])

Submission status: Wrong Answer

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

list = [0]
count = 0
next_A = 0
for i in range(len(A)):
    count = count+1
    try:
        idx = list.index(A[next_A]-1)
        idx_max = len(list)
        break
    except:
        list.append(A[next_A] - 1)
        next_A = A[next_A] - 1
print(idx_max)
if len(list) == K:
    print(list[-1]+1)
else:
    fin_idx = (K-idx)%(idx_max -idx)
    print(list[idx+fin_idx]+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    a, i = [0, 1], 0
    for _ in [0] * N:
        a.append(A[i])
        i = A[i] - 1

    if K <= N:
        print(a[K])
        return

    n = defaultdict(list)
    for i, v in enumerate(a):
        if n[v]:
            n[v].append(i)
            break
        n[v].append(i)
    l, r = n[v][0], n[v][1]
    loop = a[l:r]
    K -= r - 1
    n = K % (r - l)
    print(loop[n])


main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    if len(order) >= K:
      print(order[K-1]+1)
    else:
      K -= order.index(M[s])
      order = order[order.index(M[s]):]
      print(K, order, order[K%len(order)]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]

Submission status: Wrong Answer

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

teleport = [-1] * (N+1)
teleport[0] = 1
visited = [(False, -1)] * (N+1)
visited[1] = (True, 0)

for i in range(1, N+1):
    next_town = A[teleport[i-1]-1]
    if visited[next_town][0] == True:
        before = visited[next_town][1]
        break
    teleport[i] = next_town
    visited[next_town] = (True, i)
    
mod = i - before
ans = (K - before) % mod
print(teleport[before + ans])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
loc=1
begin=0
end=0
d={}
for i in range(n+1):
    loc=a[loc-1]
    if loc in d.keys():
        begin=d[loc]
        end=i+1
        break
    d[loc]=i+1
if k<=end:
    end=0
if end==begin+1:
    k=begin
    end=0
if end !=0:
    k=(k-begin+1)%(end-begin)+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
visit=[False for i in range(n)]
visit[0]=True
looplist=[False for i in range(n)]
mark=-1
i=0
num=0
loop=0
count=0
while True:
  num+=1
  i=a[i]
  if mark<0 and visit[i]:
    mark=a[i]
  else:
    visit[i]=True
  if mark>-1 and looplist[i]:
    count=num-loop*2
    break
  elif mark>-1:
    loop+=1
    looplist[i]=True
k-=count
k%=loop
k+=count
ans=0
for i in range(k):
  ans=a[ans]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    k = kwargs["k"]
    A = kwargs["A"]
    now = 0
    cnt = 1
    R = [0 for i in range(n)]
    R[0] = cnt

    while True:
        now = A[now] - 1
        cnt += 1

        if R[now] > 0:
            cnt += (k // (cnt - R[now]) - 1) * (cnt - R[now])

        R[now] = cnt

        if cnt > k:
            break

    res = now + 1

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"], kwargs["k"] = [int(i) for i in cin]

    cin = input().split()
    kwargs["A"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    prev_p = 1
    g = []
    for i in range(len(A)-1):
        g.append(A[prev_p-1])
        prev_p = g[-1]
        
    if K <= N - 1:
        print(g[K-1])
    else:
        prev_ = g[-1]
        new_g = []
        for i in range(len(A)):
            new_g.append(A[prev_-1])
            prev_ = new_g[-1]
        new_g = new_g[:len(new_g)//2]
        
        K_ = (K - N) % len(new_g)
        print(new_g[K_])     


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map( int, input().split())
A = list( map( int, input().split()))
 
n = 1
rooper = -1
L = []
for i in range( max( N, K )):
  n = A[ n - 1 ]
  if n in L:
    rooper = n
    break
  else:
    L.append( n )

if rooper == -1:
  print( n )
else:
  index = L.index( rooper )
  if len( L ) - index > 0:
    a = ( K - index ) % ( len( L ) - index )
  else:
    a = 0
  print( L[ index + a - 1 ] )

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
A = list(map(int, input().split()))
B = [i for i in range(1, N+1)]
d = dict(zip(B, A))
t = [0 for i in range(N)]
d1 = dict(zip(B, t))
temp = 1
cnt = 0
ans = []
for i in range(K):
    d1[d[temp]] = d1.get(d[temp], 0)+1
    if d1.get(d[temp], 0) == 2:
        cnt += 1
        ans.append(d[temp])
        # print(i, d[temp])
    if d1.get(d[temp], 0) == 3:
        break
    temp = d[temp]
    if i == K-1:
        print(d[temp])
        sys.exit()
cnt1 = 0
for i, v in enumerate(d1):
    if v == 1:
        cnt1 += 1
# print(cnt, cnt1, d1, temp, ans)
k = K-1-cnt1
print(ans[k % cnt])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)
def dfs(now):
    global m
    if now in l:
        m = now
        return None
    else:
        l.append(now)
        dfs(a[now - 1])
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = float("inf")
l = []
cycle = float("inf")
ans = []
dfs(a[0])
num = len(l)
for i in range(len(l)):
    if l[i] == m:
        ans = l[i:]
        del l[i:]
        cycle = len(ans)
        break
if k <= num - len(ans):
    print(l[k - 1])
elif k % cycle == 0:
    print(ans[-1])
else:
    print(ans[(k - (len(l) - cycle)) % cycle - 1])

Submission status: Wrong Answer

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

i = 1
rts = []
for _ in range(N):
    rts.append(i)
    i = As[i-1]
    if i in rts:
        break
    
before = rts.index(i)
new = rts[before:]
print(new[(K - before) % (len(rts)-before)])

Submission status: Wrong Answer

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

a.insert(0, 'hoge')
amari = k % n

tmp = 1
if k >= n:
    for i in range(1, n + amari + 1):
        tmp = a[tmp]
        # print(i, tmp)
else:
    for i in range(1, n+1):
        if tmp != n:
            tmp = a[tmp]
        elif tmp == n:
            print(tmp)
            exit()

print(tmp)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [1]
if k > 5*n:
    for i in range(k % n):
        ans.append(a[ans[i] - 1])
    print(a[-1])
else:
    for i in range(k):
        ans.append(a[ans[i] - 1])
    print(a[-2])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]
# print(A)
route = [0]
seen = {0}
here = 0
for _ in range(K):
    here = A[here]

    if here in seen:
        start = route.index(here)
        period = len(route) - start
        # print(period)
        r = (K - start + 1) % period
        if r == 0:
            idx = period + start - 1
        else:
            idx = r + start - 1
        print(route[idx] + 1)
        break
    seen.add(here)
    route.append(here)
else:
    print(route[K])
    # print(route)
# print(period)
# else:
#     print(route[K] + 1)

Submission status: Wrong Answer

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

next = 1
ans=[]
ans.append(1)

for i in range(n):
    if i+1==k:
        print(a[next])
        exit()
    if a[next-1] in ans:
        flag=a[next-1]
        break
    else:
        ans.append(a[next-1])
        next=a[next-1]
cycle = len(ans)-ans.index(flag)
aa=ans.index(flag)
po = (k-ans.index(flag))%cycle

print(ans[po+aa])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[1]
B.append(A[B[0]-1])
i=0
for i in range(1,K):

    B.append(A[B[i]-1])
    
    if(len(set(B))!=i+2):

        blast=B[-1]
        bfirst=B.index(blast)
        roop=len(B)-bfirst
        ans=(K-bfirst+1)%roop
        print(B[ans])
        exit()

Submission status: Wrong Answer

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc167/abc167_d
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    A = list(map(lambda a:a-1, i2nn()))
    B = [0]
    B_rev = [None] * N
    B_rev[0] = 0
    left = 0
    for _ in range(N):
        a = A[B[-1]]
        if B_rev[a] is not None:
            left = B_rev[a]
            break
        B_rev[a] = len(B)
        B.append(a)
    #print(A)
    #print(B)
    #print(left)
    p = (K - left) % (len(B) - left)
    print(B[left + p] + 1)
    return

main()

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
x=set(range(1,n+1))
dic=dict.fromkeys(x,0)
for i in x:
    dic[i]=l[i-1]
l=[-1]*n
curr=1
x=0
while x<=k:
    if l[curr-1]==-1:
        l[curr-1]=x
        x+=1
        continue
    else:
        if l[curr-1]==0:
            l[curr-1]=x
    x=k-((k-x)%l[curr-1])
    curr=dic[curr]
    x+=1
print(dic[curr])

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 = [x-1 for x in list(map(int, input().split()))]

    len_cycle = 0
    start = 0
    already_visit = set([0])
    loop_node = -1
    while True:
        start = A[start]
        len_cycle += 1
        if start in already_visit:
            loop_node = start
            break
        else:
            already_visit.add(start)

    dist_loop_node = 0
    if loop_node != 0:
        start = 0
        while True:
            start = A[start]
            dist_loop_node += 1
            if start == loop_node:
                break
        len_cycle -= dist_loop_node

    len_move = (K - dist_loop_node) % len_cycle
    start = loop_node
    for _ in range(len_move):
        start = A[start]
    print(start+1)
    pass

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline


def DFS(num):
    global time
    if D[num]==0:
        D[num]=time
        time +=1
    for i in M[num]:
        if color[i]=="white":
            color[num]="gray"
            color[i]="gray"
            DFS(i)
        elif color[i]=="gray":
            color[num]="black"
            color[i]="black"
            DFS(i)
        elif color[i]=="black":
            return


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

B=[]
point,cnt=0,0
while cnt<n*2:
    point=A[point]
    B.append(point)
    point -=1
    cnt +=1

M=[set() for _ in range(n+1)]
for i in range(len(B)-1):
    a,b=B[i],B[i+1]
    M[a].add(b)

time=1
color=["white" for _ in range(n+1)]
D=[0 for _ in range(n+1)]
DFS(A[0])
route=[]
for i in range(n+1):
    if color[i]=="black":
        route.append([D[i],i])

C=[]
for i in sorted(route):
    C.append(i[1])

k=k-D[C[0]]+1
print(C[k%len(C)-1])

Submission status: Wrong Answer

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

cur = 1
loop = -1
arr = []
for i in range(n): arr.append([False])

# ループする場所を調べる
while True:
    if arr[cur-1] == True:
        loop = cur
        break
    else:
        arr[cur-1] = True
        cur = a[cur-1]

cur = 1
cnt = 0
loop_cnt = 0
while True:
    cur = a[cur-1]
    cnt += 1
    if cur == loop and loop_cnt > 1: break
    if cur == loop: loop_cnt += 1
    if loop_cnt == 1: cnt = 0
    
v_cnt = 0
if loop == 1:
    v_cnt = 1
else:
    for i in range(n):
        v_cnt += 1
        if a[i] == loop: break

k %= cnt-v_cnt

for i in range(k):
    cur = a[cur-1]

print(cur)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
total=0
b[0]+=1
next=a[0]
while True:
  total+=1
  if b[next-1]==0:
    b[next-1]+=1
    c[next-1]+=total
    next=a[next-1]
  else:
    total1=total
    k-=(c[next-1])
    total-=c[next-1]
    start=next-1
    break
if total1>=k:
  start=0
  for i in range(k):
    start=a[start]-1
  print(start+1)
else:
  k%=total
  for i in range(k):
    start=a[start]-1
  print(start+1)

Submission status: Wrong Answer

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

tmp = a[0]

visit = []
visit.append(str(0))
visit.append(str(tmp-1))

#print('start', 0)
#count = 1
ans_on = 0
#print('tmp', tmp-1)
#print('############################')

if n >= k:
    loop_num = k
else:
    loop_num = n

for i in range(loop_num):
    #print('visited', visit)
    #print('count', count)
    #count += 1
    tmp = a[tmp-1]

    #print('tmp', tmp - 1)

    if str(tmp-1) in visit:
        on_index = visit.index(str(tmp-1))
        visit_len = len(visit)
        loop_list = visit[on_index:]

        #print(i, count)
        k_ = k-i-2
        #if k_ % (visit_len-on_index) != 0:
        k_tmp =  k_ % (visit_len-on_index)
        #else:
        #    k_tmp = k_

        ans = loop_list[k_tmp]
        ans_on = 1
        #print('break########################')
        break

    visit.append(str(tmp-1))

    #print('########################')

if ans_on == 0:
    #print('last not loop', tmp+1)
    print(tmp+1)
else:
    #print('last', int(ans)+1)
    print(int(ans)+1)

Submission status: Wrong Answer

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

for i in range(k) :
    l = a[l]-1 
    count = +1
    if l == 0 :
        x = k%count
        for i in range(x) :
            l = a[l]-1
            
        print(l+1)
        exit()
        
print(l+1)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
li = [1]
c = 1
for i in range(200001):
    cnt += 1
    c = a[c-1]
    if cnt == K:
        print(c)
    if c in li:
        break
    li.append(c)
b = li[li.index(c):]
e = (K-cnt)%len(b)
print(b[e])

Submission status: Wrong Answer

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

nlist = []

h = 1
while not h in nlist:
    nlist.append(h)
#    print(h)
    h = A[h-1]

#print(nlist)
#print(h)
x = nlist.index(h) 
#print(x)
K = K - x
K = K % (len(nlist) - x)
print(nlist[x + K])

Submission status: Wrong Answer

Solution code:
s = list(map(int, input().split()))
N = s[0]
K = s[1]
l = list(map(int, input().split()))

count1 = 1
hd = [1]
now = 1

for i in range(N):
  now = l[now-1]
  if now in hd:
    break
  else:
    hd.append(now)
    count1 += 1

start = now
count2 = 1
for i in range(N):
  now = l[now-1]
  if now == start:
    break
  else:
    count2 += 1

answer = 1

if K <= count1:
  for i in range(K):
    answer = l[answer - 1]
else:
  answer = start
  reduced = K - int(((K - count1)/count2)*count2)
  for i in range(reduced):
    answer = l[answer - 1]

print(answer)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,k=map(int, input().split())
A=[int(i)-1 for i in input().split()]
now=0
loop=[]
dd = defaultdict(int)

while k!=0:
    k-=1
    now = A[now]
    if dd[now]>0:
        break
    else:
        dd[now]+=1
    if k==0:
        print(now)
        exit()
    loop.append(now)
loop = loop[loop.index(now):]
loop.append(now)
t = len(loop)-1
a=(k%t)
while a>0:
    a-=1
    now = A[now]
print(now+1)

Submission status: Wrong Answer

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

lm = [1] * (n + 1)
visited = [False] * (n + 1)
firstVst = [-1] * (n + 1)

cycle = n
start = n - 1

for i in range(n):
    lm[i + 1] = la[lm[i] - 1]
    if not visited[lm[i] - 1]:
        visited[lm[i] - 1] = True
        firstVst[lm[i] - 1] = i
    else:
        cycle = i - firstVst[lm[i] - 1]
        start = min(start, firstVst[lm[i] - 1])

# print(cycle)
# print(start)
# print(firstVst)
# print(lm)

m = (k - start) % cycle

print(lm[start + m])

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(100000000)

stp = -1 
def main():
    n, k = map(int, readline().split())
    a = [int(i)-1 for i in readline().split()]

    order = []
    ex = set()
    def dfs(node):
        global stp
        order.append(node)
        ex.add(node)
        nx = a[node]
        if nx in ex:
            stp = nx
            return
        dfs(nx)
    dfs(0)
    cnt = order.index(stp)
    m = len(order[cnt:])
    idx = (k-cnt)%m
    print(order[cnt:][idx]+1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
o = {i:0 for i in range(1, N+1)}
idx = 1
while True:
    if o[idx]==0:
        o[idx] = 1
        idx = A[idx-1]
    else:
        print(A[(K-sum(o.values())) % len(A[A[A.index(idx)]:])])
        break