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:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

ord_ = [-1] * (n + 1) # 何回目に訪れたか
s = []
current = 0
while ord_[v] == -1:
    ord_[v] = len(s)
    s.append(v)
    current = a[current] -1
c = len(s) - ord_[current]
l = ord_[current]

if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1
P = [1]
 
for i in range(k):
    c = f(c)
    P.append(c)
    if c in P:
        roop_start = P.index(c)
        roop_num  = i + 1-  roop_start
        break
a = (k - roop_start) % roop_num
for i in range(a):
    roop_start = f(roop_start)
print(roop_start)

Submission status: Runtime Error

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = il()
    t = [0]*N
    tmp = 1
    cnt = 0
    for n in range(N):
        if t[tmp-1] != 0:
            break
        cnt += 1
        t[n] = tmp
        tmp = A[tmp - 1]
    else:
        print(t[cnt-1])
        exit()

    mod = (K - cnt) % (len(t)-cnt)
    print(t[len(t)-cnt+1:][mod])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1
P = [1]
 
for i in range(k):
    c = f(c)
    P.append(c)
    if c in P:
        roop_start = P.find(c)
        roop_num  = i + 1-  roop_start
        break
a = (k - roop_start) % roop_num
for i in range(a):
    roop_start = f(roop_start)
print(roop_start)

Submission status: Runtime Error

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = il()
    t = [0]*N
    tmp = 1
    cnt = 0
    for n in range(N):
        if t[tmp-1] == 0:
            t[tmp-1] = A[tmp-1]
            tmp = A[tmp-1]
            cnt += 1
        else:
            break
    else:
        print(t[cnt-1])
        exit()

    mod = (K - cnt) % (len(t)-cnt)
    print(t[len(t)-cnt+1:][mod])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1, P = [1]

for i in range(k):
    c = f(c)
    P.append(c)
    if c in P:
        roop_start = P.find(c)
        roop_num  = i + 1-  roop_start
        break
a = (k - roop_start) % roop_num
for i in range(a):
    roop_start = f(roop_start)
print(roop_start)

Submission status: Runtime Error

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = il()
    t = [0]*N
    tmp = 1
    cnt = 0
    for n in range(N):
        if tmp in t:
            break
        cnt += 1
        t[n] = tmp
        tmp = A[tmp - 1]
    else:
        print(t[cnt-1])
        exit()

    mod = (K - cnt) % (len(t)-cnt)
    print(t[len(t)-cnt+1:][mod])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

visited = [None]*n
cur = 0
for i in range(k):
    if visited[cur] is None:
        visited[cur] = i
        cur = a[cur]-1
    else:
        break
else:
    print(cur+1)


k -= i
k %= i-visited[cur]

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

