Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B =[1]
i = 0
Flag = True
a = A[0] 
i +=1 #1回
B.append(a)
while i < K and Flag == True:
  a = A[a-1]
  if a in B:
    Flag = False
  B.append(a)
  i += 1

t = B.index(a)
l = i-t
if Flag == True:
  print(a)
else:
  s = (K-t)%l
  a =A[t+s]
  print(a)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))
X = [1]
pos = 0
for i in range(2 * N):
    X.append(A[pos])
    pos = A[pos] - 1
#print(X)

d = defaultdict(int)
loop = len(X)
for i in range(len(X) - 1):
    if 1 == d[(X[i], X[i + 1])]:
        loop = i
        break
    else:
        d[(X[i], X[i + 1])] += 1

#print(loop)
x, y = X[loop], X[loop + 1]
for i in range(N-1):
    if X[i] == x and X[i + 1] == y:
        base = i
        break
#print(base)
diff = loop - base
if K < base:
    print(X[K])
else:
    mod = (K - base) % diff
    #print(mod)
    print(A[base + mod])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
a = l[0] 
lis = [a]
for i in range(200000):
    a = l[a-1]
    if a in lis:
        b = a
        break
    else:
        lis.append(a)

c = len(lis)
d = lis.index(b)
newlis = lis[d:c]
length = len(newlis)

t = k-d #最初のループを除く
s = t%length
if s ==0 :
    answer = newlis[length-1]
else:
    answer = newlis[s-1]
    
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict,Counter

sys.setrecursionlimit(10**7)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    a=[0]+lmis()
    memo=[1]+[-1]*n
    i=0
    while i<n:
        nt=memo[i]
        i+=1
        if a[nt] not in memo: memo[i]=a[nt]
        else:
            memo[i]=a[nt]
            break
    start=memo.index(memo[i])
    roop=i-start
    cnt=k-start
    cnt-=cnt//roop*roop
    print(memo[start+cnt])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

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

log=[]

now=0

cnt=0


while(1):
  if len(log)>1:
    if now in log:
      f=log.index(now)
      z=k-cnt #要調整

      loop=cnt

      b=z%(loop-f)

      #print(cnt,f,z,loop,b)
      print(log[f+b]+1)
      #print(log)
      sys.exit()


  log.append(now)
  now=a[now]-1
  cnt+=1

Submission status: Wrong Answer

Solution code:
import numpy as np

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

looplen = 0
loop = []

done = False

ni, nj = 0, 0
pi, pj = 0, 0
while True:
  pi = teles[pi]
  pj = teles[pj]
  ni += 1
  nj += 2
  # ni回, piにて確実にループ内
  # ループ一周は nj - ni(= ni)要素&回
  if ni == k:
    print(pi+1)
    done = True
    break
  if pi == pj:
    looplen = ni
    break
    
if not done:
  current = pi
  for i in range(looplen):
    current = teles[pi]
    loop.append(current)
  print(loop[(k - ni) % looplen]+1)

Submission status: Wrong Answer

Solution code:
n,k= map(int,input().split())
A  = [0]
A += list(map(int,input().split()))
B = [-1 for _ in range(n+1)]
B[1] = 0
now =1
cnt = 0
cic = 0
for i in range(1,n+1):
    now = A[now]
    cnt +=1
    if B[now] ==-1:
        B[now] = cnt
    else:
        cic = cnt - B[now]
        road = B[now] 
        break

if k < road:
    print(B.index(k)+1) 
