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 <stdio.h>
int check(int n,int x,int A[n]){
    int count,p,i;
    count = 0;
    p = x;
    for(i=0; i<n; i++){
        count += 1;
        p = A[p-1];
        if(p == x){
            return count;
        }
    }
    return -1;
}

int main(void){
    int i,n,p,N;
    long long int k;
    scanf("%d %lld",&n,&k);
    int A[n];
    for(i=0; i<n; i++){
        scanf("%d", &A[i]);
    }
    p = 1;
    while (k > 0){
        k -= 1;
        p = A[p-1];
        N = check(n,p,A);
        if(N != -1){
            k = k % N;
        }
    }
    printf("%d",p);
    return 0;
}

Submission status: Runtime Error

Solution code:
n,k=map(int.input().split())
al=list(map(int,input().split()))
pos=0
for val in al:
  val-=1
while(k!=0):
  i=pos
  pos=al[i]
  k-=1
print(pos)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
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
l = len(ROOP)

num = (K-l-roopstart) % (l)
print(ROOP[num])

Submission status: Runtime Error

Solution code:
import numpy as np
from scipy.sparse import csr_matrix, coo_matrix

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

value = np.ones(N)
row = np.array(range(N))
col = np.array(a)

A = coo_matrix((value,(row,col)))
A = csr_matrix(A)

A_k = A ** K
r, c = A_k.nonzero()


print(int(c[0]+1))

Submission status: Runtime Error

Solution code:
temp = input()
temp = temp.split()
town_num = int(temp[0])
move = int(temp[1])

town = input()
town = town.split()
town = [int(s) for s in town]
data = [0] * 1000

position = 1

if move < 1000:
    for i in range(move):
      position = town[position-1]

else:
    for i in range(town_num):
        data[i] = town[position-1]
        position = town[position-1]
        if data[i] == data[4] and i > 4:
            count = i
            break
    cycle = count - 4
    try:
        period = (move-4)%cycle
        position = data[period+4]
        position = town[position-1]-1
    except ZeroDivisionError:
        a = 1


print(position)

Submission status: Runtime Error

Solution code:
from _collections import deque
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 = deque(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()))

s = 0
m =[]

for i in range(K):
	s = l[s] - 1
    m.append(s)
    if len(m) != len(set(m)):
      int = m.index(s) - len(m)
      x = K - m.index(s) // int
      y = K - m.index(s) - x*int
      result = m[y]
      break
    

print(result + 1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False] * n
visit[0] = True
nx = a[0]
keiro = [1]
roop = 0
for i in range(k):
    if visit[nx-1] == False:
        visit[nx-1] = True
        keiro.append(nx)
    else:
        roop = nx
        break
    nx = a[nx-1]
if roop == 0:
    print(keiro[k])
else:
    if roop == 1:
        ix = k % len(keiro)
        print(keiro[ix])
    else:
        basyo = keiro.index(roop)-1
        ix = (k-basyo+1) % len(keiro)

        print(keiro[2:][ix-1])

Submission status: Runtime Error

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

tmp = a[0]

visit = {}
visit['0'] = 0
visit[str(tmp-1)] = 1

#print('start', 0)
count = 1
ans_on = 0
#print('tmp', tmp-1)
#print('############################')

if n >= k:
    loop_num = k
else:
    loop_num = n

for i in range(loop_num):
    #print('visited', visit)
    #print('count', count)
    count += 1
    tmp = a[tmp-1]

    #print('tmp', tmp - 1)
    if str(tmp-1) in visit:
        on_index = visit[(str(tmp-1))]
        visit_len = len(visit)
        loop_list = list(visit)[on_index:]

        #print(i, count)
        k_ = k-count
        #if k_ % (visit_len-on_index) != 0:
        k_tmp =  k_ % (visit_len-on_index)
        #else:
        #    k_tmp = k_

        ans = loop_list[k_tmp]
        ans_on = 1
        #print('break########################')
        break

    visit[str(tmp-1)] = count

    #print('########################')

