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:
#include <set>
#include <cstdio>
#include <vector>
#include <utility>
#include <iterator>
using namespace std;

int main() {
    unsigned n; unsigned long long k; scanf("%u%llu", &n, &k);
    vector<unsigned> a(n);
    for (unsigned& x : a) scanf("%u", &x), --x;
    set<pair<unsigned, unsigned>> s;
    vector<pair<unsigned, unsigned>> edges;
    pair<unsigned, unsigned> p = make_pair(0U, *a.cbegin());
    pair<set<pair<unsigned, unsigned>>::iterator, bool> _ = make_pair(s.end(), false);
    while ((_ = s.insert(p)).second) edges.emplace_back(p), p = make_pair(p.second, a.at(p.second));
    unsigned c = 0;
    vector<pair<unsigned, unsigned>>::iterator it = edges.begin();
    for (; *it != *_.first; ++c) it = next(it);
    unsigned sz = distance(it, edges.end());
    if (!sz) sz = s.size();
    printf("s.size()=%u c=%u sz=%u _.first=(%u;%u)\n", s.size(), c, sz, _.first->first, _.first->second); // DEBUG
    printf("%u", (c > --k ? next(edges.begin(), k)->second : next(it, (k - c) % sz)->second) + 1);
    return 0;
}

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import math


def get_input() -> tuple:
    """
    標準入力の取得

    Returns:\n
        tuple: 標準入力
    """
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    return N, K, A


def main(N: int, K: int, A: list) -> None:
    """
    求解処理.

    Args:
        N (int): 町の数
        K (int): テレポーターの使用回数
        A (list): テレポーターの転送先
    """
    # タブリング表の作成
    to = [A]
    for i in range(math.ceil(math.log2(K))):
        to_i = to[i]
        to_next = []
        # 使用する場合のみ、要素を入れておく
        if ((K >> i) & 1):
            for j in range(N):
                to_next.append(to_i[to_i[j]-1])

        to.append(to_next)

    result = 1
    for i in range(math.ceil(math.log2(K))):
        if ((K >> i) & 1):
            result = to[i][result-1]

    print(result)


if __name__ == "__main__":
    N, K, A = get_input()
    main(N, K, A)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int,input().split())
A=input().split()
def judge(a):
  j=0
  i=0
  for i in range(len(a)-1):
    if a[i]==a[len(a)-1]:
      j=1
      break
    else:
      i+=1
      
  ju=np.array([j,i])
  return ju
town=1
route2=[]
route=[1]
for i in range(K):
  town=A[int(town)-1]
  route.append(town)
  if judge(route)[0]==1:
    for j in range(judge(route)[1]-1,len(route)-2):
      route2.append(route[j])
    print(route2)
    print(route2[((N-judge(route)[1]+1)%len(route2))-2])
    break
          
if len(route)==N:
          print(route[N-1])

Submission status: Runtime Error

Solution code:
a, b = list(map(int, input().split()))
c = list(map(int, input().split()))
itta2 = set([])
mati = 1
i = 0
itta = []
t0 = time.time()
while True:
    if mati in itta2:
        saisyo = mati
        break
    else:
        itta2.add(mati)
        itta.append(mati)
        mati = c[mati-1]
        i += 1
print(time.time()-t0)
risuto2 = set([])
risuto = []
t0 = time.time()

while True:
    if mati in risuto2:
        break
    else:
        risuto2.add(mati)
        risuto.append(mati)
        mati = c[mati-1]
print(time.time()-t0)
if b <= i-1:
    print(itta[b])
