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:
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:
num = input().split()
n = int(num[0])
m = int(num[1])
l = input().split()
i = int(l[2])
print(i)

Submission status: Wrong Answer

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


loop_find_list_ = []
loop_point = -1

dp = [0] * (2 * 10**5 + 1)
dp[1] = data[1 - 1] # 1番目埋め
loop_find_list_.append( dp[1] )

for k_ in range(2, k+1):
  dp[k_] = data[dp[k_ - 1] - 1]
  if dp[k_] in loop_find_list_:
    # print("検出したので終了します")
    loop_point = dp[k_]
    break
  else:
    loop_find_list_.append( dp[k_] )

if k <= len(loop_find_list_):

  print(loop_find_list_[k - 1])
else:
  idx = loop_find_list_.index(loop_point)
  len_ = len(loop_find_list_)
  loop_len_ = len_ - idx
  k = k % loop_len_
  print(loop_find_list_[k - 1])

Submission status: Wrong Answer

Solution code:
def detectCycle(a):
    start, length = 0, 1
    p1, p2 = a[1], a[a[1]]
    while p1 != p2:
        p1 = a[p1]
        p2 = a[a[p2]]
    
    p2 = 1
    while p1 != p2:
        p1 = a[p1]
        p2 = a[p2]
        start += 1
    
    p2 = a[p1]
    while p1 != p2:
        p2 = a[p2]
        length += 1
    
    return start, length

n, k = (int(x) for x in input().split())
a = [-1] + [int(x) for x in input().split()]
start, length = detectCycle(a)

loc = 1
k -= start
for i in range(start):
    loc = a[loc]

k %= length
for i in range(k):
    loc = a[loc]
print(loc)

Submission status: Wrong Answer

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

times = K % N
if 1 in towns:
    print(towns[(towns.index(1) + times) % N])
else:
    print(towns[times % N])

Submission status: Wrong Answer

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

# 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():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]

    done = [int()]*(2*10**5)
    done_k = [0] * N


    i = 0
    q = 1
    while done_k[q-1] == 0:
        done[i] = q
        done_k[q-1] = 1
        i+=1
        q = A[q-1]
        # print("q: {} i:{}".format(q,i))

    f_q = done.index(q)
    # print("i :{} r:{}".format(i,f_q))
    r = i - f_q
    # print(r)
    if K <= i:
        t = K
    else:
        k = K - f_q
        k = k % r
        t = k + f_q

    print(done[t])        

    # q = A[0]
    # for _ in range(t-1):
    #     q=A[q-1]
    # print(q)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [0]
#load = []
res = 0
flag = 0
for i in range(k-1):
    #print(nxt)
    if not(nxt in seen):
        seen.append(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        print(root)
        print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

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 = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)):
        #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
        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, k = map(int, input().split())
a = list(map(int, input().split()))

visit = [] #訪れる街
loop_point = 0 #以後繰り返し
now = 1
if 1 in a:
  now = a.index(1) #今いる街

for i in range(n):
  if a[now - 1] in visit:
    loop_point = a[now - 1] #もしきたことある街だったら繰り返しポイント
    break
  else:
    visit.append(a[now - 1]) #きたことなかったらvisitに足す
    now = a[now - 1]
    continue

where = visit.index(loop_point) #繰り返しポイントの街はVISITリスト[何]？

if k <= len(visit) - where:
  print(visit[k])
else:
  print(visit[(k % (len(visit) - where)) - 1])

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, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

p = 0  # 現在位置
probs = []

for i in range(n):
    probs.append(p)
    p = maps[p]
    if (p in probs):
        g = i + 1
        break

h = probs.index(p)
s = g - h

d = k % s
ans = 0
for i in range(d):
    ans = maps[ans]

