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()))
now = 1
if n>=k:
    for i in k:
        now = A[now-1]
        exit()
past = []
while now not in past:
    past.append(now)
    now = A[now-1]
loop_size = len(past)-past.index(now)
surplus = (k-past.index(now))%loop_size
for i in range(surplus):
    now = A[now-1]
print(now)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.insert(0,0)
position=[]
unit=[]
position.append(1)

for i in range(k):
  judge=a[position[-1]] in position
  if judge==0:#まだそのノードは未探索
    position.append(a[position[-1]])
  else:#そのノードは探索済み
    sta=position.index(a[position[-1]])
    end=i+1
    unit=position[sta:end+1]
    break

if k<sta:
  print(position[-1])
else:
  print(unit[(k-sta+1)%(end-sta)-1])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[1]
X=[True]*N
X[0]=False
num=1
for i in range(K):
    num=A[num-1]
    if X[num-1]:
        l.append(num)
        X[num-1]=False
    else:
        q=l.index(num)
        l=l[q:]
        break
print(l[(K-q)%len(l)])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
X=[1]
for i in range(K):
    x=X[-1]
    if A[x-1] in X:
        q=X.index(A[x-1])
        p=i-q+1
        break
    else:
        X.append(A[x-1])
X=X[q:]
print(X[(K-q)%p])

Submission status: Runtime Error

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

a.insert(0,0)#これでi番目の要素の位置に町iが対応する。0番目のaにはテキトーに0を入れた。
position=[]
unit=[]
position.append(1)

#探索回数1は1回エッジを渡ったということ。初期値をノード1として、
#そのあとn回の探索の結果いる場所をposition[n]で表す。


#sta->最初に重複ノードが最初に現れたposition上の要素番号
#end->重複ノードが最後に現れたposition上の要素番号
#for文のiとposition上の要素番号は一つずれていることに注意
#つまりfor文がiのとき、i+1回目の探索を示している

for i in range(k):
  #print(i+1,'回目のあと')
  judge=a[position[-1]] in position
  #print('現地点',a[position[-1]])
  if judge==0:#まだそのノードは未探索
    position.append(a[position[-1]])
    if i==k:
      ans=a[position[-1]]
  else:#そのノードは探索済み
    #print('探索済み!')
    sta=position.index(a[position[-1]])
    end=i+1
    #print('あまり',(k-sta+2)%(end-sta))
    unit=position[sta:end+1]
    if (k-sta+1)%(end-sta)==0:
      ans=unit[-1]
    else:
      ans=unit[(k-sta+1)%(end-sta)-1]
    break
    #print('あまり',(k-sta+2)%(end-sta))

#print('探索履歴',position)
#print('繰り返し->',unit)
print(ans)

Submission status: Runtime Error

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

l = [-1]*n
t = 0
s = 1
while l[s-1] == -1:
  if t == k: break
  l[s-1] = t
  t += 1
  s = a[s-1]
else:
  p = (k - l[s-1]) % (t - l[s-1])
  ans = l.index(l[s-1] + p) + 1
print(ans)

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        p = town.index(ans)
        b = town[p:]
        x = len(town)
        y = len(b)
        q = k-x+y
        if y == 0:
            print(town[n-1])
            sys.exit()
        if q < 0:
            print(town[k-1])
            sys.exit()
        z = q%y
        if z == 0:
            print(town[k-1])
        else:
            print(b[z-1])
        sys.exit()
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]
if k <= n:
    print(town[k-1])
    sys.exit()

p = town.index(ans)
b = town[p:]
x = len(town)
y = len(b)
q = k-x+y
if y == 0:
    print(town[n-1])
    sys.exit()
if q < 0:
    print(town[k-1])
    sys.exit()
z = q%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

p = town.index(ans)
b = town[p-1:]
x = len(town)
y = len(b)
q = k-x+y
if y == 0:
    print(town[n-1])
    sys.exit()
if q < 0:
    print(town[k-1])
    sys.exit()
z = q%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

p = town.index(ans)
b = town[p:]
x = len(town)
y = len(b)
q = k-x+y
if y == 0:
    print(town[n-1])
    sys.exit()