if ans_on == 0:
    #print('last not loop', tmp+1)
    print(tmp+1)
else:
    #print('last', int(ans)+1)
    print(int(ans)+1)

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
        if now == A[now] - 1:
            print(now+1)
            exit()
        else:
            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:
import sys, re
import math
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul	# 
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd	# 最大公約数
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

# -*- coding: utf-8 -*-

n, k = MAP()
a = LIST()
pc = [0] * n

def A(pos, count, loop):
    global a, k, pc

    count += 1
    if k == count:
        print(pos)
    if loop:
        A(a[pos-1], count, True)
    elif pc[pos-1] == 0:
        pc[pos-1] = count
        print(a[pos-1])
        A(a[pos-1], count, False)
    else:
        k = (k - pc[pos - 1]) % (count - pc[pos - 1])
        print(str(a[pos-1]) + " : loopStart")
        A(a[pos-1], -1, True)

A(1, 0, False)

Submission status: Runtime Error

Solution code:
import collections

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

h = []
i = 0
s = 1

ans = 0
while i < 2*n: 
  g = a[s-1]
  h.append(g)
  if i == k - 1:
    ans = g
    sys.exit()
  else:
    s = g
    i+=1
j = collections.Counter(h)
m = j.most_common()[0][0]
l = [i for i, x in enumerate(h) if x == m]

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

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
x = 1
step = 0
count = [0]*N
count[0] += 1
memo = [-1]*N
memo[0] = 0
M = K
for i in range(M):
    x = A[x-1]
    count[x-1] += 1
    step += 1
    K -= 1
    if memo[x-1] != -1:
        s = memo[x-1]
        t = step-s
#        print(s,t)
        break
    memo[x-1] = step
#    print(step,x,count,memo)

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

Submission status: Runtime Error

Solution code:
n,k=map(int.input().split())
al=list(map(int,input().split()))
pos=0
for val in al:
  val-=1
while(k!=0):
  i=pos
  pos=al[i]
  k-=1

Submission status: Runtime Error

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

times = [0]*(n+1)
_map = [0]*(n+1)
idx = {}

sentou = 0
num = lst[1]
first = 0
kk = 0
for i in range(1, k+1):
    times[num] += 1
    idx[i] = num
    sentou += 1
    if times[num] == 2:
        first = num
        break
    _map[num] = i
    num = lst[num]
    kk += 1

# if kk == k:
#     print(idx[k])
#     exit()


first_idx = _map[first]
num_str = ''
for i in range(first_idx, max(_map)+1):
    num_str += str(idx[i])

ans_len = (k - sentou) % len(num_str)
print(num_str[ans_len])

Submission status: Runtime Error

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

rireki = [0] * (n+10)
curr_c = 1
rireki[1] = 0

no_loop_count = k
loop_cnt = 1

mugen = False
for i in range(n):
    next_c = a_list[curr_c-1]
    # print(i, curr_c, next_c)
    # if next_c == curr_c:
    #     mugen = True
    if next_c == 1:
        no_loop_count = 0
        loop_cnt = i + 1
        break
    elif rireki[next_c] == 0:
        rireki[next_c] = i+1
    else:
        # print(next_c)
        no_loop_count = rireki[next_c] -1
        loop_cnt = i - no_loop_count
        break
    curr_c = next_c


# if not mugen:
if k <= no_loop_count:
    ans = rireki.index(k)
else:
    # print('k',k)
    # print('no', no_loop_count)
    # print('lo', loop_cnt)
    ind = (k-1-no_loop_count)%loop_cnt +1
    rireki_fix = [i-no_loop_count for i in rireki]
    # print(ind, 'ind')
    ans = rireki_fix.index(ind)
    # print(rireki_fix)
    print(ans)

Submission status: Runtime Error