else:
    print(B.index(((k-road)%cic+road)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=list(map(int,input().split()))
d=[1]
j=1
t=0
u=0
ab=0

while 1:
    
    d.append(c[j-1])
    j=c[j-1]
    for i in range(len(d)):
        if d[i]==c[j-1]:
            
            t=d[i]
            break
    if t!=0:
        break


z=d.index(t)

d=d[z:len(d)]

ab=(b+1-z)%len(d)

if ab==0:
    print(d[-1])
else:
    print(d[ab-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split());
data = [input().split()];
a = 1;
c = int(1);
roop = list();


while a <= k:
    b = c - 1;
    c = int(data[0][b]);
    roop.append(c);
    if roop.count(c) == 2:
        break;
    a = a + 1;
    
x = len(roop);
y = roop.index(c) + 1;
if k <= x:
    print(c);
else:
    z = k % (x - y);
    print(roop[y+z]);

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):
        if a[next] in visited_set:
            flg = True
            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:
# D - Teleporter

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

done = set()
path = []
now = 0
while True:
    if now in done:
        break
    else:
        path.append(now)
        done.add(now)
        now = A[now] - 1

start = path.index(now)
loop = len(path) - start
steps = (K - path.index(now))%loop + start

print(path[steps]+1)

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
d = {0}
p = 0
while not a[p] in d:
    d.add(a[p])
    p = a[p]
#print(d)
val = [0]
p = 0
for i in range(len(d)):
    val.append(a[p])
    p = a[p]
#print(val)
cnt = 0
p = 0
while val[p] != val[-1]:
    cnt += 1
    p += 1
del val[-1]
if cnt > k:
    print(val[k]+1)
k -= cnt
k2 = k % (len(d)-cnt)
#print(k2)
#print(cnt)
print(val[cnt + k2]+1)

Submission status: Wrong Answer

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

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

a = A[0]

set1 = set([])
list1 = []

for i in range(1,K):
    a = A[a-1]
    set1.add(a)
    list1.append(a)
    if list1.count(a) == 2:
        break
l = len(list1)

list1.pop(l-1)


k = (K-1)%len(set1)-1
print(list1[k])

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):
    if a[now] not in ans:
        ans.append(a[now])
        now=a[now]-1
    else:
        ans.append(a[now])
        break
#print(ans)

ans2=collections.Counter(ans)

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

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

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

Submission status: Wrong Answer

Solution code:
N , K = map(int, input().split())
li = list(map(int, input().split()))
if K < N:
    town = 0
    for i in range(N):
        town = li[town]-1
    print(town+1)
else:
    li2 = [0for i in range(N)]
    count = 1
    town = 0
    while(1):
        if li2[town] != 0:
            num = count - li2[town]
            num2 = li2[town]
            break
        li2[town] = count
        town = li[town]-1
        count += 1

    K = K - (num2 - 1)
    mod = K % num
    for i in range(mod):
        town = li[town] -1
    print(town+1)

Submission status: Wrong Answer

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

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

  j = 0
  kiseki2 = []
  for i in range(N):
    j = towns[j]-1

  if K < N:
    print(j+1)
    return

  for i in range(N):
    j = towns[j]-1
    kiseki2.append(j)

  rem = (K-N)%N
  print(kiseki2[rem-1]+1)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

move1 = []

_num = 1
for i in range(len(al)*2):
    _num = al[_num-1]
    move1.append(_num)

move2 = move1[len(al):]
#
# print(move1)
# print(move2)

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
C=[0]+[-1]*(n-1)
p=1
c=0
for i in range(n):
    p=A[p-1]
    c+=1
    if C[p-1]==-1:
        C[p-1]=c
    else:
        s=C[p-1]
        e=c
        break
P=1
if k<=s:
    while k>1:
        P=A[P-1]
        k-=1
    print(P)
else:
    l=(k-s)%(e-s)
    k=s+l+1
    while k>1:
        P=A[P-1]
        k-=1
    print(P)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=1
l=[x]
m=[0]*n
m[x-1]=1
for i in range(n+2):
    x=a[x-1]
    if m[x-1]==0:
        m[x-1]+=1
        l.append(x)
    else:
        break
loop=l[l.index(x):]

if l==loop:
    print(loop[k%len(loop)])
else:
    f=l.index(x)
    print(loop[(k-f)%len(loop)])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
a = na()
for i in range(n):
    a[i] -= 1
ct = [0 for i in range(n)]
seen = [False] * n
def dfs(v):
    nv = a[v]
    seen[v] = True
    if ct[nv] == k:
        return nv, 0
    if seen[nv]:
        loop = ct[v] - ct[nv] + 1
        res = (k - ct[v] - 1) % loop
        return nv, res
    else:
        ct[nv] = ct[v] + 1
        return dfs(nv)

nv, res = dfs(0)
for i in range(res):
    nv = a[nv]
print(nv + 1)

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]
cur = 0
flag = 0
for i in range(n):
    if a[cur] in b:
        flag = 1
        break
    else:
        b.append(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)%cycle
        k %= cycle
        print(loop[k]+1)

Submission status: Wrong Answer

Solution code:
N,K= map(int, input().split())
flags = [-1]*N
M = 1
mods = []
mods.append(0)
A = list(map(int,input().split()))
now = 0
I = 0
flags[0] = 0
for i in range(N):
    now = A[now]-1
    
    
    if flags[now]!=-1:
        I = i
        M=i+1-flags[now]
        break
    mods.append(now)
    flags[now] = i+1

I += 1
alt = mods[I-M:]

if K-I<=0:
    print(now+1)