print(ans + 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
done=[0 for _ in range(N)]
done[0]=1
W=[0]

init=0
loop=N
city=0

for i in range(N):
    if done[A[i]-1]==2:
        init=W.index(A[i]-1)
        loop=i-init
        break
    elif city==A[city]-1:
        print(city)
        init=i
        loop=1
        break
    city=A[city]-1
    W.append(city)
    done[city]+=1

print(init,loop)
if K>=init:
    print(W[(K-init)%loop+init]+1)
else:
    print(W[K]+1)

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
while town not in visited:
    visited[town] = times
    times += 1
    town = ls[town - 1]

period = times - visited[town]
if k<=times:
    dic = defaultdict(int)
    i = 0
    while k>0:
        if ls[i] not in dic:
            dic[ls[i]] = 1
            k -= 1
            ans = ls[i]
        i += 1
    print(ans)
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())
a = list(map(int, input().split()))
now = 1
cnt = 0
loop = 0
cnts = [-1] * n
cnts[now - 1] = cnt
visited = [False] * n
visited[now - 1] = True
li = [now]
while True:
    now = a[now - 1]
    cnt += 1
    li.append(now)
    if visited[now - 1]:
        loop = (cnt - cnts[now - 1])
        pre = cnts[now - 1]
        print(li[pre + (k - pre) % loop])
        exit()
    cnts[now - 1] = cnt
    visited[now - 1] = True

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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


time=1
route=[1]
x=A[0]
Loop=0
while x!=1:
    if x not in route:
        route.append(x)
        x=A[x-1]
        time+=1
    else:
        Loop=time-route.index(x)
        a=route.index(x)
        x=1
        
if Loop==0:
    mod=K%time
    print(route[mod])
    sys.exit()

else:
    mod=(K-a)%Loop
    print(route[mod+a])

Submission status: Wrong Answer

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

i = 0
x = 1
ans = [1]
b = False
if k == 1:
    print(1)
else:
    while i < k+1:
        x = A[x-1]
        i += 1
        if x in ans:
            _a = ans.index(x)
            b = True
            break
        else:
            ans.append(x)

    if b:
        print(ans[_a + (k - _a) % (len(ans)-_a)])
    else:
        print(ans[-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
flag = 0
start = 0
position = [0]
while flag == 0:
  temp = position[-1]
  if A[temp] -1 in position:
    flag = 1
    start = position.index(A[temp] -1)
  position.append(A[temp]-1)

if K >=start:
  x = start + (K - start)%(len(position) - start -1)
  print(position[x]+1)
else:
  ans = 0
  for _ in range(K):
    ans = position[ans]
  print(position[ans]+1)

Submission status: Wrong Answer

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

kk = k % 6

for i in range(kk):
    # 次どこに移動するか
    if i == 0:
        ans = a[0]
    else:
        ans = a[ans - 1]

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
b = k%r
t = 0
for i in range(b):
  t = a[t]-1
print(t+1)

Submission status: Wrong Answer

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

limit = K
now = 1
log = []
count = 1

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
    count += 1

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

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

now = A[now - 1]

print(now)

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))
if index2-index==n:
    print(ans[(k-index-1)%(index2-index)+index-1])
else:
    print(ans[(k-index-1)%(index2-index)+index])

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


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


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()
    A = LIST()
    po = 1
    lis = [1]
    ct = defaultdict(int)
    ct[1] += 1
    for i in range(N-1):
        po = A[po-1]
        ct[po] += 1
        if ct[po] > 1:
            break
        lis.append(po)

    # print(ct.values())

    if set(ct.values()) == {1}:
        K %= len(lis)
        # print(lis)
        print(lis[K])
    else:
        ind = lis.index(po)
        lis = lis[ind:]
        # print(lis)
        # print(ind)

        K -= ind
        K %= len(lis)

        print(lis[K])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
seen = [False]*(N+1)
seen[0] = True
first_seen = [-1]*(N+1)
loop_city = [1]
loop_start = None
cnt = 1
while cnt <= N:
    nxt_city = A[loop_city[-1]-1]
    if seen[nxt_city]:
        loop_num = cnt - first_seen[nxt_city]
        loop_start = first_seen[nxt_city]
        break
    else:
        seen[nxt_city] = True
        first_seen[nxt_city] = cnt # cnt回目の移動で到着した
    loop_city.append(nxt_city)

    if cnt == K:
        print(loop_city[K]) # Kが小さい時はこれでいい
        exit()
    cnt += 1


