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:
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= 1

cnt = [0]*(10**5)
cnt2 = [0]*(10**5)#何回目に出てきたか
t = []
nxt = 0
loop = 0

for i in range(10**5):
    cnt[nxt] += 1
    if cnt[nxt] >= 2:
        # print("i: " + str(i))
        # print("nxt: " + str(nxt))
        loop = i - cnt2[nxt]
        modC = max(0, cnt2[nxt])
        break
    cnt2[nxt] = i
    t.append(nxt)
    nxt = a[nxt]

modK = (k-modC)%loop
# print("-------")
# print(t)
# print("modC: " + str(modC))
# print("modK: " + str(modK))
# print("loop: " + str(loop))
# print(modK)
# print(loop)
# print("-------")

if k < modC:
    print(t[k])
else:
    print(t[modC + modK] + 1)

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(p[K])
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[0])
    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().strip().split()))
B=1
list=[]
for i in range(K-1):
    if B in list:
        num2=list.index(B)
        break
    else:
        list.append(B)
        B=A[B-1]

del list[:num2]
num=(K-num2)%len(list)
print(list[num])

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(p[K])
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(' '))

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  now=A[now]
  if(cache.get(now)):break
  cache[now]=1

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

#print(loop_end-loop_start,loop_start)

if len(log) >loop:
  if loop==0 and loop_start>K:
    print(now)
  else:
    print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)

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(p[K])
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()))

iter = 0
path = [0]
while iter < K:
    path.append(A[path[-1]] - 1)
    if path[-1] in path[:-1] and iter > 0:
        break
    iter += 1
"""
print(path)
loop_start = path.index(path[-1])
loop_len = len(path[loop_start : -1])
print(loop_start)
print(path[loop_start : -1])
print(loop_len)"""
goal = (K % loop_len)
print(A[goal])

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(p[K])
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)])
    num=(K-count)%len(loop)
    print(loop[num])

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np
N,M,X=map(int, input().split())
book = []
for _ in range(N):
    book.append(list(map(int, input().split())))
book = np.array(book)
money=pow(10,7)
for i in itertools.product([0,1], repeat=N):
    book2=book
    dellist = []
    for n,l in enumerate(i):
        if l==0:
            dellist.append(n)
    book2=np.delete(book2, dellist, 0)
    ansar = np.sum(book2,axis=0)
    if np.sum(ansar[1:]>=X)==M:
        if money>ansar[0]:
            money = ansar[0]
if money == pow(10,7):
    print(-1)
else:
    print(money)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=0
while(count<=K+1):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count
  if count==K+1:
    print(b)
    break

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

probs = [0] * n
p = 0  # 現在位置
target = 0  # はじめて2回出現する場所
h = 0  # ターゲットの初回出現時のワープ回数
t = 0  # ターゲットの2回目の出現時のワープ回数

if (maps(0) == 0):
    print(1)
    exit

# はじめて2回出現する場所(target)と、その時点でのワープ回数を取得
for i in range(n):
    probs[p] += 1
    if (probs[p] == 2):
        target = p
        t = i
        break
    p = maps[p]

# 初回出現位置を取得(めんどうくさいのでもう一回回した。)
p = 0
for i in range(n):
    if (p == target):
        h = i
        break
    p = maps[p]

s = t - h  # 周期
d = (k - h) % s  # targetに到着後にワープすべき回数

# 実際に必要な回数だけワープ
p = 0
for i in range(h + d):
    p = maps[p]

print(p + 1)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A_dict = {i:a for i,a in enumerate(A,1)}

History = [1]
History_set = {1}
now = 1
for _ in range(N):
    now = A_dict[now]
    if now not in History_set:
        History.append(now)
        History_set.add(now)
    else:
        break
#print(History,now)
if K < N:
    print(History[K])
else:
    e = History.index(now)
    #print(e)
    #print(History[e:])
    loop_num = len(History[e:])
    #print(loop_num)
    num = (K-e)%loop_num
    #print(num)
    print(History[e:][num])

Submission status: Runtime Error

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

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  now=A[now]
  if(cache.get(now)):break
  cache[now]=1

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

#print(loop_end-loop_start,loop_start)

if len(log) >loop:
  print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

probs = [0] * n
p = 0  # 現在位置
target = 0  # はじめて2回出現する場所
h = 0  # ターゲットの初回出現時のワープ回数
t = 0  # ターゲットの2回目の出現時のワープ回数

if (map(0) == 0):
    print(1)
    exit

# はじめて2回出現する場所(target)と、その時点でのワープ回数を取得
for i in range(n):
    probs[p] += 1
    if (probs[p] == 2):
        target = p
        t = i
        break
    p = maps[p]

