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:
def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    D = [0] * (n+1)
    now = 1
    for i in range(1, k+1):
        tmp = A[now-1]
        if not D[tmp]:
            now = tmp
            D[now] += i
        else:
            mod = D[tmp] - 1
            loop = i - D[tmp]
            print(D.index((k - mod) % loop + mod) if loop > 1 else tmp)
            return
    print(now)
main()

Submission status: Wrong Answer

Solution code:
n,k =map(int,input().split())
a=list(map(int,input().split()))
a = [-1] + a
def next(i):
    return a[i]

z = [0] * (n+1)

place = 1
T = 0
interval = 0
if k < n:
    for i in range(k):
        place = next(place)
    print(place);exit()

for i in range(n):
    if z[place] != 0:
        interval = T - z[place]
        break
    place = next(place)
    z[place] = T
    T += 1

m = (k-z[place])%interval

for i in range(m):
    place = next(place)
print(place)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))
ans = 1
n = 1
check = 1
city = [-1 for i in range(N+1)]
city[1] = 0
roop = 1
i = 0
while(check == 1):
  n = A[n]
  i = i + 1 
  if city[n] != -1:
    city[n] = i
  else:
    roop = i - city[n] + 1
    check = 0
    
roop_num = (K - i)%roop
for i in range(roop_num):
  n = A[n]
print(n)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
doko = 0
for i in range(K):
    if A[doko] <= 0:
        saishou = min(A)
        loop = A[doko] - saishou + 1
        hajimarimade = A[doko]
        K += hajimarimade
        print(A)
        print(A.index(-1*(K % loop)+hajimarimade)+1)
        exit()
    if i == K - 1:
        print(doko+1)
        exit()
    hoji = doko
    doko = A[doko] - 1
    A[hoji] = (-1)*i

Submission status: Wrong Answer

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

check = {}
i = 0
number = []
next = a[0]
start = 0

for i in range(1,n+1):
    if next in check:
        start = number.index(next)
        rep = number[number.index(next):]
        break
    else:
        check[next] = 1
        number.append(next)
        next = a[next-1]

if k <= start:
    print(number[k])
else:
    c = len(rep)
    print(rep[(k-start-1)%c])

Submission status: Wrong Answer

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

if a[0] == 1:
    print(1)
    exit()

while True:
    x = a[l[-1]-1]
    if len(l) > 1 and x in l[:-1]:
        break
    l.append(x)

l.pop(0)
i = l.index(x)
l1 = l[:i-1]
l2 = l[i-1:]

if k < len(l1):
    print(l1[k])
else:
    print(l2[(k-len(l1))%len(l2)-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):
    if ANS in visted:
        beANS = K % len(visted)
        ANS = visted[beANS-1]
        print(ANS+1)
        break
    else:
        ANS = Myfunc(ANS)
        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()))
def tele(i):
    ans = A[i-1]
    return ans
i = tele(1)
for j in range(K-1):
    i = tele(i)

Submission status: Wrong Answer

Solution code:
def F(k):
    ans = 1
    for _ in range(k): ans = A[ans-1]
    return ans

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

dp = [0]*n
if A[0] == 1: exit(print(1))

p = A[0]-1
dp[0]+=1
count = 1
stop = False
while True:
    if p == A[p]-1:
        stop = True
        break
    p = A[p]-1
    if dp[p] > 0:
        count += 1
        break
    count += 1
    dp[p]+=count
dp[0]=0
if (stop and k >= count) or k == count: exit(print(p))
if k > count:
    k = k % (count-dp[p])
print(F(k))

Submission status: Wrong Answer

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

curr = 0
cnt = 0
k += 1
while True:
  if b[curr] != -1:
    loop = cnt - b[curr]
    break
  b[curr] = cnt
  cnt += 1
  k -= 1
  curr = a[curr] - 1

mod = k % loop
for i in range((mod-1) % loop):
  curr = a[curr] - 1

print(curr + 1)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split()]
A = [0] + [int(i) for i in input().split()]
B = [1]
while(True):
    tmp = A[B[-1]]
    if not tmp in B:
        B.append(tmp)
    else:
        head = B.index(tmp)
        break
K = (K - len(B[:head]))  % len(B[head:])
print(B[head:][K])

Submission status: Wrong Answer

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

