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 = [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[ix])

Submission status: Runtime Error

Solution code:
import sys
import copy

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

flag = [0 for _ in range(n)]
toNext = [-1 for _ in range(n)]

tele = [int(i) for i in input().split()]

flag[0] = 1
loop = []
i = 0
loop_start = -1
cnt1 = 0
for i in range(len(tele)):
    toNext[i] = tele[i]-1
    cnt1 += 1
    if flag[tele[i]-1] == 1:
        loop_start = tele[i]-1
        break
    loop.append(tele[i] - 1)
    flag[tele[i]-1] = 1

print("loop:", loop)
print("loop_start:", loop_start)
if k <= len(loop):
    print(loop[k-1])
    print("aaaa")
    sys.exit()

if loop_start == 0:
    cut = 0
else:
    idx = 0
    cnt2 = 0
    while True:
        cnt2 += 1
        if toNext[i] == loop_start:
            break
    cut = cnt2 - cnt1
    loop = copy.deepcopy(loop[cut:])

idx = (k-cut)%len(loop)
print("bbbb")
print("idx:", idx)
print(loop[idx]+1)

Submission status: Runtime Error

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

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

now=1

ls=[1]
count=0
for i in range(K):
    count=i
    now=CA[now-1]
    if now in ls:
        break
    ls.append(now)
    

front_index=ls.index(now)
end_index=ls.index(ls[-1])

sa=end_index-front_index-1

k=K-count

