Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
import itertools

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

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

Loop=1
Next=1

found=[0]*N
while(True):
    found[Next-1]=1
    Next=A[Next-1]
    if(found[Next-1]==1):
        break
    Loop+=1

K=K%(Loop)
#print(Loop)
for i in range(K+Loop):
    Next=A[Next-1]

print(Next)

Submission status: Wrong Answer

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

now = 1
r = [0]*N
r[0]=now
for i in range(N):
  if A[now-1] in r:
    x = r.index(A[now-1])
    break
  else:
    now = A[now-1]
    r[i]=now
r_x = r[x:]
print(r_x[(K-x)%len(r[x:])])

Submission status: Wrong Answer

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

h = set([])

c = 0
for i in range(2 * n):
  c = a[c] - 1
  if n < i:
    if c + 1 in h:
      break
    else:
      h.add(c + 1)

c = 0
d = 0
for i in range(n):
  c = a[c] - 1
  if c + 1 in h:
    break
  else:
    d += 1

c = 0
if d >= k:
  for i in range(k):
    c = a[c] - 1
else:
  for i in range(d + ((k - d) % len(h) + len(h))):
    c = a[c] - 1

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
d = deque()
di = defaultdict(int)
N, K = map(int, input().split())
A = list(map(int, input().split()))


d.append(0)
for i in range(N * 2):
    p = d.pop()
    d.append(p)
    d.append(A[p] - 1)


P = []
k = 0
n = 0
for i, v in enumerate(d):
    if (v in P):
        k = i
        n = v
        break
    P.append(v)
    di[v] = i

le = k - di[n]

amari = K % le

if (di[n] == 0):
    print(d[amari] + 1)
    exit()
else:
    print(d[amari + (di[n]) + 1] + 1)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
 
where_li = []
pre = 0
for i in range(N):
	where_li.append(A[pre])
	pre = A[pre]-1

Submission status: Wrong Answer

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

l = [A[0]]
a = A[0] - 1
for _ in range(0, N):
    if A[a] not in l:
        l.append(A[a])
        a = A[a] - 1
    else:
        break
        a = A[a]