else:
    print(risuto[(b-i) % len(risuto)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
i=0
while i<k:
  temp=a[temp-1]
  if temp in result:
    break
  result.append(temp)
  i=i+1
result.append(temp)

l=len(result)
temp=result.index(result[l-1])
if k<l:
  print(result[k+1])
else:
  l2=l-1-temp
  print(result[temp+(k-temp)%l2])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
i=0
while i<k:
  temp=a[temp-1]
  result.append(temp)
  if temp in result:
    break
  i=i+1

l=len(result)
temp=result.index(result[l-1])
if k<l:
  print(result[k+1])
else:
  l2=l-1-temp
  print(result[temp+(k-temp)%l2])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
i=0
while i<k:
  temp=a[temp-1]
  result.append(temp)
  if i+1!=result.index(temp):
    break
  i=i+1

l=len(result)
temp=result.index(result[l-1])
if k<l:
  print(result[k+1])
else:
  l2=l-1-temp
  print(result[temp+(k-temp)%l2])

Submission status: Runtime Error

Solution code:
def inp():exec('global inppp\ninpmoto,inppp=input().split(),[]\nfor i in inpmoto:\n try:inppp.append(int(i))if float(i).is_integer()else inppp.append(float(i));\n except:\n  try:inppp.append(str(i))\n  except:print("error in inp")');return inppp[0]if len(inppp)==1else list(inppp)
a, b = inp()
c = inp()
d = []
for i in c:
    d.append(i-1)
itta = []
mati = 0
i = 0
while True:
    if mati in itta:
        saisyo = mati
        break
    else:
        itta.append(mati)
        mati = d[mati]
        i += 1
risuto = []
while True:
    if mati in risuto:
        break
    else:
        risuto.append(mati)
        mati = d[mati]
if b <= i-1:
    print(itta[b]+1)
else:
    print(risuto[(b-i)%3]+1)

Submission status: Runtime Error

Solution code:
def inp():exec('global inppp\ninpmoto,inppp=input().split(),[]\nfor i in inpmoto:\n try:inppp.append(int(i))if float(i).is_integer()else inppp.append(float(i));\n except:\n  try:inppp.append(str(i))\n  except:print("error in inp")');return inppp[0]if len(inppp)==1else list(inppp)
a, b = inp()
c = inp()
d = []
for i in c:
    d.append(i-1)
itta = []
mati = 0
i = 0
while True:
    if mati in itta:
        saisyo = mati
        break
    else:
        itta.append(mati)
        mati = d[mati]
        i += 1
risuto = []
while True:
    if mati in risuto:
        break
    else:
        risuto.append(mati)
        mati = d[mati]
if b <= i:
    print(itta[b]+1)
else:
    print(risuto[(b-i)%3]+1)

Submission status: Runtime Error

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

i = 0
s = "0"
while True:
    if str(A[i]) in s:
        r = s.index(str(A[i]))
        break
    s +=str(A[i])
    i = A[i]
print(int(s[r+(1 if r != 0 else 0):][(K-r)%len(s[r:])])+1)

Submission status: Runtime Error

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


c = [0]*(N+1)
i = 1
cnt = 0
f = True

while cnt < K:
    if f and c[A[i]]:
        loop = cnt-c[A[i]]
        cnt += (K-cnt-1)//loop*loop
        f = False

    cnt += 1
    i = A[i]
    c[i] = cnt

print(i)

Submission status: Runtime Error

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


c = [0]*(N+1)
i = 1
cnt = 0
f = True

while cnt < K:
    if f and c[A[i]]:
        print(cnt, i, c)
        loop = cnt-c[A[i]]
        cnt += (K-cnt-1)//loop*loop
        if cnt == K:
            break
        f = False

    cnt += 1
    i = A[i]
    c[i] = cnt

print(i)

Submission status: Runtime Error

Solution code:
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
p = 1
B = [-1]*N
B[0] = 0
count = 1
while K < count :
	p = A[p-1]
	if B[p-1] != -1:
		break
	B[p-1] = count
	count += 1
if K < count:
	print(p)
	return
K -= count
K = K % (count-B[p-1])
for i in range(K):
	p = A[p-1]
print(p)

Submission status: Runtime Error

Solution code:
l = list(map(int,input().split(' ')))
N = l[0]
K = l[1]
A = list(map(int,input().split(' ')))
x = 1
for i in range(K):
  x = A[x]
print(x)

Submission status: Runtime Error

Solution code:
l = list(map(int,input(' ')))
N = l[0]
K = l[1]
A = list(map(int,input(' ')))
x = 1
for i in range(K):
  x = A[x]
print(x)

Submission status: Runtime Error

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

now = 0
cnt = 0
pl = [0]

while k > 0:
    now = tel[now]-1
    k -= 1
    if now in pl:
        break
    else:
        pl.append(now)
    
loop = now
cnt = 0
while k > 0:
    now = tel[now]-1
    cnt += 1
    k -= 1
    if now == loop:
        break

k %= cnt
for i in range(k):
    now = tel[now]-1

print(now+1)

Submission status: Runtime Error

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

now = 1
count = 0
history = [0]*(N+1)
history[0] = now

check = [0]*N
check[now-1] = 1

for i in range(1,N+1):
    now = lst[now-1]
    count += 1
    history[i] = now
    if check[now-1] == 1:
        break
    else:
        check[now-1] = 1

for i in range(1,N+1):
    if history[i] == now:
        # print(i + (K-i ) % (count-i))
        # print(history)
        # print(i,count)
        # print((K-i ) % (count-i))
        print(history[ i + (K-i ) % count-1])
        break

Submission status: Runtime Error

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

now = 1
count = 0
history = [0]*K
history[0] = now

for i in range(1,N+1):
    now = lst[now-1]
    count += 1
    if now==1: break
    history[i] = now

print(history[K%count])

Submission status: Runtime Error

Solution code:
import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
#A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
    else:
        A2[current_town] = i

print(current_town+1)

Submission status: Runtime Error

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

s = 1
place = [s]
first = set(place)

for i in range(n):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place(ans))
        break
    else:
        place.append(s)
        first.add(s)
else:
    print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
n,k = map(int,input().split())
a = list(map(int,input().split()))
li = [1,]
ans = [0] * n 
tmp = 1
ind = []
count = 0
for i in range(10**5):
    li.append(a[tmp-1])
    ans[tmp-1] += 1
    tmp = a[tmp-1]
    for j in range(len(ans)):
        if ans[j] == 2:
            count = sum(ans) -1
            ind.append(a[tmp-1])
            ans[j] = 3
    if any(item > 3 for item in ans):
        break
q = deque(ind)
q.rotate(-1)
s = count-2*len(ind)-1
if s >= k:
    print(li[k])
else:
    print(q[k%len(q)-s])

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
a_list = list(map(int,input().split()))
visited_town = [0]*n
visit_route = []
key_loop_start = 0
loop_start = 0
def dfs(x,y):
    global key_loop_start
    global loop_start
    if visited_town[x] == 0:
        visit_route.append(x)
        visited_town[x] = y
        yy = y+1
        dfs(a_list[x]-1,yy)
    else:
        loop_start = x
        key_loop_start = visited_town[x]-1
        pass

dfs(0,1)

kitamichi = len(visit_route)
loop = kitamichi - key_loop_start
if k < kitamichi:
    print(visit_route[k])
elif k == kitamichi:
    print(loop_start)
else:
    nokori_port = k - kitamichi
    a = nokori_port % loop
    print(visit_route[key_loop_start+a]+1)

#print(visit_route)
#print(loop_start)
#print(key_loop_start)
#print(loop)

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
a_list = list(map(int,input().split()))
visited_town = [0]*n
visit_route = []
key_loop_start = 0
loop_start = 0
def dfs(x,y):
    global key_loop_start
    global loop_start
    if visited_town[x] == 0:
        visit_route.append(x)
        visited_town[x] = y
        yy = y+1
        dfs(a_list[x]-1,yy)
    else:
        loop_start = x
        key_loop_start = visited_town[x]-1
        pass

dfs(0,1)

kitamichi = len(visit_route)
loop = kitamichi - key_loop_start
if k < kitamichi:
    print(visited_town[k])
elif k == kitamichi:
    print(loop_start)
else:
    nokori_port = k - kitamichi
    a = nokori_port % loop
    print(visit_route[key_loop_start+a]+1)

#print(visit_route)
#print(loop_start)
#print(key_loop_start)
#print(loop)

Submission status: Runtime Error

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

checker = [None for i in range(n)]
checker[0] = 0
num = 0
count = 0

flag = 0
while flag==0 and count!=k:
    if checker[lst[num]-1]==None:
        checker[lst[num]-1]=count+1
        count+=1
        num = lst[num]-1
    else:
        flag = 1
        count+=1

remaining = (k-checker[lst[num]-1])%(count-checker[lst[num]-1])
num = lst[num]-1
# print("~~~")
# print("rem",remaining)
# print("checker",checker)
# print("num",num)
# print("count",count)

for i in range(remaining):
    num = lst[num]-1

print(num+1)

Submission status: Runtime Error

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

Tel=[0]*(2*N+1)
Tel[0]=1

I=0
J=0

for i in range(1,2*N+1):
  if(A[ Tel[i-1] -1 ] in Tel ):
    J=Tel.index(A[ Tel[i-1] -1 ])
    I=i
    Tel[i]=A[ Tel[i-1] -1 ]
    break
  Tel[i]=A[ Tel[i-1] -1 ]
  

    
#print(Tel)

#print(I,J)
#print( (K-I)%J )
print( Tel[((K-I)%J) +I] )

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
l = [0] + list(map(int,input().split()))
places = [0 for _ in range(n+1)]
to = 1
time_2 = 0

for i in range(10**5+1):
    if places[to] == 1:
        x = to
        y = time_2
        break
    else:
        places[to] = 1
        to = l[to]
        time_2 += 1

to = 1
time_1 = 0
for j in range(y):
    if to == x:
        z = time_1
        break
    else:
        to = l[to]
        time_1 += 1

to = l[1]
if k >= z:
    for p in range(z):
        to = l[p]
    else:
        print(l[to])
else:
    for q in range((k-z)%(y-z)):
        to = l[q]
    else:
        print(l[to])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[]           
seen={}
k=1
for i in range (K):
  l.append(k)
  if not k in seen.keys():
      seen[k]=i
      k=A[k-1]
  else:
    r_start=seen[k]
    roop=i-r_start
    c=K//roop
    K=K-roop*c
    break
ans=l[K]
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[]           
seen={}
k=1
for i in range (K):
  if not k in seen.keys():
      l.append(k)
      seen[k]=i
      k=A[k-1]
  else:
    r_start=seen[k]
    roop=i-r_start
    c=K//roop
    K=K-roop*c
    break
ans=l[K]
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
*l, = map(int, input().split())
start = 1
seenl = [1]
seen = {1}
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seenl) - seenl.index(start)
		r = (k-i-1)%loop_len
		temp = seenl[seenl.index(start):][r]
		break
	seenl.append(start)
	seen.add(start)
