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: Wrong Answer

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
        if not loop_steps:
            continue
        steps = K - ((K - steps) % loop_steps)
        # print('steps:', steps)
        flag = True
        # print('loop detected')


print(state)

Submission status: Wrong Answer

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

for j in range(k):
  start=A[start-1]
  if start in root:
    i = start
    break
  root.append(start)
  
if k <= len(root):
  print(root[k-1])
else:
  ind = root.index(i)
  newroot=root[ind:]
  ans=(k-len(root))%len(newroot)
  print(newroot[ans])

Submission status: Wrong Answer

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=state.index(s)
        end=len(state)
        print(state[start+(k-start)%(end-start)])
        break
    else:
        count[state[i]]=i
        state.append(s)
else:
    print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [1]
loop_start = 0
loop = 0
for i in range(n):
    ans.append(a[ans[i] - 1])
for j in ans:
    if j in ans[:loop_start] or j in ans[loop_start+1:]:
        break
    loop_start += 1
loop = ans[loop_start+1:].index(j) + 1
b = 1
if k < loop_start:
    for l in range(k):
        b = a[b - 1]
    print(ans[k])
else:
    for l in range(((k-loop_start)%loop)+loop_start):
        b = a[b - 1]
print(b)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())

now=1
if k<=n:
  for i in range(k):
    now=a[now-1]
  print(now)
  exit()
# 絶対ループする
visited=[False]*n
visited[now-1]=-1
for i in range(4*10**5):
  now=a[now-1]
  if visited[now-1]==False:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
    break

k=(k-c)%(d-c)+c
#while k<n:
#  k+=(d-c)
now=1
for i in range(k):
  now=a[now-1]
print(now)

Submission status: Wrong Answer

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

cnt = 0
dic = {}
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y in dic:
        x = A[y]
        break
    else:
        dic[y] = cnt
        x = y

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

Submission status: Wrong Answer

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

cnt = 0
dic = {}
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y in dic:
        x = A[y]
        break
    else:
        dic[y] = cnt
        x = y

    if cnt == K:
        print(x)
        quit()
        break
        
s = (K-A[x]) %(cnt - A[x])
for i in range(s):    
    y = A[x-1]
    x = y
print(x)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
now=1
if k<=n:
  for i in range(k):
    now=a[now-1]
  print(now)
  exit()
# 絶対ループする
visited=[False]*n
visited[now-1]=-1
for i in range(4*10**5):
  now=a[now-1]
  if visited[now-1]==False:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
    break

k=(k-c)%(d-c)+c
while k<n:
  k+=(d-c)
for i in range(k):
  now=a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())

now=1
if k<=n:
  for i in range(k):
    now=a[now-1]
  print(now)
  exit()
# 絶対ループする
visited=[-1]*n
for i in range(4*10**5):
  now=a[now-1]
  if visited[now-1]==-1:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
    break

k=(k-c)%(d-c)+c
for i in range(k+c):
  now=a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())

now=1
if k<=n:
  for i in range(k):
    now=a[now-1]
  print(now)
  exit()
# 絶対ループする
visited=[-1]*n
for i in range(2*10**5):
  now=a[now-1]
  if visited[now-1]==-1:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
k=(k-c)%(d-c)+c
for i in range(k+c):
  now=a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

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

i_n = np.zeros(n)

idx = 1
for i in range(1, n+2):
    if (i_n[idx-1] != 0):
        rp = i_n[idx-1]
        break
    if i >= k:
        print(int(i_n[idx-1]))
        sys.exit(0)
    i_n[idx-1] = i
    idx = a[idx-1]
i_ncnt = np.where(i_n > 0, 1, 0)
cnt = sum(i_ncnt)
rp_cnt = (k - (rp - 1) ) % cnt
ai = rp
pl = rp
for _ in range(int(rp_cnt)+1):
    ai = a[int(pl)-1]
    pl = a[int(ai)-1]
print(int(pl))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

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

i_n = np.zeros(n)

idx = 1
for i in range(1, n+2):
    if (i_n[idx-1] != 0):
        rp = i_n[idx-1]
        break
    if i >= k:
        print(int(i_n[idx-1]))
        sys.exit(0)
    i_n[idx-1] = i
    idx = a[idx-1]
