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

c=0
d=[1]
while (c<K):
    if A[d[len(d)-1]-1] in d:
        break
    d.append(A[d[len(d)-1]-1])
    c=c+1

if A[d[len(d)-1]-1] == 1:
    print(d[K%len(d)])
else:
    print(d[K % len(d)]+1)

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    b_lst = [False] * N
    b_lst[0] = True
    ans = 1
    counnnt = 0
    while True:
        counnnt += 1
        ans = A[ans-1]
        if b_lst[ans - 1]:
            break
        else:
            b_lst[ans-1] = True
    counter = 0
    save = ans
    while True:
        counter += 1
        ans = A[ans-1]
        if ans == save:
            break
    K = K-(counnnt-counter)
    if K < 0:
        ans = 1
        for i in range(K):
            ans = A[ans-1]
        print(ans)
        sys.exit(0)
    K %= counter
    for i in range(K):
        ans = A[ans-1]
    print(ans)


solve()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
M = 0
count = 1
for i in range(n):
    M = i
    m = i
    while count!=n and A[i]!=M:
        M = A[M-1]
        count += 1

if i!=0:
    j = k//count
else:
    j = 0

l = 0
while l != (k-count*j):
    m = A[m-1]
    l += 1

print(m)

Submission status: Wrong Answer

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

B = [1]
i = 0
count = 1

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

Submission status: Wrong Answer

Solution code:
#スペース区切りの整数の入力
N,K = map(int, input().split())
A_list = list(map(int, input().split()))
j_list = []
num = 1

owari  = 0
hajimari = 0
amari = 0
ans = 0

if(K<10):
    for i in range(K):
        num = A_list[num-1]
    print(num)
else:
    for i in range(N):
        num = A_list[num-1]
        if((num in j_list)==True):
            owari = num - 1
            hajimari = j_list.index(num)
            continue
        j_list.append(num)
    amari = (K - (hajimari-1) )%(owari-hajimari+1)
    ans = (hajimari-1)+amari
    print(ans)

Submission status: Wrong Answer

Solution code:

[N,K] = list(map(int,input().split()))
A = list(map(int,input().split()))

#ループする場合
flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        #del patt[-1]
        break
    flag[i]=1
    i=A[i]-1


srt = patt.index(patt[-1])
#print('srt',srt)
end = len(patt)-2
ptrn = patt[srt:end+1]

if K<len(ptrn):
    print(patt[K-1])
else:
    B = (K-srt) % len(ptrn)
    #print('B',B)

    print(ptrn[B]+1)
    #print('0000000')

    #print('ptrm',ptrn)
    #print(patt)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    counta = 0
    memo = []
    while i not in memo:
        memo.append(i)
        i = A[i] - 1    # pythonは一つずれる
        counta += 1
        if counta == K:
            print(i)
            exit()
    key = memo.index(i)
    roop = memo[key:]
    print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(8388608)
# 町: N個
# 移動回数: K
N, K = map(int, input().split())

# 次の町番号
A = list(map(int, input().split()))

flg = [False]* len(A)   # 既にきたことある町

townLoop = []
def warp(k, nowTown):
    # 移動回数が残っている
    if K+1 != k:
        #print(k, nowTown, A[nowTown-1])
        
        # まだ来たことのない町
        if flg[nowTown-1] == False:
            flg[nowTown-1] = True
            townLoop.append(A[nowTown-1])
            warp(k+1, A[nowTown-1] )
        else:
            return 
    else:
        return 
        #print(A[nowTown-1])

warp(0, A[0])


#print(townLoop)
if len(townLoop) > 2:
    if townLoop[0] == townLoop[len(townLoop)-1]:
        townLoop.pop(len(townLoop)-1)

#//print((K-1) // len(townLoop))
tmp = K-1
tmp = len(townLoop) // tmp
print(townLoop[tmp])

Submission status: Wrong Answer

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

