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

#3241
#1-3-4-1-3-4

#652532
#1-6-2-5-3-2
already=[1]
tail=n+1
start=a[0]
for i in range(k):
    now=a[already[-1]-1]
    if now in already:
        start=now
        tail=i+2
        print("start",start)
        print("tail",tail)
        break

    already.append(now)

head=1
now=1
for i in range(k):
    if now==start:
        head=i+1
        print("head",head)
        break
    now=a[now-1]

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

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
    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
    if loop == 0:
        ans = tele[ans] - 1
    for i in range(loop+1):
        ans = tele[ans] - 1
print(ans+1)

Submission status: Wrong Answer

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

temp_l = [1]
now = 1
for i in range(N):
    pre = now
    now = A[pre-1]
    if now in temp_l:
        end = now
        temp = i+1
        break
    else:
        temp_l.append(now)

s_index = temp_l.index(end)
repeat = temp - s_index
K -= s_index
K = K % repeat

print(temp_l[s_index:][K])

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
check=[0]*(N)
check[0]=1
D=[]
C=[0]
p=0
for i in range(N):
  pp=A[p]-1
  if check[pp]==0:
    check[pp]=1
    C.append(pp)
    p=pp
  else:
    roop=pp
    break
rs=C.index(roop)
if K<=rs-1:
  print(C[K]+1)
else:
  K-=(rs-1)
  F=C[rs:]
  e=K%(len(F))
  print(C[e-1]+1)

Submission status: Wrong Answer

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

l = []
used = [0 for _ in range(n)]

c = 0
flag = True
while 1:
    if not flag:
        l.append(c)
    if used[c] == 1 and l:
        break
    if not flag:
        used[c] = 1
    c = a[c]
    flag = False

p = 0
length = len(l)
b = []
for i in range(length):
    if l[i] == l[-1]:
        b = l[:i]
        l = l[i:-1]
        p = i
        break

if len(b) >= k:
    print(b[k - 1])
else:
    print(l[(k - p - 1) % len(l)] + 1)

Submission status: Wrong Answer

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

MyList = [int(n) for n in MyList]

ANS = 0
visted = []

def Myfunc(A):
    return MyList[A] - 1

for i in range(K):
    ANS = Myfunc(ANS)
    if ANS in visted:
        print(visted)
        beANS = K % (len(visted) - visted.index(ANS))
        ANS = visted[beANS-1]
        print(ANS+1)
        break
    visted.append(ANS)
    if i +1 == K:
        print(ANS+1)

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
for i in range(k%mod):
    q.append(s)
    s = a[s-1]
q.reverse()
q.append(1)
print(q[0])

Submission status: Wrong Answer

Solution code:

data = input().split(" ")
N = int(data[0])
K = int(data[1])
tele = input().split(" ")
teles=[]
for t in tele:
    teles.append(int(t))
now = 1
for _ in range(K):
    now = teles[now-1]
print(now+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
li=list(map(int, input().split()))
s=m%n
ans=li[li[s-1]-1]
print(ans)

Submission status: Wrong Answer

Solution code:
# D - Teleporter

N, K = map(int, input().split())
A = [int(a) for a in input().split()]

nxt = 1
visited_num = [1]
visited = [False] * N
visited[0] = True
first = []
repeat = []
for _ in range(N):
    nxt = A[nxt - 1]
    if visited[nxt - 1]:
        i = visited_num.index(nxt)
        if i != 0:
            visited_num.append(nxt)
            first = visited_num[:i + 1]
            repeat = visited_num[i + 1:]
        break
    else:
        visited_num.append(nxt)
        visited[nxt - 1] = True

if 0 == len(repeat):
    ans = A[K % len(visited_num)]
else:
    ans = repeat[(K - len(first)) % len(repeat)]
print(ans)

Submission status: Wrong Answer

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

visit = [] #訪れる街
loop_point = 0 #以後繰り返し
now = 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()))
graph = [0] * N
point = 1
count = 0
while count < K:
  graph[point-1] += 1
  if graph[point-1] >= 3:
    stock = K - sum(graph)-1
    stock2 = sum([1 for i in graph if i >= 2])
    K = stock % stock2
    count = 0
    point = A[point-1]
    break
  point = A[point-1]