k-=(k//sa)*sa

print(front_index)
print(end_index)
print(ls)
print(ls[front_index+k])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
p = 0
a = 1
ret = [1]
init_l = list()
rec_l = list()
while p < K:
    a = A[a-1]
    if a in ret:
        ind = ret.index(a)
        init_l = ret[:ind]
        rec_l = ret[ind:]
        break:
    ret.append(a)
if init_l == [] and rec_l ==[]:
    print(a)
else:
    m = K - len(init_l)
    print(rec_l[len(ren_l)%m])

Submission status: Runtime Error

Solution code:
temp = input()
temp = temp.split()
town_num = int(temp[0])
move = int(temp[1])

town = input()
town = town.split()
town = [int(s) for s in town]
data = [0] * 1000

position = 1

if move < 1000:
    for i in range(move):
      position = town[position-1]

else:
    for i in range(town_num):
        data[i] = town[position-1]
        position = town[position-1]
        if data[i] == data[4] and i > 4:
            count = i
            break
    cycle = count - 4
    try:
        period = (move-4)%cycle
        position = data[period+4]
        position = town[position-1]-1
    except ZeroDivisionError:
        a = 1

print(position)

Submission status: Runtime Error

Solution code:
def main():
    N,K = map(int,input().split())
    As = list(map(int,input().split()))
    route = [0]
    pre = 0
    i = 0
    r_append = route.append
    for _ in range(N):
        i = As[i]-1
        if i in route:
            pre = route.index(i)
            route = route[pre:]
            break
        r_append(i)
        len_route += 1
    # print(route)
    mod = len(route)
    print(route[(K-pre)%mod]+1)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A=list(map(int, input().split()))
L=[1]
def f(L):
  L.append(A[l[-1]]) if A[l[-1]] in L
f(L)
print(L[k//len(L)])

Submission status: Runtime Error

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

before = 1
townlist = [1]
flag = 1
for i in range(k):
    town = a[before - 1]
    if flag and town not in townlist:
        townlist.append(town)
       # print(townlist)
        before = town
    else:
        num = i
        flag = 0
        break
if flag :
    print(town)
else:
    ind = townlist.index(town)
    k -= ind
    index = k % (num - ind)
    print(townlist[ind + index - 1])

Submission status: Runtime Error

Solution code:
import sys
l =  list(map(lambda x:int(x), input().split(" ")))
n = l[0]
k = l[1]
a = list(map(lambda x:int(x), input().split(" ")))
b = dict(zip(list(range(1,n+1)),a))
c = 1
for i in range(k):
    c = b[c]
    if c == 1:
        a = a[0:i]
        for j in range(i):
            if k%i == j:
                print(a[j])
                sys.exit()
            else:
                j = j+1

Submission status: Runtime Error

Solution code:
import sys, re
import math
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul	# 
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd	# 最大公約数
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

# -*- coding: utf-8 -*-

n, k = MAP()
a = LIST()
pc = [0] * n

def A(pos, count, loop):
    global a, k, pc

    count += 1
    if k == count:
        print(pos)
    if loop:
        A(a[pos-1], count, True)
    elif pc[pos-1] == 0:
        pc[pos-1] = count
        # print(a[pos-1])
        A(a[pos-1], count, False)
    else:
        k = (k - pc[pos - 1]) % (count - pc[pos - 1])
        # print(str(a[pos-1]) + " : loopStart")
        A(a[pos-1], -1, True)

A(1, 0, False)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int,input().split()))
timestamp = [0]*n
position = 1
period = 0
start=0
for i in range(n):
  if timestamp[position-1] != 0:
    start = timestamp[position-1]
    period = i-timestamp[position-1]
    break
  timestamp[position-1] =i
  position = a[position-1]
print(period,start)


if k <=start:
  position = 1
  for i in range(k-1):
    position = a[position-1]
  print(position)
  
else:
  cycle = (k-start)%period
  for i in range(cycle):
    position = a[position-1]
  print(position)

Submission status: Runtime Error

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

route_list = [1]
count = 1
now = A[0]-1
while count <= K:
  if A[now] in route_list:
    index = route_list.index(A[now])
    Q = K - index
    R = Q%(now - index)
    if R == 0:
      print(A[now - 1])
    else:
      print(A[index+R-1])
    break
  else:
    route_list.append(A[now])
    now = A[now] - 1

Submission status: Runtime Error

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:
        loop.append(x)
        x = c[x] - 1

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

Solution code:
##問題4
N,K= input().split()
tereports=list( int(x) for x in input().split())
visit_num=np.zeros(len(tereports)).astype(int)
print(visit_num)
i=0 #counter
townnum= 0
next_townnum=0 # tereports[terenum] = 次の街名
ans=1 #現在のテレポート先
while i< int(K)-1 :
  i+=1
  
  townnum=next_townnum
  
  if visit_num[townnum-1 ]!=0:
    syuuki=i-visit_num[townnum-1]
    nokori=(int(K)-1-i) % (syuuki)

    
    i= int(K)-1-nokori

  visit_num[townnum-1]=i
  next_townnum=tereports[townnum-1]
  
  
print(townnum)

Submission status: Runtime Error

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>k:
    print(replis[(k-st-len(replis))%len(replis)])
else :
    print(place[k])

Submission status: Runtime Error

Solution code:
def teleport(n):
    global flag
    if flag:
        return 0
    else:
        if n in ans_list:
            flag=True
        ans_list.append(n)
        teleport(A[n])
n=0
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.insert(0,0)
ans_list=[]
flag=False
teleport(A[1])
x=ans_list.index(ans_list[-1])
loop_list=ans_list[x:-1]
y=len(ans_list)-1
K=K-y
loop_y=len(loop_list)
print(loop_list[(K-len(loop_list))%(len(loop_list)-1)])

Submission status: Runtime Error

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

A_list = [int(a) for a in input().split()]

# print(N, K)
# print(A_list)

keiro = [1]
visit_town = [0]*N

town = 1
c = 1
while True:
    visit_town[town - 1] = c
    c += 1
    town = A_list[town - 1]
    # print('town', town)

    if len(keiro) > 1 and visit_town[town-1] != 0:
        break
    keiro.append(town)

# print(keiro)
# print(visit_town)
# print(town)


first_visit = keiro[:visit_town[A_list[keiro[-1]-1]-1]-1]
loop_visit = keiro[visit_town[A_list[keiro[-1]-1]-1]-1:]

print(first_visit)
print(loop_visit)

if K <= len(first_visit):
    print(first_visit[k - 1])
else:
    l = K - len(first_visit)
    # print(l)
    c = (l-1) % len(loop_visit)
    # print(c)
    print(loop_visit[c-1])

# print(keiro[len(first_visit) + (K - len(first_visit)) % len(loop_visit)])

Submission status: Runtime Error

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

x=1
l=deque()
l.append(1)
for i in range(k):
    y=a[x-1]
    x=y
    if y not in l:
        l.append(y)
    else:
        break

mod=(k-d)%(d-j)

print(l[mod+j])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=[q-1 for q in LI()]

visited=[-1]*(n+10)
now=0
visited[now]=0

if k<=2*10**5:
    for i in range(k):
        nxt=a[now]
        now=nxt
    print(now)
    exit()

for i in range(k):
    nxt=a[now]
    if visited[nxt]==-1:
        visited[nxt]=i
    else:
        m=i-visited[nxt]
        break
for i in range(k%m+m):
    nxt=a[now]
    now=nxt
print(now)
exit()

Submission status: Runtime Error

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

i = 1
root = [i]
while not A[i-1] in root:
  root.append(A[i-1])
  i = A[i-1]

print(root[K % len(root)+1])

Submission status: Runtime Error

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

counter=1
dest=1
To=[]

def judge(a):
  j=1
  count=1
  for i in range(len(a)-1):
    if a[i]==a[len(a)-1]:
      count=0
      break
    j+=1
  
  b=[count,j]
  return b
    
def change(a,i):
  answer=[]
  for j in range(i,len(a)):
    answer.append(a[i])
    
  return answer
for i in range(K):
  dest=int(town[dest-1])
  To.append(dest)
  if (judge(To)[0])==0:
    To=change(To,judge(To)[1])
    print(To[((K-(judge(To)[1]+1))%(counter-judge(To)[1]))-1])
    break
  counter+=1

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
N,K = map(int, input().split())		# 5 7 2
A = [0] + list( map(int, input().split()) )	# 1 2 3 4 5 ...


M = [ [0,0] for _ in range(N+1) ]			#=> [[], [], [], [], [], [], [], []]

p(M)

i = 1
x = y = 0
for k in range(1, K+1):
	x,y = M[i]
	
	if x==0:
		M[i][0] = A[i]
		M[i][1] = k
		i = A[i]
	
	else:
		
		break

else:
	ans = i
	print(ans)
	exit(0)


p(x,y,k)
p("M[1:]", M[1:])

k = k - y

j = k % (k-y)

i = 
for a in range(j):
	x,y = M[i]
	M[i][0] = A[i]
	M[i][1] = k
	i = A[i]

print(i)

Submission status: Runtime Error

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

from collections import deque 
tmp = 1
num = deque()
A_list = deque([[] for i in range(N+1)])
 
for i in range(N):
    num.append(tmp)
    A_list[tmp].append(i)
    if tmp == A[tmp-1]:
        judge = tmp
        flag1 = 1
        break
    tmp = A[tmp-1]
    if tmp in num:
        judge = tmp
        flag2 = 1
        break

if flag2 == 1:
    num_index = A_list[judge]
    print(num[(K-num_index[0])%(len(num)-num_index[0]) + num_index[0]])
elif flag1 == 1:
    if K > len(num):
        print(judge)
    else:
        print(num[K-1])

Submission status: Runtime Error

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

d = []
for i in range(n):
    d.append(i+1)

j = list(set(a)^set(d))

# a = list(set(a)-set(i))

for i in j:
    a[i-1] = 0

b = [0]*n

for i in range(n):
    if i==0:
        b[0] = a[0]
    else:
        b[i] = a[b[i-1]-1]
    
    d.append(i+1)


b = sorted(set(b), key=b.index)
b.remove(0)
c = k%len(b)
print(a[c])

Submission status: Runtime Error

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

start = 0
atesaki = 0
start_li = [0]
atesaki_li = []
count = 0
target = atesaki
for i in range(k):
    atesaki = li[start]-1
    start = atesaki
    if atesaki in atesaki_li:
        count = i+1
        break
    else:
        atesaki_li.append(atesaki)
    #print(atesaki,'tes')

first_ind = atesaki_li.index(atesaki)+1
#print(atesaki_li,atesaki,first_ind)

atesaki_li[(k-len(atesaki_li))%(len(atesaki_li)-first_ind+1)]
print(atesaki_li[(k-len(atesaki_li))%(len(atesaki_li)-first_ind+1)])+1

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import random
import time
def solve(N,K,A):
    # A = [i for i in range(2,2*10**5+2)]
    # N = 2*10**5
    t = time.time()
    ans = [1]
    i = 0
    j = 0
    ans_append = ans.append
    while N != 0:
        if(A[i] in ans):
            if(K <= len(ans)):
                return ans[K]
            else:
                index = ans.index(A[i])
                K -= len(ans[:index])
                ans = ans[index:]
                break
        else:
            ans_append(A[i])
            i = A[i] - 1
            j += 1
            N -= 1
    # print(ans,K % len(ans))
    # print(time.time() - t)
    return ans[K % len(ans)]
def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    # solve()
    print(solve(N,K,A))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

N,K=map(int,input().split())
T=list(map(int,input().split()))
M=0
A=[1]
for x in range(N+1):
    if T[M] in A or T[M]==M+1:
        key=T[M]
        break

    else:
        t=T[M]
        A+=int(t)
        M=t-1
B=len(A)
C=A.index(key)
D=B-C
if K<=B-1:
    F=A[K]
    print(F)
else:
    E=(K+1-C)%D
    if E==0:
        E=D
    F=A[C+E-1]
    print(F)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = [int(n) for n in input().split()]
dest = [1]
i = 1
while i < K:
    for j in range(len(dest)):
        if A[dest[-1]-1] == dest[j]:
            move = j
            dest = dest[j:]
            break
    else:
        dest.append(A[dest[-1]-1])
        i += 1
        continue
    break
print(A[dest[(K-move)%(len(dest))-1]-1])

Submission status: Runtime Error

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

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

a=0
i=0
l=[1]
while i<K and i<len(set(l)):
    a=A[a]
    l.append(a)
    a=a-1
    i+=1
else:
    b=l.index(l[-1])
    c=K-b
    del l[:3]
    d=len(set((l)))
    e=K%d
    a=l[e]-1
print(a+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
num = [a[0]]
while True:
    for i in range(len(num)-1):
        if num[i] == num[-1]:
            break
    num.append(a[num[-1]])
index = index(a[-1])
lstart = len(a[:index-1])
lroop = len(a[index])
number = (k-lstart)%lroop
for i in range(lroop-1):
    number = a[number-1]
    print(number)
print(number)

Submission status: Runtime Error

Solution code:
from collections import deque
import numpy as np
n,k = map(int,input().split())
a = list(map(int,input().split()))
roop_find = deque([0])
i = 0
count = 0
while a[i]-1 not in roop_find:
    roop_find.append(a[i]-1)
    i = a[i] - 1
    count += 1
    if count == k:
        print(i + 1)
        break
    else:
        pass
if b < k:
    b = roop_find.index(a[i]-1)
    roop = list(roop_find)[b:count+1]

    k -= b
    e = k % (count+1-b)
    print(roop[e]+1)

Submission status: Runtime Error

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

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

    itta_list = set()
    idou_num = 0

    pos = 0

    for i in range(K):
        # itta_list記憶
        if pos not in itta_list:
            itta_list.add(pos)
        else:
            idou_num = i
            break

        # 次の街へ
        pos = A[pos] - 1
    else:
        ans_num = pos + 1
        print(ans_num)
        return

    roop_list = itta_list[itta_list.index(pos):]

    amari = (K - idou_num) % len(roop_list)

    ans_pos = roop_list[amari] + 1

    print(ans_pos)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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:cnt]
		break
loop=len(S)-cnt2-1


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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ten=0
def telepo(p):#pはリストのインデックスｐ
    t=a[p-1]#現在地の更新
    #次のインデックスを返す
    return t
ten=telepo(ten)
#print(ten)
ton=[1]
c=0
d=0
for i in range(k):
    ton.append(telepo(ton[i]))
    #print(ton)
    if ton[:i].count(ton[i])==1:
        #print(ton[:i])
        #print(len(ton[1:i]))
        c=i
        d=ton[:i].index(ton[i])
        break
#print(c-d)
ans=[]
for i in ton[d:c]:
    ans.append(i)
for i in ans:
    ans.append(i)
    if len(ans)>6:
        break
#print(ans)
print(ans[(k-1)%(c-d)+1])

Submission status: Runtime Error

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

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

l = 0 #ループの始まり

l = m[(k - cnt)%(loop)]

print(l)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    flag = True
    D = 0
    now = 1
    LOG = {}
    PATH = []
    for i in range(N):
        PATH.append(D)
        LOG.setdefault(D,i)

        if LOG[D] == i:
            D = A[D]-1
            continue
        else:
            print("break",D)
            start = D
            break
        # print(PATH, D)
    K = K%i
    LAST = PATH[LOG[D] + K] +1
    print(LAST)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# 初期入力
from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
connection ={}
for i,v in enumerate(A):
    connection[i+1] =v 

#巡回してるか
passed=[1]
nex =connection[1]
lap =[]
for i in range(1,N+1):
    passed.append(nex)
    c=Counter(passed)
    if 2  not in c.values():
        nex =connection[nex]
    else:
        lap =deepcopy(passed)
        indx_s =passed.index(nex) +1
        indx_f =len(lap)
        lap_num =indx_f -indx_s
        break

mod =(K -indx_s)//lap_num
ans =passed[indx_s +mod]
print(ans)

Submission status: Runtime Error

Solution code:
import math
I = [input() for i in range(2)]
W = I[0].split()
A = list(map(int,I[1].split()))
N = int(W[0])
K = int(W[1])

n = 0
Sn = str()
for i in range(N) :
    #print(n)
    n = A[n-1]
    Sn = Sn + str(n)
    c = Sn.count(str(n))
    if c == 2 :
        j = 0
        l = i - 1
        x = Sn.find(str(n))
        break
    j = 1
    
if j == 0 :
    M = (K-x)%l
    #print (Sn)
    #print(M)
    ans = int(Sn[M-1])
elif j == 1 :
    ans = int(n + 1)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
sys.setrecursionlimit(410000)

x = inpl()
n = x[0]
k = x[1]
m = [0 for i in range(0, 200001)]
dic = [0 for i in range(0, 200001)]

A = inpl()

t = 0
i = 0
roop = 0
while True:
    if i == k:
        print(t + 1)
        break
    if m[t] == 0:
        m[t] = 1
        dic[t] = i
        t = A[t] -1
    else:
        roop = i - dic[t]
        break
    i += 1

for i in range(0, (k - i) % roop):
    t = A[t] - 1

print(t + 1)

Submission status: Runtime Error

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

town = 1
wpList = []
for i in range(k+1):
    if wpList.count(a[town-1]) > 0:
        rp_st = wpList.index(a[town-1])
        rp_len = i
        break
    wpList.append(a[town-1])
    town = a[town-1]
print(wpList[(k-rp_st)%rp_len-1])

Submission status: Runtime Error

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

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

lis = [1]
idx = 0

check_l = [0] * N
for i in range(N):
  lis.append(A[idx])
  if check_l[A[idx]] != 1:
    lis.pop(-1)
    found = lis.index(A[idx])
    lis.append(A[idx])
    break
  check_l[A[idx]] == 1
  idx = A[idx]-1
  
print(lis[found:i+1][(K-found) % (i - found + 1)])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = [int(input()) for _ in range(n)]
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

print(B[k-1])

Submission status: Runtime Error

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

seen = [0] * (N+1)
seen[1] = 1
cnt = 1
ans = 0

def dfs(start, limit):
  global cnt
  next = A[start-1]
  if limit <= cnt:
    ans = next
    return cnt
  if seen[next] == 1:
    return cnt
  else:
    seen[next] = 1
    cnt += 1
    return dfs(next, limit)
    
loop = dfs(1, K)
step = K % loop

x= dfs(1, step)

print(ans)

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]
        copyan[memo-1]=10**6 + movecount
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        movecount += 1
        roopstart = i
