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())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
print(be,r)
if k< len(be):
  print(be[k]+1)
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)

Submission status: Wrong Answer

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

A = list(map(int,input().split()))
B = [1]
ex = 1

for i in range(N+10):
    ex = A[ex - 1]
    if ex in B:
            p = B.index(ex)
            q = i + 1 - p
            r = ex
            Z = (K - p) % (i + 1 - p)
            break
    else:
            B.append(ex)


ans = ex
for k in range(Z):
    ans = A[ans - 1]
    k = k + 1
    
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(input().split())
place=1
b=[1]
while place!=0:
 p=place-1
 place=int(a[p])
 b.append(place)
 a[p]=0
c=(len(b)-1)-(b.index(b[-2]))-1 #cが周期を表す
d=b.index(b[-2])
if (k-d)//c>=1:
 r=(k-d)%c
 ans=b[d+r]
else:
 r=k%c
 ans=b[r]
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
noappear=[i for i in range(n)]
while True:
        way.append(a[way[-1]])
        
        if bool(way[-1] in noappear)==False:
                break

        noappear.remove(way[-1])

last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=k%step
if k>=en:
        ans=way[rest]+1

else:
        ans=way[k]+1
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A = list(map(int, input().split()))
where = [0 for k in range(N)]
where[0]=1
loop=0
for k in range(N):
    where[k+1] = A[where[k]-1]

    if where[k+1] in where[0:k]:
        li = where[0:k]
        end = k+1
        start = li.index(where[k+1])
        loop = end -start

        break
loopbefore = K -start

loopafter = loopbefore%loop

where2 = [where[end]]
for k in range(loopafter):
    where2.append(A[where2[k]-1])
