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 collections


def main():
    N, K = list(map(int, input().split(' ')))
    An  = list(map(int, input().split(' ')))
    
    dic = {}
    li = []
    STAMP = [0 for _ in range(N)]
    for i in range(len(An)):
        dic[str(i+1)] = An[i]

    status = 1
    ittatokoro = [status]
    for k in range(K):
        pre_s = status
        status = dic[str(status)]
        # print(k, pre_s, status)
        if pre_s == status:
            print(pre_s)
            return
        
        if status not in ittatokoro:
            ittatokoro.append(status)
        else:
            ittatokoro.append(status)
            break
    # print('最後の位置:', status) # 
    # print(ittatokoro, k)
    
    # c = collections.Counter(ittatokoro).most_common()[0] # nがk回数
    # K += 1 #  saisyo 1 ni irus
    START = 0
    for i, iku in enumerate(ittatokoro):
        if iku == status:
            START = i
            break
    # print('START:', START)
    
    S_ittatoko = ittatokoro[:START]
    junkan = ittatokoro[START:-1]
    len_junkan = len(junkan)
    # print('junkan:', junkan, len_junkan, S_ittatoko)
    
    junkannmadeno_hosuu = K - START
    num_junkan_kaisuu = junkannmadeno_hosuu // len_junkan
    amari_junkan_kaisuu = junkannmadeno_hosuu % len_junkan
    # print('循環回数:', num_junkan_kaisuu, amari_junkan_kaisuu)

    total = num_junkan_kaisuu * len_junkan + amari_junkan_kaisuu
    
    nokori = junkannmadeno_hosuu - total
    print(junkan[amari_junkan_kaisuu])
    
    
    
main()


# 7 4569534
# 6 5 2 5 5 3 2

Submission status: Runtime Error

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

ai=1
aj=[]
for i in range(min(n,k)):
    aim=ai
    ai=a[ai-1]
#    print(ai,aj)
    if ai in aj:
#        print(ai,aj)
        ic2=aim
        break
    else:
        aj.append(ai)
        

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

Submission status: Runtime Error

Solution code:
def Teleport(i,P):
    if(i==K):
        return print(P)
    else:
        Teleport(i+1,T[P-1])

import sys

N,K=(int(x) for x in input().split())
T=list((int(x) for x in input().split()))
sys.setrecursionlimit(100000)
Teleport(1,T[0])

Submission status: Runtime Error

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n

n,k = readInts()
a = readInts()
ex = [0]*n
ex[0] = 0
ex_r = []
f = 1
l = 1
count = 0
if a[0]==1:
	print(1)
	exit()
while 1:
	l = a[l-1]
	ex_r.append(l)
	if ex[l-1]==1:
		break
	ex[l-1]=1

first = ex_r.index(ex_r[-1])
#print(ex_r)
p2 = ex_r[:-1]
#print(p2)
repeat = len(p2)-first
#print(first,repeat)

if k<=len(p2):
	print(ex_r[k-1])
else:
	y = (k-first)%repeat
	if first!=0:
		y+=repeat
	print(ex_r[first+y-1])

Submission status: Runtime Error

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

A = list(map(int,input().split()))
visited = [1]
visited_list = [0]*N
visited_list[0] = 1
k = 0
l = 0
kaburi = 1
flag = True
def f(Ai):
    global k,l,visited,kaburi,flag
    # print("Ai",Ai,k)
    if visited_list[Ai-1] == 1 :
        kaburi = Ai
        k += 1
        l = k
    else:
        visited.append(Ai)
        visited_list[Ai-1] = 1
        k += 1
        if k == K:
            flag = False
            print(Ai)
        else:
            f(A[Ai-1])

f(A[0])
if flag == True:
    hazime = visited.index(kaburi)
    b = visited[hazime:]
    print(b[(K-hazime)%(l-hazime)])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
visited = [False for _ in range(5)]
nx = A[0] - 1
x = []
p = -1
for e, i in enumerate(range(n)):
    if visited[nx] == False:
        visited[nx] = True
        x.append(nx)
        nx = A[nx] - 1
    else:
        p = x.index(nx)
        q = x[:p]
        r = x[p:]
        break
