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, m = map(int,input().split())
a = list(map(int,input().split()))
L = [1]
La = [0] * n
La[0] = 1
tmp = a[0]
while True:
    L.append(tmp)
    if La[tmp-1] == 0:
        La[tmp-1] = 1
        tmp = a[tmp-1]
    else:        
        break
base = len(L) - L.index(L[-1]) - 1
ans = (m - L.index(L[-1])) % base + L.index(L[-1])
print(L[ans])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int,input().split())
a = [int(i) for i in input().split()]

tmp = 1
ans = [0 for i in range(2 * n)]
ans[0] = 1
flag = True
for i in range(2 * n - 1):
    tmp2 = a[tmp-1]
    if tmp2 in ans and flag:
        count = i+1
        flag = False
    ans[i+1] = tmp2
    tmp = tmp2
#print(ans, count)

if k < n:
    print(ans[k])
    #print('a')
    exit()

if ans[0] == ans[count]:
    tmp3 = count - ans.index(tmp2) + 1
    tmp3 = tmp3 % k + 1
    print(ans[ans.index(tmp2) + tmp3])
    #print('a')
else:
    tmp3 = count - ans.index(tmp2)
    #print('b')
    #print(tmp3)
    tmp3 = tmp3 % k
    print(ans[ans.index(tmp2) + tmp3])

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
n, k = map(int, readline().split())
A = list(map(int, readline().split()))

visited = [False] * n
visited[0] = True
l = [1]
while True:
    x = A[l[-1]-1]
    if visited[x-1]:
        break
    l.append(x)
    visited[x-1] = True
# print(l[l.index(x):])
if l[-1] == x:
    print(x)
else:
    print(l[l.index(x):][(k-l.index(x)) % len(l[l.index(x):])])

Submission status: Wrong Answer

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

tmp = [1]
now = 1
while True:
    next = A[now-1]
    if next in tmp:
        break
    tmp.append(next)
    now = next

print(tmp[tmp.index(next):][(K-tmp.index(next)) % len(tmp[tmp.index(next):])])

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
c = [-1]*N
c[0] = 0
now = [0,0]

while True:
    now[0] = A[now[0]]
    now[1] += 1
    if c[now[0]] == -1:
        c[now[0]] = now[1]
    else:
        a = now[1] - c[now[0]]
        K %= a
        now = [now[0],now[1]%a-a]
        while True:
            now[0] = A[now[0]]
            now[1] += 1
            if K == now[1]:
                print(now[0]+1)
                exit()

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
loop_size = 0
loop = [None]*N
now = 0
for i in range(N):
    loop_size += 1
    to = A[now] - 1
    now = to
    loop[i] = to
    if to == 0:
        break
print(loop[(K + loop_size -1) % loop_size]+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()

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

Submission status: Wrong Answer

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

# 自己ループ
if A[0] == 1:
    print(1)
    exit()

is_self = len([i for i in range(N) if A[i]-1 == i])
loop = N - is_self

seen = [-1] * N
seen[0] = 0
next = 0

# たかだか N 回回ればループに入る
for i in range(min(K, N)):
    next = A[next]-1
    if next in seen[:i]:
        last = i
        break
    seen[i+1] = next

# loop なしの場合
if A[next]-1 not in seen[:min(K, N)]:
    print(A[next])
    exit()

# それ以外
next_ind = seen[:last+1].index(next)
loop = seen[next_ind:last+1]
#print(loop)

mod = (K - next_ind) % len(loop)
ans = loop[mod]+1

print(ans)

Submission status: Wrong Answer

Solution code:
# Problem D - Teleporter

# input
N, K = map(int, input().split())
a_nums = list(map(int, input().split()))

# initialization
visit_list = [0]*N
visit_list[0] = 1
pos = a_nums[0]
visit_count = 1

while visit_list[pos-1]==0:
    visit_count += 1
    visit_list[pos-1] = visit_count
    pos = a_nums[pos-1]
visit_count += 1

# calc 1
tmp_1 = visit_count - visit_list[pos-1]
tmp_2 = (K-visit_count) % tmp_1
# tmp_2回分ループ処理を行う
for i in range(tmp_2+1):
    pos = a_nums[pos-1]

# output
print(pos)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]
    pos = 1
    tmp = [pos]
    while True:
        pos = As[pos - 1]
        if pos in tmp:
            idx_s = tmp.index(pos)
            break
        tmp.append(pos)
    K = K - idx_s
    print(tmp[idx_s:][K % (len(tmp) - idx_s)])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, k = LI()
    a = LI_()
    d = defaultdict(int)
    i = a[0]
    t = 1
    d[0] = 1
    while not d[i]:
        t += 1
        d[i] = t
        i = a[i]
    if t >= k:
        i = 0
        for _ in range(k):
            i = d[i]
        print(i + 1)
    else:
        k -= d[i] - 1
        k %= (t - d[i] + 1)
        for _ in range(k):
            i = a[i]
        print(i + 1)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10000000)
