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:
num_town, num_move = map(int, input().split())
teleports = list(map(lambda x: int(x) - 1, input().split()))

current = 0
memo = {}
for i in range(num_move):
    if memo.get(current) == 2:
        break
    if not memo.get(current):
        memo[current] = 1
    else:
        memo[current] += 1
    destination = teleports[current]
    current = destination

loop = {k:v for k,v in memo.items() if v==2}
minus = {k:v for k,v in memo.items() if v==1}

new_move_count = (num_move - len(minus)) % len(loop)

if num_move < new_move_count:
    new_move_count = num_move
    current = 0

for i in range(new_move_count):
    destination = teleports[current]
    current = destination

print(current + 1)

Submission status: Runtime Error

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

visited = []
order = {}
x = 1
T = -1
while T < 0:
  x = a[x-1] #添字に直す
  if x not in visited:
    visited.append(x)
    order[x] = len(visited)-1
  else:
    T = (len(visited)-1) - order[x]
    s = order[x]
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Runtime Error

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

t = 0
now = 1
footprint = [1]

while t < K:
  if A[now-1] not in footprint:
    footprint.append(A[now-1])
    now = A[now-1]
    t += 1
    continue
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = K % l
    now = footprint[i+e]
    break

print(now)

Submission status: Runtime Error

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

s = 0
t = 0
u=[]
for i in range(100000):
    u.append(0)

spot=1
for i in range(K):
    if u[spot-1] == 0:
        u[spot-1] = 1
    else:
        if spot == 1:
            s = 0
            t = i
        else:
            tmp = 1
            for j in range(i):
                tmp = A[tmp-1]
                if tmp == spot:
                    s = j + 1
                    t = i - s
                    break
            break
    spot=int(A[spot-1])

if t != 0:
    ans = K - s
    ans = ans % t
    spot = 1
    print(s)
    print(t)
    print(ans)
    for i in range(s + ans):
        spot=int(A[spot-1])

print(spot)

Submission status: Runtime Error

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

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

ord = [-1 for _ in range(N+1)]
v = 1
root = []

while ord[v] == -1:
    #ordに何回目のワープかが記録される
    ord[v] = len(root)
    #ワープの軌跡を記録
    root.append(v)
    #次の地点にワープ
    v = A[v-1]

# print("len(root)", len(root))
# print("ord[v]", ord[v])
# print("ord", ord)
# print("root", root)

#ord[v]:最終地点（ループ起点 or ループ前の道のり）
l = ord[v]
#ループ距離
c = len(root) - l

if K < l:
    #ループ前にワープが終了した場合、最終地点を出力
    ans = root[K]
else:
    #ループする回数を計算
    K -= l
    K %= c
    res = root[l+K]
print(res)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
def B(i):
  if i==0:
    return 1
  return A[B(i-1)-1]
C=[1]
for i in range(1,N+1):
  if C.count(B(i))==0:
    C.append(B(i))
  else:
    l=C.index(B(i))+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-1])
print(C)

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

if i != 1:
    while True:
        memo = i
        if k == movecount:
            print(i)
            
            exit()
        i = copyan[i-1]
        
        if i >=30000:
            yokei = i%(10**6)         
            break
        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())
A=list(map(int, input().split()))
cnt=0
B=[-1]*(N+1)
a=1
while B[a]==-1:
  cnt+=1
  B[a]=cnt
  a=A[a-1]
  #print(B)
  
#print(a,cnt)
c=cnt-a
K=K%c
if a==1:
  print(A[K+1])
else:
  print(A[K])

Submission status: Runtime Error

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

s = 0
t = 0
u=[]
for i in range(100000):
    u.append(0)

spot=1
for i in range(K):
    if u[spot-1] == 0:
        u[spot-1] = 1
    else:
        tmp = 1
        for j in range(i):
            tmp = A[tmp-1]
            if tmp == spot:
                s = j + 1
                t = i - s
                break
        break

    spot=int(A[spot-1])

if t != 0:
    ans = K - s
    ans = ans % t
    spot = 1
    for i in range(s + ans):
        spot=int(A[spot-1])

print(spot)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
visited = [-1]*n
route = []
now = 0
hop = 0
while visited[now] == -1:
    visited[now]=hop
    route.append(now)
    now = a[now]
    hop+=1
loop_st = 0
for r in route:
    if r==now: break
    loop_st+=1
loop = route[loop_st:]
if k-1 < len(route):
    print(route[k]+1)
else:
    print(loop[(k-loop_st)%len(loop)]+1)

