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:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
loop = 10**8
start_cnt = 0
for i in range(n):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop):
    town = A[town]

print(town+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
loop = 10**8
start_cnt = 0
for i in range(k):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop):
    town = A[town]

print(town+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
for i in range(n):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop):
    town = A[town]

print(town+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
for i in range(n*2):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop):
    town = A[town]

print(town+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

memo = []

for i in range(n*2):
    town = A[town]
    memo.append(town)
    if town == 0:
        break

loop = len(memo)

print(memo[k%loop-1]+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

memo = []

for i in range(n):
    town = A[town]
    memo.append(town)
    if town == 0:
        break

loop = len(memo)

print(memo[k%loop-1]+1)

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

cur = 0
for _ in range(min(N, K)):
    cur = A[cur] - 1

if K > N:
    root = cur
    cnt = 0
    for _ in range(min(N, K - N)):
        cur = A[cur] - 1
        cnt += 1
        if cur == root:
            break
            
    if K > 2 * N:
        res = (K - 2 * N) % cnt
        for _ in range(res):
            cur = A[cur] - 1

print(cur + 1)

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

cur = 0
for _ in range(min(N, K)):
    cur = A[cur] - 1

if N < K:
    root = cur
    cnt = 0
    for _ in range(min(N, K - N)):
        cur = A[cur] - 1
        cnt += 1
        if cur == root:
            break
            
    if K > 2 * N:
        res = K % cnt
        for _ in range(res):
            cur = A[cur] - 1

print(cur + 1)

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

cur = 0
cnt = 0
for _ in range(min(N, K)):
    cur = A[cur] - 1
    cnt += 1    
    if cur == 0:
        break

if cnt != K:
    res = K % cnt
    for _ in range(res):
        cur = A[cur] - 1

print(cur + 1)

Submission status: Wrong Answer

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

li = [1]
for i in range(10**5*2+1):
    li.append(L[li[i]-1])


last = li[-1]
idx = 1

while True:
    s1 = li[-1-idx:-1]
    s2 = li[-1-idx*2:-1-idx]
    if s1 == s2:
        #print(s1,s2)
        break
    idx +=1
l = len(s1)
p = 0
while True:
    if li[p:p+l] == s1:
        #print(p)
        break
    p +=1
div = (k-p)%l

print(li[p+div+l])

Submission status: Wrong Answer

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

li = [1]
for i in range(10**5*2+1):
    li.append(L[li[i]-1])
li.pop(0)

last = li[-1]
idx = 1

while True:
    s1 = li[-1-idx:-1]
    s2 = li[-1-idx*2:-1-idx]
    if s1 == s2:
        #print(s1,s2)
        break
    idx +=1
l = len(s1)
p = 0
while True:
    if li[p:p+l] == s1:
        #print(p)
        break
    p +=1
div = (k-p)%l

print(li[p+div+l-1])

Submission status: Wrong Answer

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

A.insert(0, 0)
route_list = [1]

while True:
    route = A[route_list[-1]]
    if route_list.count(route) == 0:
        route_list.append(route)
    else:
        break

print(route_list)

Submission status: Wrong Answer

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

town_list = [1]
last_town = 1
to_town = a[last_town-1]
while to_town not in town_list:
    last_town = a[last_town-1]
    town_list.append(last_town)
    to_town = a[last_town-1]
    
for i in range(N):
    if town_list[i] == to_town:
        roop_start = i
        break
print(roop_start)
if K <= len(town_list)-1:
    print(town_list[K])
else:
    K = K - len(town_list[:roop_start])
    K = K % len(town_list[roop_start:])
    print(town_list[roop_start:][K])

Submission status: Wrong Answer

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

d = [0] * n
d[0] = 1
t = a[0]
cnt = 0
r = [1]

while d[t-1] == 0:
    cnt += 1
    d[t-1] = cnt + 1
    r.append(t)
    t = a[t-1]
    if k == cnt:
        print(t)
        exit()

rp = r[d[t-1]-1:]
b = (k-cnt) % len(rp)
print(rp[b-1])

Submission status: Wrong Answer

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

d = [0] * n
d[0] = 1
t = a[0]
cnt = 0
r = [1]

while d[t-1] == 0:
    cnt += 1
    d[t-1] = cnt + 1
    r.append(t)
    t = a[t-1]

rp = r[d[t-1]-1:]
b = (k-cnt) % len(rp)
print(rp[b-1])

Submission status: Wrong Answer

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,K = [int(x) for x in sys.stdin.readline().split()]
g = [int(x) for x in sys.stdin.readline().split()]

#f = open('./sub1_03.txt')
#N,K = [int(x) for x in f.readline().split()]
#g = [int(x) for x in f.readline().split()]
#f.close()

quetmp = deque([1])
dest = 1
n_for_loop = 0
n_loop = 0
new_start = 0

for i in range(N):
    quetmp.append(g[dest - 1])
    n_for_loop = n_for_loop + 1
    #テレポ後にいる位置
    dest = g[dest - 1]
 #   print(tmp)
  #  print(g[dest-1])
   # print(np.any(np.array(tmp) == g[dest-1]))
    #次のテレポ位置をチェック
    ck = np.where(np.array(quetmp) == g[dest-1])
    if ck[0].size != 0:
        n_for_loop = n_for_loop+1
        break

#print(new_start)
#print(tmp)
#ループ開始街からのループ回数
#dest = new_start
#quetmp = deque([])
#for i in range(N):
#    quetmp.append(g[dest - 1])
#    n_loop = n_loop +1
#    dest = g[dest -1 ]
#    if g[dest -1] == new_start:
#        n_loop = n_loop + 1
#        quetmp.append(g[dest - 1])
#        break

n_loop = n_for_loop - ck[0][0]#n_for_loop - ( n_loop - 1 )
K = K - ck[0][0]

#print(n_for_loop)
#print(ck[0][0])
#print(n_loop)
#print(K)
ans = g[ck[0][0] + int(K%n_loop)]
#print(tmp[int(K%n_loop)-2])
#print("ans = ")
print(ans)
#print(tmp[int(K%n_loop)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
lp = [0]
for _ in range(n):
    nx = A[lp[-1]]
    if nx in set(lp):
        lp.append(nx)
        break
    lp.append(nx)

llp = lp[lp.index(nx):]
if k <= len(lp):
    print(lp[k])
else:
    llp = llp[:-1]
    k -= len(lp)-1-len(llp)
    print(llp[k%len(llp)]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
lp = [0]
for _ in range(n):
    nx = A[lp[-1]]
    if nx in lp:
        lp.append(nx)
        break
    lp.append(nx)

llp = lp[lp.index(nx):]
if k <= len(lp):
    print(lp[k])
else:
    llp = llp[:-1]
    k -= len(lp)-1-len(llp)
    print(llp[k%len(llp)]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
lp = [0]
for _ in range(n):
    nx = A[lp[-1]]
    if nx in lp:
        lp.append(nx)
        break
    lp.append(nx)

llp = lp[lp.index(nx):]
if k <= len(lp):
    print(lp[k])
else:
    k -= len(lp)-len(llp)
    llp = llp[:-1]
    print(llp[k%len(llp)]+1)

Submission status: Wrong Answer

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

for i in range(k):
	
	place = a[li[-1]-1]
	if(not place in li):
		li.append(place)
	else:
		start = li.index(place)
		break
	#print(place)

if(i == k-1):
	print(li[-1])
else:
	val = (k-len(li)-i-1) % len(li)
	print(li[val])

Submission status: Wrong Answer

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

d = [0] * n
t = 1
cnt = 1
r = []

while d[t-1] == 0:
    d[t-1] = cnt
    cnt += 1
    r.append(t)
    t = a[t-1]

rp = r[d[t-1]-1:]
print(rp[(k-cnt+1) % len(rp)])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 20 10:11:22 2020

@author: NEC-PCuser
"""

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

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

tele_set = set()
tele_set.add(1)
junkai_town = -1 
junkai_first = -1
town = 1
for i in range(N):
    if A[town - 1] not in tele_set:
        tele_set.add(A[town - 1])
    else:
        junkai_town = A[town - 1]
        break
    town = A[town - 1] 

town = 1
start_flg = False
junkaimae_list = []
junkai_list = []
for i in range(N):
    if town == junkai_town and start_flg == False:
        junkai_first = i
        start_flg = True
    elif town == junkai_town:
        break
    if start_flg == True:
        junkai_list.append(town)
    else:
        junkaimae_list.append(town)
    town = A[town - 1]


if K <= len(junkaimae_list):
    print(junkaimae_list[K - 1] + 1)
else:
    kaisu = K - len(junkaimae_list)
    print(junkai_list[kaisu % len(junkai_list)])

Submission status: Wrong Answer

Solution code:
import sys
import time
st = time.perf_counter()
# ------------------------------

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break
if K - t > 0:
    amari = (K - t) % circle
else:
    amari = K
for _ in range(amari):
    now = A[now]
print(now + 1)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
import sys
import time
st = time.perf_counter()
# ------------------------------

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break
if K - t > 0:
    amari = (K - t) % circle
else:
    amari = t
for _ in range(amari):
    now = A[now]
print(now + 1)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
import sys
import time
st = time.perf_counter()
# ------------------------------

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

if A[0] == 0:
    print(1)
    sys.exit()

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break

amari = (K - t) % circle
if amari == 0:
    print(now+1)
    sys.exit()
for _ in range(amari):
    now = A[now]
print(now + 1)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
import sys
import time
st = time.perf_counter()
# ------------------------------

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break

amari = (K - t) % circle
if amari == 0:
    print(now+1)
    sys.exit()
for _ in range(amari):
    now = A[now]
print(now + 1)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
import sys
import time
st = time.perf_counter()
# ------------------------------

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break

amari = (K - t) % circle
for _ in range(amari):
    now = A[now]
print(now + 1)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,K = [int(x) for x in sys.stdin.readline().split()]
g = [int(x) for x in sys.stdin.readline().split()]

#f = open('./sub1_03.txt')
#N,K = [int(x) for x in f.readline().split()]
#g = [int(x) for x in f.readline().split()]
#f.close()

quetmp = deque([1])
dest = 1
n_for_loop = 0
n_loop = 0
new_start = 0

for i in range(N):
    quetmp.append(g[dest - 1])
    n_for_loop = n_for_loop + 1
    #テレポ後にいる位置
    dest = g[dest - 1]
 #   print(tmp)
  #  print(g[dest-1])
   # print(np.any(np.array(tmp) == g[dest-1]))
    if np.any(np.array(quetmp) == g[dest-1]):
        new_start = g[dest -1]
        break

#print(new_start)
#print(tmp)
#ループ開始街からのループ回数
dest = new_start
quetmp = deque([])
for i in range(N):
    quetmp.append(g[dest - 1])
    n_loop = n_loop +1
    dest = g[dest -1 ]
    if g[dest -1] == new_start:
        n_loop = n_loop + 1
        quetmp.append(g[dest - 1])
        break

n_start = n_for_loop - ( n_loop - 1 )
K = K - n_start
#print(n_loop)
#print(K)
#print(tmp)
ans = quetmp[int(K%n_loop)-1]
#print(tmp[int(K%n_loop)-2])
#print("ans = ")
print(ans)
#print(tmp[int(K%n_loop)])

Submission status: Wrong Answer

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

ns = [0] * n
ns[0] = 1
# max 2*10**5
# どこかでループしてる
nexti = 0
starti = 0
endi = 0
tree.append(nexti)

while  k > 0 :
    nexti = a[nexti] - 1
    if ns[nexti] == 1:
        starti = nexti
        break
    ns[nexti] = 1
    endi = nexti
    tree.append(nexti)

x = tree.index(starti) 
# y = tree[starti:]
y = len(tree[x:])

if k - (x) < 0:
    print(tree[k-1])
else:
    k = k - (x)
    z = k % y
    temp = tree[x:]

    print(temp[z]+1)

Submission status: Wrong Answer

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

ns = [0] * n
ns[0] = 1
# max 2*10**5
# どこかでループしてる
nexti = 0
starti = 0
endi = 0
tree.append(nexti)

while  k > 0 :
    nexti = a[nexti] - 1
    if ns[nexti] == 1:
        starti = nexti
        break
    ns[nexti] = 1
    endi = nexti
    tree.append(nexti)

x = tree.index(starti) 
# y = tree[starti:]
y = len(tree[x:])

if k - (x-1) < 0:
    print(tree[k-1])
else:
    k = k - (x)
    z = k % y
    temp = tree[x:]

    print(temp[z]+1)

Submission status: Wrong Answer

Solution code:
import sys
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di=[]
a = 1
for i in range(k):
    a = ls[a-1]
    if a not in di:
        di.append(a)
    else:
        m = i-di.index(a)
        s = k % m -1
        print(di[s])
        sys.exit()
print(di[-1])

Submission status: Wrong Answer

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

A = [i for i in map(int, input().split())]
# print(N, K)
# print(A)
loop = [0]  # startは0→町1
now = 0  # now+1が町
while 1:
    now = A[now] - 1
    if now in loop:
        loop.append(now)

        break
    else:
        loop.append(now)

befo = loop.index(now)+1
new_loop = loop[(befo):]

# print([i+1 for i in loop])
print(new_loop[(K-befo) % (len(new_loop))]+1)

Submission status: Wrong Answer

Solution code:
# 写経
N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
loop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
    print(visit)
print(move)
print(loop)
if len(move)>K:
    print(move[K]+1)
else:
    print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)

Submission status: Wrong Answer

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

visited = [0 for i in range(n+1)]
move_count = 0
position = 1
loop_dis = 0

while True:
    if visited[position] != 0:
        loop_dis = move_count - visited[position] + 1
        visited[position] = move_count
        break
    else:
        move_count += 1
        visited[position] = move_count
        position = towns[position-1]
else:
    print(position)

rest_dis = (k - move_count) % loop_dis
move_count = 0
if rest_dis == 0 :
    print(position)
else:
    for i in range(rest_dis):
        move_count += 1
        position = towns[position-1]
    print(position)

Submission status: Wrong Answer

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

visited = [0 for i in range(n+1)]
move_count = 0
position = 1
loop_dis = 0

while True:
    if visited[position] != 0:
        loop_dis = move_count - visited[position] + 1
        visited[position] = move_count
        break
    else:
        move_count += 1
        visited[position] = move_count
        position = towns[position-1]


rest_dis = (k - move_count) % loop_dis
move_count = 0

if rest_dis == 0 :
    print(position)
else:
    for i in range(rest_dis):
        move_count += 1
        position = towns[position-1]
    print(position)

Submission status: Wrong Answer

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

visited = [0 for i in range(n+1)]
move_count = 0
position = 1
loop_dis = 0

while True:
    if visited[position] != 0:
        loop_dis = move_count - visited[position] + 1
        visited[position] = move_count
        break
    else:
        move_count += 1
        visited[position] = move_count
        position = towns[position-1]


rest_dis = (k - move_count) % loop_dis

move_count = 0

if rest_dis == 0:
    print(position)
else:
    for i in range(rest_dis):
        move_count += 1
        position = towns[position-1]
        print(position)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
  amari=k%n
  ans=li[amari-1]
  print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(n+10):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
  amari=k%n
  ans=li[amari-1]
  print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(n+10):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
	amari=k%n
	ans=li[amari-1]
	print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break

Submission status: Wrong Answer

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

num = 1
li = [1]
flag = [True] * n
flag[0] = False

while True:
    num = a[num-1]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d

print(li[ans])

Submission status: Wrong Answer

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

num = 1
li = [1]
flag = [True] * n
flag[0] = False

while True:
    num = a[num]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d

print(li[ans])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
L=[]
tmp=1
cnt=1
L.append(1)
while True:
  L.append(A[tmp])
  tmp=A[tmp]
  cnt+=1
  #print(cnt,tmp)
  if A[tmp] in L:
    L.append(A[tmp])
    break
#print(L)
res=0
for i in range(len(L)):
  if L[i]!=L[-1]:
    res+=1
  else:
    break
#print(res)
R=L[res:-1]
#print(R)
K-=res
num=K%len(R)
ans=R[num]
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=1
cnt=1
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
zahyo+=1
hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu+1):
    zahyo=A[zahyo]-1
print(zahyo,kaisu,count,cnt)
print(zahyo+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=1
cnt=1
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
zahyo+=1
hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
#print(zahyo,kaisu,count,cnt)
print(zahyo)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = list(map(int, input().split()))
a_list = [0] + list(map(int, input().split()))

n = 1
roop = [1]
next_a = a_list[n]

roop_end = False
while next_a not in roop:
  roop.append(next_a)
  next_a = a_list[next_a]
print(next_a)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=0
cnt=0
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
print(zahyo)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.buffer.readline


def solver(N, K, A):
    i = 0
    i_list = [0]
    reached = [False for _ in range(N)]

    while True:
        next_i = A[i]

        if reached[next_i]:
            break

        reached[next_i] = True

        i_list.append(next_i)
        i = next_i

    m = i_list.index(next_i)

    if m >= K:
        return i_list[K]

    le = len(i_list) - m
    K -= m
    ind = K % le

    return i_list[m + ind] + 1


def run():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    A = [a - 1 for a in A]

    ans = solver(N, K, A)
    print(ans)


run()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.buffer.readline


def solver(K, A):
    i = 0
    i_list = [0]
    while True:
        next_i = A[i]

        if next_i in i_list:
            break

        i_list.append(next_i)
        i = next_i

    m = i_list.index(next_i)

    if m >= K:
        return i_list[K]

    le = len(i_list) - m
    K -= m
    ind = K % le

    return i_list[m + ind] + 1


def run():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    A = [a - 1 for a in A]

    ans = solver(K, A)
    print(ans)


run()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.buffer.readline


def solver(K, A):
    i = 0

    i_list = [0]
    while True:
        next_i = A[i]

        if next_i in i_list:
            break

        i_list.append(next_i)
        i = next_i

    m = i_list.index(next_i)
    le = len(i_list) - m

    K -= m
    ind = K % le


    return i_list[m + ind] + 1


def run():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    A = [a - 1 for a in A]

    ans = solver(K, A)
    print(ans)


run()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
def tel(i):
    return a[i-1]
position = 1
if k <= n:
    for i in range(0,k):
        position = tel(position)
    print(position)
else:
    track = [0]*n
    for i in range(1,n+1):
        position = tel(position)
        if track[position-1] == 0 and position != 1:
            track[position-1] = i
        else:
            t = i - track[position]
            q = (k - i) % t
            for j in range(0,q):
                position = tel(position)
            print(position)
            break

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
def teleport(a,i):
    return a[i-1]
def teleportation(a,n,k):
    position = 1
    if n >= k:
        for i in range(0,k):
            position = teleport(a,position)
        return position
    else:
        track = [0]*n
        for j in range(1,n+1):
            position = teleport(a,position)
            if track[position - 1] == 0 and position != 1:
                track[position - 1] = j
            else:
                t = j - track[position - 1]
                q = (k - j) % t
                for r in range(0,q):
                    position = teleport(a,position)
                return position
teleportation(a,n,k)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
def teleport(a,i):
    return a[i-1]
def teleportation(a,n,k):
    position = 1
    if n >= k:
        for i in range(0,k):
            position = teleport(a,position)
        return position
    else:
        track = [0]*n
        for j in range(1,n+1):
            position = teleport(a,position)
            if track[position - 1] == 0:
                track[position - 1] = j
            else:
                t = j - track[position - 1]
                q = (k - j) % t
                for r in range(0,q):
                    position = teleport(a,position)
                return position
teleportation(a,n,k)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
def teleport(a,i):
    return a[i-1]
def teleportation(a,n,k):
    position = 1
    if n + 1 >= k:
        for i in range(0,k):
            position = teleport(a,position)
        return position
    else:
        track = [0]*n
        for j in range(1,n+2):
            position = teleport(a,position)
            if track[position - 1] == 0:
                track[position - 1] = j
            else:
                t = j - track[position - 1]
                q = (k - j) % t
                for r in range(0,q):
                    position = teleport(a,position)
                return position
teleportation(a,n,k)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
def teleport(a,i):
    return a[i-1]
def teleportation(a,n,k):
    position = 1
    track = [0]*n
    for j in range(1,n+2):
        position = teleport(a,position)
        if track[position - 1] == 0:
            track[position - 1] = j
        else:
            t = j - track[position - 1]
            q = (k - j) % t
            for r in range(0,q):
                position = teleport(a,position)
            return position
teleportation(a,n,k)

Submission status: Wrong Answer

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

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

reached = []

now = 1

if K < N:
    print(A[K])
    exit()

for _ in range(N):
    if now not in reached:
        reached.append(now)
        now = A[now-1]
    else:
        break

reached.append(now)

for i in range(len(reached)):
    if reached[i]== reached[-1]:
        cnt = i
        break


reached = reached[:-1]
roop = len(reached)- cnt
init = len(reached) 

print(reached[(K-init)%roop+cnt])