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 collections


def solve(n, k, arr):
    visited = set()
    i = 0

    cyclicArr = []
    curr = 0
    while i not in visited:
        visited.add(i)
        cyclicArr.append(i+1)

        i = arr[i]-1
        curr += 1

    cyclicArr.append(i+1)

    lastDigit = cyclicArr[-1]
    m = len(cyclicArr)

    j = 0

    while j < m:
        k -= 1

        if cyclicArr[j] == lastDigit:
            break

        j += 1

    newCyclicArray = cyclicArr[j+1:]

    dex = k % len(newCyclicArray)

    # print(cyclicArr)
    # print(newCyclicArray)
    # print("Dex: {}, k: {}".format(dex, k))

    return newCyclicArray[dex]


[n, k] = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))

print(solve(n, k, arr))

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
        for i in range(N):
            if c[i] == K:
                print(i+1)
                exit()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

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

    count = 0
    visited = []
    now = 0
    l = -1
    while count < N:
        if A[now] in visited:
            l = visited.index(A[now])
            break
        else:
            count += 1
            visited.append(A[now])
            now = A[now]-1
    r = len(visited)
    # print(l,r,visited)
    
    if K < l:
        print(visited[K])
    else:
        print(visited[l + (K-l) % (r-l)-1])
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

n,k=map(int,input().split())
a=list(map(int,input().split()))
L=[1]
count=1
nowt=1
roops=0
v=[False]*n
while(True):
    if v[a[nowt-1]-1]==True:
        roops=a[nowt-1]
        break

    L.append(a[nowt-1])
    v[a[nowt-1]-1]=True
    nowt=a[nowt-1]
    count+=1

play=0
for i in range(len(L)):
    if L[i]==roops:
        break
    play+=1

rooplen=len(L)-play

ans=L[play+(k-play)%rooplen]


print(ans)

Submission status: Wrong Answer

Solution code:
n,k = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
s = dict()
l = []
for j in range(n):
	if a[j] in s:
		break
	else:
		s[a[j]]=j
		l.append(a[j])
print(l[s[a[j]]-1+(k-s[a[j]]+1)%(-1*s[a[j]]+j+1)])

Submission status: Wrong Answer

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def readInt():
    return list(map(int,input().split()))

def main():
    n,k = readInt()
    a = readInt()
    pos = 0
    visit = [0] * n
    move = []
    while visit[pos]!=2:
        move.append(pos)
        visit[pos] += 1
        pos = a[pos] - 1
    roop = []
    while True:
        x = move.pop(-1)
        if x in roop:
            move.append(x)
            break
        roop.append(x)
    roop.reverse()
    num = (k - (len(move)-len(roop))) % len(roop)
    print(roop[num]+1)
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N,K=map(int,input().split())
A=list(map(int,input().split()))
used=[False for i in range(N)]
used[0]=True
p=1
L=[1]

for i in range(N+1):
    p=A[p-1]
    L.append(p)
    if used[p-1]:
        looper=p
        break
    used[p-1]=True

for i in range(len(L)):
    if L[-(2+i)]==looper:
        a=i+1
        b=len(L)-1-a
        break

ans=L[b+(K-b)%a]

print(ans)

Submission status: Wrong Answer

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

path = []
passed = set([])
p = 0
while 1:
  if p not in passed:
    path.append(p)
    passed.add(p)
    p = A[p]
  else:
    break
