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

genzaichi=1
rireki=[]
houmon=[0]*(N+1)
flag=0

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.append(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        flag=1
        break

print("now")
print(x)
print(roop)
print(roop_from)
print(flag)

genzaichi=1
if flag==1:
    K=K-(((K-x)//roop)-1)*roop
    print(K)

for i in range(K):
    genzaichi=A[genzaichi-1]

print(genzaichi)

Submission status: Runtime Error

Solution code:
_, n_teleport = map(int, input().split())
towns = [0] + list(map(int, input().split()))
 
visited_list = []
 
next_town = 1
while 1:
  if next_town in set(visited_set):
    loop_start_at = visited_list.index(next_town)
    loop_length = len(visited_list) - loop_start_at
    break
  visited_list.append(next_town)
  next_town = towns[next_town]
 
if n_teleport < loop_start_at:
  end_point = n_teleport
else:
  end_point = loop_start_at + (n_teleport - loop_start_at) % loop_length
 
print(visited_list[end_point])

Submission status: Runtime Error

Solution code:
_, n_teleport = map(int, input().split())
    towns = [0] + list(map(int, input().split()))
 
    visited_list = []

    next_town = 1
    while 1:
        if next_town in set(visited_set):
            loop_start_at = visited_list.index(next_town)
            loop_length = len(visited_list) - loop_start_at
            break
        visited_list.append(next_town)
        next_town = towns[next_town]
 
    if n_teleport < loop_start_at:
        end_point = n_teleport
    else:
        end_point = loop_start_at + (n_teleport - loop_start_at) % loop_length
 
    print(visited_list[end_point])

Submission status: Runtime Error

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

genzaichi=1
rireki=[]
houmon=[0]*(N+1)

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.append(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        break

genzaichi=1
if K>=x+roop:
    K=K-x-roop
    genzaichi=roop_from

for i in range(N):
    genzaichi=A[genzaichi-1]

print(genzaichi)

Submission status: Runtime Error

Solution code:
# 4 5 (4 cities and 5 steps)
# 3 2 4 1

# 1 => 3
# 2 => 2
# 3 => 4
# 4 => 1

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

paths = {}

for i in range(N):
    paths[i+1] = A_i[i]

# print('paths:', paths)

# 6 5 2 5 3 2

# 1 => 6 (1)
# 6 => 2 (2)
# 2 => 5 (3)
# 5 => 3 (4)
# 3 => 2 (5)

# loop_steps = 5 - 2 - 1 = 2
# 5 + 2 * m + r = 727202214173249351
# steps + loop_steps * m + r = K
# r = (K - steps) % loop_steps

state = 1
steps = 0
flag = False
known_states = {}

while steps < K:
    known_states[state] = steps # state => 2, steps => 2

    state = paths[state] # state => 5
    steps += 1 # steps => 3

    # print(f'steps: {steps}, state: {state}')

    if state in known_states and not flag: # state => 2 steps => 5
        loop_steps = steps - known_states[state] - 1
        steps = K - ((K - steps) % loop_steps)
        print('steps:', steps)
        flag = True
        # print('loop detected')


print(state)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
s=1
state=[s]
count={}
for i in range(1,k+1):
    s=A[s-1]
    if s in count:
        start=state.index(s)
        end=len(state)
        print(state[start+(k-start)%(end-start)])
        break
    else:
        count[state[i]]=i
        state.append(s)
        continue
else:
    print(s)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
s=1
state=[s]
count={}
for i in range(k+1):
    s=A[s-1]
    if s in count:
        start=len(state)
        end=i=1
        print(state[start+(k-start)%(end-start)])
        break
    else:
        count[state[i]]=i
        state.append[s]
else:
    print(s)

Submission status: Runtime Error

Solution code:
# 4 5 (4 cities and 5 steps)
# 3 2 4 1

# 1 => 3
# 2 => 2
# 3 => 4
# 4 => 1

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

paths = {}

for i in range(N):
    paths[i+1] = A_i[i]

# print('paths:', paths)

# 6 5 2 5 3 2

# 1 => 6 (1)
# 6 => 2 (2)
# 2 => 5 (3)
# 5 => 3 (4)
# 3 => 2 (5)

state = 1
steps = 0
flag = False
known_states = {}

while steps < K:
    known_states[state] = steps # state => 2, steps => 2

    state = paths[state] # state => 5
    steps += 1 # steps => 3

    # print(f'steps: {steps}, state: {state}')

    if state in known_states and not flag: # state => 2 steps => 5
        loop_steps = (steps - known_states[state] - 1)
        steps = K - ((K - steps) % loop_steps)
        flag = True
        # print('loop detected')


print(state)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
s = set(i for i in range(2,n+1))
q = [1]
a = l[0]
key = -1

while True:
    if a in s:
        q.append(a)
        s.remove(a)
    else:
        key = a
        break
    a = l[a-1]
    
#print(q)
#print(key)
a = (q.index(key))
#print(a)
if a >= k:
    print(q[k-1])
else:
    b = ((k-a) % key) + a
    print(q[b-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = []
i = 0
B.append(1)
for n in range(N):
  B.append(A[i])
  i = A[i]-1
start = 0
num = 0
for j in range(len(B)):
  if B[len(B)-1] == B[j]:
    start = j-1
    break
for j in range(1,len(B)-start):
  if B[start+j] == B[start]:
    num = j
    break
print(B[((K-start)%num)+start])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0] + list(map(int,input().split()))
 
foot = [1]
foots= {1}
for i in range(k):
    once = a[foot[-1]]
    if once in foots:
        monu = foot.index(once)
        break
    foot.append(once)
    foots.add(once)
 
if k < n+1:
    print(foot[k])
    exit()
 
 
foot = foot[monu:]
ros = len(foot)
ans = (k -monu)% ros
 
print(foot[ans])

Submission status: Runtime Error

Solution code:
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
 
if K<=N:
  nex=0
  for i in range(K):
    
    ans=A[nex]
    nex=ans
  print(ans)
  sys.exit()
    
n=[0]*(N+1) #行った場所
 
n[0]=A[0] #1回目にどこにいるか
nex=A[0]
 
for i in range(1,N): #2回目以降にどこにいるか 
  n[i]=A[nex-1] #いる場所
  nex=A[nex-1]
  
  if n[nex-1]!=0:
    d=n[nex-1]
    break
 
roop_start=n.index(d)
length=i+1-roop_start
 
k_remain=(K-len(n[:roop_start])+length)%length
 
print(n[roop_start:length+1][k_remain-1])

Submission status: Runtime Error

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

foot = [1]

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

    if once in foot:
        monu = foot.index(once)
        break
    foot.append(once)



foot = foot[monu:]
ros = len(foot)
ans = (k -monu)% ros

print(foot[ans])

Submission status: Runtime Error

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

if K<=N:
  nex=0
  for i in range(K):
    
    ans=A[nex]
    nex=ans
  print(ans)
  sys.exit()
    
n=[0]*N #行った場所

n[0]=A[0] #1回目にどこにいるか
nex=A[0]

for i in range(1,N): #2回目以降にどこにいるか 
  n[i]=A[nex-1] #いる場所
  nex=A[nex-1]
  
  if n[nex-1]!=0:
    d=n[nex-1]
    break

roop_start=n.index(d)
length=i+1-roop_start

k_remain=(K-len(n[:roop_start])+length)%length

print(n[roop_start:length+1][k_remain-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
    
n = 1
order = [n]
order_set = set([n])

for i in range(1, K + 1):
    n = A[n - 1]
    if n in order_set:
        Li = order.index(n)
        Ri = len(order)
        idx = Li + (K - Li) % (Ri - Li)
        print(order[idx])
        break
    else:
        order.append(n)
        order_set.add(n)
        continue
else:
    print(s)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
series="no"
s=[]
lp=l[0]
s.append(l[0])
nu=1
while series!="yes" and nu<=k:	
	if nu==k:
		break
	else:
		lp=l[lp-1]
		if lp not in s:
			s.append(lp)
		else:
			lp=s.index(lp)
			break
	nu+=1
a=len(s)-lp
ans=k%a
print(s[ans-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))
routs = {}
location = [0 for i in range(N + 1)]
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i] = tereporter[i] - 1
present_location = 0
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if location[present_location] == 0:
    location[present_location] = n
  else:
  loop = n - location[present_location]
  if (K - location[present_location]) % loop == 0:
    break
  else:
    for i in range((K - location[present_location]) % loop):
      present_location = routs[present_location]
	break
print(present_location + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))
routs = {}
location = [0 for i in range(N + 1)]
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i] = tereporter[i] - 1
present_location = 0
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if location[present_location] == 0:
    location[present_location] = n
  else:
    break
loop = n - location[present_location]
if (K - location[present_location]) % loop == 0:
  print(present_location + 1)
else:
  for i in range((K - location[present_location]) % loop):
    present_location = routs[present_location]
  print(present_location + 1)

Submission status: Runtime Error

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

if K<=N:
  nex=0
  for i in range(K):
    
    ans=A[nex]
    nex=ans
  print(ans)
  sys.exit()
    
n=[0]*N
n[0]=A[0]

for i in range(N):
  if A[n[i]-1] in n:
    a=n.index(A[n[i]-1])
    le=len(n[a:i+1]) #ループ
    break
  n[i+1]=A[n[i]-1]

k_remain=(K-len(n[:i+1])+le)%le

print(n[a:i+1][k_remain-1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
lst = list(map(int, (input().split())))
lst = [_ - 1 for _ in lst]

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break
        
k_bk = k - visited[now]
k_bk =  k_bk // (i - visited[now])

lft = k - k_bk * (i - visited[now])

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)

Submission status: Runtime Error

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

now = 0
visited = [0]

for i in range(K):
    now = A[now]-1
    if now not in visited:
        visited.append(now)
    else:
        alen = len(visited[:visited.index(now)])
        loop_len = len(visited[visited.index(now):])

        if alen == 0:
            print(visited[K%loop_len]+1)
            exit()
        else:
            print(visited[alen:][K%loop_len-alen]+1)
            exit()

print(visited[K%i])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
i=1
list=[1]
while True:
    if A[i-1] in list :
        num2=list.index(A[list[i]-1])
        break
    list.append(A[i-1])
    i=A[i-1]
    

if num2==0:
    print(list[K%len(list)])
else:
    if  K<num2:
        print(list[K])
    else:
        num=((K-num2)%(len(list)-num2))
        print(list[num+num2])

Submission status: Runtime Error

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

now = 0
visited = [0]

for i in range(K):
    now = A[now]-1
    if now not in visited:
        visited.append(now)
    else:
        alen = len(visited[:visited.index(now)])
        loop_len = len(visited[visited.index(now):])

        if alen == 0:
            print(visited[K%loop_len]+1)
            exit()
        else:
            print(visited[alen:][K%loop_len-alen]+1)
	        exit()

print(visited[K%i])

Submission status: Runtime Error

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

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = t_zero.index(t_zero[a] + k)
print(a)

Submission status: Runtime Error

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

point = 0
if K < N:
  while i < K:
    point = T_list[point]
    i += 1  

else:
  syuuki = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == 0:
      syuuki = i+1
      break
    i += 1

  second_point = point
  if syuuki != 0:
    L = K % syuuki

  else:
    j = 0
    while j < N:
      point = T_list[point]
      if point == second_point:
        syuuki = j+1
        break
      j += 1
    L = (K-i) % syuuki

  k = 0
  while k < L:
    point = T_list[point]
    k += 1
  
print(point+1)

Submission status: Runtime Error

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

"""
1からのループだけを考えれば良い

と思ったが，1に戻ってくるとは限らない
"""

i = A[0]
ok = True
R = [1]
seen = [0] * N
seen[0] = 1


while ok:
    seen[i-1] += 1
    R.append(i)
    i = A[i-1]
    if seen[i-1] == 1:
        for j in range(len(R)):
            if R[j] == i:
                remain = R[:j]
                loop = R[j:]
                ok = False

L = len(loop)

if K < len(remain):
    print(remain(K-1))
else:
    print(loop[(K - len(remain)) % L])

Submission status: Runtime Error

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

if K < N:
  while i < K:
    point = T_list[point]
    i += 1  

else:
  point = 0
  syuuki = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == 0:
      syuuki = i+1
      break
    i += 1

  second_point = point
  if syuuki != 0:
    L = K % syuuki

  else:
    j = 0
    while j < N:
      point = T_list[point]
      if point == second_point:
        syuuki = j+1
        break
      j += 1
    L = (K-i) % syuuki

  k = 0
  while k < L:
    point = T_list[point]
    k += 1
  
print(point+1)

Submission status: Runtime Error

Solution code:
def zero_index(i: str) -> int:
  return int(i) - 1

n, k = map(int, input().split())
transition = map(zero_index, input().split())
places = [0]
next_place = transition[0]
while next_place not in places:
  places.append(next_place)
  next_place = transition[next_place]
start = places.index(next_place)
period = len(places) - start
if k < start:
  t = k
else:
  t = (k - start) % priod + start
print(place[t] + 1)

Submission status: Runtime Error

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

if K < N:
  while i < K:
  point = T_list[point]
  i += 1  

else:
  point = 0
  syuuki = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == 0:
      syuuki = i+1
      break
    i += 1

  second_point = point
  if syuuki != 0:
    L = K % syuuki

  else:
    j = 0
    while j < N:
      point = T_list[point]
      if point == second_point:
        syuuki = j+1
        break
      j += 1
    L = (K-i) % syuuki

  k = 0
  while k < L:
    point = T_list[point]
    k += 1
  
print(point+1)

Submission status: Runtime Error

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

"""
1からのループだけを考えれば良い

と思ったが，1に戻ってくるとは限らない
"""

i = A[0]
ok = True
R = [1]
seen = [0] * N
seen[0] = 1


while ok:
    seen[i-1] += 1
    R.append(i)
    i = A[i-1]
    if seen[i-1] == 1:
        for j in range(len(R)):
            if R[j] == i:
                remain = R[:j]
                loop = R[j:]
                ok = False

L = len(loop)

if K <= len(remain):
    print(remain(K-1))
else:
    print(loop[(K - len(remain)) % L])

Submission status: Runtime Error

Solution code:

N, K = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
loop = []
start = 0
index = 0
check = [-1] * N
fin = False
count = 0
while True:
    if check[index] != -1:
        start = check[index]
        break
    else:
        loop.append(index)
        if len(loop) == K + 1:
            print(index + 1)
            sys.exit()
        check[index] = count
        count += 1
        index = a[index]


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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
loop = []
start = 0
index = 0
check = [-1] * N
fin = False
count = 0
while True:
    if check[index] != -1:
        start = check[index]
        break
    else:
        loop.append(index)
        if len(loop) == K + 1:
            print(index + 1)
			exit()
        check[index] = count
        count += 1
        index = a[index]


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

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(n):
    a[i]-=1

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break

if k<=len(s):
    print(a[k])
else:
    print(a[check +(k-check)%len(s)]+1)

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(n):
    a[i]-=1

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break

if k<=len(s2):
    print(a[k-1]+1)
else:
    k-=check
    k%=len(s)
    print(int(s[k])+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
if k <= first:
    ans = b[k]
elif length == 0:
    ans = 1
else:
    ans = b[(k-first)%length+first]
print(ans)

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(n):
    a[i]-=1

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break

if k<=len(s2):
    print(a[k-1]+1)
else:
    k-=check
    k%=len(s)
    print(a[check + k]+1)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
i=1
list=[1]
while True:
    if A[i-1] in list :
        num2=list.index(A[list[i]-1])
        break
    list.append(A[i-1])
    i=A[i-1]
    print(list)
    
if num2==0:
    print(list[K%len(list)])
else:
    if  K<num2:
        print(list[K])
    else:
        num=((K-num2)%(len(list)-num2))
        print(list[num+num2])

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(n):
    a[i]-=1

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break
k -= check
k%=len(s)
print(a[check+k]+1)

Submission status: Runtime Error

Solution code:
import sys
def main():
    # 町の数N, 町iの転送先はindexがAi-1の町
	input = sys.stdin.readline
    N,K = map(int, input.split())
    input = sys.stdin.readline
    A = list(map(int, input.split()))

    # K回移動した時の町番号を出力
    # もし一度出てきた番号が出てきたら記憶している道順リストを使う
    # 最初は道を覚えさせる作業が必要
    now = 0
    history = [0]
    i = 0
    while i<K:
        # まず進む
        now = A[now]-1
        # 道順登録
        if now not in history:
            history.append(now)
            i+=1
        # 以前出てきてる町ならそこから末尾までを自動で進めてiのカウントも進める
        else:
            start = history.index(now)
            r = start + (K-1-i)%(len(history)-start)
            now = history[r]
            break

    print(now+1)

main()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
if k <= first:
    ans = b[k]
else:
    ans = b[(k-first)%length+first]
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
e = [False] * (N+1)
idx = 1
move = []
while True:
  if e[idx]:
    break
  move.append(idx)
  e[idx] = True
  idx = A[idx-1]
m = move.index(idx)
c = len(move) - m

if K <= m:
    print(D[K])
else:
    print(D[m+(K-m) % c])

Submission status: Runtime Error

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

now=1
flag=[-1]*(N)

for i in range(N):
    if flag[now-1]==-1:
        flag[now-1]=i
        now = A[now-1]
    else:
        loop = i+1 - flag[now-1]
        zan=(K-(i-loop))%loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans)
        exit()

print(now)

Submission status: Runtime Error

Solution code:

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

# テレポート表を作る
te_list = [1,]
rep_list = None
current_town = 1
while True:
    if data[current_town - 1] in te_list:
        start_list = te_list[:te_list.index(data[current_town - 1])]
        rep_list = te_list[te_list.index(data[current_town - 1]):]
        break
    else:
        te_list.append(data[current_town - 1])
        current_town = data[current_town - 1]

if start_list is None:
    ans = rep_list[k % len(rep_list)]
elif len(start_list) >= k:
    ans = start_list[k]
else:
    ans = rep_list[(k - len(start_list)) % len(rep_list)]
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
ans = b[(k-first)%length+first]
print(ans)

Submission status: Runtime Error

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

t = 0
now = 1
footprint = [1]

while t < K:
  if A[now-1] not in footprint:
    footprint.append(A[now-1])
    now = A[now-1]
    t += 1
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = (K-s) % l
    now = footprint[i+e]
    break

print(now)

Submission status: Runtime Error

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

t = 0
now = 1
footprint = [1]

while t < K:
  if A[now-1] not in footprint:
    footprint.append(A[now-1])
    now = A[now-1]
    t += 1
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = K % l
    now = footprint[i+e]
    break

print(now)

Submission status: Runtime Error

Solution code:
a,b=map(int, input().split())
c=list(map(int, input().split())) 
 
l = [1]
tmp = 1
s = 0
for i in range(b):
    l.append(c[tmp-1])
    tmp = c[tmp-1]
    d = l.count(l[i])
    if d > 1:
        s = l[i]
        t = i + 1
        break

#print(l)
if a > b:
    print(l[b])
    exit()

h = l.index(s) + 1

#print(s,h,t)
l2 = l[h-1:t-1]
#print(l2)
if s == 1:
    id = b % (t - 1)
    print(l2[id])
else:
    id = (b - h + 1) % (t - h)
    print(l2[id])

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
A = [0] + list(map(int,input().split()))
T = [1]
current_town = 1
for i in range(2,K+1):
    current_town = A[current_town]
    K -= 1
    if current_town in T:
        loop_towns = T[T[current_town]-1:]
        print(loop_towns[K%len(loop_towns)])
        break
    else:
        T.append(current_town)
else:
    print(A[current_town])

Submission status: Runtime Error

Solution code:
a,b=map(int, input().split())
c=list(map(int, input().split())) 
 
l = [1]
tmp = 1
s = 0
for i in range(b):
    l.append(c[tmp-1])
    tmp = c[tmp-1]
    d = l.count(l[i])
    if d > 1:
        s = l[i]
        break

tmp2 = [i for i, x in enumerate(l) if x == s]
h = tmp2[0]+1
t = tmp2[1]+1
 
#print(s,h,t)
l2 = l[h-1:t-1]
#print(l2)
if s == 1:
    id = b % (t - 1)
    print(l2[id])
else:
    id = (b - h + 1) % (t - h)
    print(l2[id])

Submission status: Runtime Error

Solution code:
import sys

lines = sys.stdin.readlines()
N, M, X = map(int, lines[0].split())
A = []
C = []

for i in range(N):
    line = list(map(int, lines[i+1].split()))
    A.append(line[1:])
    C.append(line[0])

# return []
def full_search():
    costs = []
    for i in range(2**N):
        vals = [0] * M
        cost = 0

        # iを二進数に直して扱う。それぞれのビットがn冊目の本を買うかどうかに匹敵。
        for j in range(N):
            # ビットを移動しながらn冊目の本を買う際のコストと経験値を計算する
            if ((i >> j) & 1):
                # 現在の組み合わせに対して
                # 全てのアルゴリズム分を計算
                cost += C[j]

                # 全てのアルゴリズム(m)に対して経験値を求める
                for m in range(M):
                    vals[m] += A[j][m]

        combination_ok = True
        for m in range(M):
            if vals[m] >= X:
                continue
            else:
                combination_ok = False
                break

        if combination_ok:
            costs.append(cost)
    return costs

costs = full_search()
if len(costs) == 0:
    print(-1)
else:
    print(min(costs))

Submission status: Runtime Error

Solution code:
from math import sqrt

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

teleport=[1]
cycle=-1
x=arr[0]
while(True):
    if(x in teleport):
        cycle=x
        break
    teleport.append(x)
    x = arr[x-1]
index=-1
for i in teleport:
    if(i == cycle):
        index=i
        break
    else:
        k-=1
len_cycle = len(teleport) - index
ans = k%len_cycle
ans = teleport[index+ans]
print(ans)

Submission status: Runtime Error

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

visited = [-1 for i in range(n+1)] #地点の数字に合わせる
points = []
order = {}
x = 1
T = -1

while visited[x] == -1:
  x = a[x-1] #添字に合わせた
  points.append(x)
  visited[x] = len(points)
T = len(points) - visited[x]
s = visited[x]
if k < s:
  print(points[k])
else:
  k -= s
  mod = k % T
  print(points[s+mod])

Submission status: Runtime Error

Solution code:
cin = open(0).read().strip().split('\n')

n,k = map(int, cin[0].split(' '))
a = list(map(int, cin[1].split(' ')))

history = [1]
for i in range(n):
    history.append(a[history[i]-1])

if n >= k:
    print(a[k])
else:
    times = [0] + [-1] * (n-1)
    for i in range(n):
        if times[a[i]-1] == -1:
            times[a[i]-1] = i+1
        else:
            cycle = i-times[a[i]-1]
            pre = times[a[i]-1]
            mod = (k-pre)%cycle
            print(history[pre+mod])
            break

Submission status: Runtime Error

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

s = 0
t = 0
u=[]
for i in range(100000):
    u.append(0)

spot=1
for i in range(K):
    if u[spot-1] == 0:
        u[spot-1] = 1
    else:
        if spot == 1:
            s = 0
            t = i
        else:
            tmp = 1
            for j in range(i):
                tmp = A[tmp-1]
                if tmp == spot:
                    s = j + 1
                    t = i - s
                    break
            break
    spot=int(A[spot-1])

if t != 0:
    ans = K - s
    ans = ans % t
    spot = 1
    for i in range(s + ans):
        spot=int(A[spot-1])

print(spot)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [-1] * (n+1)
now = 1
history = []

while b[now] == -1:
  b[now] = len(history)
  history.append(now)
  now = a[now-1]  

if now == 1:
    print(history[k % len(history)])
else:
    print(history[(k - now) % (len(history) - now) + now])

Submission status: Runtime Error

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

visited = []
order = {}
x = 1
T = -1
while T < 0:
  x = a[x-1] #添字に直す
  if x not in visited:
    visited.append(x)
    order[x] = len(visited)
  else:
    T = len(visited) - order[x]
    s = order[x]
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Runtime Error

Solution code:
import numpy as np

N,K = map(int,input().split())
a = np.matrix(np.zeros((N,N)))
for k,v in enumerate(map(int,input().split())):
    v -= 1
    a[k,v] = 1

b = np.matrix(np.zeros(N))
b[0,0] = 1

o = [0] * 63

def bi(x):
    i = 0
    while x != 0:
        o[i] = x%2
        x //= 2
        i += 1

bi(K)

for i in range(63):
    if i:
        a = a * a
    if o[i]:
        b = b * a

for i in range(N):
    if b[0,i]:
        print(i+1)