for _ in range(K+1):
  point = A[point-1]
print(point)

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
l = list(map(int, input().split()))
count = 0
li = [0 for _ in range(n)]
b = 1
for i in range(1,n + 1):
#     print(b)
    if li[b-1] != 0:
        break
    li[b-1] = i
    b = l[b-1]
    count += 1
#     print('count',count)


d = count - li[b-1] + 1
intercep = li[b-1] -1
if k <= li[b-1]:
    ans = li.index(li[b-1]) + 1
    print(ans)
    sys.exit()
    
mod = (k - intercep) % d
b = 1
for i in range(intercep + mod):
#     print('b',b)
    b = l[b-1]
print(b)

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

def has_duplicates(seq):
    return len(seq) != len(set(seq))

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

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    lenA = len(A)

    tmp = []
    path = []
    setpath = set([])
    cnt = 0
    i = 1
    for _ in range(1, lenA + 1):
        setpath.add(i)
        path.append(i)
        if len(setpath) != len(path):
            path.pop()
            itmp = path.index(i)
            break
        i = A[i-1]

    lenpath = len(path)
    if K >= lenpath:
        jtmp = (K-itmp)%(lenpath-itmp)
        ans = path[itmp+jtmp]
    else:
        ans = path[K-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

flag = [0 for _ in range(N)]

tmp = 1
i = 1
flag[0] = 1
while(True):
    tmp = A[tmp-1]
    i += 1
    if (flag[tmp-1] != 0):
        break
    flag[tmp-1] = i

pre = flag[tmp-1] - 1
cyc = i - flag[tmp-1]
index = (K - pre) % cyc
print(A[flag.index(pre+index)])

Submission status: Wrong Answer

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

for i in range(100) :
    l = a[l]-1 
    count = +1
    if l == 0 :
        x = k%count
        for i in range(x) :
            l = a[l]-1
            
        print(l+1)
        exit()
print(l+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ai = [int(i) for i in input().split()]

li = [-10]*(n)
tmp = ai[0]

st = -1

for i in range(n+1):
    #tmp = ai[i]
    if li[tmp-1] != -10:
        prd = i - li[tmp-1]
        st = li[tmp-1]+1
        break
    li[tmp-1] = i
    tmp = ai[tmp-1]
    #print(tmp)
    
#print(li,prd,tmp)
pat = 0
if ai[tmp-1] == tmp:
    pat = 1

#print(prd,li,st,pat)
#exit()

if pat == 1:
    print(tmp)
    exit()
elif k <= st:
    for i in range(n):
        if li[i] == k-1:
            print(i+1)
            exit()
else:
    idx = (k-st) % prd
    #print(idx,idx,idx)
    for i in range(n):
        if li[i] == st-1+idx:
            print(i+1)
            exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[]
count=0
i=0
for n in range(10**6):
    if A[i] in l:
        count=l.index(A[i])
        break
    else:
        l.append(A[i])
        i=A[i]-1
        
print(A[(K-count)%(n)])

Submission status: Wrong Answer

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

v = [0]
s = 0
idx = 0
loop = 1
last = 0

for _ in range(k):
    vi = a[last] - 1
    if not vi in v:
        last = vi
        s += 1
        v.append(vi)
    else:
        s += 1

        idx = v.index(vi)
        loop = s - idx
        break

remain = k - s

print(v[idx:][remain % loop] + 1)

Submission status: Wrong Answer

Solution code:
import sys
import os

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

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

    d=1
    # for i in range(K):
    #     # print(d)
    #     d=A[d-1]
    li=[1]
    while True:
        d=A[d-1]
        if d in li:
            break
        li.append(d)

    d_p=li.index(d)
    # print(d_p)
    roop=len(li)-d_p
    # print(K-d_p)
    # print(len(li))
    # if len(li)>K:
    #     # print(K-1)
    #     print(li[K-1])
    # else:
    # print((K-d_p)%roop)
    print(li[(K-d_p)%roop+d_p])

    # print(d)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
k += 1
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)
if k<= len(be):
  print(be[k])
else:
  w = k-len(be)
  u = w%len(r)-1
  print(r[u]+1)

Submission status: Wrong Answer

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


route_dict = {k+1 :v for k,v in enumerate(an)}
route_dict

current_loc = 1
count = 1
count_last_visit = {i+1:0 for i in range(n)}

returned = False

count_last_visit[current_loc] = count
while not returned:
#     print(count)
    next_loc = route_dict[current_loc]
    if count_last_visit[next_loc] != 0:
        cycle_interval = count - count_last_visit[next_loc]
        returned = True
    else:
        count += 1
        current_loc = next_loc
        count_last_visit[next_loc] = count

len_pathToCycle = count - (cycle_interval + 1)


loc_at_k = list(range(len_pathToCycle+1, count + 1))[(k - len_pathToCycle) %  (cycle_interval + 1)]
print({v:k for k, v in count_last_visit.items()}[loc_at_k])

Submission status: Wrong Answer

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

new = A[0] - 1
T = [0]
while not new in T:
    T.append(new)
    new = A[new] - 1

index = T.index(new)
print(T[((K - index) % (len(T) - index)) + index] + 1)

Submission status: Wrong Answer

Solution code:
import sys

n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))

