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:
import math
n,k=map(int,input().split())
a_lis=list(map(int,input().split()))
direct={ i+1:a_lis[i] for i in range(len(a_lis))}
 
d_lis=[1]
j=direct[1]
while True:
  d_lis.append(j)
  j=direct[j]
  if j==1:
    break
dp=d_lis*(math.floor(k/len(d_lis))+1)
 
print(dp[k-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
List = list(map(int, input().split()))
a = 1
for i in range(K):
  a = List[a]

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
p=1
l=[0]*n
#l[0]+=1
if k<=100000:
    for i in range(k):
        p=A[p-1]
        l[p-1]+=1
    print(p)
    exit()
else:
    for i in range(100001):
        p=A[p-1]#A[0]の番号の町に飛ぶ
        l[p-1]+=1
        #print(p)
        if l[p-1]==2:
            x=i
            break
    #print("p=",str(p))
    #print(l,x)

    d=1
    for j in range(100001):
        d=A[d-1]#A[0]の番号の町に飛ぶ
        if d==p:
            y=j
            break
    #print("y=",str(y))
    z=(k-x-1)%(x-y)#yから数えて残り回す回数
    #print(z)
    for i in range(z):
        p=A[p-1]
        print(p)

Submission status: Runtime Error

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

li = [0]
li.extend(list(map(int, input().split())))

tuita = [0]*(n+1)

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

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

Submission status: Runtime Error

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

for _ in range(K):
    if A[loop[-1]]-1 not in loop:
        loop.append(A[loop[-1]]-1)
    else:
        idx = loop.index(A[loop[-1]]-1)
        break

l = len(loop)
print(loop[(K+1-idx)%(l-idx)+idx-1]+1)

Submission status: Runtime Error

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

def step(pos, root, cnt, k, lst):
    if cnt == k:
        return pos
    if pos in root:
        return lst[root[pos] + (K-root[pos]) % (cnt-root[pos])]
    root[pos] = cnt
    lst.append(pos)
    cnt += 1
    return step(A[pos], root, cnt, k, lst)

print(step(0, {}, 0, K, []) + 1)

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
for i in range(N):
    A[i] -= 1
    
visited = [-1] * N
visited[0] = 0
ret = [0]
next_ = A[0]

for i in range(1, K):
    if visited[next_] != -1:
        roop_start = visited[next_]
        break
    ret.append(next_)
    visited[next_] = i
    next_ = A[next_]
    
if K <= roop_start:
    print(ret[K])
else:
    roop_length = len(ret) - roop_start
    mod = (K - roop_start) % roop_length
    print(ret[roop_start + mod] + 1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
if A[1] != 1:
    memo = [1]
    def dfs(i, town=1):
        global memo
        arrival = A[town]
        if arrival in memo:
            memo.append(arrival)
            return memo
        memo.append(arrival)
        return dfs(i+1, A[town])

    table = dfs(0, 1)
    indexes = [i for i, x in enumerate(table) if x == table[-1]]
    q = indexes[0]
    r = (k - q) % (indexes[1] - q)
    print(table[q+r])
else:
    print(1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = [int(e) for e in input().split()]
 
next_map = {}

for i, e in enumerate(A):
    next_map[i+1] = e
 
 
history = ""
here = 1
p_i = 0
unit_length = 0
history += str(here)
 
for i in range(2*N):
    here = next_map[here]
    if str(here) in history:
        p_i = history.index(str(here))
        n_i = i
        unit_length = n_i - p_i + 1
        break
    history += str(here)
 
 
if K < p_i:
    print(history[K])
else:
    print(history[p_i + (K-p_i) % unit_length])
N, K = map(int, input().split())
A = [int(e) for e in input().split()]

next_map = {}

for i, e in enumerate(A):
    next_map[i+1] = e


history = ""
here = 1
p_i = 0
unit_length = 0
history += str(here)

for i in range(N):
    here = next_map[here]
    if str(here) in history:
        p_i = history.index(str(here))
        n_i = i
        unit_length = n_i - p_i + 1
        break
    history += str(here)


if K < p_i:
    print(history[K])
else:
    print(history[p_i + (K-p_i) % unit_length])
提出情報

Submission status: Runtime Error

Solution code:
from collections import defaultdict,deque

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

loop = N
pos = 0
oto = defaultdict(int)
ind = 0
tej = deque([])
for i in range(N):
    pos = A[pos] - 1
    tej.append(pos+1)
    if oto[pos] == 1:
        ind = A.index(pos+1)
        loop = i - ind + 1
        break
    oto[pos] = 1

##print("loop",loop,"ind",ind)
##print(tej)
if K <= ind:
    print(tej[K])
else:
    print(tej[(K-ind)%loop+max(0,ind-1)])

Submission status: Runtime Error

Solution code:
def calc(i, k):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count < k):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0, k)
if count < k:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[k] + 1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
for i in range(k):
	now=a[now]
	if now in lst:
		memo=now
		break
	lst.append(now)
i=a.index(memo)
l=len(lst)
if memo!=0:
	lst=lst[i:]
print(lst[(k-l)%len(lst)]+1)

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'

i = 0
while True:
    # print('stay', i)

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

    # 訪問リスト
    A_cycle = A_cycle + str(A[i])

    # 次の番号
    i = A[i] - 1

    # print('next', i)

    # 行ったことある
    if A_itta[i]:
        # print('loop at=', i)
        break

# 最初の町からループする場合
if i == 0:
    cycle = A_cycle[0:-1]
    ans = cycle[K % len(cycle)]

else:
    cycle = A_cycle[i + 1:-1]
    ans = cycle[(K - (i + 1)) % len(cycle)]

print(ans)

Submission status: Runtime Error

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

roop = []

sta = []

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

roop.append(1)

for i in range(len(s)):
    x = s[roop[-1]-1]
    if x in roop:
        break
    else:
        roop.append(x)

rc = roop.copy()
sta = rc[:s.index(roop[-1]-1)]
del roop[:s.index(roop[-1]-1)]

syuki = len(roop)

m = k-len(sta)

print(roop[m%syuki])

Submission status: Runtime Error

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

exist = [a[0]]
ex = {}

cnt = 1
while(True):
    if a[exist[-1]-1] in ex:
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    exist.append(a[exist[-1]-1])
    ex[a[exist[-1]-1]] = 0
    
    cnt += 1
    if cnt >= k-1:
        print(exist[-1])
        exit()


#k = k-idx
#print(idx,exist,cnt)
d = (k-idx) % (cnt-idx)

print(exist[d+idx-1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A_list = list(map(int,input().split()))
location = 0

for k in range(K):
    location = A_list[location]
    
print(location)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [False for _ in range(N)]

i = 1
c = 0
jun = []
while True:
    if visited[i-1]:
        lp = i
        break
    else:
        visited[i - 1] = True
        jun.append(i)
        i = A[i-1]
        c += 1
ji = jun.index(lp)
K -= len(jun[:ji+1])
jun = jun[lp:]

kai = K % c

if len(jun) == 0:
    print(1)
    exit()

if kai == len(jun):
    print(jun[0])
else:
    print(jun[kai])

Submission status: Runtime Error

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

a = list(map(int, input().split()))
nxt = a.index(1) - 1

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

ans = a[nxt]
print(ans)

Submission status: Runtime Error

Solution code:
n,k = input().split()
aa = input().split()
a = [int(s) for s in aa]
ind = a.index(1)
ans = 0
for i in range(int(k) + 1):
    if i == 0:
        ans = a[ind]
    else:
        ans = a[ans - 1]
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
line = list(map(int, input().split()))
town = 1
town_list = [1]
for i in range(k):
  town = line[town - 1]
  town_list.append(town)
  if town_list.count(town) == 2:
    break
first = town_list.index(town)
last = len(town_list) - 1
difference = last - first
r = (k - first) % difference
print(town_list[first + r])

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
loop_size = 0
loop = [None]*N
now = 0
for i in range(N):
    loop_size += 1
    to = A[now] - 1
    now = to
    loop[i] = to
    if to == 0:
        break
print(loop[K%loop_size - 1]+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
t=1
s=[1]
m=0
u=0
flag="T"
for i in range(k):
  t=a[t-1]
  for j in range(i+1):
    if t==s[j]:
      m=i-j+1
      u=j
      flag="F"
      break
  if flag=="F":
    break
  s.append(t)
print(s[(k-u)%m+u])

Submission status: Runtime Error

Solution code:
def main():

    N_ko, K_kai = map(int, input().split())
    teleport = list(map(int, input().split()))

    index = 0
    ctr = 0
    before_ctr = 0

    Visited = []
    Visited.append(ctr)

    for i in range(N_ko + 1):

        ctr += 1
        index = teleport[index] - 1
        Visited.append(index)
        
        if (index == 0):
            FLAG = 1
            break

        if (len(Visited) != len(set(Visited))):
            FLAG = 0
            break


    if (FLAG):
        mod = K_kai % ctr
        city = 0

        for i in range(mod):
            city = teleport[city] - 1

        ans = city + 1

    # else:
    #     cnt = 0
    #     before_ctr = 0

    #     for i in range(N_ko + 1):
    #         cnt += 1
    #         current_index = teleport[index] - 1
            
    #         if (current_index == index):
    #             before_ctr = cnt
    #             break
        
    #     if (K_kai < ctr):
    #         city = 0
            
    #         for i in range(K_kai):
    #             city = teleport[city] - 1

    #         ans = city + 1

    #     else:
    #         ans = (K_kai - before_ctr) % (ctr - before_ctr)
    #         ans += 1
            

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

ans=[]
judge={}
j=0 #A進み
while judge[A[j]]!=2:
    ans.append(A[j])
    if A[j] not in judge.keys():
        judge[A[j]]=0
    judge[A[j]]+=1
    j=A[j]-1
ans.append(A[j])

st=ans.index(A[j])
ans=ans[st:-1]
K-=st
l=len(ans)
#print(ans)
print(ans[K%l-1])

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
    
al = [1]
num2ind = {1:0}
for i in range(N):
    ne = A[al[-1]-1]
    if ne in al:
        tar = num2ind[ne]
        break
    al.append(ne)
    num2ind[ne] = i+1
    
ne = i+1
if K<=len(al):
    print(al[K])
else:
    K-=tar
    K%=(ne-tar)
    print(al[tar+K])

Submission status: Runtime Error

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

m = [0 for i in range(n)]
m[0] = 1
l = [a[0]]
g = a[0]

while m[g-1] == 0:
    l.append(a[g-1])
    m[g-1] = 1
    g = a[g-1]
 #   print(l)
 #   print(m)
 #   print(g)

for i in range(len(l)):
    if l[-1] == l[i]:
        loap_before = i+1
        loap_l = l[i+1:]
        break
loap = len(l) - loap_before
#print(loap_before,loap_l,loap)

k -= loap_before
k = k%loap
#print(k)
if k == 0:
    print(loap_l[-1])
else:    
    print(loap_l[k-1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = [1]
def dfs(i, town=1):
    global memo
    arrival = A[town]
    if arrival in memo:
        memo.append(arrival)
        return memo
    memo.append(arrival)
    return dfs(i+1, A[town])

table = dfs(0, 1)
indexes = [i for i, x in enumerate(table) if x == table[-1]]
q = indexes[0]
r = (k - q) % (indexes[1] - q)
print(table[q+r])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
d={}
for i in range(0,len(l)):
    d[i+1]=l[i]
ans=1
s=set()
seq=[]
for i in range(0,k):
    ans=d[ans]
    if ans in s:
        seq.append(ans)
        break
    else:
        s.add(ans)
    seq.append(ans)
else:
    print(ans)
b=seq.index(seq[-1])
rep=seq[b:-1]
sl=len(seq)-seq.index(seq[-1])-1
k=k-b-1
ans=seq[b]
x=k%sl
#print(x,sl)
print(rep[x])

Submission status: Runtime Error

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

a = [i - 1 for i in a]
l = []
used = [0 for _ in range(n)]
used[0] = 0

c = 0
flag = True
while 1:
    if flag:
        flag = False
    else:
        l.append(c)
    if used[c] == 1 and l:
        break
    used[c] = 1
    c = a[c]

if len(l)<k:
    end = l[-1]
    p = 0
    s = []
    for i in range(len(l)):
        if l[i] == end:
            s = l[i:-1]
            p = i
            break

    b = (k-p) % len(s)
    print(l[b] + 1)
else:
    print(l[k-1])

Submission status: Runtime Error

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

A = list(map(int,input().split()))
t = 0
c = 0
ans = [0] * n
L = []
s = 0
for _ in range(k):
    if ans[t] == 1:
        temp = t + k%c
        temp %= n
        t = L[temp]
        break
    else:
        ans[t] = 1
        L.append(t)
        t = A[t]-1
        c += 1
print(t+1)

Submission status: Runtime Error

Solution code:
A_list = []
rolling_list = []

def recursive_teleport(now, N):
    if N == 1:
        print(A_list[now])
        return
    else:
        next = A_list[now]
        if next in rolling_list:
            if len(rolling_list) < N:
                next_n = (N % len(rolling_list))
                recursive_teleport(next, next_n)
            else:
                recursive_teleport(next, N-1)
        else:
            rolling_list.append(next)
            recursive_teleport(next, N-1)


def resolve():
    N, K = map(int, input().split())
    global A_list
    A_list = list(map(int, input().split()))
    A_list.insert(0, 0)

    recursive_teleport(1, K)


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

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

roop = [1]
start=0

for i in range(N):
    roop.append(A[roop[-1]-1])
    if roop[-1] in set(roop[:-1]):
        start=roop[:-1].index(roop[-1])
        break
print(roop[2:-1][(K-start)%len(roop[2:-1])])

Submission status: Runtime Error

Solution code:
def calc(i, k):
    ans_list = [i]
    count = 1
    i = a[i]
    while (i not in ans_list) & (count < k):
        ans_list.append(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0, k)
if count < k:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[k] + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = [int(i)-1 for i in input().split()]
l = []
d = {}
p = 0
while p not in d:
    d[p] = len(l)
    l.append(p)
    p = A[p]
if K <= len(l):
    print(l[K] + 1)
    exit()
K -= d[p]
K %= len(l) - d[p]
print(l[K + d[p]] + 1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=LI()
b=[]
visited=[-1]*n

now=1
for i in range(k):
    nxt=a[now-1]
    b.append(nxt)
    if visited[nxt-1]==-1:
        visited[nxt-1]=i+1
    else:
        mod=(visited[nxt-1],i+1)
        break
    now=nxt

ml,mr=mod
k=(k-ml)%(mr-ml)+ml
print(k)

print(b[k-1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = [0 for _ in range(n)]
f[0] = 1

v = [0]
s = 0
idx = 0
loop = 0
last = 0

for i in range(k):
    vi = a[last] - 1
    if not f[vi]:
        f[vi] = 1
        last = vi
        s += 1
        v.append(vi)
    else:
        s += 1
        idx = v.index(vi)
        loop = s - idx
        break

remain = k - s
print(v[idx:][remain % loop] + 1)

Submission status: Runtime Error

Solution code:
A_list = []
rolling_list = []

def recursive_teleport(now, N):
    if N == 1:
        print(A_list[now])
        return
    else:
        next = A_list[now]
        if next in rolling_list:
            if len(rolling_list) < N:
                next_n = (N % len(rolling_list))
                print(next_n)
                recursive_teleport(next, next_n)
            else:
                recursive_teleport(next, N-1)
        else:
            rolling_list.append(next)
            recursive_teleport(next, N-1)


def resolve():
    N, K = map(int, input().split())
    global A_list
    A_list = list(map(int, input().split()))
    A_list.insert(0, 0)

    recursive_teleport(1, K)


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
from sys import stdin
def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x)-1 for x in stdin.readline().rstrip().split()]
    X = 0
    C = 0
    Log = []
    while C <= K:
        X = A[X-1]
        C += 1
        if (X in Log) == True:
            break
        Log += [X]
        
        #print(Log)
        #print('C',C)
        #print('X',X)
        
    K = (K - C+1)%(C - Log.index(X))
    #print(K)
    print(Log[Log.index(X)+K])
    
main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
vis = [0]*n


initial = []
loop = []
def dfs(g, i):
    if vis[i] == 2:
        return
    elif vis[i] == 1:
        loop.append(i)
    else:
        initial.append(i)
    vis[i] += 1
    return dfs(g, g[i])

dfs(a, 0)

if k < len(initial):
    print(initial[k] + 1)
else:
    k -= len(initial)
    print(loop[k%len(loop)] + 1)

Submission status: Runtime Error

Solution code:
n , k = map(int , input().split())
a = list(map(int , input().split()))
ans = [1]
i = a[0]

while i not in ans:
  ans.append(i)
  i = a[i - 1]

ind = ans.index(i)

repeat = ans[max(0 , ind - 1):]

start = ans.index(repeat[0])

# print(repeat)

print(ans[(k + 1) % len(ans) - 1 + start])

Submission status: Runtime Error

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

C = []
def move(i,id,k):
    global C
    C.append(id)
    if i == k:
        return
    move(i+1,A[id-1],k)

move(0,1,N+1)

key = 0
for i in C:
    if C.count(i) == 2:
        key = i
        break

key_list = [i for i, x in enumerate(C) if x == key]
cycle = key_list[1]-key_list[0]

if K < key_list[0]:
    ans = C[K-1]
else:
    d = (K-key_list[0]) % cycle 
    ans = C[key_list[0] + d]

print(ans)

Submission status: Runtime Error

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

A0=list(range(N))
temp=0

for i in range(N):
  temp=A[temp]
#  print(temp)

print(temp)

Submission status: Runtime Error

Solution code:
root = list()


def dfs(A, num, N):
    if A[num] in root:
        return root.index(A[num])
    else:
        root.append(A[num])
        return dfs(A, A[num]-1, N)


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

    start = dfs(A, 0, N)

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


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
p=1
l=[0]*n
#l[0]+=1
if k<=100000:
    for i in range(k):
        p=A[p-1]
        l[p-1]+=1
    print(p)
    exit()
else:
    for i in range(100001):
        p=A[p-1]#A[0]の番号の町に飛ぶ
        l[p-1]+=1
        #print(p)
        if l[p-1]==2:
            x=i
            break
    #print("p=",str(p))
    #print(l,x)

    d=1
    for j in range(100001):
        d=A[d-1]#A[0]の番号の町に飛ぶ
        if d==p:
            y=j
            break
    #print("y=",str(y))
    z=(k-x-1)%(x-y)#yから数えて残り回す回数
    for i in range(z):

Submission status: Runtime Error

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

N = NK[0]
K = NK[1]

town = 0
for i in range(K-1):
  town = A[town]
  
print(str(town))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split(" "))
a = list(map(int,input().split(" ")))
memory = [1]
i = 1
while True:
  if not a[i-1] in memory:
    memory.append(a[i-1])
    i = a[i-1]
  else:
    start = memory.index(a[i-1])
    break
  if len(memory) >= n:
    break
if k < len(memory):
  print(memory[k-1])
else:
  loop = len(memory)-start
  town = (k - start) % loop
print(memory[town + start])

Submission status: Runtime Error

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

List=[0]*n
pattern=[1]
now=0
nex=0
List[0]=1
end_flag=0

while end_flag==0:
    nex=a[now]
    if (List[nex-1]==1):
        target1=nex
        end_flag=1
        break
    else:
        List[nex-1]+=1
        pattern.append(nex)
        now=nex-1

left=pattern.index(target1)
right=len(pattern)
kaisu=right-left
kaisu2=(k-right)%kaisu

if (k<=right):
    print(pattern[k])
else:
    print(pattern[left+kaisu2])
    
    
##print("left",left,"right",right)
#print(pattern[target])
#print(kaisu,target)

Submission status: Runtime Error

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

start=1
list=[1]
kenti=[0 for i in range(n)]
kenti[0]=1

zan=-1

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

#  if start in list:
  if kenti[start-1]!=0:
    zan=k-i
    break
  else:
    list.append(start)
    kenti[start-1]+=1

if zan==-1:
  print(list[-1])
  exit()


zan-=1

zan%=len(list)-1
if zan==0:
  print(list[-1])
else:
  print(list[zan])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=LI()
b=[]
visited=[-1]*n


now=1
for i in range(k):
    nxt=a[now-1]
    b.append(nxt)
    if visited[nxt-1]==-1:
        visited[nxt-1]=i
    else:
        mod=(visited[nxt-1],i)
        break
    now=nxt
    # visited[now]
    # print(i,now)
# print(now,mod)

ml,mr=mod
k=(k-ml)%(mr-ml)+ml
# print(k)

# now=1
# for i in range(k):
#     now=a[now-1]
# print(now)
print(b[k-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
past_a = [1]
first = 0
for i in range(1,n+1):
  if a_list[past_a[-1] - 1] in past_a:
    first = i
    past_a.append(a_list[past_a[-1] - 1])
    break
  else:
    past_a.append(a_list[past_a[-1] - 1])
roop = past_a.index(a_list[first])
unit = first - roop
unit_a = past_a[roop:first]
k = (k-first) % unit
print(unit_a[k])

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

loop = N
pos = 0
oto = defaultdict(int)
ind = 0
tej = []
for i in range(N):
    pos = A[pos] - 1
    tej.append(pos+1)
    if oto[pos] == 1:
        ind = A.index(pos+1)
        loop = i - ind + 1
        break
    oto[pos] = 1

##print("loop",loop,"ind",ind)
##print(tej)
if K <= ind:
    print(tej[K])
else:
    print(tej[K%loop+ind])

Submission status: Runtime Error

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

loop_lis = [1]
visited = [0 for _ in range(N)]
visited[0] = 1
loop_time = 0
start_loop = 0
for i in range(K):
  if visited[A[loop_lis[-1]-1]-1] != 0:
    loop_time = (i+1)-(visited[A[loop_lis[-1]-1]-1])
    start_loop = i+1 - loop_time
    break
  visited[A[loop_lis[-1]-1]-1] = i+1
  loop_lis.append(A[loop_lis[-1]-1])
print(loop_lis[start_loop+(K-start_loop)%loop_time])

Submission status: Runtime Error

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

h =[]
i = 1

while  i not in h:
    h.append(i)
    i = A[i-1]
else:
    s = h[h.index(i):]
    h = h[:h.index(i)]

if K > len(h):
    K = (K-len(h))%len(s)
    print(s[K])
else:
    print(h[K])

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
visit = [-1 for i in range(n)]
root = []
now = 1
length = 0
root.append(now)
if k > 10**6:
    for i in range(n):
        if visit[now - 1] != 1:
            visit[now - 1] = 1
            now = a[now - 1]
            root.append(now)
            length += 1
        else:break
    start = 0
    for i in range(n):
        if root[i] == now:
            start = i
            break
    k -= start
    k %= length
    ans = root[start]
    for i in range(k):
        ans = root[start + i]
    print(ans)
else:
    for i in range(k):
        now = a[now - 1]
    print(now)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
loc=0
x=[-1]*N
y=0
distance=0
flag=False
for i in range(N):
  if(x[loc]>=0):
    distance=i-x[loc]
    y=x[loc]
    flag=True
    break
  else:
    x[loc]=i
  loc=A[loc]-1
if(flag==True and y+distance<K):
  print(x.index((K-y)%distance+y)+1)
else:
  print(x.index(K)+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
p=1
l=[0]*n
#l[0]+=1
if k<=100000:
    for i in range(k):
        p=A[p-1]
        l[p-1]+=1
    print(p)
    exit()
else:
    for i in range(100001):
        p=A[p-1]#A[0]の番号の町に飛ぶ
        l[p-1]+=1
        #print(p)
        if l[p-1]==2:
            x=i
            break
    #print("p=",str(p))
    #print(l,x)

    d=1
    for j in range(100001):
        d=A[d-1]#A[0]の番号の町に飛ぶ
        if d==p:
            y=j
            break
    #print("y=",str(y))
    z=(k-y-1)%(x-y)#yから数えて残り回す回数
    for i in range(z):
        p=A[p-1]
    print(p)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
a = list(map(int,input().split()))
ans = 1
c = [1]
for j in range(2*n):
    i = a[c[-1]-1]
    if i in set(c):
        g = j+1
        break
    c.append(i)
s = c.index(g)

print(c[k] if k < s else c[s - 1 + ((k-s+1) % (len(c)-s+1))])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
import numpy as np
As = list(map(lambda x: int(x)-1, input().split()))

cnt = 1
v = 0
checked = [-1]*N
checked[0] = 0
while True:
    v = As[v]
    if checked[v]!=-1:
        loop = cnt-checked[v]
        road = checked[v]
        break
    cnt += 1
    checked[v] = cnt-1

ite = K if K<=road else road+((K-road)%loop)

v = 0
for i in range(ite):
    v = As[v]

print(v+1)

Submission status: Runtime Error

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    A = readInts()
    root = []
    ok = True
    l = 0
    while True:
        if ok:
            a = 1
            ok = False
        else:
            a = root[-1]
        if not A[a-1] in root:
            root.append(A[a-1])
        else:
            j = 0
            for i in range(len(root)):
                if A[a-1] == root[i]:
                    j = i
                    break
            root = root[j:]
            l = j
            #print(root)
            break

    #print(root)
    print(root[k%len(root) - (l+1)])
if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

p = [0]

k = 0
l = -1
while k < K:
    k += 1
    n = A[p[-1]]
    if n in p:
        l = n
        break
    p.append(n)
loop = len(p) - p.index(l)
k2 = (K - k) % loop
print(p[p.index(l)+k2] + 1)

Submission status: Runtime Error

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**8)
import numpy as np
done=np.array([0]*N)
X=[0]*2
L=[]
F=[True]
def dfs(start, steps):
    done[start-1]+=1
    L.append(start)
    if done[start-1]==3:
        X[1]=steps
        return
    elif done[start-1]==2 and F[0]:
        X[0]=steps
        F[0]=False
    nex=A[start-1]
    dfs(nex, steps+1)
dfs(1,0)
#print(done)
#print(X)
#print(L)
first=L[:X[0]-(X[1]-X[0])]
sec=L[X[0]-(X[1]-X[0]):X[0]-(X[1]-X[0])+(X[1]-X[0])]
#print(first, sec)
if len(first)==0:
    first=sec
if K-1<=len(first):
    print(first[K])
else:
    K=K-len(first)
    print(sec[(K%len(sec))])