i_ncnt = np.where(i_n > 0, 1, 0)
cnt = sum(i_ncnt)
rp_cnt = (k - (rp - 1) ) % cnt
ai = rp
pl = rp
for _ in range(int(rp_cnt)+1):
    ai = a[int(pl)-1]
    pl = a[int(ai)-1]
print(int(ai))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

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

i_n = np.zeros(n)

idx = 1
for i in range(1, n+2):
    if (i_n[idx-1] != 0):
        rp = i_n[idx-1]
        break
    i_n[idx-1] = i
    idx = a[idx-1]
i_ncnt = np.where(i_n > 0, 1, 0)
cnt = sum(i_ncnt)
rp_cnt = (k - (rp - 1) ) % cnt
ai = rp
pl = rp
for _ in range(int(rp_cnt)+1):
    ai = a[int(pl)-1]
    pl = a[int(ai)-1]
print(int(ai))

Submission status: Wrong Answer

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

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

while steps < K:
    known_states[state] = steps

    state = paths[state]
    steps += 1

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

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


print(state)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

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

i_n = np.zeros(n)

idx = 1
for i in range(1, n+1):
    if (i_n[idx-1] != 0):
        rp = i_n[idx-1]
        break
    i_n[idx-1] = i
    idx = a[idx-1]
i_ncnt = np.where(i_n > 0, 1, 0)
cnt = sum(i_ncnt)
rp_cnt = (k - (rp - 1) ) % cnt
ai = rp
pl = rp
for _ in range(int(rp_cnt)+1):
    ai = a[int(pl)-1]
    pl = a[int(ai)-1]
print(int(ai))

Submission status: Wrong Answer

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

genzaichi=1
count=0
for i in range(K):
    genzaichi=A[genzaichi-1]
    count += 1
    if genzaichi == 1:
        break

K=K%count
print(count)

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


print(genzaichi)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
 
cur = 1
loop = -1
arr = []
for i in range(n): arr.append([False])
 
# ループする場所を調べる
while True:
    if arr[cur-1] == True:
        loop = cur
        break
    else:
        arr[cur-1] = True
        cur = a[cur-1]
 
cur = 1
cnt = 0
loop_cnt = 0
while True:
    cur = a[cur-1]
    cnt += 1
    if cur == loop and loop_cnt > 1: break
    if cur == loop: loop_cnt += 1
    if loop_cnt == 1: cnt = 0
    
v_cnt = 0
if loop == 1:
    v_cnt = 1
else:
    for i in range(n):
        v_cnt += 1
        if a[i] == loop: break

if k >= v_cnt:
    k %= cnt
 
for i in range(k + v_cnt):
    cur = a[cur-1]
 
print(cur)

Submission status: Wrong Answer

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

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

while steps < K:
    known_states[state] = steps

    state = paths[state]
    steps += 1

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

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


print(state)

Submission status: Wrong Answer

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
if K>N:
  start = 0
  num = 0
  for j in range(len(B)):
    if B[len(B)-1] == B[j]:
      start = j
      break
  for j in range(1,len(B)-start):
    if B[start+j] == B[start]:
      num = j
      break
  print(B[((K-start)%num)+start],((K-start)%num)+start)
else:
  print(B[K])

Submission status: Wrong Answer

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
    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: Wrong Answer

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

teleport = [-1] * (N+1)
teleport[0] = 1
visited = [(False, -1)] * (N+1)
visited[1] = (True, 0)

for i in range(1, N+1):
    next_town = A[teleport[i-1]-1]
    if visited[next_town][0]:
        before = visited[next_town][1]
        now = i
        break
    teleport[i] = next_town
    visited[next_town] = (True, i)


period = now - before
offset = (K - before) % period
print(teleport[before + offset])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

# D - Teleporter
n, k = map(int, input().split())
a = list(map(int, input().split()))

route = dict()
route[1] = a[0]
town = a[0]

while True:
	if town in route.keys():
		break
	else:
		route[town] = a[town-1]
		town = a[town- 1]

minus = 0

for r in route:
	if r != town:
		minus += 1
	else:
		break

loop_count = len(route) - minus
#print(minus)
#print(loop_count)

remainder = (k - minus) % loop_count
i = 0
is_start = False

for r in route:
	if r == town:
		is_start = True
	
	if is_start == True:
		if i == remainder:
			print(r)
			break
		else:
			i += 1