if q < 0:
    print(town[k-1])
    sys.exit()
z = q%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

p = town.index(ans)
b = town[p:]
x = len(town)
y = len(b)
q = k-x+y
if q < 0:
    print(town[k-1])
    sys.exit()
z = q%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

p = town.index(ans)
b = town[p:]
x = len(town)
y = len(b)
q = k-x+y
if y == 0:
    print(town[k-1])
z = q%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

p = town.index(ans)
b = town[p:]
x = len(town)
y = len(b)
k -= x-y
z = k%y
if z == 0:
    print(town[k-1])
else:
    print(b[z-1])

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
now, route = 1, []

while not now in route:
    route.append(now)
    now = a[now-1]
route.append(now)
route.pop(0)
num = k%len(route)

count = route.index(route[-1])
if k-1 <= count:
    print(route[k-1])
    sys.exit()
else:
    if count != len(route)-1:
        route = route[count:]
if num == 0:
    num = len(route)
print(route[num-1])

Submission status: Runtime Error

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

d = 60
dp = [[-1]*N for _ in range(d+1)]
for i in range(n):
    dp[0][i] = a[i]-1

for i in range(d):
    for j in range(n):
        dp[i+1] = dp[i][dp[i][j]]

ans = 0

for i in range(d):
    if K & (1<<i):
        ans = dp[i][ans]

print(ans)

Submission status: Runtime Error

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

ans = 0
r = 0
p = 0
seen = {0}
for i in range(k):
    x = a[p] - 1
    if x in seen:
        p = 0
        seen = {0}
        for j in range((k - 1) % r):
            p = a[p] - 1
            ans = p + 1
        print(ans)
        exit()
    p = x
    seen.add(p)
    r = i + 1
    ans = p + 1
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
cycle=[1]
i=1
while len(cycle)<n:
  if not l[i-1] in cycle: 
    cycle+=[l[i-1]]
    i=l[i-1]
  else:
    i=l[i-1]
    i=cycle.index(i)
    cycle=cycle[i:-1]
    break
m=m+1-i
m=m%len(cycle)
print(cycle[m])

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
cycle=[1]
i=1
while len(cycle)<n:
  if not l[i-1] in cycle: 
    cycle+=[l[i-1]]
    i=l[i-1]
  else:
    i=l.index(i)
    cycle=cycle[i:-1]
    break
m=(m+1-i)%len(cycle)
print(cycle[m])

Submission status: Runtime Error

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

now = 1
loop = []
checker = set()
loop.append(now)
checker.add(now)
while True:
    nxt = A[now]
    loop.append(nxt)
    checker.add(nxt)
    now = nxt
    if len(loop) != len(checker):
        break

end = loop[-1]
first_end = loop.index(end)
loop_circle = len(loop)-first_end-1
if len(loop_circle) < K:
    print(loop[K])
    quit()
loopy = loop[first_end:-1]
num = K-first_end
ans = loopy[(num%loop_circle)]
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]



#いまいるところ,
#listとnが与えられたときn回テレポートしたところを出力する
def now_place(li,n):
    now = 1
    for i in range(n):
        now = li[now-1]
    return now

#p回のテレポートでたどってきた道筋のルートを返す

def root(li,p):
    ans = []
    now = 1
    for j in range(p):
        ans.append(now_place(li,j+1))
    return ans



n,k = MI()
a = LI()
#訪れたところのメモがtown
town　= []
#訪れたかどうかを0と1で管理
visit=[0]*n
p = 1
while visit[p-1] == 0:
    town.append(p)
    visit[p-1] = 1
    p = a[p-1]
#香りだかい町がいつでてきたか→周期に入るまでのテレポート回数
#len(town[l:])は周期を表す
l = town.index(p)
if k <len(town):
    print(town[k])
else:
    print(town[l + (k-l)%(len(town[l:])))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]



#いまいるところ,
#listとnが与えられたときn回テレポートしたところを出力する
def now_place(li,n):
    now = 1
    for i in range(n):
        now = li[now-1]
    return now