visited = list()
i = 0
cnt = 0
while True:
    if cnt == k:
        print(i)
        sys.exit()
    if i in visited:
        break
    visited.append(i)
    i = a[i]
    cnt += 1

si = visited.index(i)
loop = visited[si:]
n_loop = len(loop)
left_k = k - cnt
print(loop[left_k % n_loop] + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
i_to_a = {i+1: int(a) for i, a in enumerate(input().split())}


seen = []
next_v = 1
loop_start_index = 0
while True:    
    if next_v in seen:
        break
    seen.append(next_v)
    next_v = i_to_a[next_v]
loop_start_index = seen.index(next_v)
to_loop = seen[:loop_start_index]
additional = seen[:loop_start_index]
loop = seen[loop_start_index:]

additional_l = len(additional)
loop_l = len(loop)

s = (k - additional_l) % loop_l

if k > additional_l:
    print(seen[additional_l + s])
else:
    print(seen[s])

Submission status: Wrong Answer

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

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

# print(N, K)
# print(A_list)

keiro = [1]
visit_town = [0]*N

town = 1
c = 1
while True:
    visit_town[town - 1] = c
    c += 1
    town = A_list[town - 1]
    # print('town', town)

    if len(keiro) > 1 and visit_town[town-1] != 0:
        break
    keiro.append(town)

# print(keiro)
# print(visit_town)
# print(town)


first_visit = keiro[:visit_town[A_list[keiro[-1]-1]-1]-1]
loop_visit = keiro[visit_town[A_list[keiro[-1]-1]-1]-1:]

# print(first_visit)
# print(loop_visit)
print(keiro[len(first_visit) + (K - len(first_visit)) % len(loop_visit)])

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = map(int, input().split())
An = list(map(int, input().split()))
root = deque([1])
i = 0
dup = 1
cnt = 1
for _ in range(n):
    temp = An[i]
    if temp in root:
        dup = temp
        break
    root.append(temp)
    i = temp - 1
    cnt += 1
index = root.index(dup)
print(root[(k-index) % (cnt-index) + index] if index != cnt-1 else dup)

Submission status: Wrong Answer

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

ans1 = [1]
temp = 0
count = 0
while(True):
    ans1 += [a[temp]]
    temp = a[temp]-1
    if len(ans1) !=  len(set(ans1)):
        break
    count += 1
    if count == m:
        print(ans1[len(ans1)-1])
        exit()


num1 = ans1.index(temp+1)
ans2 = ans1[num1:len(ans1)-1]
print(ans2)
num = len(ans2)

print(ans2[(m-num1)%num])

Submission status: Wrong Answer

Solution code:
int1 = lambda s: int(s)-1

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

X = []
visited = set()
now = 0
while now not in visited:
    visited.add(now)
    X.append(now)
    now = A[now]

if K < len(X):
    print(X[K])
else:
    s = X.index(now)
    print(X[s + ((K - s) % (len(X) - s))] + 1)

Submission status: Wrong Answer

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

lili = [1]
while li[(lili[-1] - 1)] not in lili:
    lili.append(li[(lili[-1] - 1)])

newlist = lili[lili.index(li[lili[-1]-1]):]

print(newlist[((k - len(lili)) % len(newlist))])

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)

    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())