N,K = [int(v) for v in input().split()]
A = [int(v) for v in input().split()]

loop = []
pre = []
visited = set()

def dfs(index,now):
    global pre
    global loop
    loop.append(now)
    if now in visited:
        for left in range(len(loop)):
            if now == loop[left]:
                break
        pre = loop[:left]
        loop = loop[left:index]
        return
    else:
        visited.add(now)
        dfs(index+1,A[now-1])
dfs(0,1)

if len(pre) >= K:
    print(pre[K-1])
else:
    K-=len(pre)
    print(loop[K%len(loop)])

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = [int(x) for x in input().split()]
    L=[1]
    a = A[0]
    for _ in range(N-1):
        L.append(a)
        a=A[a-1]
        if a==1:
            break
    g = K%len(L)
    print(L[g])
    
main()

Submission status: Wrong Answer

Solution code:
def main():

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

    zzz=[]
    oioi=0

    for i in range(1,n+1):
        oioi = a[oioi]-1
        zzz.append(oioi)
        if oioi == 0:
            break
    
    print(zzz[k%len(zzz)-1]+1)

    

if __name__ == '__main__':
    main()

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]
while 1:
    if has_duplicates(c):
        break
    d=c[-1]-1
    c.append(A[d])
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 = map(int,input().split())
a = [int(i) for i in input().split()]
done = []

def ml(a):
	cnt=1
	nxt=a[0]-1
	done.append(0)
	done.append(nxt)
	while(True):
		nxt = a[nxt]-1
		if nxt in done: 
			m = done.index(nxt) - 1
			l = cnt - m
			return (m+1,l)
		else: done.append(nxt)
		cnt+=1

# print(is_1(a))
m,l = ml(a)
num = (k-m)%l

# print(f"m={m},l={l}")
# print(f"done={done}")
# print(f"m+num={m+num}")
print(done[m+num]+1)

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 not n in d:
            d[n] = k
        else:
            if k - d[n] == 1:
                return n
            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:
import sys
n,k = map(int, input().split())

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

former = 1
has_come = [1]


for i in range(k):
    next_town = tele[former - 1]
    if next_town in has_come:
        break
    has_come.append(next_town)
    former = next_town
else:
    sys.exit(0)


loop_start = tele[former - 1]
loop_start_idx = has_come.index(loop_start)

last_tele = k - i
loop = has_come[loop_start_idx:]
print(loop[last_tele % len(loop) - 1])

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    d = []
    d_set = set()
    s = 1
    for _ in range(N):
        if s in d_set:
            if K < len(d):
                print(d[K])
            idx = d.index(s)
            K -= idx
            d = d[idx:]
            break
        d.append(s)
        d_set.add(s)
        s = A[s-1]

    # print(idx, d)
    K %= len(d)
    print(d[K])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0 or len(root)<k+2):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

if len(root)==k+1:
  print(root[-1])