else:
    print()
roopnum = movecount - yokei
x = (k-yokei)%roopnum #移動する回数

count = 0
iti = an[roopstart]
while True:
    
    if x == 0:
        print(roopstart)
        exit()
    else:
        iti = an[iti-1]
        
        count += 1
        if count == x:
            print(iti+1)
            exit()

Submission status: Runtime Error

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(k/n):
  history.append(place)
  place = A[(int(place)-1)]
  if place == 1:
    amari = k % len(history)
    print(history[amari])
    sys.exit()

Submission status: Runtime Error

Solution code:
n , k = map(int, input().split())
dic = {}
data = input().split()
for i in range(n):
    dic.setdefault(i+1 , int(data[i]))

# find common
tmp = {}
common  = []
next_t = dic[1]
for i in range(10):
    if next_t not in tmp :
        tmp.setdefault(next_t,1)
    else:
        tmp[next_t]+=1
    if tmp[next_t] ==2:
        common.append(next_t)
    if tmp[next_t] > 2:
        break
    next_t = dic[next_t]




if dic[1] in common:
    k = k-1
    print(common[k%len(common)])
else:
    k+=1
    print(common[k%len(common)])

Submission status: Runtime Error

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
print(path)
c = Counter(path)
roop1 = 0
for k, v in c.items():
    if v == 2:
        roop1 = k