Submission status: Runtime Error

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if c>k:
    if c-1>=k:

        print(b[k])
        exit()
if 1 not in b:
    # print(b,f)
    ans=(k-e)%f
    print(b[ans])
else:

    ans=k%f
    print(b[ans])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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

    count = -1
    visit = [-1]*N
    loopstart = 0
    nextcity = 1
    loopsize = 0

    for n in range(N):
        count += 1
        visit[nextcity-1] = count
        nextcity = A[nextcity-1]
        if visit[nextcity-1] != -1:
            print(f'{count=}')
            loopstart = nextcity
            loopsize = count - visit[loopstart-1] + 1
            break
    
    print(visit)
    print(f'{loopstart=}')
    
    toloop = visit[loopstart-1]
    print(f'{toloop=}')
    
    print(f'{loopsize=}')
    if loopsize > K:
        print("loopsize > K")
        nextcity = 1
        for k in range(K):
            nextcity = A[nextcity-1]
        print(nextcity)
    else:
        print("loopsize < K")
        res = (K - toloop) % loopsize
        print(f"{res=}")

        nextcity = loopstart

        for k in range(res):
            nextcity = A[nextcity-1]
        
        print(nextcity)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

s = 0
t = 0
u=[]
for i in range(100000):
    u.append(0)

spot=1
for i in range(K):
    if u[spot-1] == 0:
        u[spot-1] = 1
    else:
        tmp = 1
        for j in range(i):
            tmp = A[tmp-1]
            if tmp == spot:
                s = j + 1
                t = i - s
                break
        break

    spot=int(A[spot-1])

if t != 0:
    ans = K - s
    ans = ans % t
    spot = 1
    for i in range(s + ans):
        spot=int(A[spot-1])

print(spot)

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:]

if k <= start:
    ans = lst[k]
else:
    k -= start
    k %= loop_num
    ans = lst[start + k]

print(ans)

Submission status: Runtime Error

Solution code:
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

if k <= start:
    ans = lst[k]
else:
    k -= start
    k %= loop_num
    ans = lst[start + k]

print(ans)

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

if k <= start:
    ans = lst[k]
else:
    k -= start
    k %= loop_num
    ans = lst[start + k]

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
ls=list(map(int,input().split()))
ps=[]
length=0
goto=1
while True:
    idx=goto-1
    goto=ls[idx]
    if goto==-1:
        break
    ps.append(goto)
    ls[idx]=-1
    length+=1

amari=(k-1)%3
print(ps[amari])

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
    n, k = map(int, input().split())
    a = list(map(lambda x: int(x)-1, input().split()))
    count = 0
    foot = [None]*2*10**5
    foot[0] = 0
    is_loop = False

    for i in range(1,k):
        foot[i] = a[foot[i-1]]
        if foot.count(foot[i]) == 2:
            st_i = foot.index(foot[i], 0, i)
            lo_size = i - st_i
            loop = foot[st_i:i]

            k -= st_i
            k %= lo_size
            
            print(loop[k]+1)
            is_loop = True
            break
    
    if not is_loop:
        print(foot[k]+1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
ls=list(map(int,input().split()))
ps=[]
length=0
goto=1
for _ in range(100):
    idx=goto-1
    goto=ls[idx]
    if goto==-1:
        break
    ps.append(goto)
    ls[idx]=-1
    length+=1

amari=(k-1)%3
print(ps[amari])

Submission status: Runtime Error

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

gone = []

isLooped = False


def dfs(index):
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]


loopStartIndex = gone.index(Alist[last-1])
if K > loopStartIndex:
    divK = (K - loopStartIndex) % len(gone[loopStartIndex:])
    print(gone[divK])
else:
    print(gone[K])

Submission status: Runtime Error

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

a = 1 #現在地
count = 0 #ワープした数
toLoop = 0 #ループに入るまでのワープ回数
inLoop = 0 #ループ1周のワープ数
route = list()
route_set = set()

while count <= K:
	route.append(a)
	route_set.add(a)

	count += 1
	a = A[a-1]

	if a in route_set:
		start = route.index(a)
		if start == 0:
			inLoop = count
		else:
			toLoop = start
			inLoop = len(route) - toLoop
		break

if K < toLoop+inLoop:
	print(route[K])
else:
	print(route[toLoop+(K-toLoop)%inLoop])

Submission status: Runtime Error

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if n>k:
    print(b[k])
    exit()
if 1 not in b:
    ans=(k-e)%f
    print(b[ans])
else:
    # print(b,f)
    ans=k%f
    print(b[ans])

Submission status: Runtime Error

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