print(temp)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
*l, = map(int, input().split())
start = 1
seenl = [1]
seen = {1}
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seenl) - seenl.index(start)
		r = (k-i-1)%loop_len
		goal = seenl[seenl.index(start):][r]
		break
	seenl.append(start)
	seen.add(start)
print(goal)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
*l, = map(int, input().split())
start = 1
seenl = []
seen = set()
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seenl) - seenl.index(start)
		r = (k-i-1)%loop_len
		goal = seenl[seenl.index(start):][r]
		break
	seenl.append(start)
	seen.add(start)
print(goal)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
start = 1
seenl = []
seen = set()
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seenl) - seenl.index(start)
		r = (k-i-1)%loop_len
		goal = seenl[seenl.index(start):][r]
		break
	seenl.append(start)
	seen.add(start)
print(goal)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
start = 1
seenl = [1]
seen = {1}
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seenl) - seenl.index(start)
		r = (k-i-1)%loop_len
		goal = seenl[-loop_len:][r]
		break
	seenl.append(start)
	seen.add(start)
print(goal)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
start = 1
seenl = []
seen = set()
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seenl) - seenl.index(start)
		r = (k-i-1)%loop_len
		goal = seenl[-loop_len:][r]
		break
	seenl.append(start)
	seen.add(start)