if roop1 == 0:
    roop1 = 1
roopidx = path.index(roop1)
roopidx2 = path.index(roop1, roopidx+1)
print(roopidx,roopidx2)
k_mod = (k-roopidx) % (roopidx2-roopidx)
print(path[k_mod+roopidx-1])

Submission status: Runtime Error

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]
flag = False

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

if flag:
    r = len(mod) - idx
    k -= idx
    mod = mod[idx:]
    print(mod[k % r] + 1)
else:
    print(mod[k]+1)

Submission status: Runtime Error

Solution code:
import math

n, k = map(int, input().split(" "))
aL = list(map(int, input().split(" ")))
aFL = [False for _ in range(n)]

crnt = 1
cnt1 = 0
while True:
    if aFL[crnt - 1]:
        break
    cnt1 += 1
    aFL[crnt - 1] = True
    crnt = aL[crnt - 1]

wCrnt = crnt
cnt2 = 0
while True:
    crnt = aL[crnt - 1]
    cnt2 += 1
    if crnt == wCrnt:
        break

gp = cnt1 - cnt2

cr = aL[0] - 1
for i in range((k - gp) % n):
    cr = aL[cr - 1]

print(aL[cr])

Submission status: Runtime Error

Solution code:
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
li=[]
idx_li=[1]
flag=False
for i in range(k):
    idx=a[idx-1]
    if idx in idx_li:
        flag=True
        break
    idx_li.append(idx)
