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()))
returnnum = 0
nowplace = 1
placedic = {}
while True:
    nowplace = A[nowplace-1]
    returnnum += 1
    if nowplace in placedic.values():
        leftnum = K - placedic[nowplace]
        loopnum = returnnum - placedic[nowplace]
        print(placedic[placedic[nowplace]+leftnum%loopnum])
        break
    placedic[returnnum] = nowplace
    if nowplace == 1:
        print(placedic[K % returnnum])
        break
    elif returnnum == K:
        print(nowplace)
        break
    else:
        pass

Submission status: Runtime Error

Solution code:
inputLine=input()
N=int(inputLine.split(" ")[0])
K=int(inputLine.split(" ")[1])

inputLine=input()
A=inputLine.split(" ")

for i in range(len(A)):
    A[i] = int(A[i])

nowPoint=0
transferHistory = []
transferCount = 0
loopExistanceCheck = True
while True:
    transferHistory.append(nowPoint)
    nowPoint = A[nowPoint] - 1

    transferCount += 1
    if nowPoint in transferHistory:
        break
    if transferCount > K:
        loopExistanceCheck = False
        break

for i in range(len(transferHistory)):
    if transferHistory[i] == nowPoint:
        loopLength = len(transferHistory) - i
        break

#print(loopLength)

NumberOfTransfers = K % loopLength
nowPoint=0

for i in range(NumberOfTransfers):
    nowPoint = A[nowPoint] - 1

nowPoint = nowPoint + 1

print(nowPoint)

Submission status: Runtime Error

Solution code:
from math import *
from collections import *
from itertools import *
from bisect import *
from operator import *
def io():
    return map(int,input().split())
def op():
    return list(map(int,input().split()))
def o():
    return int(input())
def kl(con,x=0):
    if x==0:print('Yes') if con else print('No')
    elif x==1:print('yes') if con else print('no')
    elif x==2:print('YES') if con else print('NO')

MOD =    1000000007
MAX=2147483647
MIN=-2147483648
p=input
l,l1,l2,l3=[],[],[],[]
# for _ in range(o()):
n,k=io()
l=op()
x=y=0
i=0
fr=[0]*(2*(10**5))
while 1:
    if fr[l[i]]==0:
        l1.append(l[i])
        fr[l[i]]=1
        i=l[i]-1
    else:
        y=l1.index(l[i])
        x=len(l1)-y
        break
if k<=len(l1):
    print(l1[k-1])
else:
    k-=y
    k%=x
    print(l1[y+k-1])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100:
    state ,f = test, find_f[test]
    break
  
K = K-f[0]
f = f[1]-f[0]
remain = K%f
 
for i in range(remain):
  state = move_list[state-1]
  
print(state)
#state = 1

Submission status: Runtime Error

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

"""
6 727202214173249351
6 5 2 5 3 2
2
"""

Submission status: Runtime Error

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


def teleport(A,idx,k):
    if k==1:
        return A[idx-1]

    return teleport(A, A[idx-1], k-1)

idx=1
print(teleport(A,idx,k))

Submission status: Runtime Error

Solution code:
N, K = map(lambda x: int(x), input().split(" "))
As = list(map(lambda x: int(x), input().split(" ")))

visited = set()
visited_list = []
index = 0
visited.add(index)
visited_list.append(0)
count = 0
flag = False
for i in range(K):
    index = As[index] - 1
    count += 1
    if index in visited:
        flag = True
        break
    visited.add(index)
    visited_list.append(index)

temp_index = visited_list.index(index)
count = count - temp_index

if flag:
    K %= count-temp_index
    for i in range(K):
        index = As[index] - 1

print(index+1)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=input().split()
A=[int(a) for a in A]
S=[]
a=1
number=0
for _ in range(N+1):
    b=A[a-1]
    if b in S:
        c=S.index(b)
        break
    number+=1
    S.append(b)
    a=b
T=S[c:number]
if K<=N:
    print(S[K-1])