else:
  fidx = root.index(root[-1])
  lidx = len(root)-1
  lp   = lidx-fidx

  cnt = (k-fidx)%lp

  if cnt==0:
    print(root[-1]+1)
  else:
    que.append(root[-1])
    for i in range(cnt):
      v = que.pop()
      que.append(li[v])
    print(que.pop()+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
cur = 0
reach = [False]*n
x = []
for i in range(k):
    x.append(cur)
    if reach[cur]:
        break
    reach[cur]=True
    cur = a[cur]-1
cnt = 0
for i in range(len(x)):
    cnt += 1
    if x[i]==x[-1]:
        break
y = x[cnt-1:-1]
if k>cnt:
    ind = (k-cnt+1)%len(y)
    print(y[ind]+1)
else:
    print(x[k%len(x)]+1)

Submission status: Wrong Answer

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		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 -1*self.parents[self.find(x)]

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

	def members(self,x): # much time
		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()} # 1~n

n,k = readInts()
a = readInts()
ex = [0]*n
ex[0] = 1
ex_r = [1]
f = 1
l = 1
count = 0
while 1:
	l = a[l-1]
	ex_r.append(l)
	if ex[l-1]==1:
		break
	ex[l-1]=1

first = ex_r.index(ex_r[-1])
ex_r.pop()
repeat = len(ex_r)-first

first = max(first,1)

if first>=k:
	print(ex_r[k])
else:
	amari = (k-(first-1))%repeat
	print(ex_r[first+amari-1])

Submission status: Wrong Answer

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

init = [1]
while True:
    nxt = a[init[-1]-1]
    if nxt in init:
        idx_nxt = init.index(nxt)
        len_init = idx_nxt
        len_loop = len(init) - idx_nxt
        break
    else:
        init.append(nxt)

idx = (k-len_init)%len_loop
print(init[len_init+idx])

Submission status: Wrong Answer

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

done = [0]*N
now = 0
cnt = 1
while True:
    if done[now] != 0:
        start = done[now] - 1
        cycle = cnt - start - 1
        break
    else:
        done[now] = cnt
        now = A[now]-1
        cnt += 1
if K>=start:
    K -= start
    K %= cycle
for _ in range(K):
    now = A[now]-1
print(now+1)

Submission status: Wrong Answer

Solution code:
pi = 3.1415926

############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n,k = invr()
nums = [0] + inlt()
cycle = [nums[1]]

for i in range(n + 1):
    if nums[cycle[-1]] != cycle[0]:
        cycle.append(nums[cycle[-1]])
    else:
        break

if k <= len(cycle):
    print(cycle[k - 1])