# 初回出現位置を取得(めんどうくさいのでもう一回回した。)
p = 0
for i in range(n):
    if (p == target):
        h = i
        break
    p = maps[p]

s = t - h  # 周期
d = (k - h) % s  # targetに到着後にワープすべき回数

# 実際に必要な回数だけワープ
p = 0
for i in range(h + d):
    p = maps[p]

print(p + 1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
d2 = []
hist=[-1]*n
hist[0] = 0
cur = 1
d2.append(1)
st_idx = 0
rp_lgt = 0
for i in range(n):
  cur = a[cur-1]
  if hist[cur-1] >= 0 :
     d2.append(cur)
     st_idx = hist[cur-1] 
     rp_lgt = i - st_idx
     break
  else:
    d2.append(cur)
    hist[cur-1] = i
wk = k - st_idx
md = wk % rp_lgt
if k <= st_idx:
  ans = d2[k]
else:
  ans = d2[md+st_idx]
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A_dict = {i:a for i,a in enumerate(A,1)}

History = [1]
History_set = {1}
now = 1
for _ in range(N):
    now = A_dict[now]
    if now not in History_set:
        History.append(now)
        History_set.add(now)
    else:
        break
#print(History,now)
if K <= N:
    print(History[K])
else:
    e = History.index(now)
    #print(e)
    #print(History[e:])
    loop_num = len(History[e:])
    #print(loop_num)
    num = (K-e)%loop_num
    #print(num)
    print(History[e:][num])

Submission status: Runtime Error

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=[]
now=0
for i in range(n+1):
        ans.append(a[now])
        now=a[now]-1
#print(ans)

ans2=collections.Counter(ans)

ac=ans2.most_common()[0][0]
#print(ac)
index=-1
index2=-1

for i in range(len(ans)):
    if ans[i]==ac:
        if index==-1:        
            index=i
        else:
            index2=i
assert(index2 == -1)
#print(index,index2)
#print((k-index-1)%(index2-index))
if k < index:
    print(ans[k])
else:
    print(ans[(k-index-1)%(index2-index)+index])

Submission status: Runtime Error

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

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k - = len(know)
    ans = know[k%len(loop)]


print(ans)

Submission status: Runtime Error

Solution code:
[N,K]=map(int,input().split())
A = list(map(int,input().split()))
t = 1
log = []
for i in range(K):
    t = A[t-1]
    if t in log:
        n = log.index(t)
        r = log[n1:]
        nn = int((K-n)%len(r))
        t = r[nn-1]
        break
    log.append(t)
print(t)

Submission status: Runtime Error

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    cnt = [-1] * n
    cnt[0] = 0
    ary = []
    now = 1
    ary.append(now)
    if k <= n:
        for i in range(1,k+1):
            now = a[now-1]
            if i == k:
                print(now)
                return
    else:
        for i in range(1,k+1):
            now = a[now-1]
            if cnt[now-1] != -1:
                break
            else:
                cnt[now-1] = i
                ary.append(a[now-1])


        print(ary[cnt[now-1]+((k-cnt[now-1]) % (i - cnt[now-1]))+1])
        
if __name__=='__main__':
    main()

Submission status: Runtime Error

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

while b[now] == False:
  history.append(a[now])
  b[now] = True
  count += 1
  if count >= k:
    print(a[now])
    flag = False
    break
  if a[now]-1 == now:
    print(a[now])
    flag = False
    break
  now = a[now]-1
  
if flag == True:
  if now != 0:
    k -= now + 1
    history = history[now+1:]
  history.pop()
  print(history[k % len(history)])

Submission status: Runtime Error

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

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k - = len(know)
    ans = know[k%len(loop)]


print(ans)

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(p[K])
else:
    loop=[i]
    start=i
    j=A[i]
    count+=1
    while j!=start:
        loop.append(j)
        j=A[j]
        count+=1
    num=(K-count)%len(loop)
    print(loop[num])

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, K = [int(i) for i in input().split()]
A_list = [int(i) for i in input().split()]

dp = defaultdict(lambda: None)

city = 1
loop_len = 0
ans = 0
for i in range(N):
    if dp[city] is not None or i >= K:
        ans = city
        loop_len = i - dp[city]
        break

    dp[city] = i
    city = A_list[city - 1]

if K >= dp[city]:
    index = (K - dp[city]) % loop_len
    ans = list(dp)[dp[city]:][index]

print(ans)

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)

print([i + 1for i in visited])

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:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

t = [int(s) for s in stdin.readline().rstrip().split()]
n = t[0]
k = t[1]
Ak = [int(s) for s in stdin.readline().rstrip().split()]

data = [1]
tmp = 0
for el in Ak:
    data.append(Ak[tmp])
    tmp = Ak[tmp]-1