list_A = list(map(int, input().split()))
list_MAP = []
set_MAP = set()
x = 1
while True:
    list_MAP.append(x)
    set_MAP.add(x)
    x = list_A[x-1]
    if x in set_MAP:
        break


q = len(list_MAP) - list_MAP.index(x)
p = k - list_MAP.index(x)

if p < 0:
    print(list_MAP[k-1])
else:
    list_MAP = list_MAP[list_MAP.index(x):]
    print(list_MAP[p % q])

Submission status: Wrong Answer

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

x=1
l=[1]
for i in range(n):
    y=a[x-1]
    if y not in l:
        l.append(y)
        x=y
    else:
        x=y
        break

j=l.index(x)
mod=(k-len(l))%(len(l)-j)
li=l[j:]
print(li[mod])

Submission status: Wrong Answer

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

c = 1
city = 1
s = set()
s.add(city)

while True:

  city = A[city - 1]
  if city in s:
    print(A[K%c +1])
    exit()
  s.add(city)
  if c == K:
    print(city)
    exit()

  c += 1

Submission status: Wrong Answer

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

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


if(len(set(a))== len(a)):
    if(k<10**9):
        sen=1
        for i in range(k):
            sen=a[sen-1]
        print(sen)
        exit()
    else:
        sen=1
        for i in range(n-k):
            sen=a.index(sen)
        print(sen)
        exit()
s=[]
sen=1
for i in range(k):
    if(sen in s):
        break
        
    else:
        s.append(sen)
        sen=a[sen-1]

#print(s)

k=k%len(s)
if(sen!=1):
    k-=1
#sen=1
for i in range(k):
    sen=a[sen-1]
print(sen)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N,K=map(int,input().split())
A=input().split()
A=[int(a) for a in A]

P=0
L=[0]*int(N+1)
D=[0]*int(N)
c=0
R=0
while(1):
    c+=1
    P=A[P]-1
    L[c]=P
    if D[P]>0: 
        R=D[P]
        break
    D[P]=c
    if P==0: break
    

#print(L)
K-=R
K%=(c-R)
#print(R,K)
ans=L[R+K]+1

print(ans)

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)
if k<=n:
    town = 1
    ans = ls[town - 1]
    while k > 0:
        town = ans
        ans = ls[town - 1]
        k -= 1
    print(ans)
else:
    times = 0
    town = 1
    while town not in visited:
        visited[town] = times
        times += 1
        town = ls[town - 1]

    period = times - visited[town]
    iter_req = visited[town]
    k -= visited[town]
    #print(period)
    if k > period:
        k %= period
    iter_req += k
    #print("Starting town is ",town)
    #print("iter_req is",iter_req)
    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:
# coding: utf-8

N, K = map(int,input().split())
A = list(map(int,input().split()))
town = [False] * (N+1)
current = 1

cnt = 0

for i in range(N):
    if town[current]:
        current = A[current-1]
        break
    else:
        town[current] = True
        cnt += 1
        current = A[current-1]

town2 = [False] * (N+1)
T = 0
for i in range(N):
    if town2[current]:
        current = A[current-1]
        break
    else:
        town2[current] = True
        T += 1
        current = A[current-1]
if current != 1:
    current = A[current-1]
if K <= cnt:
    c = 1
    for i in range(K):
        c = A[c-1]
        #print(c)
    ans = c
else:
    K -= cnt
    K %= T
    c = current
    for i in range(K):
        c = A[c-1]
        #print(c)
    ans = c
#print(cnt,T)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
arr = list(map(int, input().split()))
town = [1]
nxt = arr[0]
while nxt not in town:
    town.append(nxt)
    nxt = arr[nxt - 1]

circle_idx = town.index(nxt)
circle_size = len(town) - circle_idx
circle = town[circle_idx:]

if K <= len(town):
    print(town[(K - circle_idx) % circle_size])
