Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
N,K = map(int, raw_input().split())
A = map(int, raw_input().split())
Visited = [False]*N

S = "1"
i = 1
while not Visited[i-1]:
    Visited[i-1] = True
    i = A[i-1]
    S += str(i)

# print S
pos = S.find(S[-1])
# print pos

K -= pos
S = S[pos:-1]
# print S

ansp = K % len(S)
# print ansp
print S[ansp]

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
nxt = A[0]
cnt = 1
pos = 0

while cnt <= K:
  for i in range(N):
    nxt = A[nxt-1]
    pos = i+1
    cnt += 1

print(pos)

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,K = LI()
    A = [0] + LI()

    his = [0] * (N+1)
    s = 1
    cnt = 1
    his[1] = 1
    while his[A[s]] == 0:
        s = A[s]
        cnt += 1
        his[s] = cnt

    s = A[s]
    cnt -= his[s] - 1

    k = (K - his[s] + 1) % cnt
    for i in range(k):
        s = A[s]
    print(s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l=[]
s=1
while s not in l:
  l.append(s)
  s=a[s-1]
  
e=l.index(s)
c=len(l)-e
f=(k+1-e)%c

print(l[e+f-1])

Submission status: Wrong Answer

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

ans = A[0]

d = {0:1, ans -1:1}
#移動する可能性がある街の個数uを出す
for i in range(N):
  #print(ans)
  ans = A[ans - 1]
  if ans -1 not in d:
    d[ans -1] = 1
  else:
    d[ans - 1] += 1
  
#print("d", d, len(d))
u = len(d)

if d[0] == 1:
  a = (K - 1) % u
else:
  a = K % u
if a == 0:
  a = u
  
#print("a", a)
ans = A[A[0] - 1]
for i in range(a + 1):
  #print(i, ans)
  ans = A[ans - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
class Telp:
    def __init__(self, to, idx):
        self.to = to
        self.idx = idx
        self.visited = False
        self.step = -1

    def visit(self, step):
        self.visited = True
        self.step = step

    def is_visited(self):
        return self.visited


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

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

net = {}

for i in range(n):
    net[i] = Telp(a[i], i)

current = net[0]
count = 0
path = []

while not current.visited:
    # print(current.to)
    path.append(current)
    current.visit(count)
    current = net[current.to - 1]
    count += 1
    # print("count", count)

offset = current.step
loop = count - offset
fact_idx = (k - offset) % loop

# print("count, offset, loop, fact_idx", count, offset, loop, fact_idx)
# print("ans", path[offset + fact_idx - 1].to)

print(path[offset + fact_idx - 1].to)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 標準入力の取得
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

# 求解処理
city = 1
cities = [city]
remain_count = 0
loop = []
loop_count = 0
while True:
    city = A[city-1]
    if city in cities:
        remain_count = cities.index(city)
        loop = cities[remain_count:]
        loop_count = len(loop)
        break
    else:
        cities.append(city)

result = loop[(K - remain_count) % loop_count]

# 結果出力
print(result)

Submission status: Wrong Answer

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

b = A[0]
count = 1

X = []
flag = 0
loop = 0
ans = 0

if A[0] == 1:
    ans = 1

elif N>=K:
    for i in range(N):
        c = A[b-1]
        b = c
    ans = b
else:   
    #print("a")
    for i in range(N):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i + 1
                #print(loop)
                break


        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
            
    #print(loop)        
    Y = K%loop
    #print(Y)
    #print(Z)
    #print(Z+Y-1-loop)
    ans = A[Z+Y-1-loop]

print(ans)

Submission status: Wrong Answer

Solution code:
def L_search(A,key):
    for i in range(len(A)):
        if A[i] == key:
            return i

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

    Loop_set,Loop_list = set([1]),[1]
    pt = 1 #最初の町
    while True:
        pt = A[pt-1]
        if pt not in Loop_set:
            Loop_set.add(pt)
            Loop_list.append(pt)
        else:
            break

    ls = L_search(Loop_list,pt)
    shuki = len(Loop_list)-ls
    gosa = ls
    K = ((K-gosa)%shuki)+gosa
    print(A[K])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l = [0]*n
s = set()
x = 0

for i in range(n):
    machi = a[x]
    if machi in s:
        hajime = l.index(machi)
        repeat = l[hajime:i]
        break
    x = machi-1
    l[i] = machi
    s.add(machi)
else:
    amari = k%n
    print(l[amari-1])
    exit()

amari = (k-hajime)%len(repeat)
if hajime + amari == 0:
    print(repeat[-1])
else:
    print(l[hajime+amari-1])

Submission status: Wrong Answer

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

n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[1]
i=0
while True:
    i=a[i]
    if(i in b):
        break
    b.append(i)
    i-=1


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

Submission status: Wrong Answer

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

visit = {}
n = A[1]
cnt = 0
remain = K
# print("remain:", remain)
while(n not in visit.keys()):
    visit[n] = cnt
    cnt+=1
    n = A[n]
    remain -= 1
    if (remain==0):
        print(n)
        exit()
remain -=1

# print(n, visit[n], cnt)
d = cnt - visit[n]
# print(d)
# print("d: ", d)
# print("remain:", remain)
remain = remain%d
# print("remain:", remain)
# print("current: ", n)
while(remain > 0):
    remain -= 1
    n = A[n]
    # print("new n:", n)
print(n)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N, K = map(int, readline().split())
A = list(map(lambda x: x - 1, map(int, readline().split())))
visited = [False for i in range(N)]

cur = 0
cycle = []
start = 0
while True:
    if visited[cur]:
        start = cur + 1
        break

    visited[cur] = True
    cycle.append(cur + 1)
    cur = A[cur]

cycle_start_idx = 0
for i in range(len(cycle)):
    if cycle[i] == start:
        cycle_start_idx = i
        break
if K < cycle_start_idx:
    print(cycle[cycle_start_idx])
    exit()

cycle_len = len(cycle) - cycle_start_idx

idx = (K - cycle_start_idx) % cycle_len
print(cycle[idx + cycle_start_idx])

Submission status: Wrong Answer

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

arr = [1]
cnt = 1

ans = [0 for i in range(N+1)]

for i in range(N):
  if i == 0:
    ans[1] += 1
    s = List[i]
    arr.append(s)    
  else:
    if ans[s] == 1:
      b = arr.index(List[s-1])
      break
    else:
      ans[s] += 1
      s = List[s-1]
      arr.append(s)
            
l = len(arr)
c = int( (K-b) % (l-b) )

if K-b > 0:
  print(arr[b+c])
else:
  print(arr[K-1])

Submission status: Wrong Answer

Solution code:
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0
t = 1
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
f = [0] * n
c = 0
d = 0
for i in range(k):
    if (f[t-1] == 1):
        d = 1
        break
    f[t-1] = 1
    t = a[t-1]
b = t
if(d == 1):
    for i in range(k):
        t = a[t-1]
        c = c + 1
        if t == b:
            break
    k = k%c
    t = 1
    for i in range(k):
        t = a[t-1]
print(t)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
An = list(map(int, input().split()))
root = [1]
i = 0
dup = 1
cnt = 1
for _ in range(n):
    temp = An[i]
    if temp in root:
        dup = temp
        break
    root.append(temp)
    i = temp - 1
    cnt += 1
index = root.index(dup)
print(root[(k-index) % (cnt-index) + index])

Submission status: Wrong Answer

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


def calc(N, K, A):
    n = 1
    k = 0
    d = {n: k}
    while True:
        k += 1
        n = A[n - 1]
        if k == K:
            return n
        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:
#int(input())
#int(x) for x in input().split()
#map(int,input().split())
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
co=0
s=set([1])
i=0
path=[1]
j=0
while True:
	co+=1
	path.append(a[i])
	if a[i] in s:
		j=path.index(a[i])
		break
	else:
		s.add(a[i])
	i=a[i]-1

k-=j
path=path[j+1:]
k-=1
k=k%(len(path))
print(path[k])

Submission status: Wrong Answer

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

now = 1
if k % n == 0:
    k = n
else:
    k = k % n

for i in range(k):
    now = a[now-1]

print(now)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def teleport(k, a):
    l = [1]
    b = a[0]
    flag = True
    while flag:
        # print(b)
        if a[b-1] in l:
            flag = False
            l.append(b)
        else:
            l.append(b)
            b = a[b-1]
            
    print(l)
    r = l.index(a[b-1])
    # print(r)
    new_l = l[r:]
    # print(new_l)
    print(new_l[(k+1-r)%(len(l)-r)-1])
    # print(l[(k+1)%len(l)-1])

n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
teleport(k, a)

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=[-1 for x in range(towns)]
rt[0]=0
now=0
for x in range(1,300000):
  rec=now
  now=al[now]
  if rt[now]!=-1:
    stp=x
    break
  else:
    rt[now]=x

ll=stp-rt[now]
k2=k-rt[now]
print(rt.index(k2%ll+rt[now])+1)

Submission status: Wrong Answer

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

visited = [-1] * N
visited[0] = 0
nxt = 1
cnt = 1
for i in range(N):
    nxt = A[nxt-1]
    if visited[nxt-1] == -1:
        visited[nxt-1] = cnt
        cnt += 1
    else:
        cycl = i + 1 - visited[nxt-1]
        m = visited[nxt-1]
        break

k = (K - m) % cycl + m

for i in range(N):
    if visited[i] == k:
        print(i+1)
        break

Submission status: Wrong Answer

Solution code:
# D - Teleporter

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

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

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

print(done[steps]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(" "))
a = list(map(int,input().split(" ")))
here = 1
past = []
for x in range(k):
    here = a[here-1]
    past.append(here)
    for z in range(1,len(past)//2):
        if past[len(past)-z*2:len(past)-z]==past[len(past)-z:]:
            print(past[(k-x)%z])
            exit()
print(here)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
ischecked=[0]*N
move=[]
move1=[]
now=0
count=0
count1=0
while 1:
    move.append(now+1)
    nex=A[now]-1
    ischecked[now]=1
    if ischecked[nex]==1:
        roop=nex+1
        break
    now=nex
if K>len(move):
    for i in range(len(move))[::-1]:
        move1.append(move[i])
        if move[i]==roop:
            b=len(move)-i
            break
    n=K-len(move)
    n=n%len(move1)
    print(move1[len(move1)-1-n])
else:
    print(move[K-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict,Counter

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,K = inputMap()
	A = inputList()

	if N == 1:
		print(1)
		sys.exit()

	mati = [0 for i in range(N)]
	for i,val in enumerate(A):
		mati[i] = val-1

	tugi = 0
	gizi = {}
	gizi2 = {}
	saigo = inf
	ans = []
	cnts = 0
	flg = False
	for i in range(K):
		tugi = mati[tugi]
		if flg == False:
			cnts += 1
			if tugi in gizi:
				flg = True
				ans.append(saigo)
				ans.append(tugi)
				gizi2[saigo] = 1
				gizi2[tugi] = 1
			else:
				gizi[tugi] = 1
				saigo = tugi
		else:
			if tugi in gizi2:
				break
			else:
				ans.append(tugi)
				gizi2[tugi] = 1

	if len(ans) == 0:
		print("1")
		sys.exit()

	tmp = len(ans)
	cnts = cnts - len(ans)
	kotae = (K-cnts+1) % (tmp)

	#print(cnts)
	#print(kotae)
	#print(ans)
	print(ans[kotae]+1)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

current = 1
st = 0
fin = 0
pat = [0] * n
index = 0

for i in range(n):
    if current in pat:
        st = pat.index(current)
        fin = i
        break
    pat[i] = current
    current = a[current-1]

index = (k - st)%(fin-st) + st
print(pat[index])

Submission status: Wrong Answer

Solution code:
# D

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

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)


if N > K:
    compute(1, K)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i

# 同一ループ
if loop_end - loop_start == 1:
    print(city)
div = (K - loop_start) // (loop_end - loop_start + 1)
# print(loop_start, loop_end, div, K % (loop_end - loop_start + 1))
compute(city, K % (loop_end - loop_start + 1) + loop_start + 1)

Submission status: Wrong Answer

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

now = 1

while(1):
    next = a[now-1]
    now = next
    if not now in town_list:
        town_list.append(now)
    else:
        idx = town_list.index(now)
        initial = town_list[:idx]
        cycle = town_list[idx:]
        break

end_idx = (k-len(initial))%len(cycle)
print(cycle[end_idx])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)

n,k = map(int,input().split())
l = list(map(int,input().split()))
e = [[] for i in range(n+1)]
for i in range(n):
    e[i+1].append(l[i])

dis = []
def dfs(x,k):
    if len(dis) == k:
        print(dis[-1])
        exit()
    a = e[x][0]
    if a in dis:
        index = dis.index(a)
        k -= index
        le = len(dis)-index
        
        print(dis[k%le+index-1])
        exit()
    else:
        dis.append(a)
        dfs(a,k)

dfs(1,k)

Submission status: Wrong Answer

Solution code:
def main():

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

    zzz=[]
    oioi=0
    flg=True

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

    print(zzz)

    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
destiny = {}

numOfTowns, numOfTravel = map(int,input().split())
destinies = list(map(int,input().split()))

for i in range(0,numOfTowns):
    destiny[i+1] = destinies[i]

beginner = 1
finded = False
for i in range(1,numOfTowns+1):
    for j in range(1,numOfTowns+1):
        if i == destiny[j] and i != j:
            beginner = i
            finded = True
            break

    if finded:
        break

stepsToBeginner = 1
if beginner==1:
    stepsToBeginner = 0
else:
    count = 0
    tempBeginner = 1
    tempArrival = tempBeginner
    while True:
        tempBeginner = tempArrival
        tempArrival = destiny[tempBeginner]
        count += 1

        if tempArrival == beginner:
            stepsToBeginner = count
            break

tempArrival = beginner
repeatedStep = 0

while True:
    tempBeginner = tempArrival
    tempArrival = destiny[tempBeginner]
    repeatedStep += 1
    if tempArrival == beginner:
        break


simplifiedSteps = (numOfTravel - stepsToBeginner) % repeatedStep + stepsToBeginner

beginningPlace = 1
arrivingPlace = beginningPlace
for i in range(0,simplifiedSteps):
    beginningPlace = arrivingPlace
    arrivingPlace = destiny[beginningPlace]

print(arrivingPlace)

Submission status: Wrong Answer

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

d = a[0]
b = [d]

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

Submission status: Wrong Answer

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

j = 1
lis = [1]
loop = 0
for i in range(K):
    j = A[j-1]
    if not j in lis:
        lis.append(j)
    else:
        n = lis.index(j)
        loop = len(lis) - n
        break

if loop == 0:
    print(j)
else:
    print(A[n + (K - len(lis)) % loop])

Submission status: Wrong Answer

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

visited = [-1] * N
visited[0] = 0
nxt = 1
cnt = 1
for i in range(N):
    nxt = A[nxt-1]
    if visited[nxt-1] == -1:
        visited[nxt-1] = cnt
        cnt += 1
    else:
        cycl = i + 1 - visited[nxt-1]
        m = visited[nxt-1]
        break
print(visited, cycl)

k = (K - m) % cycl + m

for i in range(N):
    if visited[i] == k:
        print(i+1)
        break

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

a.insert(0, 'hoge')
# a = dict(list(enumerate(a)))
amari = k % n

tmp = 1
# print(amari)
for i in range(1, n + amari+1):
    # print(tmp)
    tmp = a[tmp]

print(tmp)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
ans = 1
cir = 0
a = 0
for i in range(n):
  sub[i] = ans
  if A[ans-1] in sub:
    a = sub.index(A[ans-1])
    cir = i+1 - a
    break
  else:
    ans = A[ans-1]
print(sub[a + (k - a) % cir])

Submission status: Wrong Answer

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

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

visited=[0]*n
if k<=n:
    now=0
    nex=0
    loop_start=0
    target=0

    for i in range(n):
        now=nex
        visited[now]+=1
        nex=a[now]-1
        if i+1==k:
            print(nex+1)
            exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
s=set()
flag=False

for i in range(2*n):
    now=nex
    visited[now]+=1
    nex=a[now]-1
    if flag:
        if now+1 not in s:
           lis.append(now+1)
           s.add(now+1)
           continue
        else:
            break
    if visited[now]==2:
        loop_start=i
        target=now+1
        lis.append(now+1)
        flag=True


dif=len(lis)
first=loop_start-dif
k-=first
print(lis[k%dif])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
L.insert(0,-1)
path=[1]
count=0
x=1
t=-1
flag=False
for i in range(N+1):
    ne=L[x]
    if ne in path:
        flag=True
        t=path.index(ne)
        break
    path.append(ne)
    x=ne
if flag==True:
    loop=path[t::]
    if K<=(len(path)):
        print(path[K-1])
    else:
        K=K-len(path)
        K=K%len(loop)
        print(loop[K])
else:
    print(path[K-1])

Submission status: Wrong Answer

Solution code:
from collections import Counter

(town, tele)=[int(x) for x in input().split()]
pos=[int(x) for x in input().split()]
move=[pos[0]]
while Counter(move).most_common()[0][1] <= 1:
  move.append(pos[move[-1]-1])
loop_move=move[move.index(move[-1]):-1]
print(loop_move[(tele-(len(move)-1))%len(loop_move)-1])

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n, k = map(int, input().split())
    a = [int(i) for i in input().split()]
    costList = [-1]*n
    maxCount = max(costList)+1
    now = 0
    for i in range(k):
        now = a[now-1]
    print(now+1)

Submission status: Wrong Answer

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

cnt = 0
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y == 1:
        break
    x = y

if N < K:
    s = K % cnt
else:
    s = K
x = 1
for i in range(s):
    y = A[x-1]
    x = y
print(x)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [0] + a
tele = [1]
for i in range(1, n+1):
    tele.append(a[tele[-1]])
    descover = False
    for i in range(len(tele)-1):
        if tele[i] == tele[-1]:
            for_roop_begin = i
            roop_begin = tele[i]
            roop_length = len(tele)-i-1
            descover = True
            break
    if descover == True:
        break
now = roop_begin
for i in range((k-for_roop_begin)%roop_length):
    now = a[now]
print(now)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
point = 1 
for i in range(K%len(A)) :
    point = A[point -1 ]
print(point)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
checked=[False]*(1+n)
checked_num=[0]*(1+n)
checked[1]=True
tree = [1]
ind = 1
count = 0
for _ in range(k):
    count += 1
    ind_ = a[ind-1]
    if checked[ind_]:
        cycle = count-checked_num[ind_]
        tree = tree[checked_num[ind_]:]
        print(tree[(k-count)%cycle])
        break
    checked_num[ind_]=count
    checked[ind_]=True
    tree += [ind_]
    ind = ind_

Submission status: Wrong Answer

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

lc = [1]
lcnt = [1]
lcnt.extend([0]*(N-1))
print(lcnt)
i = 1
while lcnt[lc[i-1]-1] == 1:
    if i == N:
        break
    else:
        lc.append(la[lc[i-1]-1])
        lcnt[la[lc[i-1]-1]-1] += 1
        i += 1
print("lcnt =", lcnt)
print("lc =", lc)

lpst = lc.index(lc[-1])
lpnum = len(lc) - lpst - 1
if K <= lpst:
    loclst = K
elif lpnum == 0:
    loclst = lpst
else:
    loclst = (K - lpst) % lpnum + lpst
print(lpst, lpnum)
print(lc[loclst])

Submission status: Wrong Answer

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

visited = [0] * n
visited[0] = 1
visit_list = [0]
visit = 0
while True:
    new_visit = a[visit]
    if visited[new_visit] == 1:
        break
    visited[new_visit] = 1
    visit_list.append(new_visit)
    visit = new_visit

period_first_index = visit_list.index(a[visit_list[-1]])
period_list = visit_list[period_first_index:]

if k <= len(visit_list):
    print(visit_list[k%len(visit_list)]+1)
else:
    print(period_list[(k-period_first_index)%len(period_list)]+1)

Submission status: Wrong Answer

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

i = 0
loop = [1]

while True:
  if a[i] in loop:
    loop.append(a[i])
    break
  else:
    loop.append(a[i])
    i = a[i] - 1
    
print(loop[-1])