head = 1
mark = [int(s) for s in '0' * (n + 1)]
used = []
for el in data:
    used.append(el)
    if (mark[el-1] == 1):
        head = el
        break
    mark[el - 1] = 1
base = used[:head]
circle = used[head:]
if circle[0] == circle[-1]:
    del circle[-1]

print(used[len(base)+(k-len(base)) % len(circle)])

Submission status: Runtime Error

Solution code:
import copy
N,K = list(map(int, input().split()))
 
A = list(map(int, input().split()))
 
count = [0]
Check = [0] * N
for i in range(K):
    count.append(A[count[-1]]-1)
    if(Check[count[-1] - 1] == 1):
        break
 
    Check[count[-1] -1] = 1
    i += 1
 
 
saisyo = count.index(count[-1]) + 1
 
loop_kazu = len(count) - saisyo
K_loop = K - saisyo
 
ans_index = K_loop % loop_kazu
ans = count[saisyo + ans_index] + 1
 
print(ans)

Submission status: Runtime Error

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

history = []
is_visited = [False] * n

index = 0
cnt = 0
loop_start_city_index = 0
for i in range(k+1):
    if i == k:
        print(index+1)
        exit()

    is_visited[index] = True

    if is_visited[index]:    # すでに訪れた街にきた
        loop_start_city_index = index
        break
    history.append(index)
    index = a[index] - 1
    cnt += 1

from_start_to_loop_start = history.index(loop_start_city_index)
loop_length = len(history) - from_start_to_loop_start

print(history[from_start_to_loop_start + (k-from_start_to_loop_start)%loop_length] + 1)

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
li2[next] = 1
t=0
rep = False

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

    if li2[next] == 1:
        li.pop()
        t = li.index(next+1)
        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:
import copy
N,K = list(map(int, input().split()))
 
A = list(map(int, input().split()))
 
count = [0]
Check = [0] * N
for i in range(K):
    count.append(A[count[-1]]-1)
    if(Check[count[-1] - 1] == 1):
        break
 
    Check[count[-1]] = 1
    i += 1
 
 
saisyo = count.index(count[-1]) + 1
 
loop_kazu = len(count) - saisyo
K_loop = K - saisyo
 
ans_index = K_loop % loop_kazu
ans = count[saisyo + ans_index] + 1
 
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
s = input().split()
n = int(s[0])
k = int(s[1])
count =1

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    count += 1
    if p in fp:
        roop = count - fp.index(p)
        break
    else:
        fp.append(p)
print(fp[((k-fp.index(p))%roop)+1])

Submission status: Runtime Error

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

zyun = [a[0]]
alr = [False]*n
alr[0] = True
crr = a[0]

key = True

while key:
    if not alr[crr - 1]:
        alr[crr - 1] = True
        crr = a[crr - 1]
        zyun.append(crr)
        continue
    else:
        key = False

for i in range(n):
    if a[i] == crr:
        index = crr
        break


if index - 1 == 0:
    zyun1 = zyun
    zyun2 = zyun
else:
    zyun1 = zyun[:index]
    zyun2 = zyun[index:]

# print(zyun1, zyun2)

if len(zyun1) >= k:
    ind = len(zyun1) % k
    if ind == 0:
        print(zyun1[-1])
    else:
        print(zyun1[ind - 1])
else:
    ind = (k - len(zyun1)) % len(zyun2)
    if ind == 0:
        print(zyun2[-1])
    else:
        print(zyun2[ind - 1])

Submission status: Runtime Error

Solution code:
a = int().input(" ")
b = int().input(" ")
c = []
for i in b:
    c.append(int(i))
d = 0
for j in range(int(a[1])):
    d = c[d] - 1
print(d + 1)

Submission status: Runtime Error

Solution code:
import copy
N,K = list(map(int, input().split()))
 
A = list(map(int, input().split()))
 
count = [0]
Check = [0] * N
for i in range(K):
    count.append(A[count[-1]]-1)
    if(Check[count[-1]] == 1):
        break
 
    Check[count[-1]] = 1
    i += 1
 
 
saisyo = count.index(count[-1]) + 1
 
loop_kazu = len(count) - saisyo
K_loop = K - saisyo
 
ans_index = K_loop % loop_kazu
ans = count[saisyo + ans_index] + 1
 
print(ans)

Submission status: Runtime Error

Solution code:
import copy
N,K = list(map(int, input().split()))
 
A = list(map(int, input().split()))
 
count = [0]
Check = [0] * N
for i in range(K):
    count.append(A[count[-1]]-1)
    if(Check[count[-1] == 1]):
        break
 
    Check[count[-1]] = 1
    i += 1
 
 
saisyo = count.index(count[-1]) + 1
 
loop_kazu = len(count) - saisyo
K_loop = K - saisyo
 