#p回のテレポートでたどってきた道筋のルートを返す

def root(li,p):
    ans = []
    now = 1
    for j in range(p):
        ans.append(now_place(li,j+1))
    return ans



n,k = MI()
a = LI()
#薫り高い町を求めよう
#訪れた町をリストに入れていって重複が出てきたらそこが薫り高い町
#そこまでのstep数も数える
cnt = 0
place=[1]
s = a[1-1]
for i in range(n):
    if a[s-1] in place:
        town = a[s-1]
        cnt = i - 1
        break
    place.append(a[s-1])
    s = a[s-1]
#周期を求める
t = 0
now = town
for i in range(n):
    t += 1
    now = a[now-1]
    if now == town:
        break
answer = root(a,cnt+t)
if k <= cnt:
  print(answer[k-1])
else:
  print(answer[cnt+(k-cnt)%t-1])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]



#いまいるところ,
#listとnが与えられたときn回テレポートしたところを出力する
def now_place(li,n):
    now = 1
    for i in range(n):
        now = li[now-1]
    return now

#p回のテレポートでたどってきた道筋のルートを返す

def root(li,p):
    ans = []
    now = 1
    for j in range(p):
        ans.append(now_place(li,j+1))
    return ans



n,k = MI()
a = LI()
#薫り高い町を求めよう
#訪れた町をリストに入れていって重複が出てきたらそこが薫り高い町
#そこまでのstep数も数える
cnt = 0
place=[1]
s = a[1-1]
for i in range(n):
    if a[s-1] in place:
        town = a[s-1]
        cnt = i - 1
        break
    place.append(a[s-1])
    s = a[s-1]
#周期を求める
t = 0
now = town
for i in range(n):
    t += 1
    now = a[now-1]
    if now == town:
        break
answer = root(a,cnt+t)
print(answer)
if k <= cnt:
  print(answer[k-1])
