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: Runtime Error

Solution code:
import math
from collections import deque
n,k=[int(_) for _ in input().split()]
A=[int(_) for _ in input().split()]
s=1
vlist=deque()
vlist.append(0)
count=0
t=0
while True:
    s=A[s-1]
    count+=1
    t+=1
    if count==k:
        #print(s)
        break
    if s in vlist:
        l=len(vlist)-vlist.index(s)
        break
    else:
        vlist.append(s)
    #print(vlist)
t+=(((k-count)//l)-1)*l
while True:

    s=A[s-1]
    t+=1
    #print(t)
    if t>=k:
        print(s)
        break

Submission status: Runtime Error

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= k):
        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:
    print(ans_list[-1] + 1)

Submission status: Runtime Error

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

    if len(t) != len(set(t)):

        l = [m for m, x in enumerate(t) if x == t[i+1]]
        b = [t[i+1]]
        break

d = l[1] - l[0]
if d != 0:
    r = (k - (l[0]+1)) % d
else:
    r = (k - (l[0]+1))

for i in range(r+1):
    b.append(a[b[i]-1])

print(b[r+1])

Submission status: Runtime Error

Solution code:
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N, K = MI()
    A = LI()
    A.insert(0, 0)
    x = 1
    y = 0
    roop=1000
    l = [0]*roop
    for i in range(roop):
        x = A[x]
        l[i]=x
    ln = len(set(l))
    for i in range(N):
        for j in range(i+1,N+1):
            if l[i] == l[j]:
                y = j-i
                break
        if y != 0:
            break
    if K <= N:
        print(l[K-1])
    else:
        amari = (K - ln)%y
        ans=l[ln-1-(y-amari)]
        print(ans)

    


if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A =[0]
B=list(map(int, input().split()))
for m in range(N):
    A.append(B[m])
place=set([])
p=[]
i=1
count=0
while i not in place:
    place.add(i)
    p.append(i)
    i=A[i]
    count+=1
if count>=K:
    print(place[K+1])
else:
    loop=[i]
    start=i
    j=A[i]
    count+=1
    while j!=start:
        loop.append(j)
        j=A[j]
        count+=1
    if count>=K:
        print(loop[K-len(p)])
    else:
        num=(K-count)%len(loop)
        print(loop[num])

Submission status: Runtime Error

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

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

now=1

ls=[1]
count=0
for i in range(K):
    count=i
    now=A[now-1]
    if now in ls:
        ls.append(now)
        break
    ls.append(now)
    
print(ls)
front_index=ls.index(A[now-1])
end_index=len(ls)-1

print(front_index)
print(end_index)


sa=end_index-front_index+1

print(sa)

k=K-front_index

k-=(k//sa)*sa


print(ls[front_index+k])

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().strip().split()))
#print(a)
visited = [-1] * (n+1)


now = 0
visited[0] = 0
pre_roop = 0
pos = 0
i = 1
while i < k:
    next = a[now-1]
    #print('roop:',i,now+1,next+1)
    if visited[next] >= 0:
        r = i-visited[next]
        pre_roop = visited[next]
        pos = next
        #print(i,r,pre_roop,next)
        #print(visited)
        break
    else:
        visited[next] = i
        now = next
        #print(i,visited)
        i += 1
l = (k - pre_roop) % r
#print(l)
i = 0
p = pos
while i < l:
    p = a[p-1]
    i +=1

print(p+1)

Submission status: Runtime Error

Solution code:
import math
from collections import deque
n,k=[int(_) for _ in input().split()]
A=[int(_) for _ in input().split()]
dic={}
s=1
vlist=list()
vlist.append(0)
count=0
t=0
while True:
    s=A[s-1]
    count+=1
    t+=1
    if count==k:
        #print(s)
        break
    if s in vlist:
        l=len(vlist)-vlist.index(s)
        break
    else:
        vlist.append(s)
    #print(vlist)