ans_index = K_loop % loop_kazu
ans = count[saisyo + ans_index] + 1
 
print(ans)

Submission status: Runtime Error

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

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break
        
k_bk = k - visited[now]
k_bk =  k_bk // i

lft = (k - visited[now]) - i* k_bk

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=0
while(count<=K):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    print(a)
    print(b)
    print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count

print(b)

Submission status: Runtime Error

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

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break
print(i, now, visited)

k_bk = k - visited[now]
k_bk =  k_bk // i

lft = (k - visited[now]) - i* k_bk

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)

Submission status: Runtime Error

Solution code:
import copy
N,K = list(map(int, input().split()))
 
A = list(map(int, input().split()))
 
count = [0]
Check = [0] * N
for i in range(K):
    countCheck = copy.copy(count)
    count.append(A[count[-1]]-1)
    if(Check[count[-1] == 1]):
        break
 
    Check[count[-1]] = 1
    i += 1
 
 
saisyo = count.index(count[-1]) + 1
 
loop_kazu = len(count) - saisyo
K_loop = K - saisyo
 
ans_index = K_loop % loop_kazu
ans = count[saisyo + ans_index] + 1
 
print(ans)

Submission status: Runtime Error

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

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

l=[1]
di={}
di[1]=1

for i in a:
    b=a[l[-1]-1]
    if b in di:
        x=b
        break
    l.append(b)
    di[b]=1
t1=0
for j in l:
    if j==x:
        break
    else:
        aa+=1

t2=len(l)-t1
if k<=t1:
    print(l[k])

else:
    aa=(k-t1)%t2
    print(l[t1+aa])

Submission status: Runtime Error

Solution code:
import sys

def mink(numk,l):
    x = 1
    for _ in range(k):
        x = a[x-1]
    return x

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

# kが小さい時
if 10**5 <= n and k <= 10**10:
    print(mink(k,a))
    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 = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]


city = 1
prevs = []
for _ in range(K):
    city = A[city - 1]
    if city in prevs:
        rem = prevs.index(city)
        loop = len(prevs[rem:])
        break
    prevs.append(city)

ix = (K - rem) % loop
print(prevs[rem:][ix - 1])

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(p[K+1])
else:
    loop=[i]
    start=i
    j=A[i]
    count+=1
    while j!=start:
        loop.append(j)
        j=A[j]
        count+=1
    num=(K-count)%len(loop)
    print(loop[num])

Submission status: Runtime Error

Solution code:
import sys
N, K = map(int, input().split()) #N: 町の数, K: ちょうどこの回数でたどり着く町

visited = [False for _ in range(N)]

A = list(map(lambda n: int(n)-1, input().split()))
tmp = 0 #次に訪れる番号
visited[0] = True
#cnt = 0 #テレポートの回数
log = [0] #テレポートの記録

for i in range(1, K+1):
    tmp = A[tmp]
    if visited[tmp]:
        start = log.index(tmp)
        break

    visited[tmp] = True
    log.append(tmp)

if i == K:
    print(log[K])
else:
    loop = log[start:i+1]
    period = len(loop)
    remain = K - i
    print(loop[remain%period] + 1)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

def loop(city, past, A, N, K):
    city = A[city-1]
    if Counter(past).get(city):
        loopNumber = len(past) - past.index(city)
        loopAray = past[past.index(city):]
        loopStart = past.index(city)
        if K>=loopStart:
            print(loopAray[(K - loopStart)%loopNumber])
        else:
            print(past[K])
    else:
        past.append(city)
        loop(city, past, A, N, K)

loop(1, [1], A, N, K)

Submission status: Runtime Error

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)
check[0] = 1
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)
if index <= k:
    print(root[k-1])
elif index == cnt - 1:
    print(dup)
else:
    print(root[(k-index) % (cnt-index) + index])

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:
    keys = [k for k, v in C.items() if v == j]
    ii=keys[0]+1
    T=i-ii+1
    break
  C[i]=j
if K<ii:
  print(C[K-1])
else:
  print(C[(K-ii)%T+ii-1])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

visited = [-1] * n
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(int, input().split()))

visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1
    if is_visited[now-1] == 0:
        is_visited[now-1] = 1
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)

Submission status: Runtime Error

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

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

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

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
    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=[i for i, x in enumerate(ls) if x == A[now-1]][-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 = 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
        break
    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)
print(flag)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
for i in range(n):  
  l.append(a[l[i]-1])
loop = len(l[l[-1]:-1])
first = l.index(l[-1])
if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

Submission status: Runtime Error

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

visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1

    if is_visited[now-1] == 0:
        is_visited[now-1] = 1
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)

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)

for i in range(len(a)):
    a[i] -= 1
#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)