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 math

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

crnt = 1
st = set()
wl = []
key = 0
cy = 0
mg = 0

for i in range(n):
    if crnt in st:
        key = crnt
        cy = i - wl.index(crnt)
        mg = wl.index(crnt)
        break
    st.add(crnt)
    wl.append(crnt)
    crnt = li[crnt - 1]

k -= mg
if cy != 0:
    k %= cy
crnt = key
for i in range(k):
    crnt = li[crnt - 1]

print(crnt)

Submission status: Wrong Answer

Solution code:
import math

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

crnt = 1
st = set()
wl = []
key = 0
cy = 0
mg = 0

for i in range(n):
    if crnt in st:
        key = crnt
        cy = i - wl.index(crnt)
        mg = wl.index(crnt)
        break
    st.add(crnt)
    wl.append(crnt)
    crnt = li[crnt - 1]

k -= mg
k %= cy
crnt = key
for i in range(k):
    crnt = li[crnt - 1]

print(crnt)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = input().split()
b = [0]*n
d = {}
ind = 1
num = 0
while True:
  if b[ind-1] == 0:
    b[ind-1] = -1
    d[ind] = num
    num += 1
    ind = int(a[ind-1])
  else:
    loop = num-d[ind]
    sup = d[ind]
    break

if k-sup >=0:
  for i in range((k-sup)%loop):
    ind = int(a[ind-1])

print(ind)

Submission status: Wrong Answer

Solution code:
n,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
 
l = [1]
now = 1
notloop = 0
dupl = -1

if n >= k:
  for i in range(n):
    now = a[now - 1]
  print(now)
  exit()
  
for i in range(n):
  now = a[now - 1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
if(dupl == -1):
  print(now)
else:
  notloop = l.index(dupl)
  length = len(l)-notloop
  print(l[(k-notloop)%length+notloop])

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k-1):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
    data=twon[data-1]
    if count!=0:
        break
    #print(count,array)
    
for j in range(count,k):
    data=twon[data-1]
    #print(j)
print(data)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k-1):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
    data=twon[data-1]
    if count!=0:
        break
    print(count,array)
    
for j in range(count,k):
    data=twon[data-1]
    #print(j)
print(data,count)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k-1):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
    data=twon[data-1]
    if count!=0:
        break
    #print(count,array)
    
for j in range(count,k):
    data=twon[data-1]
    print(j)
print(data)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tel = list(map(int, input().split()))
log = [1]
end = True
while(end):
    log.append(tel[log[-1] - 1])
    for i in range(len(log) - 1):
        if(log[i] == log[-1]):
            star = i
            mod = len(log) - star - 1
            end = False
print(log)
print(mod)
if(k < len(log)):
    print(log[k])
else:
    print(log[(k - star) % mod + star])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int,input().split()))
T = 1
cnt = 0
Tlist = []
for k in range(K):
    cnt += 1
    T = A[T-1]
    if T in Tlist:
        Tlist.append(T)
        break
    Tlist.append(T)
for i in range(len(Tlist)):
    if Tlist[i] == Tlist[-1]:
        lp = len(Tlist)-i
        break

if k == K-1:
    print(T)
else:
    for k in range((K-i)%lp):
        T = A[T-1]
    print(T)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
    data=twon[data-1]
    if count!=0:
        break
    
for j in range(count,k):
    data=twon[data-1]
print(data)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
        break
    data=twon[data-1]
for j in range(count,k):
    data=twon[data-1]
data=twon[data-1]
print(data)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
next=a[0]
town=[0]*n
town[0]+=1
for i in range(n):
  if town[next-1]==0:
    town[next-1]+=1
    next=a[next-1]
  else:
    start=next
    break
next=a[0]
cnt_1=1
for i in range(n):
  if a[next-1]!=start:
    next=a[next-1]
    cnt_1+=1
  else:
    cnt_1+=1
    break
next=a[start-1]
cnt=1
for i in range(n):
  if a[next-1]!=start:
    next=a[next-1]
    cnt+=1
  else:
    cnt+=1
    break
if cnt_1>=k:
  next=a[0]
  for i in range(k):
    next=a[next-1]
  print(next)