Solution code:
n, k = list(map(int,input().split()))
# a = list(map(int,input().split()))
# 循環を調べる
tmp_city=[]
c=0
count=0
init_city=0

while (1):
    count+=1
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

Submission status: Runtime Error

Solution code:
import sys

def mink(numk,l):
    x = 1
    for _ in range(k):
        x = a[x-1]
    return x

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

# kが小さい時
if k <= 10**3:
    print(mink(k,a))
    sys.exit()

z = 1
p = [1]
for _ in range(k):
    z = a[z-1]
    if z in p:
        p.append(z)
        break
    p.append(z)

# 周期の来る前の回数
num = p.index(p[-1])

p = p[num:]

# 周期
pn = len(p)-1

k -= num

# 回数/周期のあまり
anum = k%pn
print(p[anum])

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]
    elif l.count(l[0])==1:
        break
print(l[k%len(l)-1])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
x = 1
n = [0]*N
for i in range(K):
    if n[x-1]>0:
        period = n[x-1]
        break
    else:
        for j in range(N):
            if n[j] >= 1:
                n[j] += 1
        n[x-1]+=1
    x = A[x-1]
x = 1
for a in range(K%period+period*100):
    x = A[x - 1]
print(x)

Submission status: Runtime Error

Solution code:
n, k_in=map(int, input().split())
a_l=list(map(int, input().split()))

def f(s):
    a = s
    b = s
    i=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        i+=1

    j=1
    a = a_l[0]
    while True:
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        j+=1

    k=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        k+=1

    return j, k

lam, myu = f(a_l[0])

pos=[]
pos.append(a_l[0])
for i in range(n):
    pos.append(a_l[pos[i]])

if k_in<=lam:
    print(pos[k_in-1])
else:
    k_in=(k_in-lam)%myu
    print(pos[lam+k_in-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
s_d2 = set()
d2 = []
cur = 1
d2.append(1)
st_idx = 0
rp_lgt = 0
for i in range(n):
  cur = a[cur-1]
  if cur in s_d2:
    for j in range(len(d2)):
      if cur == d2[j] :
        st_idx = j - 1 
        rp_lgt = i - j + 1
        break
  else:
    d2.append(cur)
    s_d2.add(cur)
wk = k - st_idx
md = wk % rp_lgt
if k < st_idx:
  ans = d2[k]
else:
  ans = d2[md+st_idx]
print(ans)

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+1)
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+1])
    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 = 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]<=1000:
    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:
#D
n,k = map(int,input().split())
a_list =  list(map(int,input().split()))
count = 0
warp = []
warp.append(a_list[0])
for i in range(k+1):
    if len(a_list)>count:
        
        warp.append(a_list[warp[count]-1])

        count+=1
    else:
        break
#roopが小さい時だけac
print(warp[k-1])

Submission status: Runtime Error

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

for i in range(0,n):
 x=a[x]
 for j in range(i-1):
  y=a[y]
  if x==y:
   break
  else:
   con1 +=1
  
if k<=n:
 print(x)
else:
 w=y+k%(x-y+1)
 u=0
 for l in range(w):
  u=a[u]
 print(u)

Submission status: Runtime Error

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

x=1
l=deque()
l.append(1)
for i in range(k):
    y=a[x-1]
    if y not in l:
        l.append(y)
        x=y
    else:
        x=y
        break

j=l.index(x)
d=lem(l)
mod=(k-d)%(d-j)
for i in range(j):
    l.popleft()
print(l[mod])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
x = 1
n = [0]*N
for i in range(K):
    if n[x-1]>0:
        period = n[x-1]
        break
    else:
        for j in range(N):
            if n[j] >= 1:
                n[j] += 1
        n[x-1]+=1
    x = A[x-1]
x = 1
for a in range(K%period+period*10):
    x = A[x - 1]
print(x)

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))

x = inpl()
n = x[0]
k = x[1]
m = [0 for i in range(0, 200001)]
dic = [0 for i in range(0, 200001)]