idx = loop_start + (K - loop_start)%loop_num
# print('loop_city',loop_city)
# print('loop_start',loop_start)
# print('loop_num',loop_num)
# print('idx',idx)
# print('ans',A[idx])
print(A[idx])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 1
x = 0
ss = set([1])
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+2:
        x = 1
        break
if x == 1:
    aa = k%len(ss)
    for i in range(aa):
        now = a[now-1]
print(now)

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])
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 = [0] + [int(x) for x in input().split()]


tmp = [1]
tele = A[1]
flg = True
cnt=0
while True:
    cnt += 1
    if tele in tmp:
        tmp2 = tmp[tmp.index(tele):]
        break
        
    tmp.append(tele)
    tele = A[tele]
    if cnt > (2 * 10**5):
        break

if flg:
    K -= len(tmp) - len(tmp2)
    cnt2 = K % len(tmp2)
    print(tmp2[cnt2])

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%c
x=1
#print(c)
for i in range(y.index(x)+k): 
	x=l[x-1]
print(x)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
loc=1
begin=0
end=0
d={1:0}
for i in range(n+1):
    loc=a[loc-1]
    if loc in d.keys():
        begin=d[loc]
        end=i+1
        break
    d[loc]=i+1
if k<=end:
    end=0
if end==begin+1:
    k=begin
    end=0
if end !=0:
    k=(k-begin+1)%(end-begin)+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)

Submission status: Wrong Answer

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

visited = [0]*N
i = 0
rootin = [0]*N
rootin[0] = 1
count = 0
while  visited.count(2) == 0:
  count += 1
  visited[i] += 1
  if visited[i] == 2:
    end = count
    break
  rootin[count] = A[i]
  i = A[i]-1
start = 0
for i in range(len(rootin[:end])):
  if rootin[i] == rootin[end-1]:
    start = i
    break
od = len(rootin[:start])
root = len(rootin[start:end])-1

K -= od
ans = rootin[start+K%root]
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]

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

b = A[0]
count = 1

X = []
flag = 0
loop = 0
ans = 0

if A[0] == 1:
    ans = 1

elif N>=K:
    #for i in range(K-1):
     #   c = A[b-1]
     #   b = c
    ans = 1
else:   
    #print("a")
    for i in range(N):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i + 1
                #print(loop)
                break


        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    else:        
        #print(loop)        
        Y = K%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y-1-loop]

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
loc = 0
h = []
circle_start = None

for _ in range(k):
  loc = a[loc] - 1
  if loc in h:
    circle_start = loc
    break
  h.append(loc)
if circle_start is None:
  print(a[loc])
else:
  i = h.index(circle_start)
  #print(h, circle_start, i, h[:i], h[i:])
  amari = (k + 1 - len(h[:i])) % len(h[i:])
  print(a[h[i:][amari]])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
d=[0]
w=set([0])
x=0
for i in range(n):
    if a[x]-1 not in w:
        d.append(a[x]-1)
        w.add(a[x]-1)
        x=a[x]-1
    else:
        f=a[x]-1
        break

#print(d)
if k<len(d):
    print(d[k])
else:
    x=k-len(d)
    y=d.index(f)
    z=len(d)-y
    #print(d,f,x,y,z)
    #exit()
    print(d[y+x%z]+1)

Submission status: Wrong Answer

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

ans = d[K % N]
print(ans)

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<20):
    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)
        j_list.append(num)
    amari = (K - (hajimari-1) )%(owari-hajimari+1)
    ans = (hajimari-1)+amari
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict

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

returned = None
i = 0
aa = 0
visited = OrderedDict([(0, 0)])
while i < n:
    aa = a[aa] - 1
    i = i + 1
    if aa in visited:
        returned = (visited[aa], i)
        break
    visited[aa] = i

loop = returned[1] - returned[0]
if k >= len(visited):
    k = (k - returned[0]) % (returned[1] - returned[0])
    if k < returned[0]:
        k = k + returned[0]