print(goal)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[]           
seen = {}
k=A[0]
for i in range (K):
  if not k in seen.keys():
      l.append(k)
      seen[k]=i
      k=A[k-1]

  else:
    r_start=seen[k]
    roop=i-r_start
    break
c=K//roop
K=K-roop*c
ans=l[K-1]
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
start = 1
seen = []
for i in range(k):
	start = l[start-1]
	if start in seen:
		loop_len = len(seen) - seen.index(start)
		r = (k-i-1)%loop_len
		goal = seen[-loop_len:][r]
		break
	seen.append(start)
print(goal)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[]           
seen = {}
k=A[0]
for i in range (K):
  if not k in seen.keys():
      l.append(k)
      seen[k]=i
      k=A[k-1]

  else:
    r_start=seen[k]
    roop=i-r_start
    break
K=4
c=K//roop
K=K-roop*c
ans=l[K]
print(ans)

Submission status: Runtime Error

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

before = [[-1,-1]]*N
#print(before)
def func(a,k,b):
	if b[a-1][0] >= 0:
		if K > b[a-1][0]:
			k_goal = b[a-1][0] + (K - b[a-1][0]) % (k - b[a-1][0])
		else:
			k_goal = K
		for i in range(N):
			if b[i][0] == k_goal:
				return b[i][1]
	
	b[a-1] = [k,a]
	return func(A[a-1],k+1,b)
  