print(where2[-1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = []
check = {}
key = -1
p = 0
while True:
    ans.append(p)
    check[p] = 0
    p = a[p]-1
    if p in check:
        key = ans.index(p)
        break
if len(ans) == 1:
    print(a[0])
else:
    r = ans[key:]
    mod = (k-key)%len(r)
    if mod == 0:
        print(r[0]+1)
    else:
        print(r[mod]+1)
#print(r[mod]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = [0] + list(map(int,input().split()))
t = 1
ans = 1
L = [0] * (N + 1)
loop = [0]
l1 = 0
l2 = 0
count = 0

while True:
  t = A[t]
  if L[t] == 1:
    l2 += 1
    L[t] += 1
    loop.append(t)
  elif L[t] == 0:
    L[t] += 1
    l1 += 1
    l2 += 1
  elif L[t] == 2:
    ans = t
    break
l = l2 - l1

if K <= l1:
  while count < K:
    ans = A[ans]
    count += 1
else:
  ans = loop[(K - l1) % l]

print(ans)

#print(l1)
#print(l2)
#print(loop)

Submission status: Wrong Answer

Solution code:
N, K  = map(int, input().split())
A = [int(x) for x in input().split()]
count = [0] * N
t = A[0]
count[0] = 1
for i in range(N): 
    j = A[t-1]
    t = j
    if count[t-1] > 0:
        K = K - count[t-1] + 1
        K = K % (i+2 - count[t-1] + 1)
        break
    count[t-1] = i+2
t = A[0]
j = 0
for i in range(K):
    j = A[t-1]
    t = A[j-1]
print(t)

Submission status: Wrong Answer

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

d = {}
s_list = []
count = 0
idx = 1
while count < N:
    idx = a[idx-1]
    if idx in d.keys():
        e_list = s_list
        break
    else:
        d[idx] = set(s_list)
    s_list.append(idx)
    count += 1
loop_idx = len(s_list) - len(list(d[idx]))

if len(d[idx]) > K:
    print(list(d[idx])[K-1])
elif loop_idx != 0:
    t_idx = (K - len(d[idx]))% loop_idx
    print(s_list[len(d[idx]):][t_idx-1])
else:
    t_idx = K % N
    print(s_list[t_idx-1])

Submission status: Wrong Answer

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

# print(a_list)

NN = N
if N > K:
    NN = N
else:
    NN = N * (K % N) + K % N

next = 1
for _ in range(NN):
    next = a_list[next-1]

print(next)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n):
    a[i] -= 1
b = [0]
c = [False]*n
cur = 0
flag = 0
for i in range(n):
    if c[a[cur]] : 
        flag = 1
        break
    else:
        b.append(a[cur])
        c[a[cur]] = a[cur]
        cur = a[cur]
if flag == 0:
    print(b[-1]+1)
else:
    loop = b[b.index(a[cur]):]
    cycle = len(loop)
    if loop == b:
        k %= cycle
        print(loop[k]+1)
    else:
        k -= len(b)
        k %= cycle
        print(loop[k]+1)

Submission status: Wrong Answer

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

now = 1
loop_node = []
first_loop = 0
l = [0 for _ in range(N)]
while max(l) < 3:
    l[now - 1] += 1

    if l[now - 1] == 2:
        loop_node.append(now)

    now = A[now - 1]
    first_loop += 1
loop_num = len(loop_node)

print(loop_node[((K + 1) - (first_loop - loop_num * 2)) % loop_num])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
path = [1]
pos = 1
for i in range(k-1):
    pos = a[pos-1]
    if pos in path:
        num2 = path.index(pos)
        if num2 == path.index((a[pos-1])) -1:
            num = len(path[num2:])
            x,y = divmod(k +1 -len(path) + num ,num)
            if x>1:
                path = path[num2:]
                if y ==0:
                    print(path[-1])
                else:
                    print(path[y-1])
            else:
                print(pos)
            exit()
    else:
        path.append(pos)
print(pos)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
List=list(map(int,input().split()))
s=[1]
point=1

while True:
    point=List[point-1]
    if point not in s:
        s.append(point)
    else:
        for i in range(n):
            if s[i]==point:
                p=i
                break
        break

x=len(s[p:])
count=len(s)

b=(k-count+1)%x

if b==0:
    q=len(s)
    c=s[q-1]
else:
    c=s[p+b-1]

print(c)

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    ary = [0]
    tmp = [-1] * n
    tmp[0] = 0
    now = 0
    for cnt in range(1,k+1):
        now = a[now] - 1
        ary.append(now)
        if cnt == k:
            print(now)
            return
        if tmp[now] != -1:
            break
        else:
            tmp[now] = cnt
    m = (k - tmp[now]) % (cnt - tmp[now])
    top = tmp[now]
    # print(ary)
    # print(tmp)
    # print(now)
    # print(top)

    print(ary[m+top] + 1 )




if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

know = []
now = 1
for i in range(n+1):
    if now in know: break
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

cnt = len(loop)
if k>=len(know): k -= len(know)
ans = loop[k%cnt]


print(ans)

Submission status: Wrong Answer

Solution code:
n,K=list(map(int,input().split()))
a = list(map(int,input().split()))
b = []
c = [0]*len(a)
I=0
check=0
k=0
if a[k]==1:
    print(1)
else:
    for i in range(n):
        b.append(a[k])
        tes=a[k]-1
        k=a[k]-1
        if a[k]==1:
            b.append(1)
            check=-1
            break
        else:
            if c[tes]==0:
                c[tes]=i
            else:
                b.pop(-1)
                I = c[tes]
                break
    if check==-1:
        e=K%len(b)
        print(b[e-1])
    else:
        d=len(b)-I
        e=(K-I)%d
        print(b[I+e-1])

Submission status: Wrong Answer

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

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

n,k=LI()
a=LI()
b=[1]
done=[False]*(n+1)
done[1]=0 #1ははじめにいた

#シュミレートでもとまる
if k<=n:
    for i in range(1,k+1):#i回目
        nxt=a[b[-1]-1]
        # print(i,nxt) #i回目にnxtに達した
        b.append(nxt)
    print(nxt)
    exit()

#周期を求める
else:
    for i in range(1,k+1):#i回目
        nxt=a[b[-1]-1]
        b.append(nxt)
        if done[nxt]==False:
            done[nxt]=i
        else:
            m=i-done[nxt]
            break
    # print(m,k%m+m+1)
    k=k%m+m
    b=[1]
    for i in range(1,k+1):#i回目
        nxt=a[b[-1]-1]
        b.append(nxt)
    print(nxt)
    exit()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
K += 1
A = [int(i) for i in input().split()]
s = []
i = 0
length = 0

while True:
    if not A[i]:
        start = i + 1
        break
    s.append(i + 1)
    dest = A[i]
    A[i] = None
    i = dest - 1
    length += 1

if K ==2:
    print(s[1])
elif start == s[0]:
    amari = K % length
    if amari == 0:
        print(s[-1])
    else:
        print(s[amari - 1])
else:
    idx = s.index(start)
    s2 = s[idx:]
    length2 = len(s2)
    tmp = length - length2
    if K < tmp:
        print(s[K])
    else:
        K -= tmp
        amari = K % length2

        if amari == 0:
            print(s2[-1])
        else:
            print(s2[amari - 1])

Submission status: Wrong Answer

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

times = 0
to = l[0]
for i in range(10**5+1):
    to = l[to-1]
    times += 1
    if to == 1:
        times += 1
        break
x = k%times

to_2 = l[0]
for j in range(x-1):
    to_2 = l[to_2-1]

print(to_2)

Submission status: Wrong Answer

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

def has_duplicates(seq):
    return len(seq) != len(set(seq))
a=[1]

if len(A+a) != len(set(A+a)):
    dic={0:1}
    i=1
    while i<=N:
        if has_duplicates(dic):
            break
        d=dic[i-1]-1
        dic[i]=A[d]
        i+=1
    c=list(dic.values())
    p=c.pop()
    i=c.index(p)
    c_=c[i:]
    e=(K-i)%len(c_)
    print(c_[e])

else:
    i=0
    while i<K:
        d=A[d-1]
        i+=1

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A_list = list(map(int, input().split()))
out_loop = [1]
flag = [0, 1] + [0]* (N - 1)

for a in A_list:
    if flag[a] == 0:
        out_loop.append(a)
        flag[a] = 1
    else:
        loop = out_loop[out_loop.index(a):]
        
if K <= (len(out_loop) - 1):
    print(out_loop[K])
else:
    k = K - len(out_loop) - 1
    k %= len(loop)
    print(loop[k - 1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = []
cur = 0
for _ in range(n):
    if a[cur] in d:
        break
    d.append(a[cur])
    cur = a[cur] - 1
st = d.index(a[cur])
mae = len(d[:st])
ptt = len(d[st:])
hyo = d[st:]
amari = (k-mae)%ptt
print(hyo[amari-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
A.insert(0,0)
B = [1]
count = 1
S = 1
while 1:
    if A[S] in B:
        break
    count+=1
    S = A[S]
    B.append(S)

D = B.index(A[S])
C = (K-D) % (count-D)

if len(B)>=K+1:
    print(B[K+1])
else:
    print(B[C+D])

Submission status: Wrong Answer

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

n, k = map(int, input().split())
A = list(map(int, input().split()))
a = [0]*n
b = A[0]
per = []
tes = -1

if n >= k:
    for _ in range(k-1):
        b = A[b-1]
    print(b)
    exit()


for _ in range(k*2):
    #print(b)
    a[b-1] += 1
    tes += 1
    if 3 in a:
        tes -= len(per)*2
        break
    if 2 in a:
        per.append(b)
    b = A[b-1]

_len = len(per)
print(_len, tes)

ans = (k - tes) % _len

print(per[ans-1])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

route = [1]

end = False
i = 0

while not end:
  if a[i] not in route:
    route.append(a[i])
    i = a[i] -1
    # route[1].append(1)
  else:
    position = route.index(a[i])
    # print(position)
    break
  if len(route) == n:
    overhead = 0
    end = True


overhead = position
cycle_length = len(route) - overhead

# print(route)
# print(overhead)
# print(cycle_length)

end_position = (k-overhead) % cycle_length
# print(end_position)

ans = route[overhead + end_position]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = defaultdict(int)
d[1] += 1
pos = 0
cnt = 0
X = [1]
while cnt < N:
    d[A[pos]] += 1
    X.append(A[pos])
    #print(d)
    if d[A[pos]] == 2:
        idx = A[pos]
        break
    pos = A[pos] - 1
    cnt += 1
#print(idx)
for i, x in enumerate(X):
    if x == idx:
        pos = i
        break

X = X[:-1] + X[:-1]
#print(X)
diff = len(X) - pos
if K < pos:
    print(X[K])
else:
    mod = (K - pos) % diff
    print(X[pos:][mod])

Submission status: Wrong Answer

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

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

cnt_ls = [1]
next = 1
first = 0
for i in range(n):
    next = a[next-1]
    if next in cnt_ls:
        cnt_ls.append(next)
        break
    cnt_ls.append(next)

to_loop = cnt_ls.index(cnt_ls[-1])+1
loop_num = len(cnt_ls) - to_loop
if k <= to_loop:
    print(cnt_ls[k-1])
    exit()
k -= to_loop
k %= loop_num
k += to_loop
print(cnt_ls[k])

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
a = np.array([int(i) for i in input().split()])
 
pos = a[0]
p = np.array([])
while np.count_nonzero(p == pos) == 0:
    p = np.append(p, pos)
    pos = a[pos-1]

idx = np.where(p == pos)[0].tolist()[0]
k = K - idx
p = p[idx:]
 
print(int(p[k%len(p) - 1]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A = list(map(int, input().split()))

# ループ解析
step_on = [-1] * N
current = 1
min_mv_count = 0
for i in range(N + 1):
    if step_on[current - 1] == -1:
        step_on[current - 1] = i
        current = A[current - 1]
    else:
        period = i - step_on[current - 1]
        pre_mv = i - period
        min_mv_count = pre_mv + ((K - pre_mv) % period)
        break

current = 1
for i in range(min_mv_count):
    current = A[current - 1]
print(current)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]
# print(next)
# print(link)

link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0
# if len(link2) == 1 :
#     print('a')
#     rep = len(link)+1-len(link2)
# else:
# print('b')
rep = len(link)- len(link2)

if(k > len(link2)):
    # if len(link2) == 1:
    #     print('c')
    #     p = (k-len(link2)+2)%rep
    #     print(p)

    # print('d')
    p = (k-len(link2)+1)%rep
    # print(p)
    #
    # print(next)
    for i in range(p+rep-1):
        next = a[next-1]
    # print('e')
else:
    # print('f')
    next = 0
    for i in range(k):
        next = a[next-1]

ans = next
# print(link)
# print(link2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(points):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
elif ll == M:
    print(points+1)
else:
    print(loop[(M-ll)%l]+1)

Submission status: Wrong Answer

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

loop_checker = [1]
before_loop_len = 0
loop_len = 0

now = 0
while True:
    if A[now] in loop_checker:
        before_loop_len = loop_checker.index(A[now])
        loop_slice = loop_checker[before_loop_len:]
        loop_len = len(loop_slice)
        break
    else:
        loop_checker.append(A[now])
        now = A[now] - 1
if K <= before_loop_len:
    print(loop_checker[K])
else:
    print(before_loop_len)
    print(loop_len)
    checkK_prep = (K - before_loop_len)
    checkK = checkK_prep % loop_len
    print(loop_slice[checkK])

Submission status: Wrong Answer

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

pos = 0
kiseki = [0] * N
for i in range(N-1):
    pos = A[pos] - 1
    kiseki[i+1] = pos

tail = kiseki[-1]
if tail in kiseki[:-1]:
    past = kiseki.index(tail)
    kiseki = kiseki[past:-1]
    print(kiseki[(K - past) % len(kiseki)] + 1)
else:
    print(kiseki[K % N] + 1)

Submission status: Wrong Answer

Solution code:
N, K = input().split()
AN = input().split()
if 1 in AN:
    K = K - 1
K = int(K) % int(N)
i = 1
N1 = int(AN[0])
NX = AN[N1-1]
while i < int(K):
    NX = AN[N1-1]
    N1 = int(NX)
    i += 1
print(NX)

Submission status: Wrong Answer

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N, K = readnums()
    A = list(readnums())
    l = {A[0]}
    dic = {A[0]: 0}
    a = A[0]
    for i in range(1, N):
        a = A[a - 1]
        if a in l:
            d = dic[a]
            loop = list(dic.keys())[d:]
            K -= d
            print(loop[K % len(loop) - 1])
            break
        else:
            l.add(a)
            dic[a] = i


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A
c = {}
c[1] = 0
n = 0
k = 1
while c.get(A[k]) is None or c[A[k]] != 2:
    if c.get(A[k]) is None:
        c[A[k]] = 0
    else:
        c[A[k]] += 1
    n += 1
    k = A[k]

from collections import Counter
if n >= K:
    num = Counter(c.values())[2]
    sa = len(c)-num
    bbb = K - sa
    m = bbb % num
    abc = list(c.keys())
    abc = abc[sa:len(c)]
    print(abc[m])
else:
    a = 1
    for i in range(K):
        a = A[a]
    print(a)

Submission status: Wrong Answer

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

  history = []
  current = 0
  while True:
    history.append(current)
    current = A[current]
    if current in history:
      loop_length = len( history[history.index(current):] )
      print(loop_length)
      if loop_length == 0:
        print(A[current] + 1)
        return True

      if loop_length == 1:
        print(A[current] + 1)
        return True

      length_to_loop = len( history[:history.index(current)] )
      additional_way = (K-length_to_loop)%loop_length
      print(A[length_to_loop + additional_way] + 1)
      return True

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:


N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
cur = 0
i = 0
loop = 0
list = []

while (i <= K):
  i = i+1
  cur = A[cur-1]
  if(loop == 0):
    if (cur in list):
#      loop = len(list)
#      print("loop", loop)
      loop = len(list) - list.index(cur)
      if (K > 100):
        K= K - ((K // loop))*loop
    list.append(cur)
#  print(cur)

print(cur)

Submission status: Wrong Answer

Solution code:
# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

start = 0
now = As[start]
move = 1
nows = []

# print('-----output-----')
while True:
    move += 1
    now = As[now]
    nows.append(now)
    if now in nows:
        break

# K += 1
K %= move

now = As[start]
for i in range(K):
    now = As[now]

print(now+1)

Submission status: Wrong Answer

Solution code:
# def main():
#     N,K = [int(x) for x in input().split()]
#     Al = [int(x) for x in input().split()]
#     current = 1
#     steps = 1
#     visit = [0]*N
#     initial_step = 0
#     while visit[current] > 0:
#         visit[current] = steps
#         current = Al[current-1]
#         steps += 1
#         print(current, steps)
#     initial_step = visit[current]
#     loop_step = steps
#     print(initial_step, loop_step)
#     # loop_step = len(steps) - steps.index(current)
#     # if len(steps) == initial_step:
#     #     initial_step = 0
#     # print(steps.index(current))
#     # print(initial_step)
#     # print(loop_step)
#     print((K-initial_step)%loop_step)
#     print(Al[(K-initial_step)%loop_step])

def main():
    N,K = [int(x) for x in input().split()]
    Al = [int(x) for x in input().split()]
    current = 1
    visit = [0]*N
    steps = []
    initial_step = 0
    while visit[current-1] == 0:
        visit[current-1] = 1
        steps.append(current)
        current = Al[current-1]
        initial_step += 1
        # print(initial_step, current, steps)
    loop_step = len(steps) - steps.index(current)
    if len(steps) == initial_step:
        initial_step = 0
    # print(steps.index(current))
    # print(initial_step)
    # print(loop_step)
    print(steps[(K-initial_step)%loop_step])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = (int(x) for x in input().split())
la = list(map(int, input().split()))

frg=[0]*n
frg[0]+=1
nx = la[0]

cnt = [1, 0]
cntn = 0
if k==1:
  print(nx)
elif n<=k:
  for i in range(k-1):
    nx=la[nx-1]
  print(nx)
else:
  while 1:
    if frg[nx-1] == 1:
      cntn = 1
    frg[nx-1]+=1
    if frg[nx-1]==3:
      break
    nx=la[nx-1]
    cnt[cntn] += 1

  c = (k-cnt[0])%cnt[1]
  for i in range(c):
    nx=la[nx-1]
  print(nx)

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

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

cur=a[0]
cnt=1
lst=[]



while cur not in lst:
	lst.append(cur)
	cur=a[cur-1]
	cnt+=1
	
a=lst.index(cur)+1
size=len(lst)-lst.index(cur)

print(lst[(k-a)%size+a-1])


k

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
x=set(range(1,n+1))
dic=dict.fromkeys(x,0)
for i in x:
    dic[i]=l[i-1]
l=[-1]*n
curr=1
x=0
while x<=k:
    if l[curr-1]==-1:
        l[curr-1]=x
        x+=1
        continue
    else:
        #if l[curr-1]==0:
        l[curr-1]=x
    x=k-((k-x)%l[curr-1])
    curr=dic[curr]
    x+=1
print(dic[curr])

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()))
A = list(map(int,input().split()))
tour=[0]
next_town = 0
flag=False
for i in range(n):
    next_town=A[next_town]-1
    if i==k:
        flag = True
        ans = next_town
    if next_town in tour:
        first_roop_idx = tour.index(next_town)
        break
    tour.append(next_town)
#print(tour)
if not flag:
    if k<=len(tour):
        ans = tour[k]
    else:
        k-=first_roop_idx
        #print(k)
        k=k%(len(tour)-first_roop_idx)
        #print(k)
        ans = tour[first_roop_idx+k]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
#import sympy as syp(素因数分解とか)
Mod = 1000000007
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main():  #startline-------------------------------------------
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == 1:
        print(1)
        exit()
    visit = [-1] * n
    start = 1
    tmpa = 0
    while visit[start - 1] != 1:
        visit[start - 1] = 1
        start = a[start - 1]
        tmpa += 1
    visit = [-1] * n
    #この時点ですでに周期点からのスタート
    T = 0
    while visit[start - 1] != 1:
        visit[start - 1] = 1
        start = a[start - 1]
        T += 1
    for i in range((k - tmpa) % T):
        start = a[start - 1]
    print(start)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

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

n = 0
loc = 1
flag_visit = [-1]*N
flag_when = [-1]*N
flag_visit[0] *= -1
flag_when[0] = 0
move_hist = [1,]
while 1:
    loc = A[loc-1]
    move_hist.append(loc)
    n += 1
    if flag_visit[loc-1]==-1:
        flag_visit[loc-1]*=-1
        flag_when[loc-1] = n
    else:
        m = flag_when[loc-1]
        break
print(move_hist[m+(K-m)%(n-m)])

Submission status: Wrong Answer

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

temp = [[0] * 2 for i in range(len(A))]
index = 0
previous = 0
count = 0
while True:
    temp[index][0] += 1
    
    if temp[index][0] == 2:
        previous = temp[index][1]
        count = count - temp[index][1]
        break

    temp[index][1] = count
    count += 1
    index = A[index] - 1

places = []
place_index = index
for i in range(count):
    places.append(place_index + 1)
    place_index = A[place_index] - 1

K -= previous
print(places[K % count])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
ans_list = ""
for i in range(1,k+1):
    k = k - 1
    if k == 0:
        print(a_list[int(dum)-1])
        exit()
    ans_list = ans_list+dum
    dum = a_list[int(dum)-1]
    if ans_list.count(dum) == 1:
        break
#print(k)
#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])
#print(ans_list[1:])
#rint(ans_list)
#print(dum)
#print(ind)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(points):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
elif ll == M:
    print(points)
else:
    print(loop[(M-ll)%l]+1)

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

city_l=[]
city_l.append(1)
city_c=[0]*(N+1)
city_c[1]+=1
n_city=A[0]
flag=True
r_start=-1
r_end=-1
while(flag):
    city_c[n_city]+=1
    city_l.append(n_city)
    if(city_c[n_city]>1):
        r_start=city_l.index(n_city)+1
        r_end=len(city_l)-1
        flag=False
    n_city=A[n_city-1]

if(K<=r_end):
    print(A[city_l[K-1]])

else:
    K2=K-r_end
    K2=K2%(r_end-r_start+1)
    print(A[city_l[r_start+K2-2]-1])

Submission status: Wrong Answer

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

move1 = [1]
_num = move1[0]
for i in range(n*3):
    _num = al[_num-1]
    move1.append(_num)

move2 = move1[n:n+n]
for i in range(n, 3*n):
    if move1[i:i+n] == move2:
        _index = i
        break

# print(_index)

if k < n+_index:
    print(move1[k])
else:
    nn = (k-n-_index)%n
    print(move2[nn])

Submission status: Wrong Answer

Solution code:
N,K= map(int, input().split())
a =list(map(int, input().split()))
Town = [i for i in range(1,N+1)]
root = []
root.append(1)
tmp =1
key = 0
for _ in range(N):
    key = a[tmp-1]
    if key in root:
        break
    root.append(a[tmp-1])
    tmp = a[tmp-1]
first_length = len(root)-len(root[root.index(key):N])
T_root = root[root.index(key):N]
if len(root)>=K:
    print(root[K-1])
else:
    print(T_root[(K-first_length)%len(T_root)])

Submission status: Wrong Answer

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


#一回あたりのループを計算（何回で戻ってくるか）
#ループの箇所を見つける
trace = [0]*N
loop_start = 0
loop_before = 0
cnt = 0
all_cnt = 0

if K > N:
    loop = 0
    now = 0
    while(1):
        # print(trace)
        trace[now] += 1
        if trace[now] == 3:
            break
        if trace[now] == 2:
            cnt += 1
            loop += 1 
            if cnt == 1: 
                loop_start = now
                loop_before = all_cnt
        now = A[now]-1
        all_cnt += 1

    loop_before = loop_before - loop 

    # print(loop)
    # print(loop_start)
    # print(loop_before)
    # print(loop)
    min_K = (K-1-loop_before) % loop
    # print(min_K)
    # print(loop_before)
    now = 0
    for i in range(loop_before):
        now = A[now]-1
    for i in range(min_K):
        now = A[now]-1
    print(A[now])

# if K < N:
    now = 0
    for i in range(K-1):
        now = A[now]-1
    print(A[now])


# print(K % 3)

# print(now+1)