Submission status: Wrong Answer

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

foot = {1}
flag = 0
for i in range(k):
    once = a[list(foot)[-1]]
    if once in foot:
        foot = list(foot)
        monu = foot.index(once)
        flag = 1
        break
    foot.add(once)

if flag == 0:
    foot[-1]
    exit()

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

print(foot[ans])

Submission status: Wrong Answer

Solution code:
d={}
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    d[i+1]=a[i]
v={}
g=1
l,count=[],0
while g not in v:
    v[g]=1
    l.append(g)
    count+=1
    g=d[g]
j=0
while l[0]!=g:
    l.pop(0)
    k-=1
    count-=1
#print(l)
print(l[k%count])

Submission status: Wrong Answer

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

visited = [0] * n
i = 0
now = -1
while not all(visited):
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break

st = visited[now] -1
lp = i - st

k_bk = k - st
k_bk =  k_bk // lp
lft = k - k_bk * lp

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=[0]*n
l1[0]+=1
c=0 #ループに入る前までの移動回数
now=0#現在位置
ne=a[now]-1 #次の移動位置
while True:
    if c==k:
        print(a[ne])
        exit()
    if l1[ne]==0:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        c+=1
    else:
        now=ne
        ne=a[now]-1
        c+=1
        break
roop_c=0
while True:
    if c+roop_c==k+1:
        print(a[ne])
        exit()
    if l1[ne]==1:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        roop_c+=1
    else:
        now=ne
        ne=a[now]-1
        break
re=(k-c)%roop_c
if re==0:print(a[ne])
else:
    for i in range(re):
        if i==re-1:print(a[ne])
        ne+=-ne+a[ne]-1

Submission status: Wrong Answer

Solution code:
from math import sqrt

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

teleport=[1]
s= set()
cycle=-1
x=arr[0]
while(True):
    if(x in s):
        cycle=x
        break
    teleport.append(x)
    s.add(x)
    x = arr[x-1]
index=-1
for i in range(len(teleport)):
    if(teleport[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: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
k = k - 1
ans_list = a_list[0]
while(1):
    if k == 0:
        print(dum)
        exit()
    dum = a_list[int(dum)-1]
    if ans_list.count(dum) == 1:
        break
    ans_list = ans_list + dum
    k = k - 1
#print(ans_list,k)
ind = ans_list.index(dum)
#print(ind)
loop = ans_list[ind:]
#print(loop)
len_loop = len(loop)
loop_after = (k-1)%len_loop
print(loop[loop_after])

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n):
  x = num[t[-1] - 1]
  if(x !=  t[-1]):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]
    break

list_num = list(set(t) - set(t_test))

if(len(t_test) >= 1):
  print(t_test[(k - len(list_num)) % len(t_test)])

Submission status: Wrong Answer

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


    
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: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
k = k - 1
ans_list = a_list[0]
while(1):
    if k == 0:
        print(dum)
        exit()
    dum = a_list[int(dum)-1]
    if ans_list.count(dum) == 1:
        break
    ans_list = ans_list + dum
    k = k - 1
#print(ans_list)
ind = ans_list.index(dum)
loop = ans_list[ind:]
len_loop = len(loop)
loop_after = (k)%len_loop
print(loop[loop_after-1])

Submission status: Wrong Answer

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

position=1
if k<2*10**5:
    for i in range(k):
        position=an_list[position-1]
    print(position)
else:
    position=1
    break_flag=False
    count_set=set()
    count_set.add(1)
    first_str="1"
    loop_str=""
    position=1
    for i in range(k): #これだけ回せば必ず繰り返し、今はk＞nであることがわかっている。
        position=an_list[position-1]
        first_str+=str(position)
        if position in count_set:
            loop_str+=str(position)
            
            if loop_str[0]==str(an_list[position-1]):
                break_flag=True
                break
        else:
            count_set.add(position)
        if break_flag:
            break
    if break_flag:
        k-=(len(first_str)-2*len(loop_str))
        k%=len(loop_str)
        print(loop_str[k])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
k = k - 1
ans_list = a_list[0]
while(1):
    if k == 0:
        print(dum)
        exit()
    dum = a_list[int(dum)-1]
    ans_list = ans_list + dum
    k = k - 1
    if ans_list.count(dum) == 1:
        break