if flag:
    #non_loop=idx_li[:idx_li.index(idx)]
    loop = idx_li[idx_li.index(idx):]
    
    size=len(loop)
    print(loop[(k-(len(idx_li)-len(non_loop)))%size])
else:
    print(idx)

Submission status: Runtime Error

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

t = 1
tl = [1]
ts = {1}


for i in range(len(a)):
    
    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]
         
# print(tl)
# print(tl2)

ans = 0

if k <= len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
cnt=0
B={}
B[0]=1
a=1
while 1:
  cnt+=1
  a=A[a-1]
  if a in B.values():
    break  
  B[cnt]=a
B[cnt]=a
keys = [k for k, v in B.items() if v == a]

print(B[keys[0]+K%(cnt-1)])

Submission status: Runtime Error

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

lst = []
visit_now = 1
visit_list = [0] * n

while True:
    if visit_list[visit_now - 1] == 1:
        break
    visit_list[visit_now - 1] = 1
    lst.append(visit_now)
    visit_now = a[visit_now - 1]


start = lst.index(visit_now)
loop_num = n - start - 1
lst = lst[start:]

k -= start
k %= loop_num

print(lst[k])

Submission status: Runtime Error

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, K = map(int, input().split())
    A_i = list(map(int, input().split()))
    B_i = [0 for i in range(N)]
    cycle_list = []
    
    cycle = 0
    diff = 0
    counter = 0
    machi = 1
    while counter < N:
        if B_i[machi] == 1:
            diff = cycle_list.index(machi)
            cycle = counter - diff
            cycle_list = cycle_list[diff:]
            break
        else:
            counter += 1
            B_i[mahci] = 1
            cycle_list.append(machi)
            machi = A_i[machi - 1]
            
    mod_K = (K - diff) % cycle

    print(cycle_list[mod_K])

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K= map(int, input().split())
A = list(map(int,input().split()))
num = 0
count = 0
l = []
while(count < K):
  if(l.count(num) == 0):
    l.append(num)
    num = A[num-1]
  else:
    junkai = num
    ind = l.index(num)
    le = len(l) - ind-1
  count += 1