visited = [0]
order = {}
x = 0
T = -1
i = 0
while T<0:
  x = a[x]
  i+=1
  if x in visited:
    T = i - order[x]
    s = order[x]
    break
  else:
    visited.append(x)
    order[x] = i
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Runtime Error

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

visited = [0]
order = {}
x = 0
T = -1
i = 0
while T<0:
  x = a[x]
  i+=1
  if x in visited:
    T = i - order[x]
    s = order[x]
    break
  else:
    visited.append(x)
    order[x] = i
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Runtime Error

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

a = 1 #現在地
count = 0 #ワープした数
toLoop = 0 #ループに入るまでのワープ回数
inLoop = 0 #ループ1周のワープ数
route = list()

while count <= K:
	route.append(a)

	count += 1
	a = A[a-1]

	if a in route:
		start = route.index(a)
		if start == 0:
			inLoop = count
		else:
			toLoop = start
			inLoop = len(route) - toLoop
		break

if K < toLoop+inLoop:
	print(route[K])
else:
	print(route[toLoop+(K-toLoop)%inLoop])

Submission status: Runtime Error

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]

if isLooped:
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    if divK < loopStartIndex:
        print(gone[divK])
    else:
        print(gone[divK])
else:
    print(gone[K])

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

if i != 1:
    while True:
        memo = i
        if k == movecount:
            print(i)
            exit()
        i = copyan[i-1]
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        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 = list(map(int,input().split()))
a = list(map(int,input().split()))

now=1
count=0

cities = [1]+[0]*(n)

for i in range(1,k+1):
    tmp=a[now-1]
    if tmp in cities[:i]:
        init_city=cities.index(tmp)
        if init_city>k:
            print(cities[k])
        else:
            print(cities[init_city:i][(k-init_city)%(i-init_city)])
        break
    else:
        cities[i]=tmp
        now=tmp
else:
    print(tmp_city[init_city+1])

Submission status: Runtime Error

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]

if isLooped:
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    print(divK)
    if divK < loopStartIndex:
        print(gone[divK])
    else:
        (gone[divK+1])

Submission status: Runtime Error

Solution code:
#D
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
nxt = 1
loop = []
tset = set()
final = -1
while 1:
    loop.append(nxt)
    tset.add(nxt)
    nxt = a[nxt-1]
    if nxt in tset:
        final = nxt #重複した町
        break
m = -1
for i in range(len(loop)):
    if loop[i]==final:
        m = i
        break
l = len(a)
if k < m:
    print(loop[k])
else:
    print(loop[(k+1-m)%(l-m)])
# print(loop[(k+1)%l])

Submission status: Runtime Error

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]

if isLooped:
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    print(Alist[divK])

Submission status: Runtime Error

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

journey = [1]

for i in range(N+1000):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K <= N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N
 
while(journey[loop_start] != journey[loop_end]):
    loop_start -= 1
    
period = loop_end - loop_start

K %= period
while K < N:
  K += period

print(journey[K])

Submission status: Runtime Error

Solution code:
import numpy as np
from itertools import product
n, m, x = [int(x) for x in input().split()]
a = np.stack([np.array(input().split(), dtype=int) for _ in range(n)])
tot = np.sum(a, axis=0)
if min(tot[1:]) < x:
    print(-1)
    exit(0)
cost = tot[0]
whole = np.array(list(product([0,1],repeat=n)), dtype=bool)[1:-1]
for flag in whole:
    tot = np.sum(a[flag], axis=0)
    if min(tot[1:]) >= x and tot[0] < cost:
        cost = tot[0]
print(cost)

Submission status: Runtime Error

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

visited = [0]*N
i = 0
rootin = [0]*N
rootin[0] = 1
count = 0
for _ in range(N):
  count += 1
  visited[i] += 1
  if visited[i] == 2:
    end = count-1
    break
  rootin[count] = A[i]
  i = A[i]-1
start = 0
for i in range(N):
  if rootin[i] == rootin[end]:
    start = i
    break
od = rootin[:start]
root = rootin[start:end]

K -= len(od)
if K%(len(root)-1) == 0:
  ans = root[-1]
else:
  ans = root[K%len(root)]

Submission status: Runtime Error

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

visited = [0]*N
i = 0
rootin = [0]*N
rootin[0] = 1
count = 0
for _ in range(N):
  count += 1
  visited[i] += 1
  if visited[i] == 2:
    end = count-1
    break
  rootin[count] = A[i]
  i = A[i]-1
start = 0
for i in range(N):
  if rootin[i] == rootin[end]:
    start = i
    break
