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:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = [int(s) - 1 for s in input().split()]

    pos = 0
    seen = {0}
    visited = [-1] * N
    visited[0] = 0
    path = [0]
    for i in range(1, N + 1):
        npos = A[pos]
        if i == K:
            print(npos + 1)
        if npos in seen:
            loop_start = visited[npos]
            cycle = i - visited[npos]
            break

        seen.add(npos)
        visited[npos] = i
        path.append(npos)
        pos = npos

    L = loop_start + (K - loop_start) % cycle
    pos = path[L]

    print(pos + 1)

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
DEBUG = False

import time

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

visited = [False] * N
next = A[0]
visited[0] = True
K -= 1
count = 1
while not visited[next] and K >= 0:
    if DEBUG:
        print("next: {}".format(next))
        print("visited: {}".format(visited))
        time.sleep(0.1)
    visited[next] = True
    next = A[next]
    K -= 1
    count += 1

K -= 1
count += 1
now = next

K %= count

if DEBUG:
    print("now: {}, count: {}, K: {}".format(now, count, K))

while K > 0:
    if DEBUG:
        print("now: {}".format(now))
    now = A[now]
    K -= 1

print(A[now]+1)

Submission status: Wrong Answer

Solution code:
def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    pos=set()
    pos.add(0)
    i=0
    traversal=[0]
    start=0
    nodes=[1]
    while(start<K):
        position=A[i]-1
        if position in pos:
            break
        traversal.append(position)
        pos.add(position)
        start+=1
        nodes.append(A[i])
        i=position
    if start==K:
        print(A[traversal[-1]])
    else:
        ind=traversal.index(position)
        interval=len(traversal)-ind
        print(nodes[ind+((K-start+2)%interval)])
if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

#インデックスを1オリジンに
A.insert(0,-1)

accessed_node = set([1])
accessed_node_list=[1]
loop_root = []
loop_madeno_size = 0
loop_size=0
t=1
#ループ検出
for _ in range(1,len(A)):
  if not A[t] in accessed_node:
    accessed_node.add(A[t])
    accessed_node_list.append(A[t])
    #次の場所を決める
    t=A[t]
  else:
    #ループ路を確定する
    loop_start_index=accessed_node_list.index(A[t])
    loop_root = accessed_node_list[loop_start_index:]
    loop_madeno_size = loop_start_index
    loop_size=len(loop_root)
    break
#print(accessed_node)
#print(loop_madeno_size)
K -= loop_madeno_size
print(loop_root[K%loop_size])

Submission status: Wrong Answer

Solution code:
from math import *
from collections import *
from itertools import *
from bisect import *
from operator import *
def io():
    return map(int,input().split())
def op():
    return list(map(int,input().split()))
def o():
    return int(input())
def kl(con,x=0):
    if x==0:print('Yes') if con else print('No')
    elif x==1:print('yes') if con else print('no')
    elif x==2:print('YES') if con else print('NO')

MOD =    1000000007
MAX=2147483647
MIN=-2147483648
p=input
l,l1,l2,l3=[],[],[],[]
# for _ in range(o()):
n,k=io()
l=op()
x=y=0
i=0
fr=[0]*(200000+2)
while 1:
    if fr[l[i]]==0:
        l1.append(l[i])
        fr[l[i]]=1
        i=l[i]-1
    else:
        y=l1.index(l[i])
        x=len(l1)-y
        break
if k<=len(l1):
    print(l1[k-1])
else:
    k-=y
    k%=x
    print(l1[y+k-1])

Submission status: Wrong Answer

Solution code:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
#      .      '                    Udit Gupta @luctivud         ,              
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                            ##     ##  #######  # #  ######
                            ##     ##  ##   ##  ###    ##
                            ##     ##  ##    #  # #    ##
                            #########  #######  # #    ##
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
import math as mt
# sys.setrecursionlimit(10**6)

def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end="tst, ")