print(func(1,0,before))

Submission status: Runtime Error

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

before = [[-1,-1]]*N
#print(before)
def func(a,k,b):
	if b[a-1][0] >= 0:
		k_goal = b[a-1][0] + (K - b[a-1][0]) % (k - b[a-1][0])
		for i in range(N):
			if b[i][0] == k_goal:
				return b[i][1]
	
	b[a-1] = [k,a]
	return func(A[a-1],k+1,b)
  

print(func(1,0,before))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
chk = [0] * n
flg = True
count = 0
chk[0] = 1
if k < 10 ** 5:
    for i in range(k):
        now = a[now - 1]
else:
    while flg:
        count += 1
        now = a[now - 1]
        if chk[now - 1] == 0:
            chk[now - 1] = count
        else:
            flg = False
            rem = (k - count) % (count - chk[now - 1])
    else:
        for i in range(rem):
            now = a[now - 1]
print(now)

Submission status: Runtime Error

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

before = [-1]*N
#print(before)
def func(a,k,b):
	if b[a-1] >= 0:
		roop = k - b[a-1]
		k_goal = b[a-1] + (K - b[a-1]) % roop
		return func2(1,0,k_goal)
	
	b[a-1] = k
	return func(A[a-1],k+1,b)
  
def func2(a,k,K_goal):
	if k == K_goal-1:
		return A[a-1]
	return func2(A[a-1],k+1,K_goal)
      
	
print(func(1,0,before))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n,m,k = map(int,input().split())
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))

    except:
        break;
        #または、quit(),os.exit()をして止める。
x=[[0] * (m+1) for i in range(2**n)] 
#print(listA)
for i in range(2 ** n):
    for j in range(n):
        if((i >> j) & 1 ):
            for l in range(m+1):
                x[i][l] += listA[j][l]
            
#print(x)

cnt = 10**10
t = [k] * (m+1)
#print(t)
f = 0
for i in x:
    #print(i)
    u = 0
    for j in range(2, m):
        #print(1)
        if i[j] < t[j]:
            u = 1
    if u != 1:
        if cnt >= i[0]:
            cnt = i[0]
            f = 1
    #print(cnt)    
if f != 0:
    print(cnt)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
    r.append(nx)
    if k < i:
        print(r[k] + 1)
        break
    if a[nx] in r:
        m = r.index(a[nx])
        d = i - m
        k = k - m
        k = k % d
        print(r[k + 1] + 1)
        break  
    nx = a[nx]

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
    r.append(nx)
    if k < i:
        print(r[k] + 1)
        break
    if a[nx] in r:
        if k >= len(r):
            m = r.index(a[nx])
            d = i - m
            k = k - m
            k = k % d
            print(r[k + 1] + 1)
            break  
    nx = a[nx]

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
    r.append(nx)
    if a[nx] in r:
        m = r.index(a[nx])
        d = i - m
        k = k - m
        k = k % d
        print(r[k + 1] + 1)
        break
    nx = a[nx]

Submission status: Runtime Error

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

def func(a,k):
	if k == K-1:
		return A[a-1]
	
	return func(A[a-1],k+1)
	
print(func(1,0))

Submission status: Runtime Error

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