print(cur+1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * (n + 1)

current = 0
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = 1 if (diff := nitta - nltn) == 0 else diff

if k < nltn:
    print(current + 1)
else:
    k -= nltn
    k %= nlty
    print(homon.index(k) + 1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * (n + 1)

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = 1 if (diff := nitta - nltn) == 0 else diff

if k <= nitta:
    print(current + 1)
else:
    k -= nltn
    print(homon.index(k%nlty) + 1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * n

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = 1 if (diff := nitta - nltn) == 0 else diff

if k <= nitta:
    print(current + 1)
else:
    k -= nltn
    print(homon.index(k%nlty) + 1)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    A = LI_()

    ord = [-1]*N

    city = 0
    cnt = 0
    teleport_path = []
    while ord[city]==-1:
        ord[city] = cnt
        teleport_path.append(city)
        city = A[city]
        cnt += 1
    loop_head = ord[city]
    loop_length = len(teleport_path) - loop_head
    # print(teleport_path, loop_head, loop_length)

    if K-1<loop_head:
        print(teleport_path[K]+1)
    else:
        K = loop_head + (K-loop_head)%loop_length
        print(teleport_path[K]+1)

if __name__ == '__main__':
    r

Submission status: Runtime Error

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

# ループする数を求める
loop_num = 2 * 10 ** 5
loop_start_point = -1
point = 1
count_list = [-1] * N

for i in range(K):
    #print("point:", point)
    count_list[point - 1] = i
    nextPoint = Alist[point - 1]

    if count_list[nextPoint - 1] != -1:
        loop_start_point = nextPoint
        loop_num = i - count_list[loop_start_point - 1] + 1
        break
    
    point = nextPoint

if loop_start_point == -1:
    #print(point)
else:
    point = loop_start_point
    progress_num = (K - count_list[loop_start_point-1]) % loop_num
    for i in range(progress_num):
        point = Alist[point - 1]
    #print("pro:", progress_num)
    #print("ls:", loop_start_point)
    #print("ln:", loop_num)
    print(point)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * n

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = nitta - nltn

if k <= nitta:
    print(current + 1)
else:
    k -= nltn
    print(homon.index(k%nlty) + 1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * n

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = nitta - nltn

k -= nltn
print(min(k, homon.index(k%nlty) + 1))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * n

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = nitta - nltn

k -= nltn
print(homon.index(k%nlty) + 1)

Submission status: Runtime Error

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

start = 0
end = 0
pointer = 1

for i in range(n):
    pointer = a[pointer]
    if pointer in b:
        for j in range(len(b)):
            if pointer == b[j]:
                start = j
                break
        end = i
        break
    b.append(pointer)

if k < len(b):
    print(b[k-1])
else:
    print(b[(k-start)%(start-end+1) + start])

Submission status: Runtime Error

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

#巡回してるか
passed=[1]
nex =connection[1]
lap =[]
lap_num =0
pass_set =set()
for i in range(1,N+1):
    passed.append(nex)
    if nex not in pass_set:
        pass_set.add(nex)
        nex =connection[nex]
    else:
        #lap =deepcopy(passed)
        break
#巡回している部分の町の数    
indx_s =passed.index(nex) +1
indx_f =len(passed)
lap_num =indx_f -indx_s

if indx_f < K:
    mod =(K -indx_s) % lap_num
    ans =passed[indx_s +mod]
else:
    ans =passed[K]
print(ans)

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
teleport=[int(i) for i in input().split()]
spot=[1]
now=1
if a>b:
  a=b
for i in range(a):
  now=teleport[now-1]
  if now in spot:
    cycle=i-spot.index(now)+1
    break
  else:
    spot.append(now)
rest=(b-len(spot))%cycle
for i in range(rest):
  now=teleport[now-1]
print(now)

Submission status: Runtime Error

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

Town = []
#まだ訪れていない町はTrue
rec = [True] * (n+1)
idx = 1
#周期までループをまわす
while rec[idx]:
    rec[idx] = False
    Town.append(idx)
    idx = A[idx-1]

#周期前
Exce = Town.index(idx)
#周期内
Cycl = len(Town) - Exce

if Cycl >= k:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[Exce+k])

Submission status: Runtime Error

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

Town = []
#まだ訪れていない町は-1
rec = [-1] * (n+1)
idx = 1
#周期までループをまわす
while (rec[idx] == -1):
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

#周期前
Exce = Town.index(idx)
#周期内
Cycl = len(Town) - Exce

if Cycl >= k:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[Exce+k])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
arr=[int(i) for i in input().split()]
arr.insert(0,0)
hashmap=dict()
hashindex=dict()
index=arr[1]
lastindex=arr[1]
firstindex=arr[1]
hashmap[1]=arr[1]
hashindex[arr[1]]=1
count=2
while K>0:
  if hashindex.get(arr[index])!=None:
    firstindex=hashindex[arr[index]]
    lastindex=count
    K-=2
    break
  hashmap[count]=arr[index]
  hashindex[arr[index]]=count
  index=arr[index]
  count+=1
  K-=1

count=(lastindex-firstindex)            
if count!=0:
  count=(K%count)
print(hashmap[firstindex+count])

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
spot=[int(i) for i in input().split()]
res=[1]
now=1
for i in range(b):
  now=spot[now-1]
  if now in res:
    term=i-res.index(now)+1
    break
  else:
    res.append(now)
remain=(b-len(res))%term
for i in range(remain):
  now=spot[now-1]
print(now)

Submission status: Runtime Error

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

start = 0
atesaki = 0
atesaki_li = [0]
target = atesaki
last = -1
for i in range(k):
    atesaki = li[start]-1
    start = atesaki
    if atesaki in atesaki_li:
        last = atesaki
        break
    else:
        atesaki_li.append(atesaki)
#print(last,atesaki_li)
#[0, 2, 3]

ma = len(atesaki_li)
if k+1<=ma:
    print(ma[k])
else:
    fi = atesaki_li.index(last)
    kurikaeshi = ma - fi
    ama = (k+1-ma)%kurikaeshi
    new_li = atesaki_li[fi:]
    #print(fi,kurikaeshi,ama,atesaki_li)
    #print(atesaki_li,last,kurikaeshi,ama)
    #print(new_li)
    print(new_li[ama-1]+1)

Submission status: Runtime Error

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

hashmap=dict()
hashindex=dict()

count=0
index=arr[1]
lastindex=arr[1]
firstindex=arr[1]

while K>0:
  if hashindex.get(arr[index])!=None:
    firstindex=hashindex[arr[index]]
    lastindex=count
    break
  hashmap[count]=arr[index]
  hashindex[arr[index]]=count
  index=arr[index]
  count+=1
  K-=1
count=(lastindex-firstindex)            
count=(K%count)
print(hashmap[(firstindex+count)%count])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
arr=[int(i) for i in input().split()]
arr.insert(0,0)
#print(arr)
hashmap=dict()
hashindex=dict()
count=0
index=arr[1]
lastindex=arr[1]
firstindex=arr[1]
while K>0:
  if hashindex.get(arr[index])!=None:
    firstindex=hashindex[arr[index]]
    lastindex=count
    break
  hashmap[count]=arr[index]
  hashindex[arr[index]]=count
  index=arr[index]
  count+=1
  K-=1
count=(firstindex-lastindex)            
count=(K%count)
print(hashmap[(firstindex+count)%count])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
check=[[] for i in range(n)]
c=0
g=1
tmp=[]
cy=[]
non=[]

while c<n:
    if check[g-1]==[]:
        c+=1
        tmp.append(g)
        check[g - 1].append(c)
        g=a[g-1]
    else:
        cy=tmp[(check[g-1][0]-1):]
        non=tmp[:(check[g-1][0]-1)]
        break
if k<=len(non):
    print(non[k])
    exit()
k-=len(non)
k%=len(cy)


print(cy[k])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
check=[[] for i in range(n)]
c=0
g=1
tmp=[]
cy=[]
non=[]

while c<n:
    if check[g-1]==[]:
        c+=1
        tmp.append(g)
        check[g - 1].append(c)
        g=a[g-1]
    else:
        cy=tmp[(check[g-1][0]-1):]
        non=tmp[:(check[g-1][0]-1)]
        break
k-=len(non)
k%=len(cy)
if k<=len(non):
    print(non[k-1])
    exit()

print(cy[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
dic = {}
dic[1] = 1
new = 1
rest = 0
ball = 0
for i in range(1, n):
    new = a[new - 1]
    if new in dic:
        rest = dic[new] - 1
        ball = i - rest
        break
    else:
        dic[new] = i + 1
hoge = list(dic)[rest:]
print(hoge[::-1][(k - 1) % ball + rest - 1])
#hoge

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split());
data = [input().split()];
a = 1;
c = int(1);
roop = list();
 
 
while a <= 10000:
    b = c - 1;
    c = int(data[0][b]);
    roop.append(c);
    if roop.count(c) == 2:
        break;
    a = a + 1;


x = len(roop);
y = roop.index(c) + 1;
 
if k <= x:
    print(c);
else:
    z = (k - y - 1) % (x - y);
    print(roop[y+z]);

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split());
data = [input().split()];
a = 1;
c = int(1);
roop = list();
 
 
while a <= 100:
    b = c - 1;
    c = int(data[0][b]);
    roop.append(c);
    if roop.count(c) == 2:
        break;
    a = a + 1;


x = len(roop);
y = roop.index(c) + 1;
 
if k <= x:
    print(c);
else:
    z = (k - y - 1) % (x - y);
    print(roop[y+z]);

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split());
data = [input().split()];
a = 1;
c = int(1);
roop = list();
 
 
while a <= 7:
    b = c - 1;
    c = int(data[0][b]);
    roop.append(c);
    if roop.count(c) == 2:
        break;
    a = a + 1;


x = len(roop);
y = roop.index(c) + 1;
 
if k <= x:
    print(c);
else:
    z = (k - y - 1) % (x - y);
    print(roop[y+z]);

Submission status: Runtime Error

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

start = 0
atesaki = 0
atesaki_li = [0]
target = atesaki
last = -1
for i in range(k):
    atesaki = li[start]-1
    start = atesaki
    if atesaki in atesaki_li:
        last = atesaki
        break
    else:
        atesaki_li.append(atesaki)
#print(last,atesaki_li)
#[0, 2, 3]

ma = len(atesaki_li)
if k+1<=ma:
    print(ma[k])
else:
    fi = atesaki_li.index(last)
    kurikaeshi = ma - fi
    ama = (k+1-ma)%kurikaeshi
    #print(fi,ama,atesaki_li)
    print(atesaki_li[(fi-1)+ama]+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split());
data = [input().split()];
a = 1;
c = int(1);
roop = list();
 
 
while a <= 6:
    b = c - 1;
    c = int(data[0][b]);
    roop.append(c);
    if roop.count(c) == 2:
        break;
    a = a + 1;
 
x = len(roop);
y = roop.index(c) + 1;
 
if k <= x:
    print(c);
else:
    z = (k - y - 1) % (x - y);
    print(roop[y+z]);

Submission status: Runtime Error

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

b = [1]
i = 0
while True:
    if A[i] in b:
#        print("m: "+str(b.index(A[i])))
#        print("n: "+str(len(b)-A[i]))
        m = b.index(A[i])
        n = len(b)-b.index(A[i])
        cycle = (K-m)%n
        position = A[i] - 1
        break
    b.append(A[i])
    i = A[i] - 1
    
tmp = 0
if K <= cycle:
	for i in range(K):
		next_town = A[tmp]
		tmp = next_town - 1
	print(next_town)
else:
	for i in range(cycle):
		next_town = A[position]
		position = next_town -1
	print(next_town)

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()
N, K = map(int, s[0].split())
A = list(map(int, s[1].split()))
 
b = [1]
i = 0
while True:
    if A[i] in b:
#        print("m: "+str(b.index(A[i])))
#        print("n: "+str(len(b)-A[i]))
        m = b.index(A[i])
        n = len(b)-b.index(A[i])
        K = (K-m)%n + m
        break
    b.append(A[i])
    i = A[i] - 1
    
tmp = 0
for i in range(K):
    next_town = A[tmp]
    tmp = next_town - 1
print(next_town)

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()
N, K = map(int, s[0].split())
A = list(map(int, s[1].split()))

b = [1]
i = 0
while True:
    if A[i] in b:
#        print("m: "+str(b.index(A[i])))
#        print("n: "+str(len(b)-A[i]))
        m = b.index(A[i])
        n = len(b)-A[i]
        K = (K-m)%n + m
        break
    b.append(A[i])
    i = A[i] - 1
    
tmp = 0
for i in range(K):
    next_town = A[tmp]
    tmp = next_town - 1
print(next_town)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)