MOD = int(1e9+7); BABYMOD = 998244353;
# ################################ HELPER PROGRAMS USED ###################################
# ################################## MAIN STARTS HERE #####################################
# for _testcases_ in range(int(input())):
n, ki = get_ints()
li = get_array()
pos = [0] * (n+1)
vis = [0] * (n+1)
d = {}
curr = 1
k = 0
while True:
    # print(curr, end="->")
    vis[curr] += 1
    if vis[curr] == 2:
        break
    pos[curr] = k
    d[k] = curr
    curr = li[curr-1]
    k += 1

period = k - pos[curr]
ini = pos[curr]
ki -= ini
# ki -=1
left = ki % period
# print(vis, pos, d)
# print(period, ini, k)
print(d[(ini + left)%period])


# #########################################################################################
'''
THE LOGIC AND APPROACH IS BY ME @luctivud.
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
4 5
3 2 4 1
(I Own the code if no link is provided here or I may have missed mentioning it)
: DO NOT PLAGIARISE.
'''

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(8388608)
# 町: N個
# 移動回数: K
N, K = map(int, input().split())

# 次の町番号
A = list(map(int, input().split()))

flg = [False]* len(A)   # 既にきたことある町

townLoop = []
def warp(k, nowTown):
    # 移動回数が残っている
    if K+1 != k:
        #print(k, nowTown, A[nowTown-1])
        
        # まだ来たことのない町
        if flg[nowTown-1] == False:
            flg[nowTown-1] = True
            townLoop.append(A[nowTown-1])
            warp(k+1, A[nowTown-1] )
        else:
            return 
    else:
        return 
        #print(A[nowTown-1])

warp(0, A[0])


#print(townLoop)
if len(townLoop) > 2:
    if townLoop[0] == townLoop[len(townLoop)-1]:
        townLoop.pop(len(townLoop)-1)

#//print((K-1) // len(townLoop))
tmp = K-1
while(True):
    tmp = tmp // len(townLoop)
    if tmp <= len(townLoop):
        #print(tmp)
        break