else:
    print(circle[(K - circle_idx) % circle_size])

Submission status: Wrong Answer

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

N = NK[0]
K = NK[1]
town= 1
list = []
for i in range(N+1):
  town = A[town-1]
  list.append(town)
  if i >0 and town == list[0]:
    break
list1 = list[:-1]
 
d = K % len(list1)

print(list1[d-1])

Submission status: Wrong Answer

Solution code:
import bisect
n,k = map(int,input().split())
A = list(map(int,input().split()))
memo = [0 for _ in range(n+1)]
dict = {i:0 for i in range(1,n+1)}
memo[0] = 1
dict[1] = 1
i = 1
j = 1
loopnum = 0
ans = 0
while(j <= k):
    if dict[A[i-1]] == 1:
        loopnum = A[i-1]
        break
    a = A[i-1]
    memo[j] = a
    dict[a] = 1
    i = a
    j += 1
    ans = a
if loopnum != 0:
    if loopnum == 1:
        left = 0
    else:
        left = bisect.bisect_left(memo,loopnum)+1
    f = j - left
    diff = max(left-1,0)
    index = (k - diff) % f + diff
    ans = memo[index]

print(ans)

Submission status: Wrong Answer

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

towns = [0] * N

town = 1
i = 0
while True:
    if towns[town-1] < 2:
        
        if towns[town-1] == 0:
            i += 1
            
        towns[town-1] += 1
        town = A[town-1]
    else:
        s = town
        break
    
loop = towns.count(2)
i = i - loop
k = (K - i) % loop

if K < i:
    for _ in range(k):
        town = A[town-1]
    print(town)

if k == 0:
    print(s)
    exit()

for _ in range(k):
    town = A[town-1]
print(town)

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy

# import heapq
# from collections import deque
# import decimal

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n, k = ns()
    a = na()
    a = [ai - 1 for ai in a]

    junkan_point = 0
    visited = [0, a[0]]
    next = a[0]

    visited_set = set(visited)

    for _ in range(n+5):
        if a[next] in visited_set:
            junkan_point = a[next]
            break
        else:
            visited.append(a[next])
            visited_set.add(a[next])
            next = a[next]

    tmp_idx = visited.index(junkan_point)
    junkan_visited = visited[tmp_idx:]

    tmp = len(visited) - len(junkan_visited)
    if k < tmp:
        print(k)
    else:
        idx = (k - tmp) % len(junkan_visited)
        print(junkan_visited[idx] + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

city_idx = 0
reached_city = [0]
for k in range(N):
    city_idx = A[city_idx] - 1
    if city_idx in reached_city:
        offset = reached_city.index(city_idx)
        loop_step = k - offset + 1
        break
    reached_city.append(city_idx)

if K < offset:
    print(reached_city[K])
else:
    pos = (K - offset) % loop_step
    print(reached_city[offset + pos] + 1)

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, input().split())
if K == 0:
  print(1)
  sys.exit()
A = list(map(int, input().split()))
now = 1
count = 0

now = A[now-1]
route = [now]
count += 1

def check(now, route, count, K):
  now = A[now-1]
  if now in route:
    return 0   
  else:
    route.append(now)
    count += 1

check(now, route, count, K)

print(route[(K % count)-1])

Submission status: Wrong Answer

Solution code:
import sys
from random import choice,randint
inp=sys.stdin.readline
out=sys.stdout.write
flsh=sys.stdout.flush
 