visited = set([1])
vl = [1]
l = 1
c = 0
for x in range(k):
  n = a[vl[-1]]
  if n in visited:
    # cycle detected
    c= vl.index(n)
    cl = l - c
    
    j = k -(x+1)
    j %= cl
    print(vl[c + j])
    break
  else:
    visited.add(n)
    vl.append(n)
    l+= 1
else:
  print(vl[-1])

Submission status: Runtime Error

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

visited = set([1])
vl = [1]
l = 1
c = 0
for x in range(k):
  n = a[vl[-1]]
  if n in visited:
    # cycle detected
    c= vl.index(n)
    cl = l - c
    
    j = k -(x+1)
    j %= cl
    print(vl[c + j])
    break
  else:
    visited.add(n)
    vl.append(n)
    l+= 1
else:
  print(vl[-1])

Submission status: Runtime Error

Solution code:

if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = list(map(int,A_N))

    now = 1
    nows = [0]*(n+1)
    nows[0] = now
    nows[n] = now
    for i in range(n+1):
        b_len = len(set(nows))
        now = a_n[now-1]
        nows[i+1] = now
        a_len = len(set(nows))
        if a_len == b_len:
            target = now
            target_loc = i
            break

    start = nows.index(target)-1
    end = target_loc
    new_now = nows[start:end]
    if k > start:
        k -= start
        m = len(new_now)
        print(new_now[k%m])
    else:
        print(nows[k-1])

Submission status: Runtime Error

Solution code:

if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = list(map(int,A_N))

    now = 1
    nows = [0]*(n+1)
    nows[0] = now
    nows[n] = now
    for i in range(n+1):
        b_len = len(set(nows))
        now = a_n[now-1]
        nows[i+1] = now
        a_len = len(set(nows))
        if a_len == b_len:
            target = now
            target_loc = i
            break

    start = nows.index(target)-1
    end = target_loc
    new_now = nows[start:end]
    print(new_now)
    if k > start:
        k -= start
        m = len(new_now)
        print(new_now[k%m])
    else:
        print(nows[k-1])

Submission status: Runtime Error

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

now = 0
visited = [0]
visited_set = set([0])

for i in range(K):
    now = A[now] - 1
    if now in visited_set:
        roop_start = now
        break
    visited.append(now)
    visited_set.add(now)
    
ind = visited.index(roop_start)
roop = visited[ind:]

if len(roop) != 0:
    print(roop[(K-ind)%len(roop)]+1)
else:
    print(visited[K-1]+1)

Submission status: Runtime Error

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

now = 0
visited = [0]
visited_set = set([0])

for i in range(K):
    now = A[now] - 1
    if now in visited_set:
        roop_start = now
        break
    visited.append(now)
    visited_set.add(now)
    
ind = visited.index(roop_start)
roop = visited[ind:]

if len(roop) != 0:
    print(roop[(K-ind)%len(roop)]+1)
else:
    print(visited[K]+1)

Submission status: Runtime Error

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

now = 0
visited = [0]
visited_set = set([0])

for i in range(K):
    now = A[now] - 1
    if now in visited_set:
        roop_start = now
        break
    visited.append(now)
    visited_set.add(now)
    
ind = visited.index(roop_start)
roop = visited[ind:]