ans = K - le
ans = ans%(len(l)-1)
print(A[ans])

Submission status: Runtime Error

Solution code:
def Teleport(i,P):
    if(i==K):
        return print(P)
    else:
        Teleport(i+1,T[P-1])



N,K=(int(x) for x in input().split())
T=list((int(x) for x in input().split()))

Teleport(1,T[0])

Submission status: Runtime Error

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

town_check = {0}
town_order = [0]
town = 0
check1 = True
while check1:
    next_town = A[town] - 1
    if next_town not in town_check:
        town = next_town 
        town_check.add(town)
        town_order.append(town)
    else:
        loop = (next_town, town)
        check1 = False
        
if K-2 <= len(town_order):
    print(town_order[K]+1)
else:
    a = town_order.index(loop[0])
    b = len(town_order) - 1
    c = b - a + 1
    print(town_order[a+(K-a)%(c)]+1)

Submission status: Runtime Error

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

i = 0
memo = []
cnt = 0
cnt2 = 0
s = 0
kari = 0
while True:
    memo.append(a[i])
    i = a[i]
    if i in memo[:-1]:
        cnt2 = memo[:-1].index(i)
        cnt = len(memo[cnt2:])
        cntlist = memo[cnt2]
        break
s = (k - cnt2)%cnt
ans = cntlist[s]

print(ans)