A = inpl()

t = 0
i = 0
roop = 0
while True:
    if i == k:
        print(t + 1)
        break
    if m[t] == 0:
        m[t] = 1
        dic[t] = i
        t = A[t] -1
    else:
        roop = i - dic[t]
        break
    i += 1

for i in range(0, (k - i) % roop):
    t = A[t] - 1

print(t + 1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
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)

if k <= tere:
    print(visit_route[k]+1)
else:
    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:
import sys
import math
import itertools
import collections
import random
import time
def solve(N,K,A):
    # A = [i for i in range(2,2*10**4+2)]
    if(len(set(A)) == N): return A[K % (N - 1)]
    # N = 2*10**4
    # t = time.time()
    ans = [1]
    i = 0
    j = 0
    while N != 0:
        if(A[i] in ans):
            if(K <= len(ans)):
                return ans[K]
            else:
                index = ans.index(A[i])
                K -= len(ans[:index])
                ans = ans[index:]
                break
        else:
            ans.append(A[i])
            i = A[i] - 1
            j += 1
            N -= 1
    # print(ans,K % len(ans))
    # print(time.time() - t)
    return ans[K % len(ans)]
def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    # solve()
    print(solve(N,K,A))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n,k = [int(e) for e in input().split()]
a = [int(e) for e in input().split()]
tele = [1,a[0]]
def teleport(b,j):
    tele.append(a[b-1])
    if j >= n-2:
        return None
    teleport(a[b-1],j+1)
teleport(a[0],0)

ans = sorted(tele,key = tele.index)
same = 0
for i in range(n):
    if ans[i] == ans[i+1]:
        same = ans[i]
        break
index1 = -1
index2 = -1
for i in range(n+1):
    if tele[i] == same and index1 == -1:
        index1 = i
    elif tele[i] == same:
        index2 = i
        break

res = tele[:index2]
if k < index1:
    print(res[k])
else:
    print(res[index1 + ((k-index1)%(index2-index1))])

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
ans = 1
while ans:
  a = A[b-1]
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0

print(B[k])

Submission status: Runtime Error

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

A_itta = [None] * N
# A_cycle = '1'

A_stayed = []
# A_stayed.append(1)

is_looped = None
jump_cnt = 0

i = 0
while True:
    # for i in range(K):
    # while True:

    # 行ったことある町についた
    if A_itta[i]:
        is_looped = 1
        break

    # 訪問済みフラグ
    A_itta[i] = 1

    # 訪問順リスト
    A_stayed.append(i + 1)

    # 次の町のidx
    i = A[i] - 1
    jump_cnt += 1

    # ジャンプ回数が上限を超えた
    if jump_cnt > K:
        is_looped = 0
        break

# print('ループするまでにいった町', A_stayed)

# 最初の町からループする場合
if is_looped and i == 0:
    cycle = A_stayed
    ans = cycle[K % len(cycle)]
elif is_looped and i > 0:
    cycle = A_stayed[i + 1:]
    K -= i + 1
    ans = cycle[K % len(cycle)]
else:
    cycle = 'no cycle'
    ans = A_stayed[-1]

# print('i', i)
# print('loop=', cycle)
print(ans)

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

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

else:
    print(1)

Submission status: Runtime Error

Solution code:
, 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 = list(map(int, input().split()))
out_loop = [1]
flag = [0, 1] + [0]* (N - 1)

for a in A_list:
    if flag[a] == 0:
        out_loop.append(a)
    else:
        loop = out_loop[out_loop.index(a):]
        
if K <= (len(out_loop) - 1):
    print(out_loop[K])