n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
S = {1}
def f(L, S):
    last = L[-1]-1
    if A[last] not in S:
        L.append(A[last])
        S |= {last}
        return f(L, S)
    else:
        return L
P = f(L, S)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j

if k+1 <= j:
    print(P[k])
else:
    print(P[j+(k-j)%l])

Submission status: Runtime Error

Solution code:
import numpy as np
def read_int():
    return int(input())

def read_ints():
    return map(int, input().split())

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = np.array(list(read_ints())) - 1
    portals = {i: towns[i] for i in towns}

    loop = []
    next_town = 0
    while next_town not in loop:
        loop.append(next_town)
        next_town = portals[next_town]
    loop.append(next_town)

    i = loop.index(next_town)
    all_loop = loop
    pre_loop, loop = loop[:i], loop[i:]

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



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

def read_int():
    return int(input())

def read_ints():
    return map(int, input().split())

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = [int(input()) - 1 for i in range(N)]
    portals = {i: towns[i] for i in towns}

    loop = []
    next_town = 0
    while next_town not in loop:
        loop.append(next_town)
        next_town = portals[next_town]
    loop.append(next_town)

    i = loop.index(next_town)
    all_loop = loop
    pre_loop, loop = loop[:i], loop[i:]

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



if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

ans =1
now = [1]
ed = set([1])