else:
  k-=cnt_1
  k%=cnt
  if k==0:
    print(start)
  else:
    next=a[start-1]
    for i in range(k-1):
      next=a[next-1]
    print(next)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
next=a[0]
town=[0]*n
town[0]+=1
for i in range(n):
  if town[next-1]==0:
    town[next-1]+=1
    next=a[next-1]
  else:
    start=next
    break
next=a[start-1]
cnt=1
for i in range(n):
  if a[next-1]!=start:
    next=a[next-1]
    cnt+=1
  else:
    cnt+=1
    break
k%=cnt
next=a[0]
for i in range(k-1):
  next=a[next-1]
print(next)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
next=a[0]
town=[0]*n
town[0]+=1
for i in range(n):
  if town[next-1]==0:
    town[next-1]+=1
    next=a[next-1]
  else:
    start=next
    break
next=a[start-1]
cnt=1
for i in range(n):
  if a[next-1]!=start:
    next=a[next-1]
    cnt+=1
  else:
    cnt+=1
    break
print(cnt)
k%=cnt
next=a[0]
for i in range(k-1):
  next=a[next-1]
print(next)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
    if count!=0:
        break
    data=twon[data-1]
for j in range(count,k):
    data=twon[data-1]
data=twon[data-1]
print(data)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int,input().split())
A=input().split()
def judge(a):
  j=0
  k=0
  if len(a)>=2:
    for i in range(len(a)-1):
      if a[i]==a[len(a)-1]:
        j=1
        k=i
        break
    else:
      j=0
      k=0
      
  ju=np.array([j,k])
  return ju
town=1
route2=[]
route=[1]
for i in range(K):
  town=A[int(town)-1]
  route.append(int(town))
  if judge(route)[0]==1:
  
    for j in range(judge(route)[1],len(route)-1):
      route2.append(route[j])
      
    if (K-judge(route)[1]+1)%len(route2)==0:
      print(route2[len(route2)-1])
    else:
      print(route2[((K-judge(route)[1]+1)%len(route2))-1])
    break
          