#print(ans_list)
ind = ans_list.index(dum)
loop = ans_list[ind:]
len_loop = len(loop)
loop_after = (k)%len_loop
print(loop[loop_after-1])

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
a = LI()
check = [-1]*n
cntr = 0
tmp = 0
while 1:
    if check[tmp] != -1:
        break
    check[tmp] = cntr
    cntr += 1
    tmp = a[tmp] - 1
c = check[tmp]
if cntr >= k:
    for i in range(n):
        if check[i] == k % cntr:
            ans = i+1
            break
else:
    for i in range(n):
        if check[i] - c == (k-c) % (cntr-c):
            ans = i+1
            break
print(ans)

Submission status: Wrong Answer

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

position=1
break_flag=False
count_set=set()
count_set.add(1)
first_str="1"
loop_str=""
position=1
for i in range(k): #これだけ回せば必ず繰り返し、今はk＞nであることがわかっている。
    position=an_list[position-1]
    first_str+=str(position)
    if position in count_set:
        loop_str+=str(position)
        if loop_str[0]==str(an_list[position-1]):
            break_flag=True
            break
    else:
        count_set.add(position)
    if break_flag:
        break

if break_flag:
    k-=(len(first_str)-len(loop_str))
    k%=len(loop_str)
    print(loop_str[k])
else:
    print(first_str[-1]) #print(position) is also OK!

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a=list(map(int,input().split()))
cnt=[0]*(n+1)
x=1
cnt[x]=a[0]
rep=1
ans=[1]
for i in range(2*n):
    x=a[x-1]
    
    if cnt[x]!=0:
        ans.append(x)
        break 
    else:
        rep+=1
        cnt[x]=a[x-1]
        ans.append(x)
aa=[]
val=ans[-1]

for i in range(len(ans)):
    if ans[i]==val:
        break
for j in range(i,len(ans)-1):
    aa.append(ans[j])
if k<=i:
    print(ans[k-1])
else:
    print(aa[(k-i)%len(aa)])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a=list(map(int,input().split()))
cnt=[0]*(n+1)
x=1
cnt[x]=a[0]
rep=1
ans=[1]
for i in range(2*n):
    x=a[x-1]
    
    if cnt[x]!=0:
        ans.append(x)
        break 
    else:
        rep+=1
        cnt[x]=a[x-1]
        ans.append(x)
aa=[]
val=ans[-1]

for i in range(len(ans)):
    if ans[i]==val:
        break
for j in range(i,len(ans)-1):
    aa.append(ans[j])
if k<=n:
    print(ans[k-1])
else:
    print(aa[(k-i)%len(aa)])

Submission status: Wrong Answer

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:
        flg = 0
        
k_bk = k - visited[now]
k_bk =  k_bk // i
lft = k - i* k_bk

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

Submission status: Wrong Answer

Solution code:
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
A = [0]+LI()
town = 1
route = [1]
town_set = set([1])
while 1:
    town = A[town]
    route.append(town)
    if town in town_set:
        break
    town_set.add(town)
loopstart_index = route.index(route[-1])
loop_len = (len(route)-1)-loopstart_index
ans_index = loopstart_index+(K-loopstart_index)%(loop_len)
print(route[ans_index])

Submission status: Wrong Answer

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)
    


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: Wrong Answer

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

pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0

while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

cycle = hist[last[pos]:]
preCircle = last[pos]

if (k <= preCircle):
    print(hist[k])
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Submission status: Wrong Answer

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:
        # -len(visited[:visited.index(now)])
        print(visited[K%(len(visited[visited.index(now):]))]+1)
        exit()

print()

Submission status: Wrong Answer

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

n = []
for i in range(N):
  n.append(-1)
s = 0
c = -1
d = 1
r = []
for i in range(K):
  if n[d - 1] == -1:
    n[d - 1] = i
  else:
    s = n[d - 1]
    c = i - n[d - 1]
    break
  r.append(d)
  d = A[d - 1]
if c == -1:
  print(r[-1])
else:
  cr = r[s:s+c]
  k = (K - s)%c
  print(cr[k])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
list1 = list(map(int,input().split()))
print(list1[n-1])

Submission status: Wrong Answer

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