else:
    remain = k % len(cycle)
    print(cycle[remain - 1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!#
#N,W = list(map(int,input().split()))
#w = [i for i in range(N)]


N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
for i in range(N):
    A[i] -= 1

C = [[] for i in range(N)]
for i in range(N):
    C[A[i]].append(i) #町Aiにいく町の番号

B = [0]
D = [0 for i in range(N)]
D[0] = 1
E = 0
for i in range(N):
    B.append(A[B[i]])
    D[B[i]] += 1
    if D[B[i+1]] != 0:
        break
    E += 1

for j in range(N-1):
    if B[j] == B[E+1]:
        break

if K < j:
    print(D[K])
else :
    print(B[j + (K-j)%(E-j+1)]+ 1)

Submission status: Wrong Answer

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

def solve():
    visited = [-1]*N
    visited[0] = 0
    goto = 1
    counter = 0
    period = 0
    while 1:
        counter += 1 #counter 回ワープした
        goto = A[goto-1] #counter回目につく場所

        if counter == K:
            print(A)
            return 0

        if visited[goto-1] == -1:
            visited[goto-1] = counter
        else:
            period = counter - visited[goto-1]
            break

    
    warp = 0
    here = 1
    while here != goto:
        warp += 1
        here = A[here-1]

    peri_and_rem = K - warp
    rem = peri_and_rem % period

    while rem > 0:
        rem -= 1
        here = A[here - 1]


    print(here)
    return 0
        



solve()

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
b=[1]
s_b=set()
s_b|={1}
for i in range(n):
  if a[b[-1]-1] in s_b:
    idx=b.index(a[b[-1]-1])
    break
  else:
    b.append(a[b[-1]-1])
    s_b|={b[-1]}
mod=len(b[idx:])
d=len(b[:idx])
print(b[d+(k-d)%mod])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
machi=list(map(int,input().split()))
x = machi[0]
def kansu(now):
    count = 2
    if count <= k:
        now = machi[now-1]
        count += 1
        return now
print(kansu(x))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [0]
cur = 0
for i in range(n):
    if a[cur]-1 in b:
        loop = len(b[b.index(a[cur]-1):])
        break
    else:
        b.append(a[cur]-1)
        cur = a[cur]-1
c = b[len(b)%loop:]
pos = (k-len(b)%loop)%loop
print(c[pos]+1)

Submission status: Wrong Answer

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

(N, K) = (int(i) for i in input().split())

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

# 一周する数を求める
have_been = [1]
now_place = 1
while True:
    next_place = A[now_place - 1]
    if next_place not in have_been:
        have_been.append(next_place)
        now_place = next_place
    else:
        break

#print('have_been = {}'.format(have_been))

loop = have_been[have_been.index(next_place):]
#print('loop = {}'.format(loop))

left_K = K - have_been.index(next_place)
if left_K <= 0:
    print(have_been[K - 1])
    sys.exit()

mod = left_K % len(loop)
print(have_been[mod + have_been.index(next_place)])

Submission status: Wrong Answer

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

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

for i in range(len(turn_town)-1):
    if turn_town[i] == turn_town[-1]:
        roop_index = i

if K-1 <= len(turn_town)-1:
    print(turn_town[K])
else:
    K -= roop_index+1
    turn_town = turn_town[roop_index+1 : len(turn_town)]
    K %= len(turn_town)
    print(turn_town[K]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
once = True
idx = 0
checked = [1]
last = -1
while once:
  if A[idx] in checked:
    once = False
    last = A[idx]
  else:
    checked.append(A[idx])
  idx = A[idx]-1
#print(checked,last)
l = len(checked)
cycle_len = l - checked.index(last)
extra = checked.index(last)
k -= extra
mod = k % cycle_len
print(checked[mod + extra])

Submission status: Wrong Answer

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

done = [0]*N
now = 0
cnt = 1
while True:
    if done[now] != 0:
        start = done[now] - 1
        cycle = cnt - start - 1
        break
    else:
        done[now] = cnt
        now = A[now]-1
        cnt += 1
if K>=start:
    K -= start
    K %= cycle
for _ in range(K):
    now = A[now]-1
print(now+1)
print(start, cycle, now, done)

Submission status: Wrong Answer

Solution code:
b0=input().split()
towns=int(b0[0])
k=int(b0[1])

al=[int(i)-1 for i in input().split()]

rt=[0]
now=0
for x in range(200000):
  now=al[now]
  if now in rt:
    break
  else:
    rt.append(now)
    
ll=len(rt)-rt.index(now)
ft=rt.index(now)
k2=k-ft
stp=k2%ll
print(rt[ft+stp]+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
flag = False

for i in range(1, N):
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    if memo[i] in memo[:i] and flag == False:
        repeat_point = i
        flag = True

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
    print(memo[mod-1])

Submission status: Wrong Answer

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    route = [1]
    pre = 1
    start = 0

    while True:
        now = A[pre-1]
        if now in route:
            break
        route.append(now)
        pre = now

    start = route.index(now)
    # print(route,start)

    if start+1 != N:
        K -= start
        loop = route[start:]
        # print(loop,K)
        M = len(loop)
        K %= M
        res = loop[K]
    else:
        res = route[K-1]

    print(res)



main()

Submission status: Wrong Answer

Solution code:
n,k= map(int,input().split())
temp = list(map(int,input().split()))
As = [0]*(n+1)
for i in range(n):
    As[i+1] = temp[i]

visit_list = {}
now = 1
visit_list[1] = 0
visit_num = 1

while True:
    now = As[now]
    if now in visit_list:
        break
    visit_list[now] = visit_num
    visit_num += 1

loop_num = visit_num - visit_list[now]

res = (k-visit_list[now])%loop_num

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

print(now)

Submission status: Wrong Answer

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

cnt = [0]*N
town = 1
for i in range(K):
    if cnt[town-1] ==1:
        break

    cnt[town-1] += 1
    town = A[town-1]


ans1 = sum(cnt)

import copy
town2 = copy.deepcopy(town)

cnt2 = [0]*N

for i in range(K):
    if cnt2[town2-1] ==1:
        break

    cnt2[town2-1] += 1
    town2 = A[town2-1]
    
ans2 = sum(cnt2)

if K <= (ans1-ans2):
    print(town)
else:
    town3 = 1
    cnt3=[0]*N
    K2 = ((K-(ans1-ans2))%(ans2))+(ans1-ans2)
    for i in range(K2):
        if cnt3[town3-1] ==1:
            break

        cnt[town3-1] += 1
        town3 = A[town3-1]
    print(town3)

Submission status: Wrong Answer

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

log = []

def func(x, iter):
    x = A[x-1]
    if x in log:
        tmp = K%iter
        return log[tmp]
    else:
        log.append(A[x-1])
        return func(x, iter+1)

print(func(1, 0))

Submission status: Wrong Answer

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

roop = [1]
start=0

for i in range(N):
    roop.append(A[roop[-1]-1])
    if roop[-1] in set(roop[:-1]):
        start=roop.index(roop[-1])
        break
print(roop[start:-1][(K-start)%len(roop[start:-1])])

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
k_reserved = -1
for i in range(1, N+1):
    current = A[current-1]
    if i == K:
        k_reserved = current
    if current == 1:
        break

if K < i:
    print(k_reserved)
else:
    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())
l = list(map(int,input().split()))
curr_town = 1

for i in range(k) :
    temp = l[curr_town - 1]
    curr_town = l[temp - 1]

print(curr_town + 1)

Submission status: Wrong Answer

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

# 自己ループ
if A[0] == 1:
    print(1)
    exit()

is_self = len([i for i in range(N) if A[i]-1 == i])
loop = N - is_self

seen = [-1] * N
seen[0] = 0
next = 0

# たかだか N 回回ればループに入る
for i in range(min(K, N)):
    next = A[next]-1
    if next in seen:
        last = i
        break
    seen[i+1] = next

# loop なしの場合
if A[next]-1 not in seen:
    print(A[next])
    exit()

# それ以外
next_ind = seen.index(next)
loop = seen[next_ind:last+1]
#print(loop)

mod = (K - next_ind) % len(loop)
ans = loop[mod]+1

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    g = [1]
    for i in range(len(A)-1):
        prev_p = g[-1]
        g.append(A[prev_p-1])
        
    if K <= N - 1:
        print(g[K])
    else:
        new_g = g[g[-1]-1:]
        K_ = K - N + 1
        new_K = K_ % len(new_g)
        print(new_g[new_K])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a = 1
for i in range(N):
    a = l[a-1]
    m.append(a)
    if len(m) > len(set(m)):
        break
y = m.index(a)

x = (K-y) % (len(m)-1-y)

if K > len(m):
    print(m[x-1+y])
else:
    print(m[K-1])

Submission status: Wrong Answer

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

D=[[0,0] for _ in range(N)]
D[0]=[1,0]
n=0
i=0

while n<N:
    n+=1
    i=A[i]-1
    D[i][0]+=1
    if D[i][0]==2:
        init=D[i][1]
        loop=n-init
        break
    else:
        D[i][1]=n
if n==N:
    init=0
    loop=n

k=(K-init)%loop

j=0
m=0
while m<=init+k+1:
    m+=1
    j=A[j]-1
print(A[j])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(e) for e in input().split()]
 
next_map = {}
 
for i, e in enumerate(A):
    next_map[i+1] = e
 
 
history = ""
here = 1
p_i = 0
unit_length = 0
history += str(here)
 
for i in range(2*N):
    here = next_map[here]
    if str(here) in history:
        p_i = history.index(str(here))
        n_i = i
        unit_length = n_i - p_i + 1
        break
    history += str(here)
 
 
if K < p_i:
    print(history[K])
else:
    print(history[p_i + (K-p_i) % unit_length])

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=[]
while flag==0:
  now=A[now]-1
  r.append(now)
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
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])-2]])

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
A = np.array([int(x) for x in input().split()])
B = np.zeros(N)
C = np.zeros(N)
pos = 0
for i in range(N*2):
    B[pos] += 1
    if C[pos] == 0:
        C[pos] = i+1
    pos = A[pos] - 1