else:
  print(answer[cnt+(k-cnt)%t-1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
list_a = []
list_b =[]
list_a.append(1)
count_a = 1
count_b = 0
for i in range(1,K+1):
  if i == 1:
    k = i
  if a[k-1] in list_b:
    break
  elif a[k-1] in list_a:
    list_b.append(a[k-1])
    count_b+=1
    if a[k-1] == 1:
      handan = True
  if a[k-1] not in list_a:
    list_a.append(a[k-1])
    count_a+=1
  k = a[k-1]
#print(list_a,list_b,count_a,count_b)
if count_b != 0:
  num_k = K-(count_a-count_b)
  num_k = num_k%count_b
  if handan == True and list_b[0]!=1:
    #list_b.remove(1)
    list_b.insert(0,1)
  print(list_b[num_k])
elif count_b ==0:
  print(list_a[count_a-1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
list_a = []
list_b =[]
list_a.append(1)
for i in range(1,K+1):
  if i == 1:
    k = i
  if a[k-1] in list_b:
    break
  elif a[k-1] in list_a:
    list_b.append(a[k-1])
  if a[k-1] not in list_a:
    list_a.append(a[k-1])
  k = a[k-1]
count_a = len(list_a) 
count_b = len(list_b)
num_k = K-(count_a-count_b)
num_k = num_k%count_b
if 1 in list_b:
  list_b.remove(1)
  list_b.insert(0,1)
print(list_b[num_k])

Submission status: Runtime Error

Solution code:
#import time

def main():
    N, K = map(int, input().split())
    As = list(map(int, input().split()))
    As = [0]+As
    ikisaki = As[1]
    aru = {1}
    itta = [1]
    imaitta = 1
  
    while not ikisaki in aru:
        imaitta = ikisaki
        ikisaki = As[ikisaki]
        itta.append(imaitta)
        aru.add(imaitta)
    ind = itta.index(ikisaki)
    kurikaesi = itta[ind:]
    itidokiri = itta[:ind]
    ans = itidokiri[K-1] if len(itidokiri) <= K else kurikaesi[(K-len(itidokiri))%len(kurikaesi)]


    return itta, itidokiri, kurikaesi, ans

if __name__ == '__main__':
    #start = time.time()
    print(main())
    #elapsed_time = time.time() - start
    #print("経過時間:{}".format(elapsed_time * 1000) + "[msec]")

Submission status: Runtime Error

Solution code:
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
a = [i-1 for i in a]
# print(a)
v = []
v.append(a[0])
i = 0
while True:
    if a[v[i]] not in v:
        v.append(a[v[i]])
        i += 1
    elif v[0]!=a[v[i]]:
        num = len(v)
        v = v[v.index(a[v[i]]):]
        break
    else:
        break
# print(v)
k -= num
# print(k%len(v))
print(v[(k-1)%len(v)]+1)

Submission status: Runtime Error

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

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:break
  dp[t] = i
if k == 2:
  exit(print(t+1))
l = i - dp[t]
K = (k-dp[t])%l + dp[t]

for i in range(n):
  if dp[i] == K:exit(print(i+1))

Submission status: Runtime Error

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

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:break
  dp[t] = i
if k == 2:
  exit(print(t+1))
l = i - dp[t]
K = (k-dp[t])%l + dp[t]
print(dp)
for i in range(n):
  if dp[i] == K:exit(print(i+1))

Submission status: Runtime Error

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

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:break
  dp[t] = i
  
l = i - dp[t]
K = (k-dp[t])%l + dp[t]
print(dp)
for i in range(n):
  if dp[i] == K:exit(print(i+1))

Submission status: Runtime Error

Solution code:

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

t = 0
dp = [-1] * n
dp[0] = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:break
  dp[t] = i
  
l = i - dp[t]
K = (k-dp[t])%l + dp[t]

for i in range(n):
  if dp[i] == K:exit(print(i+1))

Submission status: Runtime Error

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

a = list(map(int, input().split()))
now_list = [1]
k_count = copy.deepcopy(k)
i = 0
b = False
for _ in range(k - 1):
    if b:
        break
    else:
        k_count -= 1
        now = a[i]
        i = now - 1
        if not(now in now_list):
            now_list.append(now)
        else:
            first = True
            while True:
                if first:
                    for j in range(len(now_list)):
                        if now == now_list[j]:
                            now_list = now_list[j:]
                            break
                if k_count > len(now_list):
                    k_count %= len(now_list)
                else:
                    ans = now_list[k_count]
                    b = True
                    break

print(ans)

Submission status: Runtime Error

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

loc = a[0] - 1
pre = [False] * n
pre[0] = 0
pre[loc] = 1
i = 0
while(not i == k - 1):
    i += 1
    loc = a[loc] - 1
    if pre[loc] == False:
        pre[loc] = i + 2
    else:
        loop = i - pre[loc] + 1
        loop = (k - 1 - i - 2 + 1) % loop
        i = k - 1 - loop + 1
print(loc + 1)

Submission status: Runtime Error

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

loc = a[0] - 1
pre = [False] * n
pre[0] = 0
pre[loc] = 1
i = 0
while(not i == k - 1):
    i += 1
    loc = a[loc] - 1
    if pre[loc] == False:
        pre[loc] = i + 2
    else:
        loop = i - pre[loc] + 1
        loop = (k - 1 - i + 1) % loop
        i = k - 1 - loop + 1
print(loc + 1)

Submission status: Runtime Error

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

loc = a[0] - 1
pre = [False] * n
pre[0] = 0
pre[loc] = 1
i = 0
while(not i == k - 1):
    i += 1
    loc = a[loc] - 1
    if pre[loc] == False:
        pre[loc] = i + 2
    else:
        loop = i - pre[loc] + 1
        i += (k - 1 - i + 1) // loop * loop
print(loc + 1)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
A = list(map(int, input().split()))
now = 1
f = 0
past = [0]*k
if n>k:
	for i in range(k):
		now = A[now-1]
	print(now)
	exit()
c = 0
for i in range(n):
	past[c] = now
	now = A[now-1]
	c+=1
	if now in past:
		f = 1
		break


loop_end = c
loop_start = past.index(now)
#print(past, now, loop_end, loop_start)
res = (k-loop_start)%(loop_end-loop_start)
now = past[loop_start]
for i in range(res):
	now = A[now-1]
print(now)

Submission status: Runtime Error

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

visitTowns = list()
visitTimes = [0 for _ in range(N + 1)]

curTown = 1
timeBackTo = 0

curTime = 0
while True:
    if visitTimes[curTown] > 0:
        timeBackTo = visitTimes[curTown]
        break

    visitTowns.append(curTown)
    visitTimes[curTown] = curTime

    # teleport
    curTown = portals[curTown]

    curTime += 1
    print(visitTowns, visitTimes[1:])

nonLoopCount = timeBackTo
loopSpan = len(visitTowns) - nonLoopCount

if K <= nonLoopCount:
    print(visitTowns[K])
    exit()

rem = (K - nonLoopCount) % loopSpan
print(visitTowns[nonLoopCount + rem])

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
loop = []
loop.append(0)
def func():
        d = 0
        for i in range(a[0]):
            if b[d] in loop:
                return loop.index(b[d])
            else:
                loop.append(b[d])
                d = b[d]
        return 0
c = func()
if a[1] < len(loop) - 1:
    ans = loop(a[1])
    print(str(ans))
else:
    ans = ((a[1] - (c + 1)) % (len(loop) - c)) + c
    ans = loop[ans]
    print(str(ans))

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
loop = []
def func():
        d = 0
        for i in range(a[0]):
            if b[d] in loop:
                return loop.index(b[d])
            else:
                loop.append(b[d])
                d = b[d]
        return 0
c = func()
if a[1] < len(loop):
    ans = loop(a[1] - 1)
    print(str(ans))
else:
    ans = ((a[1] - (c + 1)) % (len(loop) - c + 1)) + c
    ans = loop[ans - 1]
    print(str(ans))

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
loop = []
def func():
        d = 0
        for i in range(a[0]):
            if b[d] in loop:
                return loop.index(b[d])
            else:
                loop.append(b[d])
                d = b[d]
        return 0
c = func()
print(loop)
if a[1] < len(loop):
    ans = loop(a[1] - 1)
    print(str(ans))
else:
    ans = ((a[1] - (c + 1)) % (len(loop) - c + 1)) + c
    ans = loop[ans - 1]
    print(str(ans))

Submission status: Runtime Error

Solution code:
from collections import deque

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


def teleport(now_index):
    next_machi_index = portals[now_index] - 1
    return next_machi_index


current = first_count = 0
keiro = list()
for _ in range(K):
    if current in keiro:
        loop_index = keiro.index(current)
        loop_keiro = keiro[loop_index:]
        break
    keiro.append(current)
    current = teleport(current)

print(keiro[(K - first_count) % len(loop_keiro)] + 1)

Submission status: Runtime Error

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

N,K=list(map(int,input().split()))
A =[0]+list(map(int,input().split()))
town = 1
town_list = [1]
while True:
    town = A[town]
    if town not in town_list:
        town_list.append(town)
    else:
        roop_town_num = town
        break
    
l_ini_index = town_list.index(roop_town_num)
len_roop = len(town_list)-l_ini_index 

if K <= l_ini_index:
    print(town_lis[K])
else:
    print(town_list[((K-l_ini_index)%len_roop+l_ini_index)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
adj=[1]*n
b=[1]
now=0
while adj[now]:
  adj[now]=0
  now=a[now]-1
  c.append(now+1)
loop_start=c.index(c[-1])
loop=c[loop_start:-1]
p=len(loop)
if k<loop_start:
  print(c[k])
else:
  k-=loop_start
  k%=p
  print(loop[k])

Submission status: Runtime Error

Solution code:
Town = []
N, K = map(int, input().split())
A = list(map(int, input().split()))
vis = [0]*N
p = 1
while vis[p-1]==0:
    Town.append(p)
    vis[p-1] = 1
    p = A[p-1]

l = Town.index[p]

if K < len(Town):
    print(Town[K])
else:
    print(Town[l + (K-l)%(len(Town[l:]))])

Submission status: Runtime Error

Solution code:
# ABC167D
Town = []
C = 0
N, K = map(int, input().split())
A = list(map(int, input().split()))
def dfs(a, i, town, c):
    if i-1 in town:
        L = len(town) - len(town[:town.index(i-1)])
        c += (K-c) - ((K-c)%L)
        town = []
    else:
        town.append(i-1)
        c += 1
    if c >= K:
        return print(i)
    nx = a[i-1]
    dfs(a, nx, town, c)

dfs(A, 1, Town, C)

Submission status: Runtime Error

Solution code:
Town = []
C = 0
N, K = map(int, input().split())
A = list(map(int, input().split()))
def dfs(a, i, town, c):
    if i-1 in town:
        L = len(town) - len(town[:town.index(i-1)])
        c += (K - c) - (K%L)
        town = []
        town.append(i-1)
    else:
        town.append(i-1)
        c += 1
    if c == K:
        return print(a[i-1])
    nx = a[i-1]
    dfs(a, nx, town, c)

dfs(A, 1, Town, C)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
alist = list(map(int,input().split()))
memo = []
p = 1
vis = ['.']*n
while vis[p-1] == '.':
    memo.append(p)
    vis[p-1] = 'v'
    p = alist[p-1]

loop = memo.index(p)
if k < len(alist):
    print(memo[k])
else:
    print(memo[loop+(k-loop)%(len(memo)-loop)])

Submission status: Runtime Error

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

memo = []
p = 1
vis = ['.']*n

while vis[p-1] == '.':
    memo.append(p)
    vis[p-1] = 'v'
    p = alist[p-1]

loopstart = memo.index(p)
if k < len(alist):
    print(memo[k])
else:
    print(memo[loopstart+(k-loopstart)%(len(memo)-loopstart)])

Submission status: Runtime Error

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

memo = []
p = 1
vis = ['.']*n

while vis[p-1] == '.':
    memo.appened(p)
    vis[p-1] = 'v'
    p = alist[p-1]

loopstart = alist.index(p)
if k < len(alist):
    print(memo[k])
else:
    print(memo[loopstart+(k-loopstart)%(len(alist)-loopstart)])

Submission status: Runtime Error

Solution code:
num,time = input().split(" ")
_map_str = input().split(" ")
_map_ref = {}
_map_index = {}
num = int(num)
time = int(time)

_index = 0
next_index = 0
num1, num2 = 0, 0
re_cal = False
while _index < time:
    if _map_ref.get(next_index,None) == None:
        _map_ref[next_index] = 0

    if _map_index.get(next_index,None) == None:
        _map_index[next_index] = _map_str[next_index]

    if _map_ref[next_index] + 1 == 3:
        re_cal = True
        break
    elif _map_ref[next_index] + 1 == 2:
        num1 = num1 - 1
        num2 = num2 + 1

    _map_ref[next_index] = _map_ref[next_index] + 1
    num1 = num1 + 1
    _index = _index + 1
    next_index = int(_map_str[next_index]) - 1

if re_cal:
    time = (time - num1) % num2 + num1
    print(_map_index[time])
else:
    print(_map_str[int(next_index) - 1])

Submission status: Runtime Error

Solution code:
num,time = input().split(" ")
_map_str = input().split(" ")
_map_ref = {}
_map_index = {}
num = int(num)
time = int(time)

_index = 0
next_index = 0
re_cal = False
while _index < time:
    if _map_ref.get(next_index,None) == None:
        _map_ref[next_index] = 0

    if _map_index.get(next_index,None) == None:
        _map_index[next_index] = _map_str[next_index]

    if _map_ref[next_index] + 1 == 3:
        re_cal = True
        break
    _map_ref[next_index] = _map_ref[next_index] + 1
    _index = _index + 1
    next_index = int(_map_str[next_index]) - 1

num1,num2 = 0,0
for k,v in _map_ref.items():
    if v == 2:
        num2 = num2 + 1
    elif v == 1:
        num1 = num1 + 1

if re_cal:
    time = (time - num1) % num2 + num1
    print(_map_index[time])
else:
    print(_map_str[int(next_index) - 1])