#loop check
gone = []
pos = 0
i = 0
while pos not in gone:
    gone.append(pos)
    pos = A[pos] - 1
    i += 1
    if (pos in gone):
        loop_flag = True
        loop_pos = gone.index(pos)
        loop_len = len(gone)-loop_pos



if (loop_flag):
    pos = loop_pos
    for i in range(((K - loop_pos) % loop_len)-1):
        pos = A[pos]-1
    
else:
    pos = 0
    for i in range(K):
        pos = A[pos]-1

print(pos+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
ka=0
kab=0
sihyou=0
while sihyou==0:
    
    
    if b[ka]==1:
        sihyou=1
    else:
        b[ka]=1
        c[ka]=kab
        ka=a[ka]-1
        kab=kab+1
ruupu=kab-c[ka]
ansa=(k-c[ka])%ruupu+c[ka]
yui=0
for i in range(ansa):
    yui=a[yui]-1
print(yui+1)
print(ansa)

Submission status: Wrong Answer

Solution code:

def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    i=1
    j=0
    
    lll=[0]*N
    lll[0]=1
    inLoop=True
    while inLoop:
        j+=1
        i=A[i-1]

        if j==K:
            print(lll[j])
            sys.exit()
        if i in lll:
            n=lll.index(i)
            K=K-n
            l=K%(j-n)
            print(lll[n+l])
            sys.exit()
        lll[j]=i
        


        


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# atcoder 
# Türkçe konuşyorum
# Türkler var mı

n, k = map(int, input().split(" "))
val = list(map(int,input().split(" "))) 
dico = {}
i = 0
for j in range(len(val)):
    #val = int(input().split(" "))
    dico[j] = val[j]
#print(dico)

while k >= 1:
    if not i < n:
         i = 0
    i += 1
    k -= 1

print(dico[i+1])

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+1):
    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)

ans = (k - tes) % _len

print(per[ans-1])

Submission status: Wrong Answer

Solution code:
import sys

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

history =[1]
place = A[0]

for i in range(n):
  history.append(place)
  place = A[(int(place)-1)]
  if place == 1:
    amari = k % len(history)
    print(history[amari])
    sys.exit()

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

c=[1]
i=0
for _ in range(N+1):
    if has_duplicates(c):
        break
    d=c[-1]-1
    c.append(A[d])
p=c.pop()
i=c.index(p)
c_=c[i:]
e=(K-i)%len(c_)
print(c_[e])

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
A = list(map(int, input().split()))

route = []
route = [1]
appearance = set()
index = 1
flg = False
for _ in range(n):
    if index in appearance:
        flg = True
        break
    appearance.add(index)
    next_city = A[index - 1]
    route.append(next_city)
    index = next_city

if flg is False:
    print(route[k])
    sys.exit()

# 繰り返し個数
duplicate = route[route.index(index):len(route) - 1]
print(duplicate[(k - route.index(index)) % (len(duplicate))])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a_list=list(map(int,input().split()))
a_list.append(0)
tmp=a_list[0]
tmp_list=[]
K2=K%N
count=1
count2=0
while count<N+K2:
    tmp=a_list[tmp-1]
    tmp_list.append(tmp)
    count+=1
    #print(count)
    #print(a_list[tmp-1])
#print(tmp_list)
while count2<K2-1:
    tmp_list.pop(0)
    count2+=1
#print(tmp_list)
count3=1
tmp2=a_list[0]
if K < N:
    #print(tmp2)
    while count3 <= (K2):
        tmp2=a_list[tmp2-1]
        count3+=1
    print(tmp2)
else:
    print(tmp_list[K2-2])

Submission status: Wrong Answer

Solution code:
machicount,flycount=map(int,input().split())
teleps=list(map(int,input().split()))
e=len(teleps)
gh=flycount%e
if gh==e-1:
    print(teleps[0])
else:    
    print(teleps[gh+1])

Submission status: Wrong Answer

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

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

temp =1

road=[1]

while len(road) < N*2:
    temp =  A[temp-1]
    road.append(temp)
    
print(road[N])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
cnt = 0
B = [-1]*n
i = 0
while True:
  if B[i] >= 0:
    break
  else:    
    B[i] = cnt    
    cnt += 1    
    i = A[i]-1
  