else:
    z=K-c-1
    y=z%(number-c)
    print(T[y])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
list=[l[0]]
num=0
while True:
  if l[list[num]-1] in set(list):
    break
  list.append(l[list[num]-1])
  num+=1
y=list.index(l[list[num]-1])
x=len(list)-y
ans=list[y:]
print(ans[(k-y)%x-1])
n,k=map(int,input().split())
l=list(map(int,input().split()))
list=[l[0]]
num=0
while True:
  if l[list[num]-1] in set(list):
    break
  list.append(l[list[num]-1])
  num+=1
y=list.index(l[list[num]-1])
x=len(list)-y
ans=list[y:]
print(ans[(k-y)%x-1])

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
a=list(map(int, input().split()))
x=1
loop=0
loopx=0
check=[0]*n
check[0]=1
owari=0
for i in range(1,k+1):
    x=a[x-1]
    check[x-1]+=1
    #print(x,'000')
    if check.count(2)==1:
        loopx=i
        #print(i,'a')
    if max(check)==3:
        check[x-1]=2
        loop=i
        #print(i,'b')
        break

    if i==k+1:
        loop=i
        owari=1

if owari==1:
    print(x)
    exit()

s=loop-loopx
yobun=loopx-s
amari=(k-yobun)%s
#print(s,yobun,amari)
x=1
for i in range(yobun+s+amari):
    x = a[x - 1]
    #print(x)

print(x)

Submission status: Runtime Error

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

L = []
V = [False] * N
now = 0

for i in range(K):
    if V[now]:
        break
    else:
        L.append(now)
        V[now] = True
        now = A[now] - 1

if now == 0:
    Bl = L[:now]
    Br = L[now:]
else:
    Bl = L[:now+1]
    Br = L[now+1:]
cnt = len(Bl)
K -= cnt
idx = K % len(Br)

print(Br[idx]+1)

Submission status: Runtime Error

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


#一回あたりのループを計算（何回で戻ってくるか）
#ループの箇所を見つける
trace = [0]*N
loop_start = 0
loop_before = 0
cnt = 0
all_cnt = 0

if K > N:
    loop = 0
    now = 0
    while(1):
        # print(trace)
        trace[now] += 1
        if trace[now] == 3:
            break
        if trace[now] == 2:
            cnt += 1
            loop += 1 
            if cnt == 1: 
                loop_start = now
                loop_before = all_cnt
        now = A[now]-1
        all_cnt += 1

loop_before = loop_before - loop 

# print(loop)
# print(loop_start)
# print(loop_before)
# print(loop)
min_K = (K-1-loop_before) % loop
# print(min_K)
# print(loop_before)
now = 0
for i in range(loop_before):
    now = A[now]-1
for i in range(min_K):
    now = A[now]-1
print(A[now])

# print(K % 3)

# print(now+1)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
a = [0]+ list(map(int, input().split()))
l = [1]
start = a[1]
to = None
for i in range(1,k+1):
    if l.count(start)>2:
        break
    l.append(start)
    to = a[start]
    start = to
del l[0] 
for i in range(0,len(l)):
    if l.count(l[i])<2:
        k-=1
        l[i]=0
    elif l.count(l[i])>=2:
        break
for i in range(0,len(l)):
    if l[0]==0:
        del l[0]
    elif l.count(l[0])>1:
        k-=1
        del l[0]
print(l[k%len(l)-1])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(points):]
ll = len(S)
l = len(loop)
if ll >= M:
    print(S[M]+1)
else:
    print(loop[(M-ll)%l]+1)

Submission status: Runtime Error

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
returnnum = 0
nowplace = 1
placedic = {}
while True:
    nowplace = A[nowplace-1]
    returnnum += 1
    if nowplace in placedic.values():
        leftnum = K - placedic[nowplace]
        loopnum = returnnum - placedic[nowplace]
        print(placedic[placedic[nowplace]+leftnum%loopnum])
        break
    placedic[returnnum] = nowplace
    if nowplace == 1:
        print(placedic[K % returnnum])
        break
    elif returnnum == K:
        print(nowplace)
        break

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-1 <= 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()))
b = 1
c = [0] * n
c[0] = 1
m = 0
while c[b-1] < 2 and k > 0:
	b = a[b-1]
	c[b-1] += 1
	k -= 1