if p!=-1:
    k = (k-len(q))%len(r)
    print(r[k-1]+1)
else:
    print(x[k-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
rep = []
rep.append(1)
onf = 0
def repeat(i,rep):
    if a[i] not in rep:
        rep.append(a[i])
        repeat(a[i]-1,rep)
    else:
        rep.append(a[i])
        return 1
onf=repeat(0,rep)
if onf==0:
    print(rep)
else:
    lr = len(rep)
    for i in range(lr):
        if rep[i]==rep[lr-1]:
            start=i
            rep2 = rep[i:lr-1]
            break
    mod = len(rep2)
    modL = [i%mod for i in range(start,start+mod)]
    resM = k%mod
    for i in range(mod):
        if resM == modL[i]:
            print(rep2[i])

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 = copy.deepcopy(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 = copy.deepcopy((K - R[P] - 1) % L)
G = 0
for j in range(S + R[P] + 1):
    G = copy.deepcopy(A[G]-1)
print(G+1)

Submission status: Runtime Error

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

route = []
i = 0

while True:
    ne = a[i]

    if i not in route:
        route.append(i)
    else:
        k = k - (len(route) - len(route[i:]))
        print(a[k%len(route[i:])])
        break

    i = ne - 1

Submission status: Runtime Error

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




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

if n == 2:
    if k % 2 == 0:
        print(a[1])
    else:
        print(a[0])
else:
    # print('start', 0)
    count = 1
    ans_on = 0
    # print('tmp', tmp-1)
    # print('############################')

    tmp = a[0]

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

    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 = [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 < index2:
    print(res[k])
else:
    print(res[index1 + ((k-index2)%(index2-index1))])

Submission status: Runtime Error

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

# ループする数を求める
loop_num = 0
loop_start_index = 0
point = 1
route_list = [1]

for i in range(K):
    point = Alist[point - 1]
    if point in route_list:
        loop_start_index = route_list.index(point)
        loop_num = i + 1 - loop_start_index
        break

    route_list.append(point)

print(route_list[loop_start_index  + ((K-loop_start_index) % loop_num)])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [1]
for i in range(n):
    if a[l[i] - 1] in l:
        res = l[:l.index(a[l[i] - 1])]
        cyc = l[l.index(a[l[i] - 1]):]
        break
    else:
        l += [a[l[i] - 1]]
print(cyc[(k - len(res)) % len(cyc)] if k > len(res) else res[k])

Submission status: Runtime Error

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


def move(cur_place, cnt):
    cnt += 1
    print(cur_place+1, cnt)
    if cnt < k:
        move(a[cur_place]-1, cnt)
    return a[cur_place]


print(move(0, 0) + 1)

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[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		j = A[j]-1

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

Submission status: Runtime Error

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

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
for i in range(N):
    town.append(i)
    flg[i] = True
    i = A[i]

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

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
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[basyo+1:][ix-1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
log = [1]
now = 1

for i in range(k):
    now = a[now-1]
    if now in log:
        log.append(now)
        break
    log.append(now)

loopstart = log.index(now)
loopend = i + 1

k = (k - loopstart) % (loopend - loopstart)

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

B = [1]
i = 0
count = 1

while True:
  if count == K:
    print(i)
    break
  elif A[i] in B:
    x = B.index(A[i])
    a = (K-x) % (len(B)-x+1)
    print(B[a+1])
    break
  else:
    B.append(A[i])
    i = A[i]-1
    count += 1

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A=list(map(int, input().split()))
ans=A[0]
roop={}
roop[1]=[1]
if ans not in roop:
    roop[ans]=[ans]
    roop[1].append(ans)
    for _ in range(K-1):
        ans=A[ans-1]
        if ans not in roop:
            for k in roop.keys():
                roop[k].append(ans)
            roop[ans]=[ans]
        else:
            for i in range(len(roop[1])):
                if roop[1][i]==ans:
                    point=i+1
            print((roop[ans][K%len(roop)]+point)%6)
            break
else:
    print(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]
if b < k:
    k -= b
    e = k % (count+1-b)
    print(roop[e]+1)

Submission status: Runtime Error

Solution code:
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
vis=[False for i in range(n)]
a=[]
start=curr=0
vis[0]=True
a.append(start)
for i in range(k):
    curr=a[-1]+1
    if vis[arr[curr-1]-1]:
        a.append(arr[curr-1] - 1)
        break
    else:
        vis[arr[curr-1]-1]=True
        a.append(arr[curr-1]-1)
#now i have req cycle
if 3>5 and a[0]==a[-1]:
    a.pop()
    #print("helo")
    #print(len(a),a)
    print(a[k%(len(a))]+1)
else:
    count=0
    #print(a,"a")
    for i in range(len(a)):
        if a[i]==a[-1]:
            new=a[i:-1]
            #print(new,a[i:-1])
            break
        else:
            count+=1
    #print(len(new),new)
    #rint(count,"c")
    print(new[(k-count)%(len(new))]+1)

Submission status: Runtime Error

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

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

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


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

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [False] * n
now = 0
count = 0
history = [1]

while b[now] == False:
  history.append(a[now])
  b[now] = True
  count += 1
  if count >= k:
    print(a[now])
    sys.exit()
  now = a[now]-1

if now != 0:
  k -= now + 1
  history = history[now+1:]
history.pop()
print(history[k % len(history)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
now=1
s=set()
jnbn=[]
for x in range(n):
  now=l[now-1]
  s.add(now)
  jnbn.append(now)
l=len(s)
#繰り返しの暫定長さを求めた
if 1 in s:
  an=k%l
  print(jnbn[an-1])
else:
  nl=l-1
  an=k%nl
  print(jnbn[an-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
        print(basyo)
        ix = (k-basyo+1) % len(keiro)

        print(keiro[basyo+1:][ix-1])

Submission status: Runtime Error

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

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

next = 0
t=0
rep = False

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

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


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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
aa=[]
res=0
ans=0
q=0
for i in range(1,k+1):
    aa.append(res)
    res=a[res]-1
    if res in aa:
        ia=aa.index(res)+1
        m=res+1
        q=i
        break
k=k-i
q=q-m
if q !=0:
    k=k%q
for i in range(1,k+1):
    res=a[res]-1
print(res+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
trans_list = list(map(int,input().split()))
cycle = []
cycle.append(1)
temp = trans_list[0]
index = -1
for i in range(n):
    cycle.append(temp)
    if temp==trans_list[temp-1]:
        if k<=temp-1:
            index = k;
            break
        else:
            index = temp-1;
            break
    if temp in cycle[0:-2]:
        break
    else:
        temp = trans_list[temp-1]

for i in range(n):
    if cycle[i]==temp:
        first = i
        break;

length = len(cycle)
cycle_length = length-1-first
if cycle_length==0 and index==-1:
    index = k
elif index==-1:
    if k<=first:
        index=k
    else:
        index = first + (k - first) % cycle_length
print(cycle[index])
####

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0] + list(map(int,input().split()))
vcnt = [0]*(n+1)
vcnt[1] += 1
v = 1
sentou = 0
slist = []
vlist = [1]
hajime = 10**10
for i in range(1,k+1):
    v = a[v]
    vlist.append(v)
    vcnt[v] += 1
    if sentou > 0:
        syuuki += 1
    if vcnt[v] == 2:
        syuuki = 1
        sentou = a[v]
        slist.append(v)
    if vcnt[v] == 3:
        syuuki += 1
        hajime = i - syuuki*2
        break
else:
    print(v)
    exit()
#print(syuuki,hajime,sentou)
#print(slist)
k -= hajime
print(slist)
slist = [0] + slist
if (k+1)%syuuki == 0:
    print(slist[-1])
else:
    print(slist[(k+1)%syuuki])

Submission status: Runtime Error

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

route = []
i = 0
count = 0

while count < k:
    ne = a[i]
    count += 1

    if i not in route:
        route.append(i)
    else:
        k = k - (len(route) - len(route[i:]))
        print(a[k%len(route[i:])])
        break

    i = ne - 1

Submission status: Runtime Error

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

town_check = {0}
town_order = [0]
town = 0
check1 = True
while check1:
    next_town = A[town] - 1
    if next_town not in town_check:
        town = next_town 
        town_check.add(town)
        town_order.append(town)
    else:
        loop = (next_town, town)
        check1 = False

if K-2 <= len(town_order):
    print(town_order[K]+1)
else:
    a = town_order.index(loop[0])
    b = len(town_order) - 1
    c = b - a + 1
    print(town_order[a+(K-a)%(c)]+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[0]*(n+1)
l[1]=1
q=[]
cnt1=0
cnt2=0
def g(r,n):
  if n==k:
    print(r)
    exit()
  q.append(r)
  global cnt2
  cnt2+=1
  l[a[r-1]]+=1
  if l[a[r-1]]==3:
    return
  g(a[r-1],n+1)

def f(r,n):
  if n==k:
    print(r)
    exit()
  global cnt1
  cnt1+=1
  l[a[r-1]]+=1
  if l[a[r-1]]==2:
    g(a[r-1],n+1)
    return
  f(a[r-1],n+1)

f(1,0)
print(cnt1,cnt2)
intr=cnt1-cnt2
p=(k-intr)%cnt2
print(q[p])

Submission status: Runtime Error

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

sys.setrecursionlimit(150000)

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:
N,K = map(int,input().split())
A = list(map(int,input().split()))

from collections import deque 
tmp = 1
num = deque()
A_list = [[] for i in range(N+1)]
 
for i in range(N):
    num.append(tmp)
    A_list[tmp].append(i)
    if tmp == A[tmp-1]:
        judge = tmp
        flag1 = 1
        break
    tmp = A[tmp-1]
    if tmp in num:
        judge = tmp
        flag2 = 1
        break

if flag2 == 1:
    num_index = A_list[judge]
    print(num[(K-num_index[0])%(len(num)-num_index[0]) + num_index[0]])
elif flag1 == 1:
    if K >= len(num):
        print(judge)
    else:
        print(num[K-1])

Submission status: Runtime Error

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

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

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


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

Submission status: Runtime Error

Solution code:
N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=2*N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=0
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
    print(s)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A_list =  list(map(int, input().split()))
A_list
toutatul = [0]*N
next_jump = 0
before =-1
toutatul[0] =1
asibumi =False
asibumitown =0
roop_start = 0
roop_end =0
roop_count =0
for x in range(len(A_list)):
    A_list[x] = A_list[x]-1
for x in range(N):
    before = next_jump
    next_jump = A_list[next_jump]
    
    toutatul[next_jump] =1
    if next_jump == before:
        asibumi =True
        asibumitown = next_jump
        break
    if toutatul[next_jump] ==1:
        roop_start =next_jump
        roop_end =before
        roop_count =x+1
        break
next_jump = 0
before =0

if asibumi:
    if N<K:
        ans =asibumitown
    else :
        for x in range(K):
            before = next_jump
            next_jump = A_list[next_jump]

        ans = next_jump
else :
    if N<K:
        next_jump = roop_start
        roop_long = 0
        for x in range(roop_count):
            if next_jump ==roop_end:
                break
            next_jump = A_list[next_jump]
            roop_long +=1
        move = (K-roop_count)%roop_long+1
        next_jump = roop_start+1
        for x in range(move):
            print(next_jump)
            before = next_jump
            next_jump = A_list[next_jump]
            
        ans = next_jump 
    else :
        for x in range(K):
            before = next_jump
            next_jump = A_list[next_jump]

        ans = next_jump
print(ans+1)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools

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

  now = 0
  times = 0
  visit =[[0],[0]]

  for _ in range(k):
    times += 1
    now = a[now] - 1
    if now in visit[0]:
      roop = times - visit[1][visit[0].index(now)] #何個の町でループしてるかをroopに代入
      roop_start = visit[1][visit[0].index(now)] #ループが始めった待ちの最初に訪れた時に何回ループしていたかをroop_startに代入
      break
    else:
      visit[0].append(now)
      visit[1].append(times)

  k = (k - roop_start) % roop

  now = 0
  for _ in range(roop_start):
    now = a[now] - 1

  for _ in range(k):
    now = a[now] -1

  print(now+1)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = []
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
for i in range(len(g)):
    if g[i] == g[i] + mod
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
g.reverse()
for i in range(len(g)):
    if g[i] == g[i] + mod:
        d = i
        break
if k <= mod:
    for i in range(k%mod):
        q.append(s)
        s = a[s-1]
    q.reverse()
    q.append(1)
    print(q[0])
else:
    print(g[d+(k-d)%mod])

Submission status: Runtime Error

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

arr = []
location = 1
dif = 0
for i in range(len(towns)):
    if towns[location-1] not in arr:
        arr.append(towns[location-1])
        location = towns[location-1]
    else:
        index = arr.index(towns[location-1])
        dif = index
        arr = arr[index:]
        break

index = ((k-1) % len(arr)) - dif

print(arr[index])

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:
        ans = print(i + 1)
        break
    else:
        pass
b = roop_find.index(a[i]-1)
roop = list(roop_find)[b:count+1]
if b < k:
    k -= b
    e = k % (count+1-b)
    print(roop[e]+1)

Submission status: Runtime Error

Solution code:
Town = map(int, input().split())
seq = map(int, input().split())
list3=[1]
for y in range(list(Town)[1]):
    for z in list3:
        list3.append(list(seq)[z-1])
print(list3[-1])

Submission status: Runtime Error

Solution code:
N, K = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
o = {i:0 for i in range(1, N+1)}
idx = 1
O = []
while True:
    if o[idx]==0:
        o[idx] = 1
        O.append(idx)
        idx = A[idx-1]
    else:
        c = K-sum(o.values())
        IDX = A.index(idx)
        l = len(O[A[IDX]-1:])
        print(O[c % l])
        break

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0

print(B[k-1])

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
lis=""
llis=[[] for i in range(a)]


c=[int(i) for i in input().split()]
j=0
while True:
    i=c[j]
    if len(llis[i-1])!=0:
        ans=lis.index(str(i))
        break
    if i==1:
        ans=0
        break
    lis+=str(i)
    llis[i-1].append(1)
    j=i-1
lis=lis[ans:]
print(lis[((b-ans)%len(lis))-1])

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

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 loop_cnt ==0:
    loop_cnt = 1


# if mugen


# if not mugen:
if k <= no_loop_count or loop_cnt == 0:
    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:
import numpy as np
N,K=map(int, input().split())  
A = list(map(int, input().split()))

#1を含む連結成分を取り出す
#1に戻ってくるまでの回数を調べる
#K回移動後の町を計算する

visited = []
t = None

def rec(A, i):
  global t
  if i in visited:
    t = i
    return
  visited.append(i)
  #print(A[i-1])
  rec(A, A[i-1])

rec(A, 1)

#print(visited)
#print(t)

visited = np.array(visited)
idx = np.where(visited==t)
f1 = idx[0][0] -1
f2 = len(visited)

roop = (K - f1)//f2

rest = K - (f1 + f2*roop)
#print(rest)
ans = visited[rest-1]

#print(f1, f2)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split(" "))
a_list = list(map(int,input().split(" ")))
visit = [0 for num in range(n)]
path = [0]
counter = 0
current_point = 0
while True:
    next_point = a_list[current_point]-1
    counter += 1
    if visit[next_point] == 1:
        current_point = next_point
        break
    visit[next_point] = 1
    path.append(next_point)
    current_point = next_point
loop_point = path.index(current_point)
loop_num = counter - loop_point
loop_path = path[loop_point:counter]
if loop_num == 0:
    print(loop_point+1)
elif k<counter:
    print(path[k+1])
else:
    print(loop_path[(k-loop_point)%loop_num]+1)

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(list(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 = int(input())
K = int(input())
A = list(map(int, input().split()))
M = N

for i in range(N):
    if A[i] == i + 1:
        M -= 1
    
J = K % M

Ai = 1

while J > 0:
	Ai = A[Ai - 1]
	J-= 1

print(Ai)

Submission status: Runtime Error

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

visited = []
x=0
for i in range(n):
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
    break
  else:
    visited.append(a[i])
mod = (k-first) % T
print(a[first+mod]+1)