loop = cnt - B[i]

j = A[i]
for i in range((k-cnt)%loop):
  j = A[j] - 1   

print(A[j])

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))

    print(A[k%n])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

citys = [False] * n
now = 0
citys[0] = True
costt = 0
while True:
    now = a[now] - 1
    if citys[now]:
        costt += 1
        break
    else:
        citys[now] = True
        costt += 1


if costt > k:
    i = 0
    while i != k:
        now = a[now] - 1
        i += 1
    print(now+1)
    quit()

citys = [False] * n
citys[now] = True
check = [0] * n
check[now] = 1
cost = 0
while True:
    now = a[now] - 1
    if citys[now]:
        cost += 1
        break
    else:
        citys[now] = True
        cost += 1
        check[now] = cost + 1

costt -= cost
d = (k - costt) % cost + 1
ans = check.index(d)
print(ans+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import copy

N, K = map(int, input().split())
List = list(map(int, input().split()))
saki=[1]
ato=[]

i=0
j=1
while i<N:
    #print(j)
    saki.append(List[j-1])
    #print(saki)
    j=List[j-1]
    i+=1
    
#print("AAA")
#print(j)

k=copy.deepcopy(N)
#print(k)

#print(j)
ato.append(List[j-1])
#print(ato)
#j=List[j-1]

start=saki.index(ato[0])
#print(start)

SA=(len(List)-start)+1
#print(SA)

#print((K-start)%SA)
o=0
check=[]
while o<SA:
    #print("AAA")
    #print(j)
    check.append(List[j-1])
    #print(check)
    j=List[j-1]
    o+=1
print(check[(K-start)%SA])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
tele = list(map(int, input().split()))
move = [-1]*n
move[0] = 0
now = 0
cnt = 0

while True:
    cnt += 1#移動回数
    now = tele[now] - 1 #移動した先

    if move[now] != -1:#探索済みなら周期を計算しbreak
        cycle = cnt - move[now]
        break
    else:
        move[now] = cnt#未探索ならこれまでの移動回数を記録
#この時cntに格納されている数は周期が起こった直後    

#ループが発生する前の場合
if cnt-1 >= k:
    ans = 0
    for i in range(k):
        ans = tele[ans] - 1
#ループが発生する場合
else:
    loop = (k-(cnt-1))%cycle#ループが起こった余り
    ans = cnt - 1 #ループ発生の直前の場所

    for i in range(loop+1):
        ans = tele[ans] - 1
print(ans+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import math

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

A = [int(x) for x in input().split()]

temp = A[0]
stack_L = [1]
for i in range(N):
    if temp in stack_L:
        all_num = i + 1
        break
    stack_L.append(temp)
    temp = A[temp - 1]


num_1 = stack_L.index(temp)
loop_num = all_num - num_1

amari = (K - (all_num - loop_num)) % loop_num

print(stack_L[num_1 + amari])

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.insert(0, i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[:len(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 = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    K -= order.index(M[s])
    order = order[order.index(M[s]):]
    if K%len(order)==0:
      print(order[-1]+1)
    else:
      print(order[K%len(order)]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]

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
visit=[False for i in range(n)]
visit[0]=True
looplist=[False for i in range(n)]
mark=-1
i=0
num=0
loop=0
count=0
while True:
  num+=1
  i=a[i]
  if mark<0 and visit[i]:
    mark=a[i]
  else:
    visit[i]=True
  if mark>-1 and looplist[i]:
    count=num-loop*2
    break
  elif mark>-1:
    loop+=1
    looplist[i]=True
if k<count:
  pass
else:
  k-=count
  k%=loop
  k+=count
ans=0
for i in range(k):
  ans=a[ans]
print(ans+1)
print(count,loop)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = []
present = 1
loop = False
while loop == False:
  if present in visited:
    loop = True
  else:
    cnt += 1
    visited.append(present)
    present = A[present-1]

ind = visited.index(present)
l = len(visited[ind:])
rest = 0
rest += (K-cnt) % l

while rest > 0:
  if rest == 0:
    continue
  else:
    present = A[present-1]
    rest -= 1
    
print(present)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
if N >= K:
    ans = A[0]
    for _ in range(K):
        ans = A[ans-1]
    print(ans)
else:
    mod = K%N
    print(A[A[mod-1]-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) == 1:
        print(1)
        return
    if len(steps) == 2:
        print(steps[1])
        return
    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=map(int, input().split())
a=list(map(int, input().split()))
a.insert(0, 0)

for i in range(1, n+1):
    if a.count(i)>=1:
        s_index=i
        break

cnt=0
n_index=a[s_index]
for j in range(1, n+1):
    if n_index==s_index:
        cnt+=1
        break
    else:
        n_index=a[n_index]
        cnt+=1

print(a)
print(s_index)
print(cnt)

Submission status: Wrong Answer

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

a.insert(0, 'hoge')
amari = k % n

tmp = 1
if k >= n:
    for i in range(1, n + amari + 1):
        tmp = a[tmp]
        # print(i, tmp)
# else:
#     for i in range(1, n+1):
#         if tmp != n:
#             tmp = a[tmp]
#         elif tmp == n:
#             print(tmp)
#             exit()

print(tmp)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
t = K
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:
    state ,f = test, find_f[test]
    K = K-f[0]
    f = f[1]-f[0]
    remain = K%f
    break

if t == K:
  print(test)
else:
  for i in range(remain):
   	state = move_list[state-1]
 
  print(state)
#state = 1

Submission status: Wrong Answer

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

limit = K
now = 1
log = []

while limit > 0 and now not in log:
    if 1 not in log:
        log.append(1)
    #print("通過1",now,limit,log)
    now = A[now - 1]
    log.append(now)
    limit -= 1

if limit > 0:
    #print("通過2",now,limit,log)
    limit = limit % len(log[log.index(now) - 1:])

    while limit >= 0:
        #print("通過3",now)
        now = A[now - 1]
        limit -= 1

print(now)

Submission status: Wrong Answer

Solution code:

[N,K] = list(map(int,input().split()))
A = list(map(int,input().split()))

#ループする場合
flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        #del patt[-1]
        break
    flag[i]=1
    i=A[i]-1


srt = patt.index(patt[-1])
#print('srt',srt)
end = len(patt)-2
ptrn = patt[srt:end+1]

if K<len(ptrn):
    print(patt[K]+1)
else:
    B = (K-srt) % len(ptrn)
    #print('B',B)

    print(ptrn[B]+1)
    #print('0000000')

    #print('ptrm',ptrn)
    #print(patt)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np
from collections import Counter

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
g = [1]
now = 1
for i in range(N):
    now = A[now-1]
    g.append(now)

c = Counter(g)

depfirst = N+1
depend = N+1
for i in c.most_common():
    if i[1] > 1:
        temp = [k for k, x in enumerate(g) if x == i[0]]
        if temp[1] < depend:
            depfirst = temp[0]
            depend = temp[1]
    else:
        break

interval = depend-depfirst+1
if K <= depfirst:
    print(g[K])
else:
    print(g[depfirst+(K-depfirst) % interval+1])

print(g, depfirst, depend)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def get_int():
    return int(input())


def get_tuple():
    return map(int, input().strip().split())


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


n, k = get_tuple()
ls = get_ls()
visited = defaultdict(int)
times = 0
town = 1
dic = defaultdict(int)
while town not in visited:
    dic[times] = ls[town - 1]
    visited[town] = times
    times += 1
    town = ls[town - 1]

period = times - visited[town]
if k<=times:
    print(dic[k])
else:
    iter_req = visited[town]
    k -= visited[town]

    if k > period:
        k %= period
    iter_req += k
    town = 1
    ans = ls[town-1]
    while iter_req >1:
            town = ans
            ans = ls[town - 1]
            k -= 1
            #print(ans)
            iter_req -= 1

    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
Line = [int(s) for s in input().split()]

X = [-1]*N

# print(X)
ikisaki = Line[0]
X[0] = 0

# print(ikisaki)

for i in range(N+1):
    if X[ikisaki-1] == -1:
        X[ikisaki-1] = i + 1
        ikisaki = Line[ikisaki-1]
    else:
        warukazu = i+1 - X[ikisaki-1]
        saisyo = X[ikisaki-1]
        break
    # print(X)
print(saisyo)
# print(warukazu)
Z = K%warukazu
# print(Z)
if saisyo >= K:
    print(X.index(K)+1)
else:
    print(X.index(Z)+1)

Submission status: Wrong Answer

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=[]
now=0
for i in range(n+1):
        ans.append(a[now])
        now=a[now]-1
#print(ans)

ans2=collections.Counter(ans)

ac=ans2.most_common()[0][0]
#print(ac)
index=-1
index2=-1

for i in range(len(ans)):
    if ans[i]==ac:
        if index==-1:
            
            index=i
        else:
            index2=i

#print(index,index2)
#print((k-index-1)%(index2-index))
print(ans[(k-index-1)%(index2-index)+index])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
nk = input('')
n = int(nk.split(' ')[0])
k = int(nk.split(' ')[1])

alist = input('').split(' ')
alist2 = []

next=1
loop_end=0
alist2.append(1)
for i in range(len(alist)):
    next = int(alist[next-1])
    if next in alist2:
        loop_end=next
        break
    else:
        alist2.append(next)

head_size=alist2.index(loop_end)
loop_size=len(alist2) - head_size

k2=(k-head_size)%loop_size+head_size
#print(alist2)
#print(k2)
pos=alist2[k2]

print(pos)

Submission status: Wrong Answer

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

tmp = a[0]
k %= n

for i in range(k-1):
    tmp = a[tmp-1]
    
print(tmp)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
now_list = []
start = 0
roop = 0
count = 0

while now not in now_list:
    now_list.append(now)
    now = where_go[now] -  1

start = now_list.index(now)
roop = len(now_list) - start



while count < (K - start) % roop:
    now = where_go[now] -  1
    count += 1

print(now+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def function(arg1, arg2):
    N,K = map(int, arg1.split())
    teleportes = list(map(int, arg2.split()))
    checked = [0] * len(teleportes) # 訪れた街にチェック

    i = 0
    c = 1
    while True :
        nextTerminal = teleportes[i] - 1
        if checked[i] == 0:
            checked[i] = c # 何番目に訪れたか
            i = nextTerminal
            c += 1
        else:
            loops = c - checked[i] # 以前訪れてから何回旅したか
            break
    
    if K <= i:
        nextTerminal = 0
        k = 0
        while k in range(0,K):
            nextTerminal = teleportes[nextTerminal] - 1
            k += 1
        goal = nextTerminal + 1
    else:
        r = (K-(checked[i]-1))%loops
        nextTerminal = 0
        while nextTerminal != i:
            nextTerminal = teleportes[nextTerminal] - 1
        k = 0
        while k in range(r):
            nextTerminal = teleportes[nextTerminal] - 1
            k+=1
        goal = nextTerminal + 1
    return str(goal)

if __name__ == '__main__':
    print(function(input(),input()))

Submission status: Wrong Answer

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

keys = range(1,n+1)
values = [-1]*n
dic = dict(zip(keys,values))
dic[1] = 0
t = 1
i = 1

def inverse_lookup(d, x):
    for k,v in d.items():
        if x == v:
            return k

while i <= k:
    t = l[t-1]
    if dic[t] != -1:
        m  = dic[t]
        d = i-m
        r = (k-i+1)%d
        ans = inverse_lookup(dic, (m+r-1))
        print(ans)
        break
    dic[t] = i
    i += 1
if i == k+1:
    print(t)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
loc_list = [1]
count = 0
for i in range(n):
    base = loc_list[-1]
    loc_list.append(a[base])
    loc_set = set(loc_list)
    if len(loc_set) != len(loc_list):
        break
    else:
        pass
for i in range(len(loc_list)):
    if loc_list[i] == loc_list[-1]:
        j = i
        circleloc = list(set(loc_list[i:]))
        break
    else:
        pass
place = (k - j) % len(circleloc)
print(circleloc[place])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
y=[]
x=1
while x not in y:	
	y.append(x)
	x=l[x-1]
	
#	print(y)
	#c+=1
c=len(y)-y.index(x)
#print(y)
r=1
s=0
while r!=x:
	r=l[r-1]
	s+=1
	 
k=(k-s)%c
x=1
#print(c)
for i in range(s+k): 
	x=l[x-1]
print(x)

Submission status: Wrong Answer

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

key = n
for i in range(k):
  key = l[key-1]
  if i>n:
    break

print(l[key-1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

def main():
  N, K = map(int, input().split())
  towns = tuple(map(int, input().split()))

  j = 0

  visited = [0]*N
  stop = False
  usual = []
  junkan = []
  while stop == False:
    j = towns[j]-1
    if visited[j] == 0:
      usual.append(j)
      visited[j] = 1
    elif visited[j] == 1:
      visited[j] = 2
      junkan.append(j)
    else:
      stop = True

  if K < len(usual):
    print(usual[K]+1)
  else:
    print(junkan[((K-len(usual))%len(junkan))-1]+1)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def main():
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  b=1
  seen=[-1]*n
  c=0
  while seen[b-1]==-1:
    seen[b-1]=c
    b=a[b-1]
    c+=1
  start=seen[b-1]
  end=c
  k=start+(k-start)%(end-start)
  b=1
  for _ in range(k):
    b=a[b-1]
  print(b)
if __name__=='__main__':
  main()

Submission status: Wrong Answer

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

# １周の周期を求める
place = 1
term = 1
i = 0
history = [1]

for x in range(N):
    place = A[place - 1]
    if place in history:
        i = history.index(place)
        term = x + 1 - i
        break
    else:
        history.append(place)

K = (K - i) % term
for x in range(K):
    place = A[place - 1]

print(place)

Submission status: Wrong Answer

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

syoki = 1
pre = []

loop_count = 0

for i in range(N):
    if syoki in pre:
        ll = pre.index(syoki)
        loop_count = i - ll
        K = K - ll
        break
    pre.append(syoki)
    syoki = A[syoki - 1]

res_N = K - (loop_count * (K // loop_count))
if res_N == 0:
    print(A[A[0]-1])
else:
    syoki = 1
    for j in range(res_N):
        syoki = A[syoki - 1]
    print(syoki)

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-1):
        next = a[next-1]

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

Submission status: Wrong Answer

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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    visited = [0] * (N+10)
    seq = []

    step = 1
    current = 1

    n1 = n2 = 0
    while(step<len(A)+1):
        #print(current)
        seq.append(current)
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            break
        else:
            visited[next_pos] = step+1
        step += 1
        current = next_pos

    #print(n1, n2)
    #print(n1-1, ((K-(n1-1))%n2))

    print(seq[n1-1+((K-(n1-1))%n2)])


if __name__ == '__main__':
    main()

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
root=[1]
for i in range(N):
    now=A[now-1]

    if now not in root:
        root.append(now)
    else:
        index=root.index(now)
        num=len(root[index:])
        root=root[index:]
        break

an = (K-i) - (K-i)//num * num
print(root[an-1])

Submission status: Wrong Answer

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

footprints = []
loop = []
start = 0
end = 0
prev = A[0]

footprints.append(1)
for i in range(0, len(A)):
    index = A[prev - 1]
    if (index in footprints):
        start = footprints.index(index)
        end = i + 2
        break
    else:
        footprints.append(index)
        prev = A[prev - 1]

if (start == 0 and end == 0):
    end = NK[0]

prev = A[start]
for i in range(start, end):
    loop.append(A[prev])
    prev = A[prev]

loopStart = NK[1] - start
tmp = loopStart % len(loop)
print(loop[tmp])