else:
    k = K - len(out_loop) - 1
    k %= len(loop)
    print(loop[k - 1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

dict = defaultdict(lambda: 0)
dict[1] = 1
next = A[0]

for k in range(1,K+1):
    if dict[next] !=0:
        roop_start = dict[next]
        roop_end = k
        break
    dict[next] = k+1
    next = A[next-1]

roop = roop_end - roop_start + 1
teleport = roop_start + (K - roop_start)%roop

next = 1
for i in range(teleport):
    next = A[next-1]

print(next)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [0] * n
f = True
visited[0] = 1
index = 0
memo = [0]
while f:
    index = a[index] - 1
    if visited[index]:
        v = index
        break
    memo.append(index)
    visited[index] = 1
f = False
list_ = []
for i in range(len(memo)):
    if not (f) and memo[i] == v:
        f = True
        list_.append(memo[i])
    if f:
        list_.append(memo[i])
if k + 1 > len(memo):
    k -= len(memo)
    if (k + 1) % len(list_) == 0:
        i = len(list_) - 1
    else:
        i = (k + 1) % len(list_) - 1
    print(memo[i] + 1)
else:
    print(memo[k + 1] + 1)

Submission status: Runtime Error

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

nlist = []

h = 1
while not h in nlist:
    nlist.append(h)
#    print(h)
    h = A[h-1]

#print(nlist)
#print(h)
if len(nlist) >= K:
    print(nlist[K])
else:
    x = nlist.index(h) 
    #print(x)
    K = K - x
    K = K % (len(nlist) - x)
    print(nlist[x + K])

Submission status: Runtime Error

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

rireki = [0] * (n+10)
curr_c = 1
rireki[1] = 0

no_loop_count = k
loop_cnt = 0

end = False
for i in range(n):
    next_c = a_list[curr_c-1]
    # print(i, curr_c, next_c)
    if next_c == curr_c:
        print(next_c)
        end = True
    if next_c == 1:
        no_loop_count = 0
        loop_cnt = i + 1
        break
    elif rireki[next_c] == 0:
        rireki[next_c] = i+1
    else:
        # print(next_c)
        no_loop_count = rireki[next_c] -1
        loop_cnt = i - no_loop_count
        break
    curr_c = next_c


if not end:
    if k <= no_loop_count:
        ans = rireki.index(k)
    else:
        # print('k',k)
        # print('no', no_loop_count)
        # print('lo', loop_cnt)
        ind = (k-1-no_loop_count)%loop_cnt +1
        rireki_fix = [i-no_loop_count for i in rireki]
        # print(ind, 'ind')
        ans = rireki_fix.index(ind)
        # print(rireki_fix)

        print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
j = 0
 
for i in range (0,K+1):
	if B.count(A[j]) > 0:
		Value = B.index(A[j])
		C = B[Value::]
		break
	else:
		B.append(A[j])
		j = A[j]-1

if len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])

Submission status: Runtime Error

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

G = {}
for i in range(1, n + 1):
    G.update({i: a[i - 1]})

seen = [False] * (n + 1)
path = []

def dfs(graph, start):
    global return_ans
    seen[start] = True
    path.append(start)
    
    if seen[graph[start]]:
        return_ans = graph[start]
    else:
        dfs(graph, graph[start])
    
    return return_ans

point = dfs(G, 1)
long = len(path)
m = path.index(point)
ans = (k + 1) % (long - m)
path = path[m:]
print(path[ans])

Submission status: Runtime Error

Solution code:
from collections import deque
import numpy as np
n,k = map(int,input().split())
a = list(map(int,input().split()))
roop_find = deque([0])
i = 0
count = 0
while a[i]-1 not in roop_find:
    roop_find.append(a[i]-1)
    i = a[i] - 1
    count += 1
    if count == k:
        print(i+1)
        break
    else:
        pass
b = roop_find.index(a[i]-1)
roop = list(roop_find)[b:count+1]
k-=b
e = k % (count+1-b)
print(roop[e]+1)

Submission status: Runtime Error