t = path.index(p)
k -= t
term = len(path) - t
k %= term
print(path[k+t]+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[-1]*N
l[0]=0
i=1
t=0
while 1:
    t=A[t]-1
    if l[t]!=-1:
        s=i-l[t]
        break
    l[t]=i
    i+=1
if K<i:
    print(l.index(K)+1)
else:
    k=K%s
    if l[t]//s+k in l:
        print(l.index(l[t]//s*s+k)+1)
    else:
        print(l.index((l[t]//s+1)*s+k)+1)

Submission status: Wrong Answer

Solution code:

N, K = map(int, input().split())
S = list(num-1 for num in map(int, input().split()))
loc = 0

arrived = [0]*N
ar = set()
cnt = 0
while not loc in ar:
    arrived[cnt] = loc
    ar.add(loc)
    loc = S[loc]
    cnt += 1

if K < cnt:
    print(arrived[K-1]+1)
else:
    for i in range(cnt):
        if arrived[i] == loc:
            K -= i
            roop = cnt-i
            K %= roop
            print(arrived[i+K]+1)
            break

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]
# print(A)
route = [0]
seen = {0}
here = 0
while True:
    here = A[here]

    if here in seen:
        start = route.index(here)
        period = len(route) - start
        idx = (K - start) % period + start
        print(route[idx] + 1)
        break
    seen.add(here)
    route.append(here)
    # print(route)
# print(period)
else:
    print(route[K] + 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,k = myinput()
a = list(myinput())

ls = [-1]*n

ls[0] = 0
s = 0
for i in range(n):
    s = a[s] - 1
    if ls[s]<0:
        ls[s] = i + 1
        # print(ls)
    else:
        b = ls[s]
        w = (i+1) - b
        c = (k-b)%w
        g = ls.index(c+b) + 1
        # print(g)
        exit()

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())
memo = [-1]*(N+1)
junban = deque()
A = [0]+list(map(int, input().split()))
i = 0
x = 1
while memo[x] < 0:
  junban.append(x)
  memo[x] = i
  x = A[x]
  i += 1
T = i-memo[x]
print(list(junban)[memo[x]:][(K-memo[x])%T])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
cnt=[]
m=0
ans=0
for i in range(n):
  if a[m] in d:
    ans+=1
    cnt.append(a[m])
    if ans==k:
      print(a[m])
      exit()
    break
  else:
    d[a[m]]=0
    cnt.append(a[m])
    ans+=1
    if ans==k:
      print(a[m])
      exit()
    m=a[m]-1

print(cnt[k%len(cnt)])

Submission status: Wrong Answer

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

ne=0
his=[]
l=False
for i in range(k+1):
    if ne in his:
        back=his.index(ne)
        loop=i-back
        l=True
        break
    his.append(ne)
    ne=a[ne]-1
if l:
    ansidx=(k-back)%loop+back
    ans=his[ansidx]+1
else:
    ans=ne+1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(_) for _ in input().split())
    A = [int(_)-1 for _ in input().split()]
    visited = [-1] * N
    now = cc = 0
    while True:
        if visited[now] == -1:
            visited[now] = cc
            cc += 1
            now = A[now]
        else:
            T = cc - visited[now]
            break
    K -= visited[now]
    for i in range(N):
        if visited[i] == K%T + visited[now]:
            print(i+1)
            break
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

B = [300000] * N
l = 0
i = 0
p = 0
while l == 0:
  x = A[i] - 1
  #print(i, x)
  if x in B:
    l = 1
    index = B.index(x)
  else:
    B[p] = x
    p += 1
    i = x
    #print(B)
    if p == N:
      #roop = N
      if K >= N:
        K = K % N
      print(B[K] + 1)
      #print(K, B)
      quit()
      
    
roop = p - index
#print(p, index)
if K < (index + 1):
  ans = B[K]
else:
  K = K - (index + 1)
  #print(K)
  if K >= roop:
    K = K % roop
  #print(K, K + index, B)
  ans = B[K + index] + 1
  #print("K")
  
print(ans)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    """ Union-Find木の実装（ランクあり） """
    def __init__(self, n):
        self.n = n
        self.parent = [-1]*(n+1)    # parent 親 マイナスだったら根で、絶対値が集合の数
        self.rank   = [0]*(n+1)     # rank 深さ

    def root(self, x):
        """ 木の根を求める """
        if self.parent[x] < 0:      # xが根
            return x
        else:
            self.parent[x] = self.root(self.parent[x])  # 経路圧縮
            return self.parent[x]
            
    def unite(self, x, y):
        """ xとyの属する集合を併合 """
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return 0
        elif self.rank[x] > self.rank[y]:
            self.parent[x] += self.parent[y]
            self.parent[y] = x
        else:
            self.parent[y] += self.parent[x]
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def isSame(self, x, y):
        """ xとyが同じ集合に属するか否か """
        return self.root(x) == self.root(y) or x==y
    
    def size(self, x):
        """ xと同じ集合に属する数 """
        return -self.parent[self.root(x)]

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

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

now = 0
K %= uf.size(now)
for _ in range(K):
    now = A[now]-1
print(now+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
seq=list(map(int,input().split()))
v=[False]*n
path=[1]
i=0
while True:
        if v[seq[i]-1]==False:
                v[seq[i]-1]=True
                path.append(seq[i])
                i=seq[i]-1
                
        else:
                t=seq[i]
                break

ind=path.index(t)
path2=path[ind:]
path1=path[:ind]
k-=len(path1)
#print(t,ind,path1,path2)
print(path2[(k%len(path2))])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
while True:
  if M[s] in order:
    #print(order, M[s])
    K -= order.index(M[s])
    order = order[order.index(M[s]):]
    break
  order.append(M[s])
  s = M[s]

#print(order, K)
print(order[K%len(order)]+1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi

MOD = 10 ** 9 + 7
INF = 10 ** 18

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

cnt = [0] * (N + 1)
p = A[0]
c = 1

while 1:
    if cnt[p] == 0:
        cnt[p] = c
    else:
        c1 = cnt[p]
        c2 = c
        break
    p = A[p - 1]
    c += 1

#print(c2- c1)

B = [0, A[0]]
for _ in range(3 * 10 ** 5):
    B.append(A[B[-1] - 1])

BN = len(B)

for i in range(BN - 1, -1, -1):
    if i % (c2 - c1) == K % (c2 - c1):
        print(B[i])
        exit()

Submission status: Wrong Answer

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

d = {0:0}
pre_a = -1
a = 0
c = 0
while K:
    K -= 1
    next_a = A[a] - 1
    c += 1
    if next_a in d:
        idx = d[next_a]
        n = len(d)
        K %= (n - idx)
        break
    else:
        d[next_a] = c
        pre_a = a
        a = next_a

while K>=0:
    pre_a = a
    a = A[a] - 1
    K -= 1

print(a+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ok=set()
now=0
l=[]
while now not in ok and k:
    l.append(now)
    ok.add(now)
    now=a[now]-1
    k-=1
if k==0:print(now+1);exit()
length=len(ok)-(h:=l.index(now))
l=l[h:]
print(l,h,length)
k=(k)%length

print(l[k]+1)

Submission status: Wrong Answer

Solution code:

N, K = map(int, input().split())
S = list(num-1 for num in map(int, input().split()))
loc = 0

arrived = [0]*N
ar = set()
cnt = 0
while not loc in ar:
    arrived[cnt] = loc
    ar.add(loc)
    loc = S[loc]
    cnt += 1

if K < cnt:
    print(arrived[K-1])
else:
    for i in range(cnt):
        if arrived[i] == loc:
            K -= i
            roop = cnt-i
            K %= roop
            print(arrived[i+K]+1)
            break

Submission status: Wrong Answer

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

D = A
T = [D]
for i in range(180):
    L = T[-1]
    T.append([L[i] for i in range(N)])

p = 0
i = 0
while K:
    if K&1 ==1:
        p = T[i][p]
    A = [A[A[i]] for i in range(N)]
    #print("i=%d"%i, "p=%d"%p, A)
    K//=2
    i+=1
print(p+1)

Submission status: Wrong Answer

Solution code:
import collections

n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
a = [int(i)-1 for i in a]

ex = collections.defaultdict(lambda:-1)
step=1
cur=0
ex[0]=0
while 1:
    if ex[a[cur]]!=-1:
        cycle = step-ex[a[cur]]
        k-=ex[a[cur]]
        #print(k%cycle)
        break
    else:
        ex[a[cur]]=step
        cur=a[cur]
    step+=1

for i in range(k%cycle):
    cur = a[cur]
print(a[cur]+1)

Submission status: Wrong Answer

Solution code:
def resolve():
    N,K = map(int,input().split())
    A = [int(i)-1 for i in input().split()]
    visited =[False]*N
    steps = [K for i in range(N)]

    current = 0
    while True:

        if K == 0:

            return
        if not visited[current]:
            visited[current]=True
            steps[current]=K
            current=A[current]
            K-=1
        else:
            K= K % (steps[current]-K)
            break
    
    while K != 0:

        current= A[current]
        K-=1
    print(current+1)




if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

d = [-1 for _ in range(n)]
d[0] = 0
cur = 0
rou = 10 ** 6
rou_start = 0
for _ in range(n):
    to = move[cur]
    if d[to] == -1:
        d[to] = d[cur] + 1
    else:
        rou = d[cur] + 1 - d[to]
        rou_start = to
        break
    cur = to

if k < d[rou_start]:
    x = k
else:
    x = (k - d[rou_start]) % rou
cur = rou_start
for _ in range(x):
    cur = move[cur]


print(cur + 1)

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 1 in m:
        break

x = K % len(m)

print(m[x-1])

Submission status: Wrong Answer

Solution code:
def calc(i, k):
    ans_list = [0]
    j = i
    count = 1
    i = a[i]
    ans_list.append(i)
    while (i != j) & (count < n):
        i = a[i]
        count += 1
        ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0, n)
print(ans_list[k % count] + 1)

Submission status: Wrong Answer

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

N,K = map(int, input().split())
A = [0]+list(map(int, input().split()))

town_list = [-1,0]+[-1 for i in range(N-1)]
town_now = 1
n_teleport = 0

while n_teleport<N:
  n_teleport += 1
  town_now = A[town_now]
  if town_list[town_now]==-1:
    town_list[town_now] = n_teleport
  else:
    break

if n_teleport==N:
  print(town_now)
else:
  loop_start_town = town_now
  loop_start_time = town_list[town_now]
  loop_len = n_teleport-town_list[town_now]
  M = (K-loop_start_time)%loop_len
  for _ in range(M):
    town_now = A[town_now]
  print(town_now)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    n, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.insert(0,0)

    visited = [1]
    i = 1
    acc = 0
    while True:
        i = arr[i]
        acc += 1
        k -= 1
        if i not in visited:
            visited.append(i)
        else:
            break
        
    k %= acc+1
    while k > 0:
        i = arr[i]
        k -= 1
    print(i)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l =[0]+ list(map(int,input().split()))
q=l[1]
cnt=1
d=[]
d.append(q)
while True:
    q=l[q]
    cnt+=1
    if q in d:
        d.append(q)
        break
    else:
        d.append(q)
a=d.index(d[-1])
la=len(d)-1
cir=la-a
k=k-a
k=k%cir

print(d[a+k-1])

Submission status: Wrong Answer

Solution code:
# ABC167d


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    pos = [-1] * n
    pos[0] = 0

    nex = a[0] - 1
    now = 0
    turn = 0

    mode = False

    while True:
        if not mode and pos[nex] == -1:
            pos[nex] = pos[now] + 1
            now = nex
            nex = a[nex] - 1
            turn += 1
            continue
        if mode:
            if turn == k:
                print(now+1)
                break
            pos[nex] = pos[now] + 1
            now = nex
            nex = a[nex] - 1
            turn += 1
            continue
        turn += (k-turn) // (turn - pos[nex] + 1) * (turn - pos[nex] + 1)
        mode = True

    # print(pos)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = K % N
ne = 0
for i in range(ans - 1):
    ne = A[ne] - 1

if ne == 0:
    print(N)
else:
    print(ne)

Submission status: Wrong Answer

Solution code:
#code
N,K=map(int,input().split())
A=list(map(int,input().split()))
visited=[0 for i in range(N+1)]
s=""
p=1
s+=str(p)
visited[p]=1
p=A[p-1]
while (visited[p]==0):
    s+=str(p)
    visited[p]=1
    p=A[p-1]
i=s.index(str(p))
if(K<i):
    print(s[K])
else:
    K-=i
    s=s[i:]
    print(s[K%len(s)])

Submission status: Wrong Answer

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

a = [0] + a
now = 1
cities = [now]
done = set()
done.add(now)
for i in range(n):
    now = a[now]
    if now in done:
        break
    done.add(now)
    cities.append(now)
idx_s = cities.index(now)
# print(idx_s)
f = (len(cities) - idx_s)
# print(f)
d = k - idx_s
idx = d%f
ans = cities[idx + idx_s]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    went = [0] * N
    went[0] = 1
    Flag = True
    check = [0]
    cnt = 1
    now = 0
    while Flag and (cnt < K):
        now = A[now] - 1
        if went[now] == 1:
            ind = check.index(now) - 1
            cnt = cnt - ind - 1
            Flag = False
        else:
            check.append(now)
            went[now] = 1
            cnt += 1
            continue
    if len(check) >= K:
        print(check[K - 1])
    else:
        K = (K - ind) % cnt
        print(check[ind + K] + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
cnt = 0
cur = 1
memo = [1]
kumi = set([1])
for i in range(2 * (10 ** 5) + 5):
    cur = A[cur]
    cnt += 1
    memo.append(cur)
    if cur in kumi:
        break
    kumi.add(cur)

for j in range(cnt):
    if memo[j] == cur:
        start = j
loop = memo[start:]

mae = start  # 予め行った分
loop_num = i + 1 - start
print(loop[(k - mae) % loop_num])

Submission status: Wrong Answer

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

dp = [[0]*(N+1) for _ in range(51)]
for i in range(1, N+1):
  dp[0][i] = line[i]

for i in range(50):
  for j in range(1, N+1):
    dp[i+1][j] = dp[i][dp[i][j]]

for i in range(1, N+1):
  now = i 
  for j in range(50):
    # Kを2ビットに変換し、1のところだけ行う
    if (K&(1<<j)) != 0:
      now = dp[j][now]

  print(now)
  exit()

Submission status: Wrong Answer

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

exist = [0]

cnt = 1
while(True):
    if a[exist[-1]-1] in exist:
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    exist.append(a[exist[-1]-1])
    cnt += 1

k = k-idx

d = k % (cnt-idx)
print(a[idx+d])

Submission status: Wrong Answer

Solution code:
n,k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
loop = []
seen = [0]*n
num = 0
while True:
    if seen[num]==1:
        break
    seen[num]=1
    loop.append(num)
    num = a[num]
    k-=1
index = loop.index(num)
loop = loop[index:]
print(loop[(k%len(loop))]+1)

Submission status: Wrong Answer

Solution code:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()
for i in range(N):
    A[i] -= 1
P = dict()
k = 0
former = True
while i < K:
    if k in P.keys() and former:
        loop = i - P[k]
        K = i + (K-i) % loop
        former = False
    else:
        P[k] = i
        k = A[k]
    i += 1
else:
    print(k+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def get_int():
    return int(input())

def get_tuple():
    return map(int,input().strip().split())

def get_ls():
    return list(map(int,input().strip().split()))


n,k = get_tuple()
ls = get_ls()
visited = defaultdict(int)
times = 0
town = 1
while town not in visited:
    visited[town] = times
    times += 1
    town = ls[town-1]
    
period = times-visited[ls[town]]
if k>=period:
    k %=period

town = 1
ans = ls[town-1]
while k>1:
    town = ans
    ans = ls[town-1]
    k -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    n, k = LI()
    A = LI_()
    visited = set()
    path = []
    cur = 0
    while True:
        visited.add(cur)
        path.append(cur)
        cur = A[cur]
        if cur in visited:
            break
    for i, city in enumerate(path):
        if cur == city:
            loop_st = i
            break
    loop_en = len(path) - 1
    n_loop = loop_en - loop_st + 1
    
    k -= loop_st
    k %= n_loop
    print(path[loop_st + k] + 1)

main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
loc=0
num=[0]*N
x=[-1]*N
y=0
distance=0
flag=False
for i in range(N):
  if(x[loc]>=0):
    distance=i-x[loc]
    y=x[loc]
    flag=True
    break
  else:
    x[loc]=i
  loc=A[loc]-1
if(distance>0):
  print(x.index((K-y)%distance+y)+1)
elif(flag==True and distance==0):
  print(y)
else:
  print(x.index(K-1)+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, K = map(int,input().split())
A = list(map(int,input().split()))
town = [False] * (N+1)
current = 1

cnt = 0

for i in range(N):
    if town[current]:
        current = A[current-1]
        break
    else:
        town[current] = True
        cnt += 1
        current = A[current-1]

town2 = [False] * (N+1)
T = 0
for i in range(N):
    if town2[current]:
        current = A[current-1]
        break
    else:
        town2[current] = True
        T += 1
        current = A[current-1]
        
if K <= cnt:
    c = 1
    for i in range(K):
        c = A[c-1]
        #print(c)
    ans = c
else:
    K -= cnt
    K %= T
    c = current
    for i in range(K+1):
        c = A[c-1]
        #print(c)
    ans = c
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
count = 1
li2 = [1]
li2.append(li[0])
for i in range(k):
    li2.append(li[li2[i+1]-1])
    count += 1
    if len(set(li2)) != count:
        break
print(li2[k % len(li2)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[0 for i in range(n)]
i=0
s[i]=1
while s.count(s[i])==1:
  i+=1
  s[i]=a[s[i-1]-1]

l=[j for j, x in enumerate(s) if x == s[i]] 
x=l[1]-l[0]
y=(k-l[0])%x
print(s[l[0]+y])

Submission status: Wrong Answer

Solution code:
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
n,k=map(int,input().split())
l=list(map(int,input().split()))
a=1
s=set()
count=0
while a not in s:
	s.add(a)
	a=l[a-1]
	count+=1
	if count==k:
		print(a)
		exit()
if count<k:
	k-=count
	# print(k)
else:
	a=1
	for i in range(k):
		a=l[a-1]
	print(a)
	exit()
s=set()
while a not in s:
	s.add(a)
	a=l[a-1]
	count+=1
if count<k:
	f=k%count+1
	for i in range(f):
		a=l[a-1]
	print(a)
else:
	for i in range(k):
		a=l[a-1]
	print(a)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
s = set()
pas1 = []
pas2 = []
now = 1
count = 0
if k == 1:
    print(l[0])
    exit()
for i in range(2*n):
    a = l[now-1]
    if a in s:
        index = pas1.index(a)
        k -= index
        le = len(pas1)-index
        print(pas1[k%le+index-1])
        
        exit()
    else:
        now = a
        s.add(a)
        count += 1
        pas1.append(a)
        
        if count == k:
            print(a)
            exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
alist=[0]+list(map(int,input().split()))
#print(alist)

tlist=[]
pos=1
pos_set=set()
while True:
  tlist.append(pos)
  if pos in pos_set:
    break
  pos_set.add(pos)
  pos=alist[pos]
  
dup_pos2=len(tlist)-1
dup_pos1=-1
for i in range(len(tlist)):
  if tlist[i]==tlist[-1]:
    dup_pos1=i
    break
    
#print(tlist)
cycle=dup_pos2-dup_pos1
#print(dup_pos1,dup_pos2,cycle)
print(tlist[(K-dup_pos1)%(cycle)+dup_pos1])

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
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])

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()
    A = LIST()
    po = 1
    lis = [1]
    for i in range(N):
        po = A[po-1]
        if po == 1:
            break
        lis.append(po)

    K %= len(lis)
    print(lis[K])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[-1]*N
l[0]=0
i=1
t=0
while 1:
    t=A[t]-1
    if l[t]!=-1:
        s=i-l[t]
        break
    l[t]=i
    i+=1
if K<i:
    print(l.index(K)+1)
else:
    k=K%s
    if l[t]//s+k in l:
        print(l.index(l[t]//s*s+k)+1)
    else:
        print(l.index((l[t]//s-1)*s+k)+1)