i=0
n=[0]*N
n[0]=A[0]
while True:
  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]
  i+=1

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
list1 = list(map(int,input().split()))
if(k%2!=0 and n%2==0):
    print(list1[n-2])
else:
    print(list1[n-1])

Submission status: Wrong Answer

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

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[last[pos]:]

preCircle = last[pos]

if (k <= preCircle):
    print(hist[preCircle])
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Submission status: Wrong Answer

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

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[last[pos]:]

preCircle = last[pos]

if (k < preCircle):
    print(hist[preCircle])
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Submission status: Wrong Answer

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

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[last[pos]:]

preCircle = last[pos]

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Submission status: Wrong Answer

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

k += 1
pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[last[pos]:]

preCircle -= cnt

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Submission status: Wrong Answer

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

l = [1]
now = 1
next = a[0]
count = 0
dupl = -1

while(True):
  now = next
  next = a[now-1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)

if(dupl == -1):
  print(now)

else:
  length = len(l)
  for i in range(length):
    if l[0] == dupl:
      break
    else:
      count += 1
      l.remove(l[0])
    
  length = len(l)
  print(l[(k-count)%length])

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
towns = [int(i) for i in input().split()]
ind_ = 1
ts = []
visited = [0]*n
loop_s = None
while(True):
    ind_ = towns[ind_-1]
    if visited[ind_-1]:
        loop_s = ts.index(ind_)
        break
    else:
        visited[ind_-1] = 1
        ts.append(ind_)
init = len(ts[:loop_s])
loop_l = len(ts) - init
loop = ts[loop_s:]
print(loop[((k-init)%loop_l)-1])

Submission status: Wrong Answer

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

position=1
break_flag=False
count_set=set()
count_set.add(1)
first_str="1"
loop_str=""
position=1
for i in range(k): #これだけ回せば必ず繰り返し、今はk＞nであることがわかっている。
    position=an_list[position-1]
    if position in count_set:
        loop_first=position
        position=an_list[position-1]
        loop_str=str(loop_first)+str(position)
        while True:
            position=an_list[position-1]
            if loop_first==position:
                break_flag=True
                break
            loop_str+=str(position)
    else:
        count_set.add(position)
    if break_flag:
        break
    first_str+=str(an_list[position-1])

if break_flag:
    k-=(len(first_str)-len(loop_str))
    k%=len(loop_str)
    print(loop_str[k])
else:
    print(first_str[-1])

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
c=[int(i) for i in c]


#それぞれ頂点が向かう先。



current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
      current_num_set.add(current_num)
      current_num_list.append(current_num)
      current_num = c[current_num-1]


c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break


cicle=c-first_index
first=first_index

#cがグルグルのリストの長さ
#グルグルの始まりのインデックス

index = (K- first) % cicle + first
print(current_num_list[index])

Submission status: Wrong Answer

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

l = [1]
now = 1
next = a[0]
count = 0

while(True):
  now = next
  next = a[now-1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
length = len(l)
for i in range(length):
  if l[0] == dupl:
    break
  else:
    count += 1
    l.remove(l[0])
    
length = len(l)
print(l[(k-count)%length])

Submission status: Wrong Answer

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
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
if K < N:
  L = K
k = 0
while k < L:
  point = T_list[point]
  k += 1
  
print(point+1)

Submission status: Wrong Answer

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

position=1
if k<2*10**5:
    for i in range(k):
        position=an_list[position-1]
    print(position)
else:
    break_flag=False
    count_set=set()
    count_set.add(1)
    first_str="1"
    roop_str=""
    position=1
    for i in range(2*10**5): #これだけ回せば必ず繰り返し、今はk＞nであることがわかっている。
        position=an_list[position-1]
        if position in count_set:
            roop_first=position
            position=an_list[position-1]
            roop_str=str(roop_first)+str(position)
            while True:
                position=an_list[position-1]
                if roop_first==position:
                    break_flag=True
                    break
                roop_str+=str(position)
        else:
            count_set.add(position)
        if break_flag:
            break
        first_str+=str(an_list[position-1])
    k-=len(first_str)-len(roop_str)
    k%=len(roop_str)
    print(roop_str[k])

Submission status: Wrong Answer

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 - flag[now-1]
        zan=(K-(i-loop))%loop
        ans = flag.index(i-loop+zan)+1
        print(ans)
        exit()

print(now)