print(roop[(K-ind)%len(roop)]+1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
teleporter = list(map(lambda x: int(x)-1, input().split()))

flaglist = []
for i in range(0, N):
	flaglist.append(False)
flaglist[0] = True
movelist = [0]
now = 0
notlp = []
lp = []
for i in range(0, N):
	next = teleporter[now]
	if flaglist[next]:
		if next == 0:
			lp = movelist
		else:
			notlp = movelist[:movelist.index(next)]
			lp = movelist[movelist.index(next):]
		break
	else:
		flaglist[next]=True
		movelist.append(teleporter[next])
		now = next

if notlp != []:
	K -= 1

if K < len(notlp):
	print(notlp[K]+1)
else:
	print(lp[(K-len(notlp))%(len(lp)-1)]+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
doubleA = {}
doubleA[1] = [A[i-1] for i in range(n)]

def f(x):
    return A[x-1]
for i in range(2, 73):
    doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]
c = k - 2**(len(bin(k))-3)
start = doubleA[2**(len(bin(k))-3)][0]
for i in range(c):
    start = f(start)
print(start)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[a-1 for a in A]
Arrived=[-1 for i in range(N)]
Appeared=[]
k=0
location=0
T=0
T_List=[]
while k<=K:
    k+=1
    Arrived[location]=k-1
    Appeared.append(location)
    location=A[location]
    if Arrived[location]!=-1:
        T=k-Arrived[location]
        T_List=Appeared[k-T:k]
        break
if len(T_List)==0:
    print(Arrived.index(k-1)+1)
else:
    Destination=(K-(k-T-1))%T
    print(T_List[Destination-1]+1)

Submission status: Runtime Error

Solution code:
import numpy as np
l=list(map(int,input().split()))
N=l[0]
K=l[1]
L=list(map(int,input().split()))
L=np.array(L)
a=L[0]
v=0
X=np.zeros(N, dtype = int)
Y=np.zeros(N, dtype = int)
X[0]=1
Y[0]=0
for i in range(K):
    b=a
    if X[b-1]==1:
        k1=Y[b-1]
        k2=i+1-k1
        v=1
        a=L[a-1]
        break
    X[a-1]+=1
    Y[a-1]=i+1
    a=L[a-1]
    
if v==1:
    K=(K-k1)%k2
    for i in range(K):
        b=a
        a=L[a-1]


print(b)

Submission status: Runtime Error

Solution code:

if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = list(map(int,A_N))

    now = 1
    nows = [0]*(n+1)
    nows[0] = now
    nows[n] = now
    for i in range(n+1):
        b_len = len(set(nows))
        now = a_n[now-1]
        nows[i+1] = now
        a_len = len(set(nows))
        if a_len == b_len:
            target = now
            target_loc = i
            break

    start = nows.index(target)-1
    end = target_loc
    new_now = nows[start:end]
    print(new_now)
    if k >= start:
        k -= start
        m = len(new_now)
        print(new_now[k%m])
    else:
        print(nows[k-1])

Submission status: Runtime Error

Solution code:

if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = list(map(int,A_N))

    now = 1
    nows = [0]*(n+1)
    nows[0] = now
    nows[n] = now
    for i in range(n):
        b_len = len(set(nows))
        now = a_n[now-1]
        nows[i+1] = now
        a_len = len(set(nows))
        if a_len == b_len:
            target = now
            target_loc = i
            break

    start = nows.index(target)-1
    end = target_loc
    new_now = nows[start:end]

    k -= start
    m = len(new_now)

    print(new_now[k%m])

Submission status: Runtime Error

Solution code:

if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = list(map(int,A_N))

    now = 1
    nows = [0]*(n+1)
    nows[0] = now
    nows[n] = now
    for i in range(n):
        now = a_n[now-1]
        for j in range(len(nows)):
            if now == nows[j]:
                target = now
                target_loc = i
                break
        nows[i+1] = now

    start = nows.index(target)-1
    end = target_loc
    new_now = nows[start:end]

    k -= start
    m = len(new_now)

    print(new_now[k%m])

Submission status: Runtime Error

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

if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = list(map(int,A_N))

    now = 1
    nows = [0]*(n+1)
    nows[0] = now
    nows[n] = now
    for i in range(n):
        now = a_n[now-1]
        nows[i+1] = now
        if has_duplicates(nows):
            target = now
            target_loc = i
            break

    start = nows.index(target)-1
    end = target_loc
    new_now = nows[start:end]

    k -= start
    m = len(new_now)

    print(new_now[k%m])

Submission status: Runtime Error

Solution code:
N, K = [int(v) for v in input().split(" ")]
Ai = [int(v) for v in input().split(" ")]
assert len(Ai) == N


path = [0]
while True:
  next_city = Ai[path[len(path) - 1]] - 1
  if next_city in path:
    break
  path.append(next_city)

for i, city in enumerate(path):
  if city == next_city:
    start = path[:i]
    circle = path[i:]
    break

if K <= len(start):
  print(start[K] + 1)
else:
  K -= len(start)
  print(circle[K % len(circle)] + 1)