od = rootin[:start]
root = rootin[start:end]

if K >= len(od):
  K -= len(od)
  if K%(len(root)-1) == 0:
    ans = root[-1]
  else:
    ans = root[K%len(root)]
else:
  ans = od[K-1]
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[1]
for i in range(k-1):
    if not(a[b[i]-1] in b):
        b.append(a[b[i]-1])
    else:
        r=i-b.index(a[b[i]-1])
        amari=(k-i)%r
        p=b[b.index(a[b[i]-1])+amari+1]
        t=1
        break
if t==1:
    print(p)
else:
    print(b[-1])

Submission status: Runtime Error

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

idx = [-1 for _ in range(len(A))]
now = 0
period = 0
bias = 0
tmp = []
for i in range(K+1):
    tmp.append(now+1)
    if idx[now] == -1:
        idx[now] = i
    else:
        period = i - idx[now]
        cycle = tmp[-period-1:-1]
        bias = i - period
        break
    now = A[now]-1

if K <= bias:
    now = 0
    for _ in range(K):
        now = A[now] - 1
    print(now + 1)
else:
    K -= bias
    K = K % period
    print(cycle[K])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[1]
for i in range(k-1):
    if not(a[b[i]-1] in b):
        b.append(a[b[i]-1])
    else:
        r=i-b.index(a[b[i]-1])
        #繰り返しの単位
        amari=(k-i)%r
        p=b[b.index(a[b[i]-1])+amari+1]
        #リストbの中での位置
        t=1
        break
if t==1:
    print(p)
else:
    print(b[-1])

Submission status: Runtime Error

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

ans = 0
b = [1]
for i in range(k):
  if a[b[-1] - 1] in b:
    c = a[b[-1] - 1]
    break
  b.append(a[b[-1] - 1])

d = b[:b.index(c)]
b = b[b.index(c):]

k = k - len(d)
k = k % len(b)
#print(d,b,k)
ans = b[k]
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
def B(i):
  if i==0:
    return 1
  return A[B(i-1)-1]
C=[1]
for i in range(1,N+1):
  if C.count(B(i))==0:
    C.append(B(i))
  else:
    l=C.index(B(i))+1
    break
if K<=l-1:
  print(C[K-1])
else:
  print(C[l-2+(K+2-l)%(len(C)-l+1)])

Submission status: Runtime Error

Solution code:
import sys

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]
      count+=1
      if count==K+1:
        print(b)
        sys.exit()
    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())
A = tuple(map(int, input().split()))

costs = [-1] * (N + 1)

loc = 1
costs[loc] = 0
for i in range(1, K):
    loc = A[loc - 1]

    if costs[loc] >= 0:
        break

    costs[loc] = i

offset = costs[loc]
loop = i - offset

K -= i
K %= loop

for _ in range(K):
    loc = A[loc - 1]
print(loc)

Submission status: Runtime Error

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

costs = [0] * (N + 1)

loc = 1
for i in range(1, K+1):
    loc = A[loc - 1]

    if costs[loc] > 0:
        break

    costs[loc] = i

offset = costs[loc]
loop = i - offset

K -= i
K %= loop

for _ in range(K):
    loc = A[loc - 1]
print(loc)

Submission status: Runtime Error

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

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

if now != 0:
  k -= now+1
  for i in range(now+1):
    history.pop(0)     

print(history[k % len(history)])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [1]
for i in range(k):
    if a[l[i] - 1] in l:
        res = l[:l.index(a[l[i] - 1])]
        cyc = l[l.index(a[l[i] - 1]):]
        break
    else:
        l.append(a[l[i] - 1])
print(cyc[(k - len(res)) % len(cyc)] if k > len(res) - 1 else res[k])

Submission status: Runtime Error

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

ans = 0
b = [1]
for i in range(k):
  if a[b[-1] - 1] in b:
    c = a[b[-1] - 1]
    break
  b.append(a[b[-1] - 1])

d = b[:b.index(c)]
b = b[b.index(c):]

k = k - len(d) - k // len(b) * len(b)
#print(d,b,k)
ans = b[k]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

n, k = map(int, input().split())
a = np.array(input().split(), dtype='int')
len_max = n
array1 = [1]
now_city = 1
loop = 0
flg = 0