print(list(visited.keys())[k] + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
town_check = {0}
town_order = [0]
town = 0
check1 = True
while check1:
    next_town = A[town] - 1
    if next_town not in town_check:
        town = next_town 
        town_check.add(town)
        town_order.append(town)
    else:
        loop = (next_town, town)
        check1 = False
        
if K-1 <= len(town_order):
    print(town_order[K-1])
else:
    a = town_order.index(loop[0])
    b = len(town_order) - 1
    c = b - a + 1
    print(town_order[a + (K-a)%(c)]+1)

Submission status: Wrong Answer

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()

nokori=(K-startat) % loopcount

#print(startat)
#print(nokori)
#print(loopcount)

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x) for x in stdin.readline().rstrip().split()]
    
    X=1
    if N < K :
    	K = K % N
    for i in range(K):
        X = A[X-1]
    
    print(X)
    
main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
q=[1]
from collections import Counter
d=Counter()
d[1]=1
while True:
    q.append(a[i])
    if d[a[i]]==1:
        break
    else:
        d[a[i]]+=1
    i=a[i]-1
l=0
k+=1
for i ,j in enumerate(q):
    if j==q[-1] :
        l=i
        break
if k>len(q[:l]):
    if q[-1]!=1:
        k-=len(q[:l])-1
    k%=len(q[l:])
    print(q[l+k])
else:
    print(q[k])

Submission status: Wrong Answer

Solution code:
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
r = ans[key:]
mod = (k-key)%len(r)
print(1)

Submission status: Wrong Answer

Solution code:
import sys

n, k = map(int, input().split())
a = []
a = list(map(int, input().split()))
a = [1] + a
# print(a)

i = 0
ans = [1]

check = 1
for j in range(len(a)):
    if (len(ans) != len(set(ans))):
        check = 0
        break
    else:
        ans.append(a[(ans[i])])
        i += 1
    if (j == k):
        answer = ans[-1]

if (check == 1):
    print(answer)
    sys.exit()

# print(ans)
delcount = 0
for _ in range(len(ans)):
    if ans[-1] == ans[0]:
        ans.pop(-1)
        break
    else:
        ans.pop(0)
        delcount += 1

#print(ans, delcount)
print(ans[(k-delcount) % len(ans)])

Submission status: Wrong Answer

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

t = [int(s) for s in stdin.readline().rstrip().split()]
n = t[0]
k = t[1]
Ak = [int(s) for s in stdin.readline().rstrip().split()]

data = [1]
tmp = 0
for el in Ak:
    data.append(Ak[tmp])
    tmp = Ak[tmp]-1
used = []
head = 1
mark = [int(s) for s in '0'*(n+1)]
for el in data:
    if (mark[el-1] == 1):
        head = el
        break
    mark[el - 1] = 1
    used.append(el)
key = used.index(head)
k -= key
print(used[key+k % (len(used) - key)])

Submission status: Wrong Answer

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

Arrival = [0]*N
NowTown = 0
Arrival[NowTown] = 1
ArrivalStep = [0]*N

Step = 0

while True:
    Step += 1
    NowTown = A[NowTown]
    if Arrival[NowTown] == 1:
        break
    ArrivalStep[NowTown] = Step
    Arrival[NowTown] = 1

RoopNumper = Step - ArrivalStep[NowTown]

Rest = K-Step

Rest %= RoopNumper

if Rest == 0:
    print(NowTown+1)
else:
    for i in range(Rest):
        NowTown = A[NowTown]
    print(NowTown+1)

Submission status: Wrong Answer

Solution code:
def L_search(A,key):
    for i in range(len(A)):
        if A[i] == key:
            return i

def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    Loop_set,Loop_list = set([1]),[1]
    pt = 1 #最初の町
    while True:
        pt = A[pt-1]
        if pt not in Loop_set:
            Loop_set.add(pt)
            Loop_list.append(pt)
        else:
            break

    gosa = L_search(Loop_list,pt)
    shuki = len(Loop_list) - gosa
    K -= gosa
    K %= shuki
    K += gosa
    print(Loop_list[K])

if __name__ == "__main__":
    main()

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])
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 = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]

dest = []

current = 0
next_dest = A[current]
count = 0
seen = set()

while not next_dest in seen:
    dest.append(next_dest)
    current = next_dest-1
    next_dest = A[current]
    seen.add(current)