while c[b-1] < 3 and k > 0:
	b = a[b-1]
	c[b-1] += 1
	m += 1
o = k % m
for i in range(o):
	b = a[b-1]
print(b)

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = np.zeros((N+1, N+1))
tmp = 0
B[0][0] = 1
list = [1]

for n in range(1,N+1):
    B[A[tmp]-1][n] += 1
    tmp = A[tmp] -1
    list.append(tmp+1)
    if any(np.sum(B, axis=1) >= 2):
        C = B[tmp]
        array = [i for i, x in enumerate(C) if x == 1]
        repeat = array[1]-array[0]
        break

amari = (K + 1 - n + repeat) % repeat

if amari == 0:
    print(list[-2])
else:
    print(list[(-1) * repeat - 2 + amari])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
num=[0 for i in range(n)]
first=[0 for i in range(n)]
first[0]=1
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
num[0]=1
loop=0
s=0
for i in range(n):
  if num[a[s]]==0:
    num[a[s]]+=1
    first[a[s]]=i
  if num[a[s]]==2:
    loop1=loop
    break
  s=a[s]

kai=first[a[s]]+(k-first[a[s]])%(loop1-first[a[s]])
s=0
for i in range(kai):
  s=a[s]
print(s+1)

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
city=list(map(int,input().split()))
ne=[0]*(30000)
ne[0]=1
for i in range(29999):
    ne[i+1]=city[ne[i]-1]
rpp=[]
nrp=[]
for i in ne[1:15000]:
    if i in ne[15001:30000]:
        rpp.append(i)
for i in ne[0:15000]:
    if i not in ne[15001:30000]:
        nrp.append(i)
if len(nrp)>=b-1:
    print(ne[b])
