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:
# -*- coding: utf-8 -*-
import numpy as np
import time

start_time = time.time()

n, k = map(int, input().split())
a = np.array(input().split(), dtype='int')
len_max = n
array1 = [1]
now_city = 1
loop = 0
flg = 0

for i in range(len_max):
    now_city = a[now_city - 1]
    if np.any(array1 == now_city) and i != 0:
        flg = 1
        loop_begin = np.where(array1 == now_city)[0][0]
        loop_end = i + 1

        now_city = 1
        for i in range(loop_begin):
            now_city = a[now_city - 1]
        k -= loop_begin
        loop = loop_end - loop_begin
        k = k % loop
        for i in range(k):
            now_city = a[now_city - 1]
        ans = now_city

        break
    if i < len_max:
        array1.append(now_city)

    if i == k - 1:
        ans = now_city
   

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
#print(path)
c = Counter(path)
#print(c)
roop_st = 0
for key, v in c.items():
    if v >= 2:
        roop_st = key
        break
roop_st_idx = 0
roop_fin_idx = n
if roop_st != 0:
    roop_st_idx = path.index(roop_st)
    roop_fin_idx = path.index(roop_st, roop_st_idx+1)
#print(roopidx,roopidx2)
k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)
print(path[roop_st_idx+k_mod])

Submission status: Wrong Answer

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

visit=set()
order=[-1]*(N+1)

p=0
cnt=0
while not (p in visit):
    visit.add(p)
    order[cnt]=p
    p=A[p]-1
    cnt+=1

ind=order.index(p)
K=(K-ind)%(cnt-ind)

while K>0:
    p=A[p]-1
    K-=1

print(p+1)

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1



now = 0
if rep_start >= K:
    for i in range(K):
        now = As[now]
        print(now+1)
        sys.exit()

now = 0
for i in range(rep_start):
    now = As[now]


K -= rep_start
K %= move
now = p

for i in range(K):
    now = As[now]

print(now+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [1]
for i in range(n):
    if a[l[i] - 1] in l:
        res = l[:l.index(a[l[i] - 1])]
        cyc = l[l.index(a[l[i] - 1]):]
        break
    else:
        l += [a[l[i] - 1]]
print(cyc[(k - len(res)) % len(cyc)] if k > len(res) else res[k - 1])

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
print(path)
c = Counter(path)
print(c)
roop_st = 0
for key, v in c.items():
    if v >= 2:
        roop_st = key
        break
roop_st_idx = 0
roop_fin_idx = n
if roop_st != 0:
    roop_st_idx = path.index(roop_st)
    roop_fin_idx = path.index(roop_st, roop_st_idx+1)
#print(roopidx,roopidx2)
k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)
print(path[roop_st_idx+k_mod])

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    lis = []
    i = 1
    for _ in range(N):
        K -= 1
        if not i in lis:
            lis.append(i)
        else:
            lis = lis[lis.index(i):]
            ind = (K-1) % len(lis)
            if ind != 0:
                return lis[ind-1]
            else:
                return lis[-1]

        if K == 0:
            return i
        
        i = A[i-1]


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*(n+1)
tmp = 0
t = 1
index = 0

for i in range(n+1):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        break

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[t])
    else:
        print(memo2[indexes[0]+tmp2-1])

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    cnt = [-1] * n
    cnt[0] = 0
    ary = []
    now = 1
    ary.append(now)
    if k <= n:
        for i in range(1,k+1):
            now = a[now-1]
            if i == k:
                print(now)
                return
    else:
        for i in range(1,k+1):
            now = a[now-1]
            if cnt[now-1] != -1:
                break
            else:
                cnt[now-1] = i
                ary.append(a[now-1])

        
        print(ary[cnt[now-1] - 1 +((k-cnt[now-1]) % (i - cnt[now-1]))])

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
from copy import copy
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
c = copy(a)
ans = 0
while k:
    if k & 1:
        ans = c[ans]
    t = copy(c)
    for i in range(n):
        t[i] = c[i]
    c = copy(t)
    k >>= 1
print(ans + 1)

Submission status: Wrong Answer

Solution code:
import copy
N,K = list(map(int, input().split()))
 
A = list(map(int, input().split()))
 
count = [0]
Check = [0] * N
for i in range(K):
    count.append(A[count[-1]]-1)
#    print(count)
    if(Check[count[-1]-1] == 1):
        break
 
    Check[count[-1]-1] = 1
    i += 1