else:
    print(alt[(K-I)%(len(alt))]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1 #i番目の町からどこにつくか
tmp=0#現在知
num2city={}
city2num={}
city2num[0]=0#どのまちが何回目か
num2city[0]=0#j回目にどこにいるか
for j in range(k):
    if tmp==dict[tmp]:
        break
    tmp=dict[tmp]#現在地更新
    if tmp not in num2city.values():#現在地がまだきたことない
        num2city[j]=tmp
        city2num[tmp]=j
    else:
        mae=city2num[tmp]
        term=j-mae 
        res=k-j-1
        r=res%term
        #print(mae,term,res,r)
        temp=num2city[mae+r]
        break
  #  print(tmp+1)
    

print(tmp+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
hako=[0]*N
for i in range(N):
    A[i]-=1
ichi=0
count=0
i=0
while True:
    if hako[A[i]]>=2:
        break
    hako[A[i]]+=1
    i=A[i]
    count+=1
for i in range(K%count):
    ichi=A[ichi]
print(ichi+1)
#print(count)
#print(hako)

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    n, k = map(int, input().strip().split())  # """ 街数n, 移動回数k """
    a = list(map(lambda x: int(x) - 1, input().strip().split()))  # """ 転送先 : 街i から街a[i]"""

    #
    path = []
    visitedlist = [False for _ in range(n)]
    pos = 0
    cnt=0

    while True:
        if cnt==k:
            print(pos)
            return
        if visitedlist[pos]:
            start = pos
            break
        cnt+=1
        path.append(pos)
        visitedlist[pos] = True
        pos = a[pos]


    for i in range(len(path)):
        if path[i] == start:
            cycle = path[i:]
    m=len(cycle)
    cnt1=k-cnt
    cnt2=cnt1%m
    print(cycle[cnt2]+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)
# print(a)
if a[1] == 1:
    print(1)
else:
    reached = {i: 0 for i in range(1, n+1)}
    now = 1
    count = 0
    while reached[now] == 0:
        reached[now] = count
        now = a[now]
        count += 1
    roop = count - reached[now]
    index = (k - reached[now]) % roop + reached[now]
    # print(reached, count)
    # print(roop, index)
    for key, value in reached.items():
        if value == index:
            print(key)
            break

Submission status: Wrong Answer

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

s = [0] * (N+1)
p = 1
h = []
while True:
    h.append(p)
    s[p] = 1
    p = A[p]

    if s[p] != 0:
        break

last = h.index(p)
leng = len(h) - last
rem = (K - last) % leng
print(h[last:][rem])

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 = 0
for el in data:
    if (el in used):
        head = el
        break
    used.append(el)
key = used.index(head)
k -= key
print(used[key+k % (len(used) - key)])

Submission status: Wrong Answer

Solution code:


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

       
        lll[j]=i
        i=A[i-1]
        j+=1
        


if __name__ == '__main__':
    main()

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]
s = set()
s.add(1)
while(True):
    tmp = A[B[-1]]
    if not tmp in s:
        B.append(tmp)
        s.add(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:
num, tere_num = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
i = 1

while 1:
    dire = path[i-1]
    if a[dire-1] in path:
        back_point = i - 1
        front_point = path.index(a[dire-1])
        break
    path.append(a[dire-1])
    i += 1
    
if tere_num <= front_point:
    print(path[tere_num])
else:
    tere_num_rest = tere_num - (front_point - 1)
    cycle = back_point - front_point + 1
    a = tere_num_rest % cycle
    ans_index = front_point - 1 + a
    print(path[ans_index])

Submission status: Wrong Answer

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

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

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

nokori=(K-startat) % loopcount

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
i = 0
while True:
    if i == 0:
        i += 1
    elif next_num in checked:
        loop = checked[checked.index(next_num):]
        break
    checked.append(next_num)
    next_num = a[next_num]-1
dif = len(checked) - len(loop)
if k <= dif:
    print(checked[k-1]+1)
else:
    k = k-dif
    print(loop[k%len(loop)]+1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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

    count = 0

    nextcity = 1

    for n in range(N):
        count += 1
        nextcity = A[nextcity-1]
        if nextcity == 1:
            break
    
    res = K % count

    nextcity = 1

    for k in range(res):
        nextcity = A[nextcity-1]
    
    print(nextcity)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
town=1
townhairetu=[1]
n=0
for i in range(N):
	town=A[town-1]
	if(town in townhairetu):
		n=i+1-townhairetu.index(town)
		x=townhairetu.index(town)
		break
	else:
		townhairetu.append(town)
print(townhairetu[(K-x)%n+x])

Submission status: Wrong Answer

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

current = 1
N_counter = [0] * N 
N_counter_idx = [0] * N 
N_counter[current-1] += 1

k_reserved = -1
for i in range(1, N+1):
    current = A[current-1]
    N_counter[current-1] += 1
    if i == K:
        k_reserved = current
    if N_counter[current-1] == 2:
        span = i
        break
    N_counter_idx[current-1] = i

if K < i:
    print(k_reserved)
else:
    #print(N_counter)
    #print(N_counter_idx)
    pre_loop_length = N_counter_idx[current-1] - 1
    loop_length = max(N_counter_idx) - pre_loop_length
    K_new = (K - pre_loop_length) % loop_length + pre_loop_length
    
    # K_new = K - (K // i) * i 
    current = 1
    for k in range(K_new):
        current = A[current-1]
    print(current)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from math import sqrt
from collections import Counter, defaultdict, deque

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


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    al = LI()

    al.insert(0, 0)
    passed = [-1] * (n + 1)
    i = 0
    now = 1
    while passed[now] == -1:
        passed[now] = i
        now = al[now]
        i += 1

    loop = i - passed[now]

    for _ in range((k - passed[now]) % loop):
        now = al[now]

    print(now)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

count = 0

for num, i in enumerate(lst):
    if i == pos and count == 1:
        loop = num - before
        rem = (k - before) % loop
        print(lst[before + rem])
        break
    elif i == pos:
        count = 1
        before = num

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A -= 1

flg = [False for _ in range(N)]
loop = 0
town = []
i = 0
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
    loop += 1
else:
    s = i
    to_s = town.index(s)
    loop -= to_s

print(town[(K - to_s) % loop + to_s] + 1)

Submission status: Wrong Answer

Solution code:
n, k, *a = map(int, open(0).read().split())
town = [0]*n
tmp_posi = 0
for i in range(n):
    tmp_posi = a[tmp_posi] - 1
    if town[tmp_posi]:
        first = town[tmp_posi]
        mod = i+1 - first
        k_ = k - first
        break
    else:
        town[tmp_posi] = i+1

cicle = [ [i,t] for i, t in enumerate(town) if t >= first]
cicle = sorted(cicle, key = lambda x:x[1])

print(cicle[k_ % mod][0]+1)

Submission status: Wrong Answer

Solution code:
a = list(map(int, input().split()))
N = a[0]
K = a[1]
b = list(map(int, input().split()))

St = b[0]
print(St)
for i in range(K-1):
    St = b[St-1]
print(St)

Submission status: Wrong Answer

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


a = list(map(int,input().split()))
b = [0 for i in range(2*10**5)]
l = []
x = 1

if k <= 2*10**5:
    for i in range(k-1):
        x = a[x-1]
    

    print(x)
    exit()

x = 0

for i in range(2*10**5):
    if b[x] == 0:
        b[x] += 1
    else:
        res = x+1
        break
    
    l.append(x+1)
    x = a[x]-1




roup = l[l.index(res):]

k -= (len(l)-len(roup))

print(roup[k % len(roup)])

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)
D=[0]*int(N)
c=0
R=0
while(1):
    c+=1
    P=A[P]-1
    if D[P]>0: 
        R=D[P]
        break
    D[P]=c
    if P==0: break
    L[c]=P

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

print(ans)

Submission status: Wrong Answer

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

import sys


input = sys.stdin.readline
#n = int(input())
#n,k = map(int,input().split())
#A = list(map(int,input().split()))
#s=input()
#a = input()
n,k= map(int,input().split())
A  = [0]
A += list(map(int,input().split()))
B = [-1 for _ in range(n+1)]
B[1] = 0
now =1
cnt = 0
cic = 0
for i in range(1,n+1):
    now = A[now]
    cnt +=1
    if B[now] ==-1:
        B[now] = cnt
    else:
        cic = cnt - B[now]
        road = B[now] 
        break
print(B,road,cic,cnt)
if k < road:
    print(B.index(k)+1) 
else:
    print(B.index(((k-road)%cic+road)))

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
if K - s >= 0:
    ans = (K - s) % (len(tel) - s) 
    print(tel[ans + s] + 1)
else:
    print(tel[K])

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 = [1]
first = []
repeat = []
for _ in range(N):
    nxt = A[nxt - 1]
    if nxt in visited:
        i = visited.index(nxt)
        if i != 0:
            visited.append(nxt)
            first = visited[:i + 1]
            repeat = visited[i + 1:]
        break
    else:
        visited.append(nxt)

if 0 == len(repeat):
    ans = A[K % len(visited)]
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()))

temp = 0
temp_l = [1]
now = 1
while True:
    temp += 1
    pre = now
    now = A[pre-1]
    if now in temp_l:
        end = now
        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())
l = list(map(int,input().split()))

pas1 = []

now = 1
count = 0
if k == 1:
    print(l[0])
    exit()
for i in range(2*n):
    a = l[now-1]
    if a in pas1:
        index = pas1.index(a)
        k -= index
        le = len(pas1)-index
        print(pas1[k%le+index-1])
        
        exit()
    else:
        now = a
        count += 1
        pas1.append(a)
        
        if count == k:
            print(a)
            exit()