sys.setrecursionlimit(10**9)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def MI(): return map(int, inp().strip().split())
def LI(): return list(map(int, inp().strip().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]
def LI_(): return [int(x)-1 for x in inp().strip().split()]
def LF(): return [float(x) for x in inp().strip().split()]
def LS(): return inp().strip().split()
def I(): return int(inp().strip())
def F(): return float(inp().strip())
def S(): return inp().strip()
def pf(s): return out(s+'\n')
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n,k=MI()
    a = LI()
    
    d = dict()
    d[1]=1
    nxt = 2
    cnt = 0
    prev =1 
    while True:
        prev = a[prev-1]
        if prev in d:
            cnt = nxt-d[prev]
            break
        d[prev]=nxt
        nxt+=1
    if k>d[prev]-1:
        k-=(d[prev]-1)
    k= k%cnt
    while k>0:
        prev = a[prev-1]
        k-=1
    print(prev)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

loop = [1]
P = 1
for i in range(2*(10**5)):
  G = A[P-1]
  if G in loop:
    Comeback = loop.index(G)
    length = len(loop)
    looplen = length - Comeback
    Shippo = Comeback

    break
  else:
    loop.append(G)
    P = G

if K <= Shippo-1:
  ans = K
else:
  ans = ((K+1)-Shippo) % looplen + Shippo-1

print(loop[ans])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
roop = [1]
while(len(set(roop)) == len(roop)):#O(n)
    roop.append(a[roop[-1]-1])
ii = 0
last = roop.pop(-1)
while (roop[ii] != last):
    ii += 1
first = ii
if(first != 0):
    atama = len(roop) - first
    gachiroop = roop[atama-1:]
    print(gachiroop[(k-atama+1)%len(gachiroop)])
else:
    print(roop[k%len(roop)])

Submission status: Wrong Answer

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

visited_count = [0]*N

cnt = 1
next_visit = A[0] - 1
for _ in range(K):
    cnt += 1
    if not visited_count[next_visit]:
        visited_count[next_visit] = cnt
        next_visit = A[next_visit] - 1
    else:
        c2 = cnt - visited_count[next_visit]
        c1 = visited_count[next_visit] - 1
        place_count = ((K-c1) % c2) + c1 + 1
        place = visited_count.index(place_count) + 1
        print(place)
        exit(0)

place_count = K
place = visited_count.index(place_count) + 1
print(place)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
a=[0]+p
flag=0
bef=1
town=[1]
town_set=set(town)
cnt=0
while flag==0 and cnt<k:
    if not a[bef] in town_set:
        town.append(a[bef])
        town_set.add(a[bef])
        bef=a[bef]
    else:
        flag=1
        memo=a[bef]
        print(cnt)
    cnt+=1
if flag==0:
    print(bef)
    exit()
for j in range(len(town)):
    if town[j]==memo:
        num=j
x=len(town)-num
y=(k-num)%x
print(town[num+y])

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    b_lst = [False] * N
    b_lst[0] = True
    ans = 1
    save = -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 -= (counnnt-counter)
    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()))
B = [0]*N
B[0] = 1
num1 = 10**6
num2 = 0
x = A[0]
for i in range(len(A)*2):

    if B[x-1] == 0:
        B[x-1] = 1
    elif B[x-1] == 1:
        num1 = min(i+1,num1)
        B[x-1] = 2
    elif B[x-1] == 2:
        num2 = i+1
        break
    else:
        None
    x = A[x-1]
    #print(x)
    #print(B)
#print(num1,num2)
x = A[0]
if num1 >= K:
    for j in range(K-1):
        x = A[x-1]
    print(x)
else:
    for j in range((K-num1)%num2-1):
        x = A[x-1]
    print(x)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K=list(map(int,(stdin.readline().strip().split())))
A=list(map(int,(stdin.readline().strip().split())))
cycle=[1]
count=[0]*N
tele=0
count[tele] += 1
while(True):
    cycle.append(A[tele])
    tele=A[tele]-1
    count[tele] += 1
    if count[tele] == 2:
        break
a=cycle.index(tele+1)
num=len(cycle[a:-1])
num=(K-len(cycle[:a]))%num
print(cycle[num+len(cycle[:a])])

Submission status: Wrong Answer

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

cnt = 0
dic = {1 : 1}
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y in dic:
        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)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
arr = list(map(int,input().split()))
visit_arr = [0]*N
visit_arr[0] = 1
idx = 1
for i in range(K-1):
  next_idx = arr[idx-1]
  if visit_arr[next_idx-1]!=0:
    break
  else:
    visit_arr[next_idx-1] = i+2
    idx = next_idx

loop = visit_arr[idx-1]-visit_arr[next_idx-1]+1
vis_num = visit_arr[next_idx-1]+(K+1-visit_arr[next_idx-1]+1-1)%loop
print(visit_arr.index(vis_num)+1)