ans = K - len(B[B <= 1])
ans %= len(B[B > 1])
D = sorted(C[B > 1])
tmp = D[ans]
index = A[C == tmp]
print(*index)

Submission status: Wrong Answer

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

route = [1]
ind = A[0]
while len(route) <= N:
    if ind in route:
        b_ind = route.index(ind)
        break
    route.append(ind)
    ind = A[ind-1]

a = (K-b_ind)%(len(route)-b_ind)
print(route[a+b_ind])

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
newlist = [1,A[0]]
num = A[0] - 1
ans = [0]*N
ans[0] = 1
ans[A[0]-1] = 1
b = 0
c = 0
r = 0
for i in range(10000000000000000000):
    if(ans[A[num]-1] == 1):
        b = A[num]
        break
    newlist.append(A[num])
    ans[A[num]-1] = 1
    num = A[num]-1
for i in range (len(newlist)):
    if(newlist[i] == b):
        c = i
r = len(newlist)-c
a = (K-c)%r
print(newlist[c+a])

Submission status: Wrong Answer

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

n = 1
rooper = -1
L = []
for i in range( K ):
  n = A[ n - 1 ]
  if n in L:
    rooper = n
    break
  else:
    L.append( n )
if rooper == -1:
  print( n )
else:
  index = L.index( rooper )
  print( L, index, rooper )
  if len( L ) - index > 0:
    a = ( K - index ) % ( len( L ) - index )
  else:
    a = 0
  print( L[ index + a - 1 ] )

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
ka=0
kab=0
sihyou=0
while sihyou==0:
    
    
    if b[ka]==1:
        sihyou=1
    else:
        b[ka]=1
        c[ka]=kab
        ka=a[ka]-1
        kab=kab+1