print(townLoop[tmp-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
L= list(map(int, input().split()))
S=[0]*n
S[0]=1
p=0
cnt=0
for i in range(n):
	if L[p] in S:
		cnt+=1
		S[i+1]=L[p]
		break
	else:
		cnt+=1
		S[i+1]=L[p]
		p=L[p]-1

cnt2=0
for i in range(n):
	if S[i]!=L[p]:
		cnt2+=1
	else:
		S2=S[i:]
		break
loop=len(S)-cnt2-1


if cnt>=k:
	print(S[k])
else:
	print(S2[(k-cnt)%loop])

Submission status: Wrong Answer

Solution code:
import sys

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

history =[1]
place = A[0]

for i in range(n):
  history.append(place)
  place = A[(int(place)-1)]
  if place == 1:
    amari = k % len(history)
    print(history[amari])
    sys.exit()
    
print(A[A[0]-1])

Submission status: Wrong Answer

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

"""
def distance(current, l):
  if A[current] == 1:
    return l
  else:
    return distance(A[current]-1, l+1)
"""

def distance(current):
  start = -1
  l = 0
  first = False
  while True:
    #print(f'{current=}')
    if not first and current in experience:
      start = current
      first = True
      l += 1
      current = A[current]-1
    elif first and current == start:
      break
    elif not first:
      experience.append(current)
      current = A[current]-1
    else:
      l += 1
      current = A[current]-1
  return l

"""
def go_next(current, rest):
  if rest == 0:
    return current
  else:
    return go_next(A[current]-1, rest-1)
"""
def go_next(current, rest):
  while True:
    if rest == 0:
      break
    else:
      current = A[current]-1
      rest -= 1      
  return current

l = distance(0)
#print(f'{l=}')
K = K % l
#print(f'{K=}')
ans = go_next(0, K)
print(ans+1)

Submission status: Wrong Answer

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

TeList = list(map(int, input().split()))
Tmp = 0
TmpList = [0]
SetTmp = {0, }
for _ in range(N):
    Tmp = TeList[Tmp] - 1
    if Tmp in SetTmp:
        ResultList = TmpList[TmpList.index(Tmp):]
        break
    else:
        SetTmp.add(Tmp)
        TmpList.append(Tmp)

FirstStep = len(TmpList) - len(ResultList)

if K < len(ResultList):
    print(TmpList[K] + 1)
else:
    print(ResultList[(K - FirstStep) % (len(TmpList) - FirstStep)] + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
end = k % n - 1
prev = a[end]-1
print(a[prev])

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np
n, k = map(int, input().split())
An = list(map(int, input().split()))
root = deque([1])
check = np.zeros(n, np.int)
i = 0
dup = 1
cnt = 1
for _ in range(n):
    temp = An[i]
    if check[temp-1] == 1:
        dup = temp
        break
    root.append(temp)
    check[temp-1] = 1
    i = temp - 1
    cnt += 1
index = root.index(dup)
print(root[(k-index) % (cnt-index) + index] if index != cnt-1 else dup)

Submission status: Wrong Answer

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

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.insert(0, i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[:len(hist) - last[pos]]



preCircle -= cnt

cycle = list(reversed(cycle))

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [0]*N
B[0] = 1
num1 = 10**6
num2 = 0
x = A[0]
x2 = 0
for i in range(len(A)*2):

    if B[x-1] == 0:
        B[x-1] = 1
    elif B[x-1] == 1:
        num1 = min(i+1,num1)
        B[x-1] = 2
    elif B[x-1] == 2:
        num2 = i+2
        x2 = x-1
        break
    else:
        None
    x = A[x-1]
    #print(x)
    #print(B)
#print("num1,num2",num1,num2)
x = A[0]
if num1 >= K:
    for j in range(K-1):
        x = A[x-1]
    print(x)
elif num1 == N-1:
    print("elif")
    x = A[x-1]
    for j in range((K-num1)%num2):
        x = A[x - 1]
    print(x)
else:
    x = A[x-1]
    for j in range((K-num1)%num2-1):
        x = A[x-1]
    print(x)

'''
8 6
2 3 2 3 2 3 2 3
'''

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
point = [int(i) for i in input().split()]
lst = [1]
i = 1
while True:
    i = point[i - 1]
    if i not in lst:
        lst.append(i)
    else:
        break
for j in range(len(lst)):
    if lst[j] == i:
        break
la = lst[:j]
re = lst[j:]
x = K - len(la)
y = x % len(re)
print(re[y])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
list=[l[0]]
num=0
while True:
  if l[list[num]-1] in set(list):
    break
  list.append(l[list[num]-1])
  num+=1
y=list.index(l[list[num]-1])
x=len(list)-y
ans=list[y:]
print(ans[(k-y)%x-1])

Submission status: Wrong Answer

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

i = 1
dct = {}
for j in range(N):
    dct[j+1] = 0
rts = []
for _ in range(N):
    if dct[i] == 1:
        break
    else:
        dct[i]+=1
        rts.append(i)
        i = As[i-1]
before = rts.index(i)
new = rts[before:]
print(int(new[(K - before) % (len(rts)-before)]))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split()) #int
a = input().replace(" ","") #str
chokudai = "1" #str
fp = "1" #str
telenum = 0 #int
while True:
  chokudai = a[int(chokudai)-1]
  telenum += 1
  rooph = fp.find(chokudai) #int
  if rooph != -1:
    rooplen = len(fp)-rooph
    break
  else:
    fp = fp+chokudai
print(fp[rooph+(k-rooph)%rooplen])

Submission status: Wrong Answer

Solution code:
def main():
	n, k = [int(x) for x in input().split(" ")]
	a = [int(x) for x in input().split(" ")]
	d = {}
	pos = 0
	step = 0
	cycle = False
	way = []
	while step < k and cycle == False:
		if pos in d:
			cycle = True
			step += 1
		else:
			d[pos] = step
			way.append(a[pos])
			pos = a[pos] - 1
			step += 1

	k -= step
	if k > 0:
		print(k)
		cycle = way[d[pos]:step]
		x = k % len(cycle)
		print(cycle[x])
	else:
		print(a[pos])

main()

Submission status: Wrong Answer

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

def teleport(a):
    idx = 1
    for _ in range(k%len(a)):
        now = a[idx-1]
        idx = now
    return now

print(teleport(a))

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = tuple(map(int, input().split()))
    l = []
    t = 1
    for i in range(n):
        l.append(t)
        if a[t - 1] in l:
            k -= (i+1)
            break
        t = a[t - 1]
    i = l.index(a[t - 1])
    k = k % (len(l) - i)
    print(l[k + i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

loop = [1, a[0]]
ind = a[0]
for i in range(n):
    loop.append(a[ind-1])
    ind = a[ind-1]

cnt = 0
loop_num = 0
for i in range(len(loop)):
    if loop[i] in loop[:i]:
        cnt = loop[:i].index(loop[i])
        loop_num = i - cnt
        break

print(loop[(k - cnt) % (loop_num)+2])

Submission status: Wrong Answer

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

loop = []
x = 1

for i in range(k):
    if x not in loop:
        x = c[x] - 1
        loop.append(x)
        kc -= 1
    else:
        loop.append(x)
        loops = x
        kc -= 1 
        break

s = loop.index(loops)

y = len(loop[s:])

ans = kc % y

print(c[s+1 + ans])

Submission status: Wrong Answer

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

mod = [0]
flag = [0 for i in range(n)]
flag[0] = 1
idx = A[0]

for i in range(n):
    if flag[idx] == 1:
        idx = mod.index(idx)
        break
    flag[idx] = 1
    mod.append(idx)
    idx = A[idx]

r = len(mod) - idx
k -= idx
mod = mod[idx:]

print(mod[k % r] + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
way = A[0]
List = [0 for n in range(N)]
List[way-1] += 1
count = 0
string = f"{way}"
#print(f"string:{string}")
#print(f"way:{way}")

for i in range(10**7):
    way = A[way-1]
    List[way-1] += 1 
    count += 1
    #print(f"way:{way}")
    #print(f"count:{count}")
    #print(List)
    if max(List) == 3:
        break
    else:
        string += f"{way}"
        #print(f"string:{string}")
        #print()

#print(List)
#print()
new_list = [i for i in List if i == 1]
length = len(new_list)
n_list = [j for j in List if j >= 2]
#print(new_list)
#print(n_list)
amari = (K - length)% len(n_list)

print(int(n_list[amari]))

Submission status: Wrong Answer

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

route = []
route = [1]
appearance = set()
index = 1
for _ in range(n):
    if index in appearance:
        break
    appearance.add(index)
    next_city = A[index - 1]
    route.append(next_city)
    index = next_city

# 繰り返し個数
duplicate = route[route.index(index):len(route) - 1]
print(duplicate[(k - route.index(index)) % (len(duplicate))])

Submission status: Wrong Answer

Solution code:
import sys

n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
m=[0]*n
pla=a[0]
for i in range(k-1):
    if a[pla-1] in m:
        q=i-m.index(a[pla-1])
        del a[0:m.index(a[pla-1])]
        p=len(a)%q
        print(a[p])
        sys.exit()
    else:
        m[i]=a[i]
        pla=a[pla-1]
print(pla)

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
city=0

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3

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

while (i <= K):
  i = i+1
  cur = A[cur-1]
  if(loop == 0):
    if (cur in list):
#      loop = len(list)
#      print("loop", loop)
      loop = len(list) - list.index(cur)
      if (K > 100):
        K= K - ((K // loop))*loop
    list.append(cur)
#  print(cur)

print(cur)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.insert(0,0)
root=[]
current=1
i=0
while True:
    if i==K:
        ans=current
        break
    else:
        root.append(current)
        current=A[current]
        i+=1
        if current in root:
            index=root.index(current)
            roop=len(root)-index
            x=(K-index)%roop
            ans=root[index+x-1]
            break

#print(index)
#print(root)
print(ans)

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count <= n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    index = ans_list[-1]
    ans_list = ans_list[index:]
    k = k - index + 1
    k = k % len(ans_list)
    print(ans_list[k] + 1)

Submission status: Wrong Answer

Solution code:
def main():
    
    IN = [int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    N = IN[0]
    K = IN[1]

    loop = []
    target = 1
    mark = [False] * N
    print(mark)
    mark[0] = True
    while True:
        target = A[target -1]
        loop.append(target)
        if mark[target-1]:
            break
        mark[target-1] = True 

    looper = loop
    si = len(loop)-1
    if loop[si] != 1:
        K = K - loop[si] -1
        looper = loop[loop[si]-1:si]
        
    ind = (len(looper) % K) - 1
    print(A[ind])
        



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

N = None
K = None
A = None

def check(start):
    global N,K,A
    slow = A[start]
    fast = A[A[start]]
    while slow != fast:
        slow = A[slow]
        fast = A[A[fast]]

    slow = start
    while slow != fast:
        slow = A[slow]
        fast = A[fast]

    cycle_start = slow

    cycle_length = 1
    slow = A[slow]
    fast = A[A[fast]]
    while slow != fast:
        slow = A[slow]
        fast = A[A[fast]]
        cycle_length += 1

    return cycle_start, cycle_length

def main():
    global N,K,A
    N,K = map(int, input().split())
    A = list(map(int, input().split()))
    A = [""] + A
    start,length = check(1)
    current = 1
    count = 0
    while current != start:
        current = A[current]
        count += 1
    K = (K-count)%length
    for _ in range(K):
        current = A[current] 
    return current

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]

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

tmp = [0]*(N+1)
cnt = 1
zyunkai = 0
idou = A[0]
tmp[idou] = 1
A = [0] + A
ans = [0]*(N+1)
ans[1] = A[1]

cook = 0
for i in range(N):
   
    cnt += 1
    ans[cnt] = A[idou]
    idou = A[idou]
    if visited[idou]:
        zyunkai = cnt - tmp[idou]
        cook = tmp[idou]
        K -= tmp[idou]
        break
    tmp[idou] = cnt
    visited[idou] = True

ooo = K%zyunkai
#print(zyunkai,cook)
ans = ans[ooo+cook]
print(ans)

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count <= n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    index = ans_list[-1]
    ans_list = ans_list[index:]
    k = k - index + 1
    k = k % len(ans_list)
    print(ans_list[k - 1] + 1)

Submission status: Wrong Answer

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

loc = 0
history = []

for current_idx in range(K):
    loc = A[loc]
    if loc in history:
        hist_idx = history.index(loc)
        syuuki = current_idx - hist_idx
        loc = history[hist_idx + ((K - current_idx) % syuuki)]
        break
    history.append(loc)
        
print(loc+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
c = [0] * n
nex = 1
ans = 0
ane = 1
d=0
for i in range(n):
    if c[nex - 1] == 1:
        break
    c[nex - 1] += 1
    ane = nex
    nex = a[nex - 1]
    cnt += 1


cnt2 = 0
nex2 = nex
anex = nex
for i in range(n):
    if c[nex2 - 1] == 2:
        break
    c[nex2 - 1] += 1
    anex = nex2
    nex2 = a[nex2 - 1]
    cnt2 += 1
#print(nex, cnt)
#print(nex2, cnt2)
if k < cnt + cnt2:
    d = k // cnt
    d = k - cnt * d
    #print(123,d)
    if d==0:
        print(a[ane - 1])
        exit(0)
    for j in range(d):
        #print(ans)
        ans = a[nex - 1]
        nex = ans
    print(ans)
    exit(0)
d = k - cnt
d2 = d // cnt2
d2 = d - cnt2 * d2

#print(d2)
if d2 == 0:
    print(a[anex - 1])
    exit(0)
for i in range(d2):
    #print(ans)
    ans = a[nex2 - 1]
    nex2 = ans
print(ans)

Submission status: Wrong Answer

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

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1

if i == n+1:
    print(place[-1])
else:
    print(replis[(k-st-len(replis))%len(replis)])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 標準入力の取得
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))


def main() -> None:
    # 求解処理
    city = 1
    cities = [city]
    remain_count = 0
    loop = []
    loop_count = 0
    while True:
        city = A[city - 1]
        if city in set(cities):
            remain_count = cities.index(city)
            loop = cities[remain_count:]
            loop_count = len(loop)
            break
        else:
            cities.append(city)

    result = loop[(K - remain_count) % loop_count]

    # 結果出力
    print(result)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

#loop check
gone = []
pos = 0
i = 0
while pos not in gone:
    gone.append(pos)
    pos = A[pos] - 1
    i += 1
    if (pos in gone):
        loop_flag = True
        loop_pos = gone.index(pos)
        loop_len = len(gone)-loop_pos



if (loop_flag):
    pos = loop_pos
    for i in range((K - loop_pos) % loop_len):
        pos = A[pos]-1
    
else:
    pos = 0
    for i in range(K):
        pos = A[pos]-1

print(pos+1)

Submission status: Wrong Answer

Solution code:
N, K  = map(int, input().split())
A = [int(x) for x in input().split()]
count = [0] * N
t = A[0]
count[0] = 1
for i in range(N*2): 
    j = A[t-1]
    t = j
    if count[t-1] > 0:
        K = K - count[t-1] + 1
        K = K % (i+2 - count[t-1] + 1)
        break
    count[t-1] = i+2
t = A[0]
j = 0
for i in range(K):
    j = A[t-1]
    t = A[j-1]
print(t)

Submission status: Wrong Answer

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

index = [0] * N
city = 1
check1, check2 = 0, 0

for i in range(0,K):
    index[city - 1] += 1    
    city = A[city - 1]
    if 2 in index:
        route=[i]
        check1 = i      
        break
        
for j in range(i+1,K):
    index[city - 1] += 1    
    city = A[city - 1]
    if 3 in index:
        check2 = j
        route.insert(0,route.pop(len(route)-1))
        break
    route.append(city)
    
if check2 == 0:
    print(city)
else:
    length = check2 - check1
    loop = (K - check1) % length
    print(route[loop])

Submission status: Wrong Answer

Solution code:
(N,K) = input().split()
N = int(N)
K = int(K)
tel = map(int,input().split())
tel = list(tel)
now = 1
cou = [0]*N
cou[0] = cou[0]+1
print (cou)
for i in range (K):
    now = tel[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
import sys

n,k = (int(i) for i in input().split())

a = [int(i)-1 for i in input().split()]

hantei = [True]*(n+1)
otozure = [None]*(n+1)
mokuteki = a[0]
moto = k
kazoe = 0
otozure[0] = 0
while k>0:
        
    if hantei[mokuteki] == False: #二週目突入
       # print("break\nmokuteki= {}\nk = {}".format(mokuteki,k))
        otozure[kazoe+1] = mokuteki
        saigo = otozure
        break

    else:
        hantei[mokuteki]= False
        otozure[kazoe+1] = mokuteki#今いるところを記憶
       # print(otozure[kazoe])
        mokuteki = a[mokuteki]#次に行くところ
        kazoe+=1
    k-=1
else:
    print(otozure[kazoe-1])
    sys.exit()
i = 0
while True:
    if otozure[kazoe+1] == otozure[kazoe-i]:
        break
    i+=1


nokori = k%(i+1)
#print(nokori)
i=0
while otozure[i] != mokuteki:
    i+=1

if a[0]==0:
    print("1")
else:
    print(otozure[i-1+nokori]+1)

Submission status: Wrong Answer

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

amap = {}
for i, dst  in enumerate(a_list):
    amap[i + 1] = dst

print(amap)

prev = 1
curr = None
for k in range(1, K + 1):
    curr = amap[prev]
    prev = curr
print(curr)

Submission status: Wrong Answer

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

    table = [0] * n
    j = 1
    i = 1
    while True:
        if table[j - 1] != 0:
            break
        table[j - 1] = i
        j = a[j - 1]
        i += 1

    t_max = max(table)
    ini_roop = a[table.index(t_max)] - 1
    roop = t_max - ini_roop
    
    if roop == 0:
        print(ini_roop + 1)
        return

    k_ini = k - table[ini_roop] + 1

    amari = k_ini % roop
    if ini_roop + amari + 1 in table:
        print(table.index(ini_roop + amari + 1) + 1)

    else:
        print(ini_roop + 1)
        
resolve()

Submission status: Wrong Answer

Solution code:
import numpy as np

# n = int(input()) 
n, k = map(int, input().split())
a = list(map(int, input().split()))
# b = [int(input()) for i in range(n)]
# c = [list(map(int, input().split())) for i in range(n)]

# # a = [3, 2, 4, 1]
# a = [6, 5, 2, 5, 3, 2]
# k = 34488
# print(a)

# k = 8

i = 0
l = 1
l_ref = []

while not l in l_ref or i == 0:
  l_ref.append(l)
  i += 1
  l = a[l - 1]
  # print(i, l, l_ref) 

# print(i, l, l_ref) 

loop = i - l_ref.index(l)
offset = len(l_ref) - loop
mod = (k - offset) % loop
div = k // i

# print(loop, offset, mod, div)

l2_ref = l_ref[offset:]
# print(l2_ref)

res = l2_ref[mod]

print(res)

Submission status: Wrong Answer

Solution code:
def main():
    [n, k] = [int(i) for i in input().split(" ", 2)]
    t = [int(i) for i in input().split(" ")]
    t.insert(0, 0)
    h = []
    l = []
    o = 0
    c = 1
    while True:
        c = t[c]
        h.append(c)
        if c in h[:-1]:
            o = h.index(c)
            l = h[o:-1]
            break
    # print(o, h, l)
    s = (k + 1 - o) % len(l)
    # print(f's={s}')
    print(l[s])


main()

Submission status: Wrong Answer

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

ans = A[0]

d = {0:1, ans -1:1}
#移動する可能性がある街の個数uを出す
for i in range(2 * N):
  #print(ans)
  ans = A[ans - 1]
  if ans -1 not in d:
    d[ans -1] = 1
  else:
    d[ans - 1] += 1
  
#print("d", d, len(d))
u = len(d)

if d[0] == 1:
  a = (K - 1) % u
else:
  a = K % u
if a == 0:
  a = u
  
#print("a", a)
ans = A[A[0] - 1]
for i in range(a + 1):
  #print(i, ans)
  ans = A[ans - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(" "))
A = input().split(" ")
for i in range(len(A)):
  A[i] = int(A[i])-1
revenir = 0
avant = [0]
precedent = 0
while A[precedent] not in avant:
  avant.append(A[precedent])
  if revenir ==K:
    print(A[precedent])
  revenir+=1
  precedent = A[precedent]
if revenir <K:
  modulo = revenir+1-avant.index(A[precedent])
  indice =(K-avant.index(A[precedent]))%modulo
  a = avant[indice+avant.index(A[precedent])]
  print(a+1)