t+=(((k-count)//l)-1)*l
while True:

    s=A[s-1]
    t+=1
    #print(t)
    if t>=k:
        print(s)
        break

Submission status: Runtime Error

Solution code:
import math
from collections import deque
n,k=[int(_) for _ in input().split()]
A=[int(_) for _ in input().split()]
dic={}
s=1
vlist=list()
vlist.append(0)
count=0
t=0
while True:
    s=A[s-1]
    count+=1
    t+=1
    if count==k:
        #print(s)
        break
    if s in vlist:
        l=len(vlist)-vlist.index(s)
        break
    else:
        vlist.append(s)
    #print(vlist)
t+=(((k-count)//l)-1)*l
while True:

    s=A[s-1]
    t+=1
    #print(t)
    if t>=k:
        print(s)
        break

Submission status: Runtime Error

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

num = 1
l = []
l.append(num)
for i in range(K):
  num = A[num-1]
  if num not in l:
    l.append(num)
  else:
    mod = (K-1-i) % (i+1-(l.index(num)))
    break;
    
for i in range(mod):
  num = A[num-1]
  
print(num)

Submission status: Runtime Error

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

li = [-1] * n
li[0] = 0
town = [0]
now = 0

for i in range(k):
    now = a[now] - 1
    if li[now] != -1:
        s = li[now]
        break
    else:
        li[now] = i + 1
        town.append(now)

if s == n:
    print(town[-1])
    exit()
town2 = town[s:]
m = len(town2)
l = len(town) - len(town2)

if m == 0:
    print(town[-1])
else:
    print(town2[(k - l) % m]+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(input().split())
p = [0]*200000
p[0]=1
p[1]=int(a[0])
f=0
for i in range(k-1):
    p[i+2]=int(a[p[i+1]-1])
    for j in range(i+2):
        if p[i+2] == p[j]:
            r=j
            q=i+2-j
            f=1
            break
    if f==1:
        break
l=k-r
l%=q
s=p[r+l]
if f==0:    
    print(p[k])
else:
    print(s)

Submission status: Runtime Error

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

for i in range(N):
  town[i] = [i,A[i] - 1]

def teleport(i,con):
  goal = town[i][1]
  if goal in visited or con == K:
    return goal
  else:
    visited.append(goal)
    return teleport(goal,con + 1)

l = teleport(visited[0],0)

if K < len(visited):
  print(visited[K] + 1)
else:
  if l in visited:
    cs = visited.index(l)
    cycle = visited[cs:]
    K = K - len(visited) + len(cycle)
  print(cycle[K % len(cycle)] + 1)

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = map(int, input().split())
a = list(map(int, input().strip().split()))
a = np.array(a)
a -= 1
#print(a)
visited = [-1] * (n+1)


now = 0
visited[0] = 0
pre_roop = 0
pos = 0
i = 1
while i < k:
    next = a[now]
    #print('roop:',i,now+1,next+1)
    if visited[next] >= 0:
        r = i-visited[next]
        pre_roop = visited[next]
        pos = next
        #print(i,r,pre_roop,next)
        #print(visited)
        break
    else:
        visited[next] = i
        now = next
        #print(i,visited)
        i += 1
l = (k - pre_roop) % r
#print(l)
i = 0
p = pos
while i < l:
    p = a[p]
    i +=1

print(p+1)

Submission status: Runtime Error

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

s = {0}
loop = [0]

while a[loop[-1]] not in s:
    loop.append(a[loop[-1]])
    s.add(loop[-1])

    
idx = loop.index(a[loop[-1]])

if k <= len(loop):
    print(loop[k]+1)
else:
    print(loop[(k - idx)% (len(loop) - idx) + idx] + 1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import collections


# 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():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    cur = 1
    c = {}
    route = []
    roop = None
    while (True):
        if cur in c:
            roop = cur
            break
        route.append(cur)
        c[cur] = 1
        cur = A[cur - 1]
    idx = route.index(roop)
    route = route[idx:]
    print(route[K % len(route) - idx])
    return


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K <= key:
        print(lst_memo[K - 1] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[key + (K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current)
else:
    isReturn = [0]*(N)
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(N):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = (K-step) % r
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
N, K = map(int, sys.stdin.readline().split())
A = inpl()
dic = {}
for i in range(N):
    dic[i+1] = A[i]
i = 1
visit = [1]
flag = 0
while dic[i] not in visit:
    visit.append(dic[i])
    i = dic[i]
    if len(visit) == K:
        print(visit[-1])
        flag = 1
        break
if not flag:
    find = visit.index(dic[i])
    visit = visit[find:]
    length = len(visit) - find
    K = (K-find) % length
    print(visit[K])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
Vector = [0]*N
j = 0
 
for i in range (0,K+1):
	if Vector[A[j]] == 1:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		Vector[A[j]] = 1
		j = A[j]-1

if A[0] ==1:
	print(1)
elif len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])

Submission status: Runtime Error

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

G = {}
for i in range(1, n + 1):
    G.update({i: a[i - 1]})

seen = [False] * (n + 1)
path = []
return_ans = 0

def dfs(graph, start):
    global return_ans
    seen[start] = True
    path.append(start)
    
    if seen[graph[start]]:
        return_ans += graph[start]
        return return_ans
    
    dfs(graph, graph[start])


point = dfs(G, 1)
m = path.index(return_ans)

if m > k:
    print(path[k])
else:
    k -= m
    path = path[m:]
    k %= len(path)
    print(path[k])

Submission status: Runtime Error

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

rute = list()
visited = dict()
city = A[0]
i = 0
while city not in rute:
    rute.append(city)
    visited[city] = i
    city = A[city]
    i += 1

loopstart = visited[city]
loopend = len(rute) - 1

if K < loopstart:
    print(rute[K-1]+1)
else:
    K -= loopstart
    K %= loopend - loopstart
    print(rute[loopstart + K]+1)

Submission status: Runtime Error

Solution code:
NK = [int(x) for x in input().split()]
N=NK[0]
K=NK[1]
A = [int(x) for x in input().split()]
    
j=1 
ii=0
C=[]
for i in range(N):#i:1~N
  j=A[j-1]
  try:
    ii=C.index(j)+1
    T=i-ii+1
    break
  C.append(j)
if K<ii:
  print(C[K-1])
else:
  print(C[(K-ii)%T+ii-1])

Submission status: Runtime Error

Solution code:
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N, K = MI()
    A = LI()
    A.insert(0, 0)
    x = 1
    y = 0
    roop=1000
    l = [0]*roop
    for i in range(roop):
        x = A[x]
        l[i]=x
    ln = len(l)
    for i in range(N):
        for j in range(i+1,N+1):
            if l[i] == l[j]:
                y = j-i
                break
        if y != 0:
            break
    if K <= N:
        print(l[K-1])
    else:
        amari = (K - ln)%y
        ans=l[ln-1-(y-amari)]
        print(ans)

    


if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=[1]
now=0
passed = set()
for i in range(n+1):
    if a[now] in passed:
        ans.append(a[now])
        break
    else:
        ans.append(a[now])
        passed.add(a[now])
        now=a[now]-1
#print(ans)
#変更
passed = set()
index = ans[:-1].index(ans[-1])
index_1 = len(ans)-2
#print(index)
#print(index_1)
print(ans[(k-index-1)%(index_1-index)+index])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
Vector = [0]*N
j = 0
 
for i in range (0,K+1):
	if Vector(A[j]) == 1:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		Vector(A[j]) = 1
		j = A[j]-1

if A[0] ==1:
	print(1)
elif len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=map(int, input().split())

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0
t=0
rep = False

for i in range(n+1000):
    li.append(A[next])
    li2[next] = 1
    next = A[next]-1

    if li2[next] == 1:
        t = li.index(A[next])
        rep = True
        break

if rep == True:
    tmp = (k%len(li[t:])) - len(li[0:t])
    g=li[t:]
    print(g[tmp])
elif len(li) > k:
    print(li[k-1])
else:
    tmp = k%len(li[t:])
    print(li[tmp])

Submission status: Runtime Error

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

tele=[0]*N
for i in range(N):
    tele[i]=list[i]-1

temp=[0]*100
town=0
for i in range(N):
    temp[i]=tele[town]
    town=tele[town]

for i in range(N):
    for j in range(N):
        if temp[i]-temp[j]==0:
            m=abs(i-j)
            break
K_temp=K%m

city=0
for i in range(K_temp):
    city=tele[city]

print(city+1)

Submission status: Runtime Error

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

li = [-1] * n
li[0] = 0
town = [0]
now = 0

for i in range(k):
    now = a[now] - 1
    if li[now] != -1:
        s = li[now]
        break
    else:
        li[now] = i + 1
        town.append(now)

town2 = town[s:]
m = len(town2)
l = len(town) - len(town2)

if m == 0:
    print(town[-1])
else:
    print(town2[(k - l) % m]+1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,K = map(int, input().split())
Line = [int(s) for s in input().split()]

X = [-1]*N

# print(X)
ikisaki = Line[0]
X[0] = 0

# print(ikisaki)

for i in range(K+1):
    if X[ikisaki-1] == -1:
        X[ikisaki-1] = i + 1
        ikisaki = Line[ikisaki-1]
        warukazu = i+1 - X[ikisaki-1]
    else:
        warukazu = i+1 - X[ikisaki-1]
        saisyo = X[ikisaki-1]
        break
    # print(X)
# print(saisyo)
# print(warukazu)
Z = K%warukazu
# print(Z)
if saisyo >= K:
    print(X.index(K)+1)
else:
    print(X.index(Z)+1)

Submission status: Runtime Error

Solution code:
NK = [int(x) for x in input().split()]
N=NK[0]
K=NK[1]
A = [int(x) for x in input().split()]
    
j=1 
ii=0
C={}
for i in range(N):#i:1~N
  j=A[j-1]
  if j in C:
    ii=C[j]+1
    T=i-ii+1
    break
  C[j] = i
if K<ii:
  print(C[K-1])
else:
  print(C[(K-ii)%T+ii-1])

Submission status: Runtime Error

Solution code:
n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 0
s = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1

m = len(warp)
print(warp[(k-s)%m])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
an = list(map(int, input().split()))
copyan = an[:]
i = copyan[0]
copyan[0]=10**6
movecount = 1
roopstart = 0
if i != 1:
    while True:
        memo = i
        i = copyan[i-1]
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        if k == movecount:
            print(i)
            exit()
        copyan[memo-1]=10**6 + movecount
        movecount += 1
        
else:
    print(1)
    exit()
roopnum = movecount - yokei
x = (k-yokei)%roopnum #移動する回数

ans = copyan.index(10**6+yokei+x) +1
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
Line = [int(s) for s in input().split()]

X = [-1]*N

# print(X)
ikisaki = Line[0]
X[0] = 0

# print(ikisaki)

for i in range(K):
    if X[ikisaki-1] == -1:
        X[ikisaki-1] = i + 1
        ikisaki = Line[ikisaki-1]
        warukazu = i+1 - X[ikisaki-1]
    else:
        warukazu = i+1 - X[ikisaki-1]
        saisyo = X[ikisaki-1]
        break
    # print(X)
# print(saisyo)
# print(warukazu)
Z = K%warukazu
# print(Z)
if saisyo >= K:
    print(X.index(K)+1)
else:
    print(X.index(Z)+1)

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current)
else:
    isReturn = [0]*(N)
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(K):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = (K-step) % r
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Submission status: Runtime Error

Solution code:
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
vis=[False for i in range(n)]
a=[]
start=curr=0
vis[0]=True
a.append(start)
flag=0
for i in range(k):
    curr=a[-1]+1
    if vis[arr[curr-1]-1]:
        a.append(arr[curr-1] - 1)
        flag=1
        break
    else:
        vis[arr[curr-1]-1]=True
        a.append(arr[curr-1]-1)
if flag==0:
    print(curr)
#now i have req cycle
if 5<6:
    count=0
    #print(a,"a")
    for i in range(len(a)):
        if a[i]==a[-1]:
            new=a[i:-1]
            #print(new,a[i:-1])
            break
        else:
            count+=1
    #print(len(new),new)
    #rint(count,"c")
    print(new[(k-count)%(len(new))]+1)

Submission status: Runtime Error

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

for i in range(N):
  town[i] = [i,A[i] - 1]

def teleport(i,con):
  goal = town[i][1]
  if goal in visited or con == N:
    return goal
  else:
    visited.append(goal)
    return teleport(goal,con + 1)

l = teleport(visited[0],0)

if K < len(visited):
  print(visited[K] + 1)
else:
  cs = visited.index(l)
  cycle = visited[cs:]
  K = K - len(visited) + len(cycle)
  print(cycle[K % len(cycle)] + 1)

Submission status: Runtime Error

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

s = {0}
loop = [0]

while a[loop[-1]] not in s:
    loop.append(a[loop[-1]])
    s.add(loop[-1])

    
idx = loop.index(a[loop[-1]])

if k <= len(loop):
    print(loop[k-1]+1)
else:
    print(loop[(k - idx)% (len(loop) - idx) + idx] + 1)

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [False] * n
now = 0
count = 0
history = [1]

while b[now] == False:
  history.append(a[now])
  b[now] = True
  count += 1
  if count >= k:
    print(a[now])
    sys.exit()
  if a[now]-1 == now:
    print(a[now])
    sys.exit()
  now = a[now]-1


if now != 0:
  k -= now + 1
  history = history[now+1:]
history.pop()
print(history[k % len(history)])

Submission status: Runtime Error

Solution code:
import sys
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
vis={0:0}
now=0
for i in range(1,k+1):
  now=a[now]-1
  if now in vis:
    bgn=vis.index(now)
    print(vis[bgn+((k-bgn)%(len(vis)-bgn))]+1)
    sys.exit()
  else:
    vis[i]=now
print(vis[-1]+1)

Submission status: Runtime Error

Solution code:
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if d[nex]:
        first=c.index(nex)
        flag=1
        break
    d[nex-1]=1
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline
n,k = map(int,input().split())
a = list(map(int,input().split()))

if k ==1:
    print(a[a[0]-1])
    sys.exit()

z = 1
p = [1]
for _ in range(k):
    z = a[z-1]
    if z in p:
        p.append(z)
        break
    p.append(z)

# 周期の来る前の回数
num = p.index(p[-1])

p = p[num:]

# 周期
pn = len(p)-1

k -= num

# 回数/周期のあまり
anum = k%pn
print(p[anum])

Submission status: Runtime Error

Solution code:

n, k = map(int, input().split(" "))
a = [-1] + list(map(int, input().split(" ")))

i = 1
head = []
used = {}
roop = []

inte=False
index = 0
for _ in range(k):
    index += 1
    i = a[i]
    if i in used:
        head = [ k for k, v in used.items() if v < used[i]]
        roop = [ k for k, v in sorted(used.items())]
        inte =True
        break
    else:
        used[i] = index

if inte:
    if len(head) >= k:
        print(head[k-1])
    else:
        x = (k - 1 - len(head)) % len(roop)
        print(roop[x])
else:
    print(used[k-1])

Submission status: Runtime Error

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

for i in range(N):
  town[i] = [i,A[i] - 1]

def teleport(i,con):
  goal = town[i][1]
  if goal in visited or con == N:
    return goal
  else:
    visited.append(goal)
    return teleport(goal,con + 1)

l = teleport(visited[0],0)

cs = visited.index(l)
cycle = visited[cs:]

if K < len(visited):
  print(visited[K] + 1)
else:
  K = K - len(visited) + len(cycle)
  print(cycle[K % len(cycle)] + 1)

Submission status: Runtime Error

Solution code:
n,k = list(map(int,input().split()))
A = list(map(int,input().split()))
tour=[0]
visited=[0]*n
visited[0]=1
next_town = 0
flag=False
for i in range(n):
    next_town=A[next_town]-1
    if i==k:
        flag = True
        ans = next_town
    if next_town==0:
        first_roop_idx = 0
        break
    if visited[next_town]>0:
        first_roop_idx = visited[next_town]
        break
    tour.append(next_town)
    visited[next_town]=i+1
#print(tour)
#print(visited)
#print(first_roop_idx)
if not flag:
    if k<=len(tour):
        ans = tour[k]
    else:
        k-=first_roop_idx
        #print("k=",k)
        if len(tour)-first_roop_idx==0:
            k=0
        else:k=k%(len(tour)-first_roop_idx)
        #print("k=",k)
        ans = tour[first_roop_idx+k]
print(ans+1)

Submission status: Runtime Error

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

i = 1
head = []
used = {}
roop = []

inte=False
index = 0
for _ in range(k):
    index += 1
    i = a[i]
    if i in used:
        head = [ k for k, v in used.items() if v < used[i]]
        roop = [ k for k, v in sorted(used.items())]
        inte =True
        break
    else:
        used[i] = index

print(head, roop)

if inte:
    if len(head) >= k:
        print(head[k-1])
    else:
        x = (k - 1 - len(head)) % len(roop)
        print(roop[x])
else:
    print(used[k-1])

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
N, K = map(int, sys.stdin.readline().split())
A = inpl()
dic = {}
for i in range(N):
    dic[i+1] = A[i]
i = 1
visit = [1]
while dic[i] not in visit:
    visit.append(dic[i])
    i = dic[i]
find = visit.index(dic[i])
visit = visit[find:]
length = len(visit) - find
K = (K-find)%length
print(visit[K])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import math


def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def main():
    N, M, K = map(int, input().split())
    const = 998244353

    def hoge(k):
        return comb(N - 1, k) * M * ((M - 1) ** (N - k - 1))

    ans = sum([hoge(k) for k in range(K + 1)])

    print(ans % const)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current)
else:
    isReturn = [0]*(N+1)
    step = 1
    current = 1
    isReturn[current] = step
    for i in range(K):
        current = A[current-1]
        step += 1
        if isReturn[current] == 0:
            isReturn[current] = step
        else:
            r = step - isReturn[current]
            break
    
    t = (K-step) % r
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Submission status: Runtime Error

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

ind = 1
tl = [1]
ts = {1}
for i in range(n):
    ind = a[ind-1]
    if ind in ts:
        break
    
    tl.append(ind)
    ts.add(ind)

tl2 = tl[tl.index(ind):len(tl)]
ans = tl[k] if k < len(tl) else (k - len(tl)) % len(tl2)
print(tl2[ans])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
C = B * 10000000000
print(C[k-1])

Submission status: Runtime Error

Solution code:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
else:
  print(a[current]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
an = list(map(int, input().split()))
copyan = an[:]
i = copyan[0]
copyan[0]=10**6
movecount = 1
roopstart = 0
if i != 1:
    while True:
        memo = i
        i = copyan[i-1]
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        copyan[memo-1]=10**6 + movecount
        movecount += 1
        roopstart = i-1
else:
    print(1)
    exit()
roopnum = movecount - yokei
x = (k-yokei)%roopnum #移動する回数

ans = copyan.index(10**6+yokei+x) +1
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    a[i]-=1
b=[0]
c=dict()
visited=[False]*n
visited[0]=True
c[0]=0
cnt=0
dap=0
while True:
    n=b[-1]
    if visited[a[n]]:
        dap=c[a[n]] 
        break
    visited[a[n]]=True
    b.append(a[n])
    c[n]=cnt
    cnt+=1
l=len(b)
print(b[(k-dap)%(l-dap)+dap]+1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2000)
L = []

def Search(pos):
  if pos - 1 not in L:
    L.append(pos - 1)
    Search(A[pos-1])
  else:
    L.append(pos - 1)
    return pos
  
N, K = map(int, input().split())
A = list(map(int, input().split()))
Search(1)
#print(L)
if K < L.index(L[-1]):
  print(L[K])
else:
  t = L.index(L[-1])
  k = len(L) - t - 1
  K -= t
  #print(t, k, K % k)
  print(L[t + (K % k)] + 1)

Submission status: Runtime Error

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

route = []
i = 0

while True:
    ne = a[i]

    if i not in route:
        route.append(i)
    else:
        temp = len(route) - len(route[i:])
        print(a[(k-temp)%len(route[i:])])
        break

    i = ne - 1

Submission status: Runtime Error

Solution code:
paramN, paramK = list(map(lambda x: int(x), input().split(' ')))
paramA = list(map(lambda x: int(x), input().split(' ')))

route = [0] * paramN + 1
route[0] = 1
for i in range(1, paramN + 1):
    route[i] = paramA[route[i - 1] - 1]

loop_start = 0
history = [False] * paramN
for i, r in enumerate(route):
    if history[r - 1]:
        loop_start = i
        break
    else:
        history[r - 1] = True

x = route[loop_start]
loop_start_0 = route.index(x)

y = paramK - loop_start_0
z = y % (loop_start - loop_start_0)
print(route[loop_start_0 + z])

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=map(int, input().split())

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0
t=0
rep = False

for i in range(n):
    li.append(A[next])
    li2[next] = 1
    next = A[next]-1

    if li2[next] == 1:
        t = li.index(A[next])
        rep = True
        break

if rep == True:
    tmp = (k%len(li[t:])) - len(li[0:t])
    g=li[t:]
    print(g[tmp])
elif len(li) > k:
    print(li[k-1])
else:
    tmp = k%len(li[t:])
    print(li[tmp])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*1000
tmp = 0
t = 1
index = 0

for i in range(1000):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        exit

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-1])