n = len(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
S = (K - b) % T
ans = l[S - 1]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(A) == len(B):
    ans = 0

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(A)
t=1
T=1
C=list("1")
n=0
while t!=0:
    t=B[t-1]
    T=B[T-1]
    B[t-1]=0
    C.append(B[T-1])
    n+=1
z=len(C)-C.index(T)
y=max((k-n)//z,0)
x=k-(n+z*y)+C.index(T)
print(A[x])

Submission status: Wrong Answer

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations,combinations,combinations_with_replacement
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N,K = map(int,input().split())
lisA = [0] + list(map(int,input().split()))
# arrA = np.array(input().split(),dtype=np.int64)

checked = {1:0}
roopstnum = 0
i = 1
for n in range(1,N+2):
    if not lisA[i] in checked:
        checked[lisA[i]] = n
        i = lisA[i]
    else:
        roopstnum = lisA[i]
        roopstidx = checked[roopstnum]
        rooplen = n - roopstidx
        break

print(roopstnum,rooplen,roopstidx,checked)
warukazu = rooplen
for k,v in checked.items():
    if (K-roopstidx) % warukazu == v - roopstidx:
        ans = k
print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)

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

def dfs(pos, depth):
    if dp[pos][0] != 0 and dp[pos][1] != 0:
        return
    # 初めての訪問なら
    if dp[pos][0] == 0:
        dp[pos][0] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
    elif dp[pos][1] == 0:
        dp[pos][1] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
dfs(0, 0)
for i in range(N):
    if dp[i][0] != 0:
        if dp[i][0] == K:
            print(i + 1)
            break
        else:
            split = dp[i][1] - dp[i][0]
            if split > 0:
                if (K - dp[i][0]) % split == 0:
                    print(i + 1)
                    break

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
  
temp=1
trace=[1]
circle=[]
for i in range(K):
  temp=A[temp-1]
  ans=func1(trace,temp)
  if len(ans)==1:
    circle.append(i-ans[0]+1)
    #print(circle)
    break
  trace.append(temp)
  #print("temp",temp)
#print((K-i)%len(circle)-1)
if len(circle)!=0:
  amari=(K-i)%circle[0]
  for j in range(i+amari):
    temp=A[temp-1]

print(temp)

Submission status: Wrong Answer

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

town_check = {0}
town_order = [0]
town = 0
check1 = True
while check1:
    next_town = A[town] - 1
    if next_town not in town_check:
        town = next_town 
        town_check.add(town)
        town_order.append(town)
    else:
        loop = (next_town, town)
        check1 = False
        
a = town_order.index(loop[0])
b = len(town_order) - 1
c = b - a + 1
print(town_order[a+(K-a)%(c)]+1)

Submission status: Wrong Answer

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

def f(lst,value):
  return [i for i,x in enumerate(lst) if x == value]
for x in X:
  if X.count(x) >= 2:
    a = f(X,x)
    X_ans = X[a[0]:a[1]]
    break
b = (k - a[0] + 1) % (a[1]-a[0])
print(X_ans[b-1])

Submission status: Wrong Answer

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

rec = [0] * (N + 1)
costs = [0] * (N + 1)

loc = 1
rec[loc] = 1

finised = False
for i in range(1, K+1):
    next_loc = A[loc-1]

    if rec[next_loc] == 1:
        break

    costs[next_loc] = i
    rec[next_loc] = 1
    loc = next_loc
else:
    finised = True

if finised:
    print(i)
else:
    loop_cost = i - costs[next_loc]

    K -= costs[next_loc]
    K %= loop_cost

    for i in range(K):
        next_loc = A[next_loc-1]

    print(next_loc)

Submission status: Wrong Answer

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

nums = []
num = 1
nums.append(num)


for i in A:
    num = A[num-1]
    if num not in nums: nums.append(num)
    else: 
        c = num
        break
print(nums)


cc = nums.index(c)
circle = len(nums)-cc
yu = len(nums)-circle

l = (K - yu) % circle
a = nums[cc:]

print(circle)
print(yu)
print(a[l])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
As = list(map(int,input().split()))
towns = [0]*N
route = [0]
pre = 0
i = 0
r_append = route.append
for _ in range(N):
    i = As[i]-1
    if towns[i]>0:
        pre = route.index(i)
        route = route[pre:]
        break
    r_append(i)
    towns[i] = 1
# print(route)
mod = len(route)
print(route[(K-pre)%mod]+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(20000)

dest_array = []


def move_destination(dest, counta, limit, teleporte_destination):

    # print('目的地は{}'.format(dest))
    # print('次に行くのは{}'.format(teleporte_destination[dest]))
    # print('カウントは{}'.format(counta))
    if limit <= 0:
        print(dest)
        return
    loop_number = None
    if dest_array.count(dest) == 1:
        loop_number = counta - dest_array.index(dest)
    if loop_number is not None:
        limit = limit % loop_number

    dest_array.append(teleporte_destination[dest])

    move_destination(
        teleporte_destination[dest], counta+1, limit-1, teleporte_destination)


town_number, limit = map(int, input().split())  # 街の数 K回
teleporte_destination = list(map(int, input().split()))
teleporte_destination = [0] + teleporte_destination  # インデックスを合わせるため
# print(teleporte_destination)
move_destination(1, 0, limit, teleporte_destination)

Submission status: Wrong Answer

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

dic={}

i=0
dic[1]=i
next_town=1

while True:
    next_town=A[next_town-1]
    if next_town in dic:
        lp_st=dic[next_town]
        lp_len=i+1-lp_st
        break

    else:
        dic[next_town]=i+1
        i +=1
if K<=i:
    for j in dic:
        if dic[j]==K:
            print(j)

else:
    if lp_st==0:
        lp_st=lp_len
    L=(K-lp_st+1)%lp_len+lp_st-1

    for j in dic:
        if dic[j]==L:
            print(j)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ar = list(map(int, input().strip().split(' ')))
route=[]

Town=1
X=0
loop = False

while loop == False:
      for i in range(0,K):
            X=ar[(Town-1)]
            Town=(X)
            route.append(Town)
            if Town in route:
                  loop=True
                  break
      
S=len(route)
K=K-i
Z=K % S
if Z==0:
      Z=Z+1

for i in range(0,Z):
      X=ar[(Town-1)]
      Town=X

      
print(Town)

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    array = [1]
    now = 1
    for i in range(n):
        next = A[now-1]
        array.append(next)
        now = next
    array = list(set(array))
    length = len(array)
    print(array[k%length])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
b=[-1]*(n+1)
b[1]=0
c=a[0]
d=[0]*(n+1)
d[0]=1
for i in range(1,k+1):
    d[i]=c
    if b[c]!=-1:
        print(d[b[c]+(k-b[c])%(i-b[c])])
        exit()
    b[c]=i    
    c=a[c-1]
print(d[-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()))
    li = [0]
    nt = 0
    de_num = 0
    bk = False

    for i in range(n):
        nt = a[nt]-1

        # 既に行ったか検証
        for j in range(len(li)):
            if nt == li[j]:
                de_num = j
                li[0:j] = []
                # print(li)
                bk = True
                break
        if bk:
            break
        li.append(nt)
        # print(li)

    # print(li)

    x = (k-de_num) % len(li)
    # print(x)
    print(li[x]+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = [1]
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
g.reverse()
for i in range(len(g)):
    if g[i] == g[i + mod]:
        d = i
        break
if k <= mod:
    for i in range(k):
        q.append(s)
        s = a[s-1]
    q.reverse()
    print(q[0])
else:
    print(g[d+(k-d)%mod])

Submission status: Wrong Answer

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

if k<=n:
    #k回シミュレートすればよい
    now=0
    for i in range(1,k+1):
        now=a[now]
    print(now+1)
    exit()

now=0
visited=[-100]*n
visited[now]=0
for i in range(1,2*10**5+1):
    now=a[now]
    if visited[now]==-100:
        visited[now]=i
    else:
        m=i-visited[now]
        break
k=k%m+m
for i in range(1,k+1):
    now=a[now]
print(now+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1

Submission status: Wrong Answer

Solution code:
#Kから繰り返しのパターンの入口までを引いた数を、繰り返しのパターンで割り、繰り返しリストの番号を返せばよい、というところまで分かった。
#WAだが記録用に提出。
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

B = [0] * N
C = 0
D = 0
E = [1]
for i in range(K):
    if i == 0:
        B[i] += 1
        C = A[0]
        D += 1
        E.append(C)
        #print(E)
        #print(B)
    else:
        if B[C - 1] == 0:
            B[C - 1] += 1
            C = A[C - 1]
            D += 1
            E.append(C)
            #print(E)
            #print(B)
        else:
            #print("２回目")
            #print(D, K % D)
            #print(E)
            print(E[K % D])
            exit()

print(C)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np
from collections import Counter

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

c = Counter(g)

depfirst = N+1
depend = N+1
for i in c.most_common():
    if i[1] > 1:
        temp = [k for k, x in enumerate(g) if x == i[0]]
        if temp[1] < depend:
            depfirst = temp[0]
            depend = temp[1]
    else:
        break

interval = depend-depfirst+1
if K <= depfirst:
    print(g[K])
else:
    if depfirst == 0:
        print(g[depfirst+((K-depfirst) % interval)+1])
    else:
        print(g[depfirst+((K-depfirst+1) % interval)+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()))
    li = [0]
    nt = 0
    de_num = 0
    bk = False

    for i in range(n):
        nt = a[nt]-1

        # 既に行ったか検証
        for j in range(len(li)):
            if nt == li[j]:
                de_num = j
                li[0:j] = []
                # print(li)
                bk = True
                break
        if bk:
            break
        li.append(nt)
        print(li)

    # print(li)

    x = (k-de_num) % len(li)
    # print(x)
    print(li[x]+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np
from collections import Counter

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

c = Counter(g)

depfirst = N+1
depend = N+1
for i in c.most_common():
    if i[1] > 1:
        temp = [k for k, x in enumerate(g) if x == i[0]]
        if temp[1] < depend:
            depfirst = temp[0]
            depend = temp[1]
    else:
        break

interval = depend-depfirst+1
if K <= depfirst:
    print(g[K])
else:
    if depfirst == 0:
        print(g[depfirst+((K-depfirst) % interval)+1])
    else:
        print(g[depfirst+((K-depfirst+1) % interval)+1])


print(g, depfirst, depend)

Submission status: Wrong Answer

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

def nexttown(i):
    return A[i-1]

done = [1]
now = 1
for i in range(1,2*N+1):
    nexttown = A[now-1]
    #print(now, "->", nexttown)
    if nexttown not in done:
        done.append(nexttown)
        now = nexttown
    else:
        break

start = done.index(nexttown)
end = len(done)-1
#print("done",done)
#print("start", start, "end", end)
before = start #ループに入るまでの転移数
loop = done[start:]

if K < before:
    saisyuu = done[K-1]
else:
    nokori = K-before
    n_loop = int(nokori/len(loop))
    hasuu = nokori%len(loop)
    loopfirst = A[done[-1]-1]
    saisyuu = loop[hasuu]
    #print("1から",before,"回転移して",loopfirst,"に到着した後、大きさ",loop,"のループを",n_loop,"回転移して",loopfirst,"にいます。")
    #print("その後、",hasuu,"回の転移をして",saisyuu,"に到着します。")
print(saisyuu)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
tele = list(map(int, input().split()))
move = [-1]*n
move[0] = 0
now = 0
cnt = 0

while True:
    cnt += 1#移動回数
    now = tele[now] - 1 #移動した先

    if move[now] != -1:#探索済みなら周期を計算しbreak
        cycle = cnt - move[now]
        break
    else:
        move[now] = cnt#未探索ならこれまでの移動回数を記録
#この時cntに格納されている数は周期が起こった直後    

#ループが発生する前の場合
if cnt-1 >= k:
    ans = 0
    for i in range(k):
        ans = tele[ans] - 1
#ループが発生する場合
else:
    loop = (k-(cnt))%cycle#ループが起こった余り
    now = now
    for i in range(loop):
        now = tele[now] - 1
print(now+1)

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    array = [1]
    now = 1
    for i in range(n):
        next = A[now-1]
        array.append(next)
        now = next
    array = list(set(array))
    length = len(array)
    print(array)
    print(array[k%length])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    pos=set()
    pos.add(0)
    i=0
    traversal=[0]
    start=0
    nodes=[1]
    while(start<K):
        position=A[i]-1
        if position in pos:
            break
        traversal.append(position)
        pos.add(position)
        start+=1
        nodes.append(A[i])
        i=position
    if start==K:
        print(A[traversal[-1]])
    else:
        ind=traversal.index(position)
        interval=len(traversal)-ind
        if interval%2==0:
            print(nodes[ind+((K-start+3)%interval)])
        else:
            print(nodes[ind+((K-start+2)%interval)])
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
s=set()
s.add(1)
order=[1]
while 1:
    if a[i] in s:
        order.append(a[i])
        break
    else:
        order.append(a[i])
        s.add(a[i])
    i=a[i]-1
for i in range(len(order)):
    if order[-1]==order[i]:
        idx=i
        break
#print(order)
k-=idx+1
cnt=len(order)-(idx+1)
if len(order)>k:
    print(order[k+1])
else:
    order=order[idx+1:]
    #print(cnt)
    #print(order)
    k%=cnt
    #print(k)
    print(order[k])
#print(order)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    roop = lst_memo[key:]
    print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
for e,i in enumerate(A):
    if dp[i-1]:
        ans = e
        break
    else:
        dp[i-1]+=e
        B.append(i)
        ans = e
k %= ans
print(B[k])

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()))
    li = [0]
    nt = 0
    de_num = 0
    bk = False

    for i in range(n):
        nt = a[nt]-1

        # 既に行ったか検証
        for j in range(len(li)):
            if nt == li[j]:
                de_num = j
                li[0:j] = []
                # print(li)
                bk = True
                break
        if bk:
            break
        li.append(nt)
        print(li)

    print(li)

    x = (k-de_num) % len(li)
    # print(x)
    print(li[x]+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# 1から順に探索
visited=[-1]*(1+n)
cl=[0]*(1+n)
visited[1]=0
c=0
now=1
t=0

start=0

for i in range(1,n+1):
    if visited[al[now]]==-1: # まだきてない
        visited[al[now]]=0
        c+=1
        cl[al[now]]=c
        now=al[now]
    else:  
        start=cl[al[now]]
        t=i-start
        break
if start==0:
    print(now)
else:
    cl2=cl[1:]
    print(cl2.index(start+(k-start)%t)+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()

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:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]

b = [0]*n
fi = a[0]-1
s=0
b[fi]=1
while(1):
    t= a[fi]

    b[t-1] += 1
    s+=1
    fi = t-1
    if b[t-1]>2:
        break
j=0
for i in b:
    if i==1:
        j+=1
s = (s-j)//2

amari = (k-j)%s

fi = a[0]-1
if amari==0:
    if j==0:
        amari=s
    else:
        amari=j

for _ in range(amari+j-1):
    t = a[fi]
    fi = t-1
print(t)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
bl = [False]*n
now = 1
bl[now - 1] = True
ct = 1
res = [1]
ky = int()
while True:
    if bl[a[now - 1] - 1]:
        ky = a[now - 1]
        break
    now = a[now - 1]
    bl[now - 1] = True
    res.append(now)
    ct += 1
roop = ct - res.index(ky)
x = ct - roop
k -= x
print(res[x + k % roop])

Submission status: Wrong Answer

Solution code:
paramN, paramK = list(map(lambda x: int(x), input().split(' ')))
paramA = list(map(lambda x: int(x), input().split(' ')))

route = [1]
for i in range(paramN):
    route.append(paramA[route[len(route) - 1] - 1])

loop_start = 0
for i, r in enumerate(route):
    if r in route[:i]:
        loop_start = i
        break

x = route[loop_start]
loop_start_0 = route.index(x)

y = paramK - loop_start_0
z = y % (loop_start - loop_start_0)
print(route[loop_start_0 + z])

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(K-1):
        c = A[b-1]
        b = c
    ans = b
else:   
    #print("a")
    for i in range(N+1):
        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)
    if loop ==1:
        ans =A[Z]
    else:        
        #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:
n,k=map(int,input().split())
a=list(map(int,input().split()))

edge=[0]*n

for i in range(n):
    edge[i]=a[i]

now=1
to=0
t=k
l=[0]*n
cnt=1
while t!=0:
    to=edge[now-1]
    t-=1
    if to!=now:
        if l[to-1]!=0:
            break
        else:
            l[to-1]=cnt
            cnt+=1
        now=to
    else:
        print(to)
        exit()

if t==0:
    print(to)
else:
    print(l.index((k-l[to-1]+1)%(max(l)-l[to-1]+1)+l[to-1]-1)+1)

Submission status: Wrong Answer

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

#print(A)
#print(list)
num = 0
ans = A[0]
list[ans-1] += 1
while num <= 2*N+1:
  num += 1
  ans = A[ans-1]
  list[ans-1] += 1
  #print(list)

x = list.count(1)
y = N-x-list.count(0)
#print(x,y)

K = K%y
#print(K)
ans = A[0]
while K > 1:
  K -= 1
  ans = A[ans-1]
      
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = 1
t = []
while not s in t:
    t.append(s)
    s = a[s-1]
t = t[::-1]
x = 0
for u in t:
    if u == s:
        break
    else:
        x += 1
        t.pop()
y = (k-x) % len(t)
t[::-1]
print(t[y-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
pointer=1
visited=[1]
looplength=0
flag=0
for i in range(k-1):
    visited.append(a[pointer])
    pointer=a[pointer]
    if a[pointer] in visited:
        loopstart=visited.index(a[pointer])
        looplength=len(visited)-loopstart
        print(visited[loopstart+(k-i+1)%looplength])
        flag=1
        break
print(a[pointer] if flag==0 else"")

Submission status: Wrong Answer

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

for i in range(n+1):
    road.append(now)
    now = a[now-1]

loop = [x for x in dict.fromkeys(road) if road.count(x) > 1]
ans = loop[(k-road.index(loop[0]))%len(loop)]

print(ans)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
lis="1"
llis=[[] for i in range(a)]


c=[int(i) for i in input().split()]
j=0
while True:
    i=c[j]
    if len(llis[i-1])!=0:
        ans=lis.index(str(i))
        break
    lis+=str(i)
    llis[i-1].append(1)
    j=i-1
#print(lis)
lis=lis[ans:]
print(lis[((b-ans)%len(lis))])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
c = r
while True:
  if c > m[t]:
    break
  c += c
b = k%r + c
t = 0
for i in range(b):
  t = a[t]-1
print(t+1)

Submission status: Wrong Answer

Solution code:
import sys

n,k = (int(i) for i in input().split())

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

hantei = [True]*(n+1)
otozure = [None]*(n+1)
mokuteki = a[0]
moto = k
kazoe = 0
otozure[0] = 0
while k>0:
        
    if hantei[mokuteki] == False: #二週目突入
       # print("break\nmokuteki= {}\nk = {}".format(mokuteki,k))
        otozure[kazoe+1] = mokuteki
        saigo = otozure
        break

    else:
        hantei[mokuteki]= False
        otozure[kazoe+1] = mokuteki#今いるところを記憶
       # print(otozure[kazoe])
        mokuteki = a[mokuteki]#次に行くところ
        kazoe+=1
    k-=1
else:
    print(otozure[kazoe-1]+1)
    sys.exit()
i = 0
while True:
    if otozure[kazoe+1] == otozure[kazoe-i]:
        break
    i+=1


nokori = k%(i+1)
#print(nokori)
i=0
while otozure[i] != mokuteki:
    i+=1

if a[0]==0:
    print("1")
else:
    print(otozure[i-1+nokori]+1)

Submission status: Wrong Answer

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

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

next_city = cities[0]
city_logs = [next_city]

for _ in range(K):
    next_city = cities[next_city-1]
    if next_city in city_logs:
        break
    city_logs.append(next_city)
    
loop_start = city_logs.index(next_city)
repeat_city = city_logs[loop_start:]

left_length = K - _ + 1

output = repeat_city[left_length % len(repeat_city)]
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    now = 0
    print(now)
    hoge= []
    bit = 0
    piyo = 0
    huga = []
    for i in range(k):
        hoge.append(now)
        now = a[now]-1
        if now in hoge:
            huga = hoge[hoge.index(now):len(hoge)]
            piyo = hoge.index(now)
            bit = 1
            break
    if bit == 1:
        print(huga)
        ans = huga[(k-piyo)%len(huga)]
        print(ans+1)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

ans = 0
ans_list = []
end = 0
stpos = 0


for i in range(K):
    ans = A[ans] - 1
    ans_list.append(ans)
    if len(set(ans_list)) != i+1:
        end = i - 1
        stpos = ans_list.index(ans)
        break
    if i == K:
        print(ans)
        exit()


print(ans_list[stpos:end+1][(K-(stpos+1)) % (end-stpos+1)]+1)

Submission status: Wrong Answer

Solution code:
import sys
import itertools as iter
import math as m

def main():
    N, K = list(map(int, input().split()))
    A = [-1]
    A += [int(i) for i in input().split()]
    L = [0] * (N + 1)

    L[1] = 1
    x = A[1]
    L[x] = 1
    for i in range(K-1):
        x = A[x]
        if L[x] == 2:
            lc = (K-i) % L.count(2)
            for _ in range(lc):
                x = A[x]
            print(A[x])
            return
        else:
            L[x] += 1
    print(x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]

arrived = set()
circle = [0]
circle1 = []
arrived.add(0)
pre = 0
l = 0
while(1):
    nt = A[pre]
    if nt not in arrived:
        arrived.add(nt)
        circle.append(nt)
    else:
        l = len(circle)
        #print(circle1)
        #print("下一個是", nt)
        if not circle1:
            circle1.append(nt)
        elif nt != circle1[0]:
            circle1.append(nt)
        else:
            break
    pre = nt
#print(circle)
#print(circle1)
#print(K - l)
index = (K - l)%len(circle1)
print(circle1[index] + 1)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link)- len(link2)

if(k > len(link2)):
    p = (k-len(link2))%rep
    for i in range(p):
        next = a[next-1]
else:
    next = 0
    for i in range(k-1):
        next = a[next-1]

ans = next
print(ans)