for i in range(n+1):
    if a[now[i]-1] ==2:
        break
    now.append(a[now[i]-1])
    ed = set(now)
    
t = len(now)-now.index(a[now[i]-1])
f = now.index(a[now[i]-1])


if k >= f + t:
    p = (k-f)%t
    k = f + p
    

print(now[k])

Submission status: Runtime Error

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

#n=6
#k=727202214173249351
#a=[6, 5, 2, 5, 3, 2]

#n=4
#k=5
#a=[3,2,4,1]

if n<k:
    ai=1
    aj=set()
    aj.add(1)
    for i in range(n):
        ai=a[ai-1]
        if ai in aj:
            ic2=i
            ai2=ai
            break
        else:
            aj.add(ai)

    ai=1
    for i in range(n):
        ai=a[ai-1]
        if ai==ai2:
            ic1=i
            break

    icyc=ic2-ic1
    k2=(k-ic1)%icyc+ic1
else:
    k2=k
    
ai=1
for i in range(k2):
    ai=a[ai-1]
        
print(ai)

Submission status: Runtime Error

Solution code:
def is_dupl(a):
  if 1 in a:
    return True
  else:
    return len(a) != len(set(a))

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

l = [1]
now = 1
next = a[0]
count = 0
dupl = -1

if not is_dupl(a):
  print(a[-1])