dest.append(next_dest)
modulus = len(dest)

print(dest[(K - 1) % modulus])

Submission status: Wrong Answer

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, K = map(int, input().split())
    A_i = list(map(int, input().split()))
    B_i = []
    cycle_list = []
    
    cycle = 0
    diff = 0
    counter = 0
    machi = 1
    while counter < N:
        if machi in B_i:
            diff = B_i.index(machi)
            cycle = counter - diff
            cycle_list = B_i[diff:]
            break
        else:
            counter += 1
            B_i.append(machi)
            machi = A_i[machi - 1]
            
    mod_K = (K - diff) % cycle

    print(cycle_list[mod_K])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
no_towns, no_moves = list(map(int, input().split()))

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

pos = moves[0]
chain_len = len(set(moves))-1
chain = [moves[min(moves)-1]]
while len(chain) < chain_len+1:
    pos = moves[pos-1]
    chain.append(pos)
chain = chain[1:]

current_pos = moves[0]
while current_pos != chain[0]:
    current_pos = moves[current_pos-1]
    no_moves -= 1

extra_pos = no_moves % chain_len

current_pos = chain.pop()

for i in range(extra_pos):
    current_pos = moves[current_pos-1]

print(current_pos)

Submission status: Wrong Answer

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

mod = [0]
idx = A[0]

while True:
    if idx in mod:
        idx = mod.index(idx)
        break
    mod.append(idx)
    idx = A[idx]

r = len(mod) - idx
k -= idx
mod = mod[idx:]

print(mod[k % r] + 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.insert(0,0)
root=[]
current=1
i=0
while True:
    if i==K:
        ans=current
        break
    else:
        root.append(current)
        current=A[A[current]]
        i+=1
        if current in root:
            index=root.index(current)
            roop=len(root)-index
            x=(K-index)%roop
            ans=root[index+x-1]
            break

#print(index)
#print(root)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
o=a[0]-1
for i in range(k):
    o=a[o]-1
print(o)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

s = 1
w = [1]
y = defaultdict(lambda: 0)
y[1] = 1

flag = 0
zen = 0
for i in range(k):
#	print(s,a[s-1])
	if(y[a[s-1]] == 1):
#		print("w",w,a[s-1])
		flag = 1
		if(a[s-1] == 1):
			break
		else:
			for j in range(len(w)):
#				print("aa",w[j],a[s-1])
				if(w[j] == a[s-1]):
					zen = j
					w = w[j-1:]
					break
		break
	s = a[s-1]
	y[s] = 1
	w.append(s)

#print(s,w)

if(flag==0):
	print(s)
else:
	print(w[((k - zen) % (len(w)))])
#	print(w,zen,((k - zen) % (len(w))))

Submission status: Wrong Answer

Solution code:

#import numpy as np
from functools import *
import sys
sys.setrecursionlimit(300000)
input = sys.stdin.readline




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




def search(x,step,vnode):
    global L,visited
    
    #print(visited,x)
    visited[x]=step
    vnode.append(x)
    
    
        
    if step==K+1:
        print(step)
        sys.exit(0)
        
    if L[x]==x:
        print(x)
        sys.exit(0)
        
    if visited[L[x]]>=0:
        C=step+1-visited[L[x]]
        k=(K%(2*C)-visited[L[x]])%C
        if k<0:
            k=(k+C)%C
            
        #print(vnode)
        #print(k,L[x],visited[L[x]])
        vnode.append(L[x])
        print(vnode[visited[L[x]]+k])
               
        #print(step,x,visited)
        sys.exit(0)
        return
    
    
    search(L[x],step+1,vnode)
    
    
    

N,K=acinput()
L=[0]
L.extend(acinput())

visited=[-1]*(N+1)
#visited[1]=1
search(1,0,[])

#4 500000000000000000000000001
#2 3 4 4

Submission status: Wrong Answer

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

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


if N < K:
    s = K % cnt  + dic[y] 
    #x = y
else:
    s = K
    x = 1
for i in range(s):    
    y = A[x-1]
    x = y
print(x)