Solution code:
import copy
N, K = map(int, input().split())
A = list(map(int,input().split()))
P = 0
Q = [0] + [1 for _ in range(N-1)]
R = [-1] + [0 for _ in range(N-1)]
L = 0
for i in range(K):
    P = A[P]-1
    if Q[P] == 0:
        L = copy.deepcopy(i - R[P])
        break
    if Q[P] == 1:
        R[P] = copy.deepcopy(i)
        Q[P] = 0
S = (K - R[P] - 1) % L
G = 0
for j in range(S + R[P] + 1):
    G = A[G]-1
print(G+1)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
p = 0
a = A[0]
while p < k:
    a = A[a]
print(a)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    next = a[0]
    b.append(next)
    e = 0
    s = 0
    for i in range(n-1):
        next = a[next-1]
        if next in b:
            e = i
            s = b.index(next)
            break
        b.append(next)
    c = b[s:e+1].copy()
    if k > e:
        ans = c[(k-s)%e]
    else:
        ans = b[k%e]
    print(ans)

if __name__ == '__main__':
    main()

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

Submission status: Runtime Error

Solution code:
l = list(map(int, input().split(' ')))
n = l[0]
k = l[1]
l = list(map(int, input().split(' ')))
index = l[0]-1
e = l[index]-1
lindex = [0]
count=k-1
while True and count>0:
    count-=1
    lindex += [index]
    if lindex.index(index)!=len(lindex)-1-lindex[::-1].index(index):
        break
    index = e
    e = l[index]-1
tloopsize = len(lindex)-lindex[::-1].index(index)-1-lindex.index(index)
k = k%tloopsize
if count==0:
    print(index+1)
else:
    print(l[lindex[k-1]])

Submission status: Runtime Error

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

class Args(object):
    def __init__(self, N):
        self.num_of_transition = 0
        self.path = []
        self.count_visit_node_i = [0] * N

def solve(N, K, A):
    args = Args(N)

    def walk(p):
        if max(args.count_visit_node_i) > 2:
            return True
        args.num_of_transition += 1
        args.path.append(p)
        args.count_visit_node_i[p] += 1
        walk(A[p]-1)

    if walk(0):
        pass
#    print(args.num_of_transition)
#    print(args.path)
#    print(args.count_visit_node_i)

    freq = collections.Counter(args.count_visit_node_i)
#    print(freq)

    const_step = freq[1]
    roop_step = freq[2] + freq[3]
    if K < const_step:
        ans_step = K
    else:
        ans_step = (K-const_step) % roop_step + const_step
#    print(const_step, roop_step, ans_step)

    ans_args = Args(N)

    def walk_ans(p):
        if ans_args.num_of_transition > ans_step:
            return True
        ans_args.num_of_transition += 1
        ans_args.path.append(p)
        walk_ans(A[p]-1)

    if walk_ans(0):
        pass
#    print(ans_args.num_of_transition)
#    print(ans_args.path)
#    print(ans_args.path[-1])
    print(ans_args.path[-1]+1)

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
x = 1
n = [0]*N
for i in range(K):
    if n[x-1]>0:
        period = n[x-1]
        break
    else:
        for j in range(N):
            if n[j] >= 1:
                n[j] += 1
        n[x-1]+=1
    x = A[x-1]
x = 1
for a in range(K%period+period*N):
    x = A[x - 1]
print(x)

Submission status: Runtime Error

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(len(a)):
    a[i] -= 1
pre = 0
d = []
for i in range(2*n+10):
    d.append(pre)
    pre = a[pre]
cnt = [0]*(n)
s = ""
for i in range(len(d)):
    cnt[d[i]]+=1
    if cnt[d[i]]>=3 and cnt[d[i]]<4:
        s += str(d[i])
ff = int(s[0])
for i in range(len(d)):
    if ff == d[i]:
        check = i
        break

k%=len(s)
print(a[k]+1)

Submission status: Runtime Error

Solution code:
machicount,flycount=map(int,input().split())
teleps=list(map(int,input().split()))
e=len(teleps)
gh=flycount%e
print(teleps[gh+1])