ruupu=kab-c[ka]
ansa=(k-c[ka])%ruupu+c[ka]
print(a[ansa])

Submission status: Wrong Answer

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

    base, cycle = 0, 0
    order = [-1] * N
    order[0] = 0
    cnt = 1
    idx = A[0] - 1
    while True:
        if order[idx] == -1:
            order[idx] = cnt
            cnt += 1
            idx = A[idx] - 1
            continue
        base = order[idx]
        cycle = cnt - order[idx]
        break

    rest = (K - base) % cycle
    ans = order.index(base + rest) + 1
    print(ans)

abc167_d()

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
tele = list(map(int, input().split()))
loc = 1
act = [0]*n
count = 1
log = []
log.append(loc)
flg = True
while True:
  newloc=tele[loc-1]
  if count ==k:
    print(newloc)
    flg = False
  log.append(newloc)
  #print(newloc)
  if act[newloc-1] == 0:
    act[newloc-1] =count
  else:
    loop=count-act[newloc-1]
    loopstart=act[newloc-1]
    break
  count+=1
  loc=newloc

  

if flg:
  num = (k-(loopstart-1)) % loop 
  ans=log[loopstart-1+num]
  print(ans)
#print(loop)
#print(loopstart)
#print(count)
#print(act)
#print(log)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [1]+a
v = [-1 for _ in range(n+1)]
v[1] = 0
x = a[1]
c = 1
k -= 1
while k > 0 and v[x] < 0:
    y = a[x]
    v[y] = v[x]+1
    x = y
    k -= 1
    c += 1

if k==0:
    print(x)
else:
    z = c - v[x]
    k = k%z
    while k > 1:
        k -= 1
        x = a[x]
    print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

l=[1]

for i in a:
    b=a[l[-1]-1]
    l.append(b)

aa=len(l)
x=(k-1)%aa-1
y=a[x-1]
print(y)