else:
  for i in range(k):
    now = next
    next = a[now-1]
  
    if now in l:
      dupl = now
      break
    else:
      l.append(now)
    
  count = l.index(dupl)
  length = len(l)-count
  print(l[(k-count)%length+count])

Submission status: Runtime Error

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

l = [1]
now = 1
next = a[0]
count = 0
dupl = a[-1]

for i in range(k):
  now = next
  next = a[now-1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
count = l.index(dupl)
length = len(l)-count
print(l[(k-count)%length+count])

Submission status: Runtime Error

Solution code:
n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,steps=1,0
root=dict()

while start not in root and steps < k:
  steps+=1
  root[start]=steps
  start=A[start-1]

if steps == k:
  print(start+1)
else:
  pa = steps-root[start]
  s=root[start]
  ans=(k-steps)%pa
  root={v: k for k, v in root.items()}
  print(root[s+ans])

Submission status: Runtime Error

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

l = [1]
now = 1
next = a[0]
count = 0
dupl = -1

for i in range(k):
  now = next
  next = a[now-1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
count = l.index(dupl)
length = len(l)-count
print(l[(k-count)%length+count])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(L):
    if A[L[-1]-1] not in L:
        L_next = L + [A[L[-1]-1]]
        return f(L_next)
    else:
      return L
L = [1]
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
    print(P[k])
else:
    print(P[j+(k-j)%l])

Submission status: Runtime Error

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

vlist = [1]
vcnt = [0]*(n+1)
vcnt[1] += 1
v = 1
for i in range(1,n+1):
    v = a[v]
    vlist.append(v)
    if i == k:
        print(vlist[-1])
        exit()
    if vcnt[v] == 0:
        vcnt[v] += i
    else:
        syuuki = i - vcnt[v]
        hajime = i - syuuki
        break
k -= hajime
k %= syuuki
k += hajime 
print(vlist[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
def f(L):
    if A[L[-1]-1] not in L:
        L.append(A[L[-1]-1])
        return f(L)
    else:
        return L
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
    print(P[k])
else:
    print(P[j+((k-j)%l)])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
#N=6
#K=1
#A=[6,5,2,5,3,2]
iti = 1
ex_A = []
maru=0
nokori=0
 
for k in range(K):
    ex_A.append(iti)
    iti = A[iti-1]
    if iti in ex_A:
        maru = len(ex_A[:ex_A.index(iti)])
        nokori = (K - maru) % (len(ex_A[ex_A.index(iti):])-maru)
        break
 
 
for i in range(nokori):
    iti = A[iti-1]
#print(ex_A)
print(iti)

Submission status: Runtime Error

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

iti = 1
ex_A = []
basyo=0

for k in range(N):
    ex_A.append(iti)
    iti = A[iti-1]
    if iti in ex_A:
        basyo = ex_A.index(iti)
        maru = len(ex_A[:basyo])
        nokori = (K - maru) % (len(ex_A[basyo:])-maru)
        break

if basyo:
    iti = ex_A[basyo+nokori]
#print(ex_A)
print(iti)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
def f(L):
    global A
    if A[L[-1]-1] not in L:
        L.append(A[L[-1]-1])
        return f(L)
    else:
        return L
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
    print(P[k])
else:
    print(P[j+(k-j)%l])

Submission status: Runtime Error

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

s = [True] * n
c = [1]
now = 0
while s[now]:
    s[now-1] = False
    now = a[now]
    c.append(now)

start_cycle = c.index(c[-1])
loop = c[start_cycle:-1]
cycle = len(loop)

if k < start_cycle:
    print(c[k])
else:
    k -= start_cycle
    k %= cycle
    print(loop[k])

Submission status: Runtime Error

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

s = [-1] * n
c = [1]
now = 0
while s[now] == -1:
    s[now] = t
    now = a[now] - 1
    c.append(now+1)

start_cycle = c.index(c[-1])
loop = c[start_cycle:-1]
cycle = len(loop)

if k < start_cycle:
    print(c[k])
else:
    k -= start_cycle
    k %= cycle
    print(loop[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
def f(L):
    if A[L[-1]-1] not in L:
        L.append(A[L[-1]-1])
        return f(L)
    else:
        return L
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
    print(P[k])
else:
    print(P[j+(k-j)%l])