dic = {1: 1}
dis = [1]
position = 0
for i in range(len(A_list)):
    if A_list[position] not in dic:
        dic[A_list[position]] = 1
        dis.append(A_list[position])

    else:
        dis.append(A_list[position])
        break
    position = A_list[position] - 1

# print(dic)
temp = dis.index(dis[-1])
loop = len(dis) - temp - 1

#print(dis, loop)
K -= len(dis) - (loop + 1)
# print(K)
if K <= 0:
    print(dis[K - 1])
else:
    loop_list = dis[temp:]
    # print(loop_list)
    loop_temp = K % loop
    # print(loop_temp)
    print(loop_list[loop_temp])

Submission status: Wrong Answer

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

B = [float("inf")] * N
l = 0
i = 0
p = 0
while l == 0:
  x = A[i] - 1
  #print(i, x)
  if x in B:
    l = 1
    index = B.index(x)
  else:
    B[p] = x
    p += 1
    i = x
    #print(B)
    
  if p == N:
    #roop = N
    if K >= N:
      K = K % N
    print(B[K] + 1)
    #print(K, B)
    quit()
      
    
roop = p - index
#print(p, index)
if K < (index + 1):
  ans = B[K]
else:
  K = K - (index + 1)
  #print(K)
  if K >= roop:
    K = K % roop
  #print(K, K + index, B)
  ans = B[K + index] + 1
  #print("K")
  
print(ans)

Submission status: Wrong Answer

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

already = [False for i in range(N)]
already[0] = True
tel = [0]

while True:
    tmp = A[tel[-1]]
    tel.append(tmp)
    
    if already[tmp]:
        tel.pop()
        break
        
    already[tmp] = True

for i in range(len(tel)):
    if tel[i] == tmp:
        s = i
        break

ans = (K - s) % (len(tel) - s)
print(tel[ans + s] + 1)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(len(a)):
    a[i] -= 1
pre = 0
d = []
for i in range(2*n+1):
    d.append(pre)
    pre = a[pre]
cnt = [0]*(n)
s = []

for i in range(len(d)):
    if cnt[d[i]]>=2 and cnt[d[i]]<3:
        s.append(d[i])
    cnt[d[i]]+=1

ff = s[0]
if ff == 0:
    k %= len(s)
    print(s[k]+1)
else:
    for i in range(len(a)):
        if a[i] == ff:
            check = i
            break
    k -= check
    k %= len(s)
    print(s[k]+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]):]
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]

#print(order, K)
print(order[K%len(order)]+1)

Submission status: Wrong Answer

Solution code:
def search(now, teleporter, limit):
    visit = [-1] * len(teleporter)
    cnt = 0
    
    while visit[now] == -1:
        if limit <= cnt:
            break
        
        visit[now] = cnt
        cnt += 1
        now = teleporter[now]
        
    
    return now, cnt, cnt - visit[now]
    

n, k = map(int, input().split())
a = [int(x) - 1 for x in input().split()]

start, initial_cnt, repeat_num = search(0, a, 10**5 + 1)

#print(start, initial_cnt, repeat_num)

if k < initial_cnt:
    r = k
else:
    r = (k - initial_cnt) % repeat_num

#print(r)

ans, _, _ = search(start, a, r)

print(ans + 1)

Submission status: Wrong Answer

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

for i,value in enumerate(A):
    graph[i+1]=value

stack=[1]
visited=[0 for i in range(N+1)]
C=[]
ct=0
while stack:
    x=stack.pop()
    if visited[x]==0:
        ct+=1
        visited[x]+=1
        stack.append(graph[x])
        C.append(x)
        
stack=[graph[C[-1]]]
visited=[0 for i in range(N+1)]
counter=0
while stack:
    x=stack.pop()
    if visited[x]==0:
        visited[x]+=1
        stack.append(graph[x])
        counter+=1
        
stack=[graph[C[-1]]]
for i in range((K-ct)%(counter)):
    x=stack.pop()
    stack.append(graph[x])

print(stack[0])

Submission status: Wrong Answer

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

neworder=[]
for i in range(n):
    if i==loop:
        loop=[order[i],i]
    if order[i]!=-1:
        neworder.append([order[i],i])