if len(route)==K:
          print(route[N-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visit=[]
pos=0
while True:
    visit.append(a[pos])
    pos=a[pos]-1
    if a[pos] in visit:
        break
loop=[]
looppos=visit[-1]-1
while True:
    loop.append(a[looppos])
    looppos=a[looppos]-1
    if a[looppos] in loop:
        break
if len(visit)>=k:
    print(visit[k])
else:
    t=(k-len(visit)+len(loop))%len(loop)-1
    print(loop[t])

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
count=0
for i in range(1,k):
    if array[data-1]==-1:
        array[data-1]=i
    else:
        cnout=(k-i)%(i-array[data-1])
        exit()
    data=twon[data-1]
for j in range(count):
    data=twon[data-1]
print(data)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
from collections import deque
roop=deque([1])
now=l[0]
while now not in roop:
   roop.append(now)
   now=l[now-1]
start=roop.index(now)
K-=start
for i in range(start):roop.popleft()
K=K%len(roop)
print(roop[K])

Submission status: Wrong Answer

Solution code:
a, b = list(map(int, input().split()))
c = list(map(int, input().split()))
itta = []
mati = 1
i = 0
while True:
    if mati in itta:
        saisyo = mati
        break
    else:
        itta.append(mati)
        mati = c[mati-1]
        i += 1
risuto = []
while True:
    if mati in risuto:
        break
    else:
        risuto.append(mati)
        mati = c[mati-1]
if b <= i-1:
    print(itta[b])
else:
    print(risuto[(b-i) % len(risuto)])

import random
string = ""
for i in range(20000):
    string += f"{i+1} "
print(string)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K=map(int,input().split())

A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0
 
 
 
i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - ((K-i) % cycle)
        t=i+1
        i=t
        break
    else:
        A2[current_town] = i
        t=i+1
        i=t

while i<K:
    current_town = A[current_town]
    t=i+1
    i=t

print(current_town+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K=map(int,input().split())

A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0
 
 
 
i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - ((K-i) % cycle)
        break
    else:
        A2[current_town] = i
        t=i+1
        i=t

while i<K:
    current_town = A[current_town]
    t=i+1
    i=t

print(current_town+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K=map(int,input().split())

A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0
 
 
 
i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - ((K-i) % cycle)
        i=i+1
        break
    else:
        A2[current_town] = i
        i=i+1

while i<K:
    current_town = A[current_town]
    t=i+1
    i=t

print(current_town+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K=map(int,input().split())

A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0
 
 
 
i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - ((K-i) % cycle)
        i=i+1
        break
    else:
        A2[current_town] = i
        i=i+1

while i<K:
    current_town = A[current_town]
    t=i+10000
    i=t

print(current_town+1)

Submission status: Wrong Answer

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

if K==1:
    print(arr[0])
    exit()

flg_arr = [0]*N
flg_arr[0]=1

keiro =[1]

for i in range(N):
    keiro.append(arr[keiro[-1]-1])
    if flg_arr[keiro[-1]-1]==1:
        break
    else:
        flg_arr[keiro[-1]-1]=1

loop_start=keiro.index(keiro[-1])
loop_kai = len(keiro)-loop_start-1
del keiro[-1]

if K<=loop_start:
    print(keiro[K])
    exit()

del keiro[0:loop_start]
#print(loop_start)
#print(loop_kai)
#print(keiro)
print(keiro[N%len(keiro)])

Submission status: Wrong Answer

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


c = [0]*(N+1)
i = 1
cnt = 0
f = True

while cnt < K:
    if f and c[A[i]]:
        loop = cnt-c[A[i]]+1
        cnt += (K-cnt)//loop*loop
        f = False

    cnt += 1
    i = A[i]
    c[i] = cnt

print(i)

Submission status: Wrong Answer

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

i = 0
s = "0"
while True:
    if str(A[i]) in s:
        r = s.index(str(A[i]))
        break
    s +=str(A[i])
    i = A[i]

print(int(s[r:][(K-r)%len(s[r:])])+1)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
p = 1
B = [-1]*N
B[0] = 0
count = 1
while 1 == 1 :
	p = A[p-1]
	if B[p-1] != -1:
		break
	B[p-1] = count
	count += 1
K -= count
K = K % (count-B[p-1])
for i in range(K):
	p = A[p-1]
print(p)

Submission status: Wrong Answer

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

city = [0] * (2 * N + 3)
city[0] = 1
arriedCity = {1}
secondArrivedCity = set()

roopHazimari = 0
cnt = 0
for i in range(1, len(city)):
    if A[city[i - 1] - 1] in arriedCity:
        if cnt == 0:
            roopHazimari = i
        if A[city[i - 1] - 1] in secondArrivedCity:
            city[i] = A[city[i - 1] - 1]
            break
        cnt += 1
        secondArrivedCity.add(A[city[i - 1] - 1])
        city[i] = A[city[i - 1] - 1]
    else:
        city[i] = A[city[i - 1] - 1]
        arriedCity.add(A[city[i - 1] - 1])
# print(city)
if (N - roopHazimari) >= K:
    print(city[K])
else:
    temp = (K - roopHazimari) % cnt
    print(city[roopHazimari + temp])

Submission status: Wrong Answer

Solution code:
l = list(map(int,input().split(' ')))
N = l[0]
K = l[1]
A = list(map(int,input().split(' ')))
x = 1
period= 0
p = [0 for i in range(N)]
while(p[x-1]!=2):
    p[x-1]+=1
    period+= 1
    x = A[x-1]
    
x = 1   
for i in range(K%period):
    x = A[x-1]
print(x)

Submission status: Wrong Answer

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

city = [0] * (2 * N + 2)
city[0] = 1
arriedCity = {1}
secondArrivedCity = set()

roopHazimari = 0
cnt = 0
for i in range(1, len(city)):
    if A[city[i - 1] - 1] in arriedCity:
        if cnt == 0:
            roopHazimari = i
        if A[city[i - 1] - 1] in secondArrivedCity:
            city[i] = A[city[i - 1] - 1]
            break
        cnt += 1
        secondArrivedCity.add(A[city[i - 1] - 1])
        city[i] = A[city[i - 1] - 1]
    else:
        city[i] = A[city[i - 1] - 1]
        arriedCity.add(A[city[i - 1] - 1])

if (N - roopHazimari) > K:
    print(city[K])
else:
    temp = (K - roopHazimari) % cnt
    print(city[roopHazimari + temp])

Submission status: Wrong Answer

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

city = [0] * (2 * N + 1)
city[0] = 1
arriedCity = {1}
secondArrivedCity = set()

roopHazimari = 0
cnt = 0
for i in range(1, len(city)):
    if A[city[i - 1] - 1] in arriedCity:
        if cnt == 0:
            roopHazimari = i
        if A[city[i - 1] - 1] in secondArrivedCity:
            city[i] = A[city[i - 1] - 1]
            break
        cnt += 1
        secondArrivedCity.add(A[city[i - 1] - 1])
        city[i] = A[city[i - 1] - 1]
    else:
        city[i] = A[city[i - 1] - 1]
        arriedCity.add(A[city[i - 1] - 1])
# print(city)
if (N - roopHazimari) > K:
    print(city[K])
else:
    temp = (K - roopHazimari) % cnt
    print(city[roopHazimari + temp])

Submission status: Wrong Answer

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

now = 1
count = 0
history = [0]*(N+1)
history[0] = now

check = [0]*N
check[now-1] = 1

for i in range(1,N+1):
    now = lst[now-1]
    count += 1
    history[i] = now
    if check[now-1] == 1:
        break
    else:
        check[now-1] = 1

for i in range(0,N+1):
    if history[i] == now:
        # print(history)
        # print(i,count,now)
        # print((K-i ) % (count-i))
        print(history[ i + (K-i ) % (count-i)])
        break

Submission status: Wrong Answer

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

now = 1
count = 0
history = [0]*(N+1)
history[0] = now

for i in range(1,N+1):
    now = lst[now-1]
    count += 1
    if now==1: break
    history[i] = now

print(history[K%count])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(int, input().split()))
l = [0]
s = {0}

for i in range(K):
    nex = A[l[-1]]-1
    
    if nex in s:
        for i in range(len(l)):
            if l[i]==nex:
                mark = i
                break
        
        t = len(l)-mark
        print(l[mark+(K-mark)%t]+1)
        exit()
    
    l.append(nex)
    s.add(nex)

print(nex)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(int, input().split()))
l = [0]
s = {0}

while True:
    nex = A[l[-1]]-1
    
    if nex in s:
        for i in range(len(l)):
            if l[i]==nex:
                mark = i
                break
        
        t = len(l)-mark
        print(l[mark+(K-mark)%t]+1)
        exit()
    
    l.append(nex)
    s.add(nex)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0
 
 
 
i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
        i=i+1
        break
    else:
        A2[current_town] = i
        i=i+1

while i<K:
    current_town = A[current_town]
    i=i+1

print(current_town+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]
    i=i+1
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
        break
    else:
        A2[current_town] = i


while i<K:
    current_town = A[current_town]
    i=i+1

print(current_town+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
#A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]-1
    i=i+1
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
        break
    else:
        A2[current_town] = i

while i<K:
    current_town = A[current_town]-1
    i=i+1

print(current_town+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
L = list(map(int, input().split()))
X = []
X.append(1) 
for i in range(n):
  if L[X[i]-1] in X:
    s = L[X[i]-1]
    break
  X.append(L[X[i]-1])

if k <= X.index(s):
  print(X[k])
else:
  S = X.index(s)
  loop = len(X) - S
  b = (k- S +1) % loop
  print(X[S+b-1])

Submission status: Wrong Answer

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

s = 1
place = [s]
first = set(place)

for i in range(n):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place[ans])
        break
    else:
        place.append(s)
        first.add(s)
        continue
else:
    print(s)

Submission status: Wrong Answer

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

s = 1
place = [s]
first = set(place)

for i in range(n):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place[ans])
        break
    else:
        place.append(s)
        first.add(s)
else:
    print(s)

Submission status: Wrong Answer

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

s = 1
place = [s]
first = set(place)

for i in range(n):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place[ans])
        exit()
    else:
        place.append(s)
        first.add(s)

print(s)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
L = list(map(int,input().split()))
X = [0]*(n+1)
X[0] = 1
for i in range(n):
  if L[X[i]-1] in X:
    X[i+1] = L[X[i]-1]  
    break
  X[i+1] = L[X[i]-1]
    
def f(lst,value):
  return [i for i,x in enumerate(lst) if x == value]
for x in X:
  if X.count(x) == 2:
    a = f(X,x)
    X_ans = X[a[0]:a[1]]
    break

b = (k - a[0] + 1) % (a[1]-a[0])
print(X_ans[b-1])

Submission status: Wrong Answer

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

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
      
A =  list(map(int, input().split()))

uf = UnionFind(N)
for i in range(N):
  uf.union(i, A[i]-1)

#print(uf.size(0))
amari = K % uf.size(0)

now = 1
for i in range(amari):
  now = A[now-1]
print(now)

Submission status: Wrong Answer

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

route = [1]
start = 1
duplicate = 0
while True:
  start = A[start]
  if start in route:
    route.append(start)
    duplicate = start
    break
  else:
  	route.append(start)
loop = len(route) - route.index(duplicate) - 1
bias = route.index(duplicate) 

K -= bias
K = K % loop
route = route[route.index(duplicate):-1]

print(route[K])

Submission status: Wrong Answer

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

visited = []
current = 1
step = 0
loop_length = 1
loop_start = 0 
while True: 
    if step == K:
        print(A[current]) 
        exit()

    if first_visit[current] != 0: 
        loop_length = step - first_visit[current]
        loop_start = first_visit[current]
        break

    
    first_visit[current] = step
    visited.append(current)
    current = A[current]
    step = step + 1

loop_index = loop_start + (K - loop_start) % loop_length

print(visited[loop_index])

Submission status: Wrong Answer

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

route = [1]
start = 1
duplicate = 0
while True:
  start = A[start]
  try: 
    route.index(start)
    route.append(start)
    duplicate = start
    break
  except:
  	route.append(start)
loop = len(route) - route.index(duplicate) - 1
bias = route.index(duplicate) 

K -= bias
K = K % loop
route = route[route.index(duplicate):-1]

print(route[K])

Submission status: Wrong Answer

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

town_num_tmp = 1  # 今いる町
seen = [-1]*(n+1) # indexの街に訪れたことがあるか（訪れたことがない: -1）
town_hist_list = []

while seen[town_num_tmp] == -1:
    seen[town_num_tmp] = 1
    town_hist_list.append(town_num_tmp)
    # 更新
    town_num_tmp = a_list[town_num_tmp]

roop_end = len(town_hist_list)
roop_start = town_hist_list.index(town_num_tmp)
roop_range = roop_end - roop_start

if k < roop_end:
    print(town_hist_list[k])
else:
    print(town_hist_list[(k-roop_start)%roop_range+roop_start-1])

Submission status: Wrong Answer

Solution code:
def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    place,cal=0,0
    cal=A[0]

    for i in range(1,K):
        place=A[cal-1]
        cal=A[place-1]

    print(place)
    return


main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[1]           
seen={}
k=1
for i in range (K):
  if not k in seen.keys():
      seen[k]=i
      k=A[k-1]
      l.append(k)
  else:
    r_start=seen[k]
    roop=i-r_start
    c=K//roop
    K=K-roop*c
    break
ans=l[K]
print(ans)

Submission status: Wrong Answer

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

start = l[0]
p = [start]
while(True):
	start = l[start-1]
	if start not in p:
		p.append(start)
	else:
		for i, n in enumerate(p):
			if n == start:
				loop_start_index = i
		break

loop = p[loop_start_index:]
not_loop = p[:loop_start_index]

print(loop[((k - len(not_loop)) % len(loop)) - 1])

Submission status: Wrong Answer

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

i = 1
dct = {}
for j in range(N):
    dct[j+1] = 0
rts = []
for _ in range(N):
    if dct[i] == 1:
        break
    else:
        dct[i]+=1
        rts.append(i)
        i = As[i-1]
before = rts.index(i)
new = rts[before:]
print(int(new[(K - before) % (len(rts)-before)]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
chk = [0] * n
flg = True
count = 0
if k < 10 ** 5:
    for i in range(k):
        now = a[now - 1]
else:
    while flg:
        count += 1
        now = a[now - 1]
        if chk[now - 1] == 0 and now != 1:
            chk[now - 1] = count
        else:
            flg = False
            if (k - count) != 0 and (count - chk[now - 1]) != 0:
                rem = (k - count) % (count - chk[now - 1])
            else:
                rem = 0
    else:
        for i in range(rem):
            now = a[now - 1]
            count += 1
print(now)

Submission status: Wrong Answer

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

i=1
B=A[0]
while i<=K:
    C=A[B-1]
    B=A[C-1]
    i=i+1
print(B)

Submission status: Wrong Answer

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

res = []
pos = 1

if k <= n:
    for _ in range(k):
        pos = arr[pos]
    print(pos)
    exit()

for _ in range(n):
    pos = arr[pos]

for _ in range(n):
    res.append(pos)
    pos = arr[pos]

print(res[k % n])

Submission status: Wrong Answer

Solution code:
def intlist(x):
    y = []
    for i in range(len(x)):
        y.append(int(x[i]))
    return y


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

A = intlist(input().split())

now = 1
count = -1
past = [1]

for j in range(0,len(A)):
    now = A[now-1]
    if now in past:
        past.append(now)
        break
    past.append(now)



    


for k in range(len(past)):
    if past[k] == past[len(past)-1]:
        count = k
        break
        
repeat = past[count:len(past)-1]


        


  
if count != -1:    
    B = (K - count) % len(repeat)
    print(repeat[B])
else:
    print(past[len(past)])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
    r.append(nx)
    tnx = a[nx]
    if tnx in r:
        m = r.index(tnx)
        d = i - m + 1
        k = k - m
        k = k % d
        print(r[k + m] + 1)
        break  
    nx = tnx

Submission status: Wrong Answer

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

n,k = map(int,input().split())
li = list(map(int,input().split()))
for i in range(len(li)):
    li[i] -= 1

p = 0
t = 0
u = []
for i in range(k):
    t += 1
    p = li[p]
    if i >= 1 and p in u:
        break
    u.append(p)
#print(p) 
#print(t)
#print(u)
#print(t-u.index(p))
f = t-u.index(p)

l = k % f
#print(l)

for i in range(l):
    t += 1
    p = li[p]
    if i >= 1 and p == 0:
        break
print(p+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
sumi=[0]*N
root=[0]*N
root[0]=1
k=0
idx=0
for i in range(1,N+1):
    chk=root[i-1]-1
    nxt=A[chk]
    if sumi[chk]==0:
        root[i]=nxt
        sumi[chk]=1
    elif sumi[chk]!=0:
        k=i-1
        break
idx1=root.index(nxt)
ans=root[idx1:k+1]
print(ans[(K-(idx1-1))%len(ans)-1])

Submission status: Wrong Answer

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

i = 0
m = [-1] * n
p = 0

while m[i] == -1:
    if i == k:
        break
    m[i] = p
    p += 1
    i = l[i]
else:
    z = (k - m[i]) % (p - m[i])
    i = m.index(m[i] + z)
print(i + 1)

Submission status: Wrong Answer

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

reach = [False] * N
order = [0]
now_v = 0
cnt = 1
while 1:
    reach[now_v] = True
    next_v = A[now_v] - 1
    if reach[next_v]:
        goal = next_v
        break
    now_v = next_v
    order.append(now_v)
    cnt += 1

now_v = 0
cnt_tmp = 1

while 1:
    next_v = A[now_v] - 1
    if next_v == goal:
        break
    cnt_tmp += 1
    now_v = next_v
if cnt == cnt_tmp:
    cnt_tmp = 0

check = (K - cnt_tmp) % (cnt - cnt_tmp)
print(order[cnt_tmp+check]+1)