saisyo = count.index(count[-1]) + 1
#print(count,saisyo)

if(saisyo == len(count)):
    saisyo = 0

loop_kazu = len(count) - saisyo

if(K > loop_kazu):
    K_loop = K - saisyo
 
    ans_index = K_loop % loop_kazu
    ans = count[saisyo + ans_index] + 1
 
    print(ans)

else:
    print(count[K-1]+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import collections
from collections import OrderedDict

n,k = map(int,input().split())
L = list(map(int,input().split()))
od = collections.OrderedDict()

t = 1
r = 0
i = 0
c = 0
for j in range(n*2):
    #print(L2)
    t = L[t-1]
    if ｔ in od:
        l = list(od.keys())
        i = l.index(t)
        r = len(od) - i
        break
    else:
        od[t] = t
#print(r)
#print(i)

k2 = (k - i)%r
#print(k)
l = list(od.keys())
#print((k - i)//r)
if (k - i)//r == 0:
    print(l[i+k2])
else:
    print(l[i+k2-1])

Submission status: Wrong Answer

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

s = 0
seen = [False for _ in range(n)]
d = []
while not seen[s]:
    seen[s] = True
    d.append(s)
    s = a[s]-1

noncyi = d.index(s)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k-1])
else:
    k -= noncyi
    print(d[noncyi + k%cyi]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
now=1
arr=[0]*(n+1)
for i in range(1,10**6):
  now=a[now-1]
  if i==k:
    print(now)
    quit()
  if arr[now]!=0:
    first=arr[now]
    break
  arr[now]=i
# print(arr)
cycle=max(arr)-first+1
# print(cycle)
mod=(k-first+1)%cycle
# print(mod)
ans=arr.index(first+mod-1)
print(ans)

Submission status: Wrong Answer

Solution code:

from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = next_i
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    if k > start:
        k = ((k - start) % l) + start
    for i in range(n):
        if seen[i] == k:
            print(i + 1)
            exit()


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis1 = list(map(int,input().split()))
i = 1
lis2 = [1]
while lis1[i-1] not in lis2:
    lis2.append(lis1[i-1])
    i = lis1[i-1]

r = k%len(lis2)
print(lis2[r])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
L = list(map(int, input().split()))
#N=6
#K=5
#L=[6,5,2,5,3,2]
#同じところに戻ってくるのを消す
global q   
global a
a=[1]
q=1
def fun(q):
    a.append(L[q-1])
    return L[q-1]
for i in range(N):
    q=fun(q)
#print(a)
#for o in range(N):
 #   qw=[i for i, x in enumerate(a) if x == o+1]
  #  print(qw)
l=list(dict.fromkeys(a))
num=a[len(l)]
num1=l.index(num)
#print(num)
#print(num1)
#print(l)
#print(l[num1:])
print(l[num1:][(K-num1)%len(l[num1:])])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
now=0
i=0
count=0
c=[0]*N
r=[0]
while flag==0:
  now=A[now]-1
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
  r.append(now)
if flag==1:
  print(now+1)
else:
  o=[i for i, x in enumerate(r) if x == now]
  print(A[r[K%(o[1]-o[0])]-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=map(int, input().split())

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0

t=0
rep = False

for i in range(k):
    if i == k:
        print(next+1)
        exit()

    if li2[next] == 1:
        t = li.index(next+1)
        rep = True
        li.pop()
        break

    li.append(A[next])
    li2[next] = 1
    next = A[next]-1

print(li,t)

tmp = (k-t)%len(li[t:])
print(li[tmp])

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
list1 = []
for i in range(N):
  list1.append([i + 1, int(c[i])])

current_num_list = [1]
current_num = 1
for item in list1:
  if item[0] == current_num:
    current_num_list.append(item[1])
    current_num = item[1]
    break

while (current_num not in current_num_list[:-1]):
  for item in list1:
    if item[0] == current_num:
      current_num_list.append(item[1])

      current_num = item[1]
      break

c = len(current_num_list)

first_index = 0
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break

current_num_list1=current_num_list[:-1]
cicle=c-first_index-1
first=first_index
if c<=K-1 :
  index = (K + 1 - first) % cicle + first-1
  print(current_num_list1[index])
else :
  print(current_num_list[K+1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*(n+1)
tmp = 0
t = 1
index = 0

for i in range(n+1):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        break

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo2[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-1])

Submission status: Wrong Answer

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

rute = list()
visited = dict()
city = A[0]
i = 0
while city not in visited:
    rute.append(city)
    visited[city] = i
    city = A[city]
    i += 1

loopstart = visited[city]
loopend = len(rute) - 1

if K < loopstart:
    print(rute[K-1]+1)
else:
    K -= loopstart
    K %= loopend - loopstart + 1
    print(rute[loopstart + K - 1]+1)

Submission status: Wrong Answer

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

d=set()
e=[]
mir=1
while 1:
  if mir in d:
    g=e.index(mir)
    f=e[g:]
    e=e[:g]
    break
  else:
    d.add(mir)
    e.append(mir)
    mir=c[mir]
print(e)
print(f)
if b<len(e):
  print(e[b])
else:
  b=b-len(e)
  print(f[b%(len(f))])

Submission status: Wrong Answer

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

visit=set()
order=[-1]*N

p=0
cnt=0
while not (p in visit):
    visit.add(p)
    order[cnt]=p
    p=A[p]-1
    cnt+=1

ind=order.index(p)
K=(K-ind)%(cnt-ind)

while K>0:
    p=A[p]-1
    K-=1

print(p+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
s = input().split()
n = int(s[0])
k = int(s[1])
count =0

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    count += 1
    if p in fp:
        roop = count - fp.index(p)
        break
    else:
        fp.append(p)
ans = fp[fp.index(p):]
print(ans[(k-fp.index(p))%roop])

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1



now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
        print(now+1)
        sys.exit()

now = 0
for i in range(rep_start):
    now = As[now]


K -= rep_start
K %= move
now = p

for i in range(K):
    now = As[now]

print(now+1)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    check=[-1]*n
    roop=0
    iti=0
    rireki=[]
    while roop==0:
        iti=a[iti]-1
        for i in range(len(rireki)):
            if check[iti]==0:
                mae=rireki.index(iti)
                Mae=rireki[:mae]
                Loop=rireki[mae:]
                roop=len(rireki)-mae
                break
        rireki.append(iti)
        check[iti]=0
    if k <= mae:
        print(Mae[k-1]+1)
    else:
        k-=mae
        print(rireki[(k%roop)-1]+1)
resolve()

Submission status: Wrong Answer

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

c = 0 # 現在の場所
cK = 0 # 現在の回数
flg = True
mem[c] += 1
mem[c][1] = 0
for k in range(1, K+1):
    c = A[c] # k回目の移動先
    mem[c][0] += 1
    if np.any(mem.T[0]>1):
        T = k - mem[c][1]
        cK = k
        flg = False
        break
    mem[c][1] = k

if flg:
    print(c+1)
else:
    iterN = int((K - cK) % T)
    if iterN > 0:
        for i in range(iterN):
            c = A[c]
    print(c+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
s = input().split()
n = int(s[0])
k = int(s[1])
count =0

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    count += 1
    if p in fp:
        print(p)
        roop = count - fp.index(p)
        break
    else:
        fp.append(p)
ans = fp[fp.index(p):]
print(ans[(k-fp.index(p))%roop])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
tp = a[0]-1
for i in range(n):  
    l.append(a[tp])
    tp = a[tp] -1    
p = l.index(l[-1])
if len(l)>k:
  print(l[k])
  exit()
  
x = (k-n)%(n-p)
if x==0:
  print(l[n-1])
else:
  print(l[x+p-1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
temp = list(map(int, input().split()))
warp = {}
for i in range(1,N+1):
    warp[i] = temp[i-1]

way = []
for k in range(1,K+1):
    if k == 1:
        way.append(warp[1])
    else:
        if warp[way[k-2]] in way:
            loop_start = way.index(warp[way[k-2]])
            loop_length = k - 1 - loop_start
            print(way[(K-loop_start)%loop_length + loop_start - 1])
            break
        else:
            way.append(warp[way[k-2]])
else:
    print(way[-1])

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = next_i
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    index = k
    if k > start:
        index = ((k - start) % l) + start
    for i in range(n):
        if seen[i] == index:
            print(i + 1)
            exit()


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=map(int, input().split())

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0

t=0
rep = False

for i in range(k):
    if i == k:
        print(next+1)
        exit()

    if li2[next] == 1:
        t = li.index(next+1)
        rep = True
        li.pop()
        break

    li.append(A[next])
    li2[next] = 1
    next = A[next]-1


tmp = k%len(li[t:])
print(li[t + tmp])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*(n+1)
tmp = 0
t = 1
index = 0

for i in range(n+1):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        break

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-1])

Submission status: Wrong Answer

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

aList = list(map(int, input().split()))
seen = []
order = [0]*n
tsugi = 0
flag = False
for i in range(k):
  tsugi = aList[tsugi]-1
  if tsugi not in seen:
    seen.append(tsugi)
    order[tsugi] = i
  else:
    index = order[tsugi]
    length = i - index
    flag = True
    break
if flag:
  print(seen[index + (k-index)%length-1]+1)
else:
  print(tsugi+1)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

s = 0
seen = [False for _ in range(n)]
d = [0]
cy = -1
while not seen[s]:
    seen[s] = True
    s = a[s]-1
    if seen[s]:
        cy = s
        break
    d.append(s)

if cy == -1:
    print(d[k-1])
    exit()

noncyi = d.index(cy)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k-1])
else:
    k -= noncyi
    print(d[noncyi + k%cyi]+1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    visited = {i:0 for i in range(N)}
    visited[0] = 1
    count = 0
    pre_count = 0
    cicle_idx = 0
    is_cicle = False
    idx = 0
    while not is_cicle:
        count += 1
        idx = A[idx]-1
        if visited[idx] == 1:
            visited[idx] = 2
            is_cicle = True
            cicle_idx = idx
            break
        visited[idx] = 1
    cicle = []
    will_push = False
    while True:
        idx = A[idx]-1
        if idx == cicle_idx:will_push = True
        if len(cicle) > 0 and visited[idx] == 2:break
        if will_push:
            cicle.append(idx)
    pre_count = count - len(cicle)
    ans_idx = (K - pre_count) % len(cicle)
    print(cicle[ans_idx]+1)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#coding:utf-8

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

A = [int(i) for i in input().split()]

state = 1
count = 0
flag = True
track = [0] * N

while count < K:

    count += 1

    if track[state-1] == 0:
        track[state-1] = count
    elif flag:
        roop = count - track[state-1]
        path = track[state-1] -1
        count = K - ((K - path) % roop)
        flag = False

    if count >= K:
        break

    state = A[state-1]

print("{}".format(state))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
tp = a[0]-1
for i in range(n):  
    l.append(a[tp])
    tp = a[tp] -1    
p = l.index(l[-1])
if len(l)>k:
  print(l[k])
  exit()
  
x = (k-n)%(n-p)
if k==0:
  print(l[n-1])
else:
  print(l[x+p-1])

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
#print(path)
c = Counter(path)
#print(c)
roop_st = 0
for key, v in c.items():
    if v >= 2:
        roop_st = key
        break
roop_st_idx = 0
roop_fin_idx = n-1
if roop_st != 0:
    roop_st_idx = path.index(roop_st)
    roop_fin_idx = path.index(roop_st, roop_st_idx+1)
#print(roopidx,roopidx2)
k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)
print(path[roop_st_idx+k_mod])

Submission status: Wrong Answer

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

time=0
color=["white" for _ in range(n+1)]
D=[-1 for _ in range(n+1)]
C,s=[],A[0]
point=0
while 1:
    if color[s]=="white":
        color[s]="gray"
        C.append(s)
        D[s]=time
        time +=1
        s=A[s-1]
    else:
        point=D[s]
        break

loop=C[point:]
if k>len(C):
    k -=point+1
    print(C[k%len(C)])
else:
    print(C[k-1])

Submission status: Wrong Answer

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

a = [0] + list(map(int, input().split()))
a1 = [0] * (n+1)

step = 1
start = 0
while(True):
    start += 1
    a1[step] += 1
    step = a[step]
    if a1[step] == 1:
        break
c = step
gap = 0
while(True):
    gap += 1
    step = a[step]
    if step == c:
        break
k = (k+n*gap-start) % gap
result = c
for _ in range(k):
    result = a[result]

print(result)

Submission status: Wrong Answer

Solution code:
[N,K]=map(int,input().split())
A = list(map(int,input().split()))
t = 1
log = []
for i in range(K-1):
    t = A[t-1]
    if t in log:
        n = log.index(t)
        r = log[n:]
        nn = int((K-n)%len(r))
        t = r[nn-1]
        break
    log.append(t)
print(t)

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    lis = [1]
    i = 1
    while True:
        i = A[i-1]
        # print(K)
        # print(lis)
        if not i in lis:
            lis.append(i)
        else:
            lis = lis[lis.index(i):]
            ind = K % len(lis)
            if ind != 0:
                return lis[ind-1]
            else:
                return lis[-1]

        if K == 0:
            return i
        
        K -= 1


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
from copy import copy
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
c = copy(a)
ans = 0
while k:
    if k & 1:
        ans = c[ans]
    c = copy(a)
    for i in range(n):
        c[i] = a[i]
    k >>= 1
print(ans + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
temp = list(map(int, input().split()))
warp = {}
for i in range(1,N+1):
    warp[i] = temp[i-1]
print(warp)
way = []
for k in range(1,K+1):
    if k == 1:
        way.append(warp[1])
    else:
        if warp[way[k-2]] in way:
            loop_start = way.index(warp[way[k-2]])
            loop_length = k - 1 - loop_start
            print(way[(K-loop_start)%loop_length + loop_start - 1])
            break
        else:
            way.append(warp[way[k-2]])
else:
    print(way[-1])

Submission status: Wrong Answer

Solution code:

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

#n=6
#k=727202214173249351
#a=[6, 5, 2, 5, 3, 2]

#n=4
#k=5
#a=[3,2,4,1]

if n<k:
#if 1==1:
    ai=1
    aj=set()
    for i in range(min(n,k)):
        aim=ai
        ai=a[ai-1]
        if ai in aj:
            ic2=i
            ai2=ai
            break
        else:
            aj.add(ai)

    ai=1
    for i in range(min(n,k)):
        ai=a[ai-1]
        if ai==ai2:
            ic1=i
            break

    icyc=ic2-ic1
    k2=(k-ic1)%icyc+ic1
else:
    ic1=0
    icyc=1
    k2=k
    
ai=1
for i in range(k2):
    ai=a[ai-1]
        
print(ai)

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]

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-c)%len(X)])  
else: 
    print(i+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

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

probs = [0] * n
p = 0  # 現在位置
target = 0  # はじめて2回出現する場所
h = 0  # ターゲットの初回出現時のワープ回数
t = 0  # ターゲットの2回目の出現時のワープ回数

# はじめて2回出現する場所(target)と、その時点でのワープ回数を取得
for i in range(n + 1):
    probs[p] += 1
    if (probs[p] == 2):
        target = p
        t = i
        break
    p = maps[p]

# 初回出現位置を取得(めんどうくさいのでもう一回回した。)
p = 0
for i in range(n):
    if (p == target):
        h = i
        break
    p = maps[p]

s = t - h  # 周期
d = (k - h) % s  # targetに到着後にワープすべき回数

# 実際に必要な回数だけワープ
p = 0
for i in range(h + d):
    p = maps[p]

print(p + 1)

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1

now = 0
# move = 1
nows = [0]
p = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        break
    visited[now] = 1

print('実行速度計測')

"""
now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
        print(now+1)
        sys.exit()

now = 0
for i in range(rep_start):
    now = As[now]


K -= rep_start
K %= move
now = p

for i in range(K):
    now = As[now]

print(now+1)
"""

Submission status: Wrong Answer

Solution code:
import numpy as np
s = input().split()
n = int(s[0])
k = int(s[1])
count =0

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    count += 1
    if p in fp:
        print(p)
        roop = count - fp.index(p)
        break
    else:
        fp.append(p)
print(fp)
ans = fp[fp.index(p):]
print(ans[(k-fp.index(p))%roop])

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    check=[-1]*n
    roop=0
    iti=0
    rirekin=0
    rireki=[]
    while roop==0:
        iti=a[iti]-1
        for i in range(len(rireki)):
            if check[iti]==0:
                mae=rireki.index(iti)
                Mae=rireki[:mae]
                rireki=rireki[mae:]
                roop=rirekin-mae
                break
        rireki.append(iti)
        check[iti]=0
        rirekin+=1
    if k <= mae:
        print(Mae[k-1]+1)
    else:
        k-=mae
        print(rireki[(k%roop)-1]+1)
resolve()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().strip().split()))
B=1
list=[]
for i in range(N):
    if B in list:
        num2=list.index(B)
        break
    else:
        list.append(B)
        B=A[B-1]

del list[:num2]
num=(K-num2)%len(list)
print(list[num])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*(n+1)
tmp = 0
t = 1
index = 0

for i in range(n+1):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        break

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-1])

Submission status: Wrong Answer

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

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start
#print(loop_end-loop_start,loop_start)

if len(log) >= loop:
  if loop_end>K:
    print(now)
  else:
    print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = next_i
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    index = k
    if k > start:
        index = (k - start) % l + start
    for i in range(n):
        if seen[i] == index:
            print(i + 1)
            exit()


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()