neworder=sorted(neworder)
#print(neworder)
if loop==-1 or loop==0:
    print(neworder[k%len(neworder)][1]+1)
else:
    k-=loop[0]
    neworder=neworder[loop[0]:]
    print(neworder[k%len(neworder)][1]+1)

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

    len_cycle = 0
    start = 0
    already_visit = set([0])
    loop_node = -1
    while True:
        start = A[start]
        len_cycle += 1
        if start in already_visit or len_cycle > N:
            loop_node = start
            break
        else:
            already_visit.add(start)

    dist_loop_node = 0
    if loop_node != 0:
        start = 0
        while True:
            start = A[start]
            dist_loop_node += 1
            if start == loop_node:
                break
        len_cycle -= dist_loop_node

    len_move = (K - dist_loop_node) % len_cycle
    start = loop_node
    for _ in range(len_move):
        start = A[start]
    print(start+1)
    pass

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=[]
b=0
a.append(1)
s=A[0]
a.append(s)
while a.count(1)!=2:
  t=A.index(s)
  s=A[t]
  a.append(s)
  b+=1
else:
  exit()
c=K%b
print(A[c])

Submission status: Wrong Answer

Solution code:
import math
from collections import defaultdict


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

    visited = [-1 for _ in range(N)]
    visited[0] = 0

    current = 0
    th = -1
    per_loop = -1
    for i in range(1, N+1):
        current = A[current] - 1

        if visited[current] >= 0:
            # print(i, current, visited[current])
            per_loop = i - visited[current]
            th = visited[current]
            break
        else:
            visited[current] = i

    current = 0
    # print(th, per_loop)

    if th < 0:
        num_rep = K
    else:
        num_rep = th + (K - th) % per_loop

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

    print(current + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = a[0]
b = [d]

for i in range(k):
    dn = a[d-1]
    if b[0]!=dn:
        d = dn
        b.append(d)
    else:
        break
        

if len(b)<k:
    print(d)
else:
    print(b[k%len(b)])

Submission status: Wrong Answer

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

edge=[0]*n

for i in range(n):
    edge[i]=a[i]

now=1
to=0
t=k
l=[]
line=0
while t!=0:
    to=edge[now-1]
    t-=1
    if to in l:
        break
    else:
        l.append(to)
        line+=1
    now=to

if t==0:
    print(l[-1])
o=l.index(to)
x=line-o
y=o
cnt=(k-y)%x
print(l[y+cnt-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(" "))
temp = [1]

a = list(map(int, input().split(" ")))
num = 0
while(True):
    if a[temp[-1] - 1] in temp:
        num = temp.index(a[temp[-1] - 1])
        break
    temp.append(a[temp[-1] - 1])
syuuki = len(temp) - num
k = (k - num) % syuuki + num
print(temp[k])

Submission status: Wrong Answer

Solution code:
import collections
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]
N = collections.Counter()
t = 0
cnt = 0
kcnt = 0
if k > n:
    for i in range(n):
        if N[t]:
            break
        N[t] = 1
        t = A[t]
        cnt += 1
    T = []
    for i in N:
        T.append(i)
    tmp = T.index(t)
    S = T[tmp:]
    s_len = len(S)
    k -= cnt
    ind = k % s_len
    print(S[ind] + 1)
else:
    for i in range(k):
        t = A[t]
        print(t+1)
    print(t+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
ex = [0]*n
now = 1
for i in range(k) :
    if ex[now-1] == 0 :
        ex[now-1] = i+1
        now = A[now-1]
    else :
        J = (k-i)%(i-ex[now-1]+1)
        for j in range(J) :
            now = A[now-1]
        print(now)
        exit()

Submission status: Wrong Answer

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

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
    done[0] = 1
    done_k[0] = 1
    i = 1
    q = A[0]
    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:
def main():

    import sys
    input = sys.stdin.readline

    N_ko, K_kai = map(int, input().split())
    
    teleport = list(map(int, input().split()))

    index = 0
    ctr = 0
    before_ctr = 0

    Visited = []
    Visited.append(ctr)

    for i in range(N_ko + 1):

        ctr += 1
        index = teleport[index] - 1
        Visited.append(index)
        
        if (index == 0):
            FLAG = 1
            break

        if (len(Visited) != len(set(Visited))):
            FLAG = 0
            break


    if (FLAG):
        mod = K_kai % ctr
        city = 0

        for i in range(mod):
            city = teleport[city] - 1

        ans = city + 1
    
    else:
        ans = 0


    # else:
    #     cnt = 0
    #     before_ctr = 0

    #     for i in range(N_ko + 1):
    #         cnt += 1
    #         current_index = teleport[index] - 1
            
    #         if (current_index == index):
    #             before_ctr = cnt
    #             break
        
    #     if (K_kai < ctr):
    #         city = 0
            
    #         for i in range(K_kai):
    #             city = teleport[city] - 1

    #         ans = city + 1

    #     else:
    #         ans = (K_kai - before_ctr) % (ctr - before_ctr)
    #         ans += 1
            

    # print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

c = list()
i = 1
while True:
    if a[i] in c:
        break
    else:
        c.append(a[i])
        i = a[i]

s = c.index(a[i])
print(c)
if k < s:
    print(c[k-1])
else:
    k -= s
    c = c[s:]
    ans = c[k % len(c) - 1]
    print(ans)

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

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

    print(l[(k-p)%len(l)-1]+1)

else:
    print(l[k - 1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
N =[0 for i in range(n)]
s = 1
cnt = 0
b = []
while True:
    if N[s-1]==1:
        rep = b.index(s)
        t = cnt-rep
        num = (k-rep)%t
        print(b[rep+num])
        break
    else:
        b.append(s)
        N[s-1] = 1
        s = a[s-1] 
        cnt +=1

Submission status: Wrong Answer

Solution code:
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

    N,K = LMIIS()
    A = LMIIS()
    visited = [False]*N
    dest = defaultdict(int)
    ep = 1
    for i,a in enumerate(A):
        dest[i+1] = a

    visited = [0]*(N+1)
    o = 1
    loop = 0
    visited[o] = loop

    while visited[dest[o]] == 0:
        loop += 1
        visited[dest[o]] = loop
        o = dest[o]
    
    # print('o=',o)
    preloop = visited[dest[o]]
    loop = loop - visited[dest[o]] + 1
    # print(preloop,loop)
    # print(visited)
    num_visit = (K - preloop) % loop
    origin = 1
    for i in range(preloop):
        origin = dest[origin]
    for i in range(num_visit):
        if dest[origin] == origin:
            break
        origin = dest[origin]
    print(origin)




    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[i-1 for i in A]
c=A[A[0]]
 
s=set()
s.add(A[0])
d=[]
d.append(A[0])
 
while True:
  if c in s:
    break
  s.add(c)
  d.append(c)
  c=A[c]

a=d.index(c)
K-=a
 
mod=len(s)-a
K%=mod

if K==0:
  print(1)
  exit()
ans=A[0]
for i in range(a):
  ans=A[ans]

for i in range(K-1):
  ans=A[ans]
print(ans+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
memo = [-1] * N

teleporter = A[0]
memo[0] = teleporter
repeat_point = -1

for i in range(1, N):
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    if memo[i] in memo[:i]:
        repeat_point = i
        break

start_point = memo.index(memo[repeat_point])
repeat = repeat_point - start_point
# print(memo, start_point, repeat_point)

if K <= N:
    print(memo[K-1])
else:
    mod = K % repeat
    if mod == 0:
        mod = repeat_point
    print(memo[mod-1])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

A = np.array(list(map(int, input().split()))) - 1

roop = [-1]*N
is_visited = [-1]*N

roop[0] = 0
is_visited[0] = 0

for i in range(N-1):
    if is_visited[A[roop[i]]] >= 0:
        for idx, j in enumerate(roop):
            if j == A[roop[i]]:
                r = i+1-idx
                print(roop[idx + (K - idx) % r] + 1)
                exit()

    elif i == K:
        print(roop[i])
        exit()

    roop[i+1] =  A[roop[i]]
    is_visited[roop[i]] = i

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

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

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
num = [0] * (n)
num[0] = 1
now = 0
for i in range(2,300000):
  nex = a[now]
  if num[nex]:
    start = num[nex]
    cycle = i - num[nex]
    v = (k+1-i) % cycle + start
    ans = num.index(v) + 1
    break
  else:
    num[nex] = i
    now = nex
print(ans)

Submission status: Wrong Answer

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

exist = [a[0]]
ex = {}
ex[a[0]] = 0

cnt = 1
while(True):
    if a[exist[-1]-1] in ex:        
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    ex[a[exist[-1]-1]] = 0
    exist.append(a[exist[-1]-1])
    
    cnt += 1
    if cnt >= k-1:
        print(exist[-2])
        exit()


#k = k-idx
d = (k-idx) % (cnt-idx)

print(exist[d+idx-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))

T = defaultdict(int)

key_town = 0
count = 0

if K < 2 * (10 ** 5):
    ans = 1
    for k in range(K):
        ans = A[ans]
    print(ans)
    sys.exit(0)


T[1] = 1

now = 1
for i in range(1, len(A)):
    now = A[now]
    if T[now] == 0:
        T[now] = i + 1
    else:
        key_town = now
        count = i - T[now]
        break

if count == 1:
    print(key_town)
    sys.exit(0)

k = (K - T[key_town]) % (count - 1)

ans = key_town
for i in range(k):
    ans = A[ans]
print(ans)

Submission status: Wrong Answer

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

while (not a in C):
  C.append(a)
  a = A[a]
  l += 1
  
b=A[a]
B = [a]
o=1
if k == l:
  print(a)
else:
  while b != a:
    B.append(b)
    b = A[b]
    o += 1
  m = (k-l) % o
  print(B[m])

Submission status: Wrong Answer

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

def search(maps):
  current = 0
  passed = [0]
  while True:
    current = maps[current]
    if current in passed:
      base = passed.index(current)
      return (base, len(passed) - base)

    passed.append(current)


def main():
  N, K = map(int, input().split(' '))
  maps = [m - 1 for m in map(int, input().split(' '))]

  # 探索
  base, cycle_len = search(maps)

  if K <= base:
    print(maps[K] + 1)
  else:
    print(maps[((K - base) % cycle_len) + base] + 1)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

flag = [0]*(N+5) # 何回目に初めて訪れたか 

period = 1
now = 0
for i in range(N):
  now = A[now]-1
  #print(now+1)
  #print(flag)
  #print()
  if(flag[now] != 0):
    period = (i+1)-flag[now] 
    break
  flag[now] = i+1

#print('period',period)  
num = K % period    
#print('num',num)

now = 0
for i in range(num):
  now = A[now]-1
ans = now+1 # index→town_no  
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
rep = []
rep.append(1)
i = 0
while True:
    go = a[i]-1
    if a[i] not in rep:
        rep.append(a[i])
        i = go
    else:
        rep.append(a[i])
        break
lr = len(rep)
for i in range(lr):
    if rep[i]==rep[lr-1]:
        start=i
        rep2 = rep[i:lr-1]
        break
mod = len(rep2)
modL = [i%mod for i in range(start,start+mod)]
resM = k%mod
for i in range(mod):
    if resM == modL[i]:
        print(rep2[i])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline().rstrip('\n')


def calc(N, K, A):
    n = 1
    k = 0
    d = {n: k}
    while True:
        k += 1
        n = A[n - 1]
        if k == K:
            return n, 1
        if not n in d:
            d[n] = k
        else:
            if k - d[n] == 1:
                return n, 2
            else:
                x = (K - d[n] + 1) % (k - d[n])
                for i in d:
                    if d[n] - 1 + x == d[i]:
                        return i

    

(N, K) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
print(calc(N, K, A))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans_check = [1]
ans = [1]
f = 0
for _ in range(k % n):
    ans_check.append(a[ans_check[_] - 1])
ans_unique = list(set(ans_check))
if len(ans_unique) == len(ans_check):
    f = 1
if f == 0:
    for i in range(k % n):
        ans.append(a[ans[i] - 1])
    print(a[-1])
else:
    for i in range(k):
        ans.append(a[ans[i] - 1])
    print(a[-2])

Submission status: Wrong Answer

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

loop_count = 0
syoki = 1
for i in range(N):
    if loop_count != 0 and syoki == 1:
        break
    loop_count += 1
    syoki = A[syoki - 1]

# print(K - (loop_count * (K // loop_count)))
res_N = K - (loop_count * (K // loop_count))

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

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

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

Submission status: Wrong Answer

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

route = [0]

k = 0
l = -1
while True:
    k += 1
    n = A[route[-1]]
    if n in route:
        l = n
        break
    route.append(n)

lp = route.index(l)
loop = route[lp:]
route = route[:lp]
# print(l, route, loop)
K -= len(route)
p = K % len(loop)
print(loop[p] + 1)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K = map(int, readline().split())
    A = [int(i) for i in readline().split()]

    d = [0] * (N+1)
    d[1] = 1
    now = 1
    next_ = A[0]
    c = [0] * (N+1)
    cnt = 0
    while d[next_] == 0:
        d[next_] = d[now] + 1
        now = next_
        next_ = A[next_-1]
        cnt += 1
        c[now] = cnt

    s = c[next_]
    loop = cnt - s

    if K <= s:
        ans = c.index(K)
    else:
        n = (K - s) % (loop + 1)
        ans = c.index(s + n)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
i=1
S=[1]
count=0
while(1):
	S.append(L[i-1])
	i=S[-1]
	count+=1
	if len(set(S))!=len(S):
		break
	if count-1==K:
		print(S[-1])
		exit()
for i in range(len(S)):
	if S[-1]==S[i]:
		K=K-i
		K=K%(len(S)-1-i)
		print(S[K+i])
		exit()

Submission status: Wrong Answer

Solution code:
# # for i in range(m):
# s = input()
# t = input()
# n = len(s)
# m = len(t)
# # print(s,t,n,m)
# if n+1==m:
# 	for i in range(n):
# 		# print(s[i], t[i])
# 		if s[i]!=t[i]:
# 			print("No")
# 			break
# 	else:
# 		print("Yes")
# else:
# 	print("No")
	# l, r = [int(j)-1 for j in input().split()]
	################
# def fun(a,b,c,k):
# 	ans =0 
# 	if k<=a:
# 		return k
# 	else:
# 		ans = a
# 		k-=a
# 	if k<=b:
# 		return ans
# 	else:
# 		k-=b
# 	# if k<=c:
# 	# print(ans, c)
# 	return ans-min(c,k)
# a,b,c,k = [int(j) for j in input().split()]
# # l = [a,b,c]
# # j = 0
# print(fun(a,b,c,k))
################################

# while(k>0):
# 	a = min(k, l[j])
# 	k-=a

#######################

n,k = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
s = dict()
l = []
temp = 1
ct = 0
rep = None
# k_copy = 
while(k>0):
	ct+=1
	k-=1
	# print(temp)
	temp = a[temp-1]
	if temp in s:
		rep = s[temp]
		break
	else:
		s[temp]=ct
		l.append(temp)
if k==0:
	print(l[-1])
else:
	n = len(l)
	# print(l, rep, k, n)


	print(l[rep-1+(k)%(n-rep+1)])
# print(l[s[a[ct]]-1+(k-s[a[ct]]+1)%(-1*s[a[ct]]+ct+1)])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
lst=[0]*(N+1)
lst[0]=1
kaisuu=1
c=[-1]*(N+1)
c[1]=0
roo=0
rooa=0
while kaisuu>=0:
 lst[kaisuu]=A[lst[kaisuu-1]-1]
 if c[lst[kaisuu]]>=0:
  roo=kaisuu-c[lst[kaisuu]]
  rooa=c[lst[kaisuu]]
  break
 else:
  c[lst[kaisuu]]=kaisuu
  kaisuu+=1
if K<=rooa+1:
 print(lst[K+1])
else:
 d= (K-rooa-1)%roo
 print(lst[rooa+1+d])

Submission status: Wrong Answer

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

  history = []
  current = 0
  for i in range(len(A)):
    history.append(current)
    current = A[current]
    if current in history:
      loop_length = len(history[history.index(current):] )
      length_to_loop = len(history[:history.index(current)])
      additional_way = (K-length_to_loop)%loop_length
      print(A[length_to_loop + additional_way] + 1)
      return True
if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
cnt = 0
loop = 0
visited = [False] * n
visited[now - 1] = cnt
while True:
    now = a[now - 1]
    cnt += 1
    if visited[now - 1]:
        loop = (cnt - visited[now - 1])
        print(a[(visited[now - 1]) + ((k - (visited[now - 1])) % loop)])
        exit()
    visited[now - 1] = cnt