for i in range(max_len):
    now_city = a[now_city - 1]
    if np.any(array1 == now_city) and i != 0:
        flg = 1
        loop_begin = np.where(array1 == now_city)[0][0]
        loop_end = i + 1

        now_city = 1
        for i in range(loop_begin):
            now_city = a[now_city - 1]
        k -= loop_begin
        loop = loop_end - loop_begin
        k = k % loop
        for i in range(k):
            now_city = a[now_city - 1]
        ans = now_city

        break
    if i < len_max:
        array1.append(now_city)

    if i == k - 1:
        ans = now_city
   

print(ans)

Submission status: Runtime Error

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

journey = [1]

for i in range(N):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K < N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N

finished = False
for i in range(1,N):
    for j in range(i+1,N+1):
        if journey[i] == journey[j]:
            loop_start = i
            loop_end   = j
            finished = True
            break
    if finished:
        break

period = loop_end - loop_start

K %= period
while K < period:
  K += period

print(journey[K])

Submission status: Runtime Error

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

journey = [1]

for i in range(N):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K < N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N

finished = False
for i in range(1,N):
    for j in range(i+1,N+1):
        if journey[i] == journey[j]:
            loop_start = i
            loop_end   = j
            finished = True
            break
    if finished:
        break

period = loop_end - loop_start

ans = K % period
while ans < period:
  ans += period

print(journey[ans])

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
def B(i):
  if i==0:
    return 1
  return A[B(i-1)-1]
C=[1]
for i in range(1,N+1):
  if C.count(B(i))==0:
    C.append(B(i))
  else:
    l=C.index(B(i))+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-2+(K+2-l)%(len(C)-l+1)])

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
t = [1]
x = [0]*n
x[0] = 1
y = 1
z = 0
d = 0
for i in range(k):
    t.append(a[t[i]-1])
    if x[a[t[i]-1]-1] != 0:
        y = x[a[t[i]-1]-1]
        z = i+2
        b = [a[t[i]-1]]
        break
    else:
        x[a[t[i]-1]-1] += i+2
        z = i+2

d = z-y


if d != 0:
    r = (k - y) % d
else:
    r = k - y

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

print(b[r+1])

Submission status: Runtime Error

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

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%(len(check)-check.index(i)))  
else: 
    print(i+1)

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
t = [1]
x = [0]*n
x[0] = 1
y = 1
z = 0
d = 0
for i in range(k):
    t.append(a[t[i]-1])
    if x[a[t[i]-1]-1] != 0:
        y = x[a[t[i]-1]-1]
        z = i+2
        b = [a[t[i]-1]]
        break
    else:
        x[a[t[i]-1]-1] += i+2
        z = i+2

d = z-y

print(t)
print(x)
print(y)
print(z)

if d != 0:
    r = (k - y) % d
else:
    r = k - y

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

print(b[r+1])

Submission status: Runtime Error

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

ans = 0
b = [1]
for i in range(k):
  if a[b[-1] - 1] in b:
    c = a[b[-1] - 1]
    break
  b.append(a[b[-1] - 1])

d = b[:b.index(c)]
b = b[b.index(c):]

k = k - len(d) - k // len(b) * len(b)
#print(d,b,k)
ans =a[k]
#print(ans)
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)

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] + 1)

Submission status: Runtime Error

Solution code:
def d():
    N, K = map(int, input().split())
    # A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[warps[-1] - 1]
        # print('city, warps = {}, {}'.format(city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()

Submission status: Runtime Error

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

def teleport(graph, times):
    now = 1
    for i in times:
        now = graph[i]
    return now

idx = 1
seen = [-1 for _ in range(N+1)]
states = []
for i, n in enumerate(range(N+1)):
    if seen[idx] >= 0:
        break
    states.append(idx)
    seen[idx] = i
    idx = A[idx]

loop_size = i - seen[idx]
firsts = len(states) - loop_size
loop = states[firsts:]
    
if K <= firsts:
    print(teleport(A, K))
else:
    remain = (K - firsts) % len(loop)
    print(loop[remain])

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:
    next_town = A[path[-1]] - 1
    path.append(next_town)
    if next_town in set(path[:-1]) and iter > 0:
        break
    iter += 1

loop_start = path.index(path[-1])
loop_len = len(path[loop_start : -1])
goal = ((K - loop_start) % loop_len) + loop_start
print(A[goal])

Submission status: Runtime Error

Solution code:
def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[warps[-1] - 1]
        print('city, warps = {}, {}'.format(city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()

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]
idx = []

if k == 1:
    print(a[0])
    exit()
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:
        idx.append(a[i])
        break

index = idx[0]

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

# print(zyun1, zyun2)

if len(zyun1) >= k:
    ind = k % len(zyun1)
    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])


# https://atcoder.jp/contests/abc167/submissions/13051707