else:
    print(rpp[((b-len(nrp)-1)%len(rpp))])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [i - 1 for i in a]
    ac = Counter(a)
    kaiten = [0] * n
    count = 0
    koko = 0
    kaitenkikan = 0
    while k > 0:
        koko = a[koko]
        count += 1
        k -= 1
        if kaiten[koko] > 0:
            kaitenkikan = abs(count - kaiten[koko])
            break
        else:
            kaiten[koko] = count
    k_remain = k % kaitenkikan
    if k_remain < 0:
        k_remain += kaitenkikan

    while k_remain > 0:
        koko = a[koko]
        k_remain -= 1

    print(koko + 1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

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

d = l[1] - l[0]
r = (k - (l[0]+1)) % d

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

print(b[r+1])

Submission status: Runtime Error

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

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

next = 0
t=0
rep = False

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

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


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

Submission status: Runtime Error

Solution code:
import sys

n,k = (int(i) for i in input().split())

a = [int(i)-1 for i in input().split()]

hantei = [True]*n
otozure = [None]*(n+1)
mokuteki = a[0]
moto = k
kazoe = 0
otozure[0] = 0
while k>0:
        
    if hantei[mokuteki] == False: #二週目突入
       # print("break\nmokuteki= {}\nk = {}".format(mokuteki,k))
        otozure[kazoe+1] = mokuteki
        saigo = otozure
        break

    else:
        hantei[mokuteki]= False
        otozure[kazoe+1] = mokuteki#今いるところを記憶
       # print(otozure[kazoe])
        mokuteki = a[mokuteki]#次に行くところ
        kazoe+=1
    k-=1
else:
   # print(otozure[kazoe])
    sys.exit()
i = 0
while True:
    if otozure[kazoe+1] == otozure[kazoe-i]:
        break
    i+=1


nokori = k%(i+1)
#print(nokori)
i=0
while otozure[i] != mokuteki:
    i+=1

print(otozure[i-1+nokori]+1)

Submission status: Runtime Error

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

li = [0]
li.extend(list(map(int, input().split())))
tuita = [0]*(n+1)
cnt = 0
town = 1

def search(town, times):
  if town > n:
    return 1
  if times == 0:
    return town
  return search(li[town], times - 1)

while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      print(search(1, k))
    elif k == tuita[town]:
      print(town)
    else:
      print(search(town, (k - tuita[town]) % (cnt - tuita[town])))
    break

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
b = 1

while True:
  a = A[b-1]
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    break

print(B[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
cyc = [a[0]]
i=0
while(1):
  if a[a[i]-1] not in cyc:
    cyc.append(a[a[i]-1])
    i = a[i]-1
  else:
    cyc.append(a[a[i]-1])
    break
print(cyc[k%(n - a.index(a[cyc[-1]])) + 1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
sys.setrecursionlimit(20000000)
A = list(map(int,input().split()))
adj_list=[0]*N
for i in range(N):
    adj_list[i]=(A[i])
k=0
node = 1
c=[0]*N
count=1
flag=0
ROOP=[]
roopstart=0
while k<K:
    k+=1
    c[node-1]+=1
    node = adj_list[node-1]
    if c[node-1] ==1:
        if flag ==0:
            roopstart = k
            flag=1
        ROOP.append(node)
    if c[node-1] ==2:
        break
num = (K-len(ROOP)-roopstart) % (len(ROOP))
print(ROOP[num])

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
li = list(map(int,input().split()))
now = li[0]
for _ in range(K):
    now = li[now - 1]
printa(now)

Submission status: Runtime Error

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

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

key = True

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


# print(zyun, zyun[index1])
if zyun.index(zyun[index1]) == 0:
    zyun1 = zyun
    zyun2 = zyun
else:
    zyun1 = zyun[:zyun.index(zyun[index1]) + 1]
    zyun2 = zyun[zyun.index(zyun[index1]) + 1:]

# print(zyun1, zyun2)

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

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 B(i)!=1:
    C.append(B(i))
  else:
    break
print(C[K%len(C)])

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:
N, K = map(int, input().split())
dist = [[] for i in range(N)]
A = list(map(int, input().split()))
for i in range(N):
    dist[i].append(A[i] - 1)
dp = [[0, 0] for i in range(N)]

def dfs(pos, depth):
    # 初めての訪問なら
    if dp[pos][0] == 0:
        dp[pos][0] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
    elif dp[pos][1] == 0:
        dp[pos][1] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
dfs(0, 0)
for i in range(N):
    if dp[i][0] != 0:
        if dp[i][0] == K or dp[i][1] == K:
            print(i + 1)
            break
        else:
            split = dp[i][1] - dp[i][0]
            if split > 0:
                if (K - dp[i][0]) % split == 0:
                    print(i + 1)
                    break

Submission status: Runtime Error

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

moves = [1]
i = 1

while True:
    next_i = A[i-1]
    if (next_i in moves) or K == len(moves):
        break
    moves.append(next_i)
    i = next_i

p = moves.index(next_i)
l = len(moves) - p

ans = moves[p+(K-p)%l]

if K < p:
    ans = moves[K]

print(ans)

Submission status: Runtime Error

Solution code:
def main():

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

    zzz=[]
    oioi=0
    flg=True

    for i in range(n):
        oioi = a[oioi]-1
        
        if oioi == 0:
            zzz.append(oioi)
            break
        if oioi in zzz:
            flg=False
            ppp=zzz.index(oioi)
            del zzz[0:ppp+1]
            zzz.append(oioi)
            break
    
    if flg == False:
        print(zzz[(k+ppp)%len(zzz)-1]+1)
    else:
        print(zzz[k%len(zzz)-1]+1)

    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

d = 1
b = [d]

for i in range(k):
    d = a[d-1]
    if d in b:
        st=b.index(d)
        break
    else:
        b.append(d)
 
b = b[st:]

print( b[(k-st)%len(b)] )

Submission status: Runtime Error

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

# 循環する場所を探す(最大N回)
used = [1, 1]
used.extend([0] * (N-1)) # すでに通った町は1にする
route = [1]
while used[A[route[-1]]] == 0:
    used[A[route[-1]]] = 1
    route.append(A[route[-1]])

if A.index(A[route[-1]]) == 0:
    print(route[K%(len(route))])
else:
    x = A.index(A[route[-1]]) - 1
    y = len(route) - x
    print(route[x + (K-x)%y])

Submission status: Runtime Error

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

for _ in range(k):
  loc = a[loc] - 1
  if loc in h:
    circle_start = loc
    break
  h.append(loc)
i = h.index(circle_start)
#print(h, circle_start, i, h[:i], h[i:])

amari = (k + 1 - len(h[:i])) % len(h[i:])
print(a[h[i:][amari]])

Submission status: Runtime Error

Solution code:

def looptime(n, a):
    for i in range(n):
        loop = 0
        start = i
        now = i

        time = 0

        while(loop < n):
            now = a[now]
            loop += 1
            now -= 1

            if now == start:
                return i+1, loop

def start_loop(goal, a):
    loop = 0
    now = 0

    time = 0

    while(loop < n):
        now = a[now]
        loop += 1
        now -= 1

        if now == goal-1:
            return loop

if __name__ == '__main__':

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    start, loop = looptime(n, a)
    how = start_loop(start, a)
    point = k%loop

    #print(start, loop, point, how)

    if how == loop:
        # startからループ開始
        time = 0
        now = 0
        while(time < point):
            now = a[now]
            now -= 1
            time += 1
        print(now+1)

    else:
        # 途中からループ開始
        if how > k:
            time = 0
            now = 0
            while(time < k):
                now = a[now]
                now -= 1
                time += 1
            print(now+1)

        else:
            time = 0
            now = start-1
            point = (point+how)%loop
            while(time < point-1):
                now = a[now]
                now -= 1
                time += 1
            print(now+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be,r)
if k<= len(be):
  print(be[k])
else:
  w = k-len(be)
  u = (w%len(r))
  print(r[u]+1)

Submission status: Runtime Error

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

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

city = 1
route = []
cnt = [0] * n
route.append(0)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)

if k <= len(route):
    print(route[k])

else:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
s=set()
s.add(1)
order=[1]
while 1:
    if a[i] in s:
        order.append(a[i])
        break
    else:
        order.append(a[i])
        s.add(a[i])
    i=a[i]-1
for i in range(len(order)):
    if order[-1]==order[i]:
        idx=i
        break
#print(order)
if idx==0:
    cnt=len(order)-(idx+1)
else:
    cnt=len(order)-idx
if len(order)>k:
    print(order[k])
else:
    order=order[idx+1:]
    #print(order)
    k%=cnt
    if k>=1:
        k-=1
    else:
        k=cnt-1
    #print(k)
    print(order[k])
#print(order)

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

print(replis[1+(k-i-len(replis))%len(replis)])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
cyc = [a[0]]
i=0
while(1):
  if a[a[i]-1] not in cyc:
    cyc.append(a[a[i]-1])
    i = a[i]-1
  else:
    cyc.append(a[a[i]-1])
    break
print(cyc[k%(n + 1 - a.index(a[cyc[-1]]))])

Submission status: Runtime Error

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


visit_route = []#今までの道のり
visited_town = [0]*n#0=初めてきた、1=来たことがある
rooop = 0


def machi(x):
    global visit_route
    global visited_town
    global rooop
    if visited_town[x] == 1:
        rooop = x
        pass
    else:
        visited_town[x] += 1
        visit_route.append(x)
        machi(a_list[x]-1)

machi(0)
st = 0
for i in visit_route:
    st += 1
    if i == rooop:
        start = st
        break
tere = len(visit_route)
loop = tere - start +1

ans = (k - tere) % loop

#print(a_list)
#print(visit_route)
#print(start,loop,tere)
print(visit_route[start + ans-1]+1)

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:
    state ,f = test, find_f[test]
    break
  
K = K-f[0]
f = f[1]-f[0]
remain = K%f
 
for i in range(remain):
  state = move_list[state-1]
  
print(state)
#state = 1

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

if K == 0:
    print(1)
    sys.exit()

flg = [False for _ in range(N + 1)]
town = []
i = 1
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s

if len(town) >= K:
    print(town[K])
else:
    print(town[(K - to_s) % loop + to_s])

Submission status: Runtime Error

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

# １周の周期を求める
place = 1
term = 0
i = 0
history = [1]


for x in range(K):
    place = A[place - 1]
    if place in history:
        i = history.index(place)
        term = x + 1 - i
        break
    else:
        history.append(place)

K = (K - i) % term
for x in range(K):
    place = A[place - 1]

print(place)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    city = 1
    visited = {city: 0}
    cycle_len = 1

    for i in range(N):
        city = A[city - 1]
        visited[city] = i
        if visited.get(A[city - 1], False):
            offset = visited[A[city -1]]
            cycle_len = i
            break

    K = (K - offset) % abs(cycle_len - offset)

    city = A[city - 1]
    for i in range(cycle_len):
        city = A[city - 1]

    print(city)
    
    pass

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

# ループの値を取得
work = 1
a_set = []
loop_start_num = 0
loop_start_count = 0

for _ in range(K):
    work = A[work - 1]
    if work in a_set:
        loop_start_num = work
        break
    else:
        a_set.append(work)
        loop_start_count += 1
idx = a_set.index(loop_start_num)
a_list = a_set[idx : len(a_set)]
# print(a_list)

if K <= loop_start_count:
    print(work)
else:
    print(a_list[(K - loop_start_count - 1) % len(a_list)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split(" "))
tele=list(map(int,input().split(" ")))
c=[]
c1=[]
special=[]
times=0
city=1
rem=0
for j in range(n):
    if j+1==tele[j] :
        special.append(j+1)
for i in range(n):
    times+=1
    city=tele[city-1]
    if city in special:
        print(city)
        break
    if city in c:
        break
    c.append(city)
for cit in c:
    if cit != city:
        rem+=1
        c.remove(cit)
        c1.append(cit)
    else:
        break
if k>rem:
    k-=rem
    k=k%len(c)
    print(c[k-1])
elif k ==0:
    print(1)
elif k <=rem:
    print(c1[k-1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
list = list(map(int,input().split()))
a = 1
list2=[]
begin = 0
time = 0
flag=False
while flag==False:
    a = list[a-1]
    for i,x in enumerate(list2):
        if x == a:
            begin=i
            time = len(list2)-begin
            flag=True
    list2.append(a)
list3 = list2[begin:begin+time]
if K<=begin:
    print(list2[K-1])
else:
    print(list3[(K-begin)%time-time-1])

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 = {}
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
    next_t = dic[next_t]

common  = []
has_one = False
for k,v in tmp.items():
    if v!= 1:
        common.append(k)
    else:
        has_one = True

if has_one:
    print(common[k%len(common)])
else:
    print(common[k%len(common)])

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
x = 1
point = [0]*N
point[0] = 1
for i in range(K):
    x = A[x - 1]
    if point[x-1]>0:
        law = i+1-point[x-1]
        yobun = i + 1
        break
    point[x-1] = i+1

S = (K - yobun)%law
for i in range(S):
    x = A[x - 1]
print(x)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a

state = 1
connect = []
looplen= 10**18
for n in range(K):
    connect.append(state)
    state = teleport[state] 
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
index = (k-loop_init)%looplen
end = connect[index+loop_init]
print(end)

Submission status: Runtime Error

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

done = [0]
while True:
    if A[done[-1]] not in done:
        done.append(A[done[-1]])
    else:
        index = A[done[-1]]
        break

if K < len(done):
    print(done[K] + 1)
else:
    print(done[((K - len(done[:index])) % len(done[index:]))] + 1)