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 sys
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))

kyori = [-1]*n

now = 0;kyori[now] = 0
jyunkan = 0;v = 0
for i in range(n):
    tugi = a[now] - 1
    if kyori[tugi] != -1:
        jyunkan = kyori[now] + 1
        v = tugi
        break
    kyori[tugi] = kyori[now] + 1
    if kyori[tugi] == k + 1:
        print(tugi)
        sys.exit()
    now = tugi
k -= kyori[v]
mawaru = jyunkan - kyori[v]
amari = k % mawaru

now = v;cunt = 0
for i in range(amari):
    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()))

ni = 1
loop = [1]
while True:
    if a[ni-1] in loop:
        break
    else:
        ni = a[ni-1]
        loop.append(ni)
ln = a[ni-1]
k -= loop.index(ln)
loop2 = []
for i in range(loop.index(ln),len(loop)):
    loop2.append(loop[i])

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

Submission status: Wrong Answer

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

num = [0]*(N+1)
inv = [1]

p = 1
count = 1
while num[p] == 0:
    num[p] = count
    p = A[p]
    inv.append(p)
    count += 1

m = max(num)

###
#print(p)
#print(num)
#print(inv)
#print((K-(num[p]-1))%(m-(num[p]-1))+num[p])
###

print(inv[(K-(num[p]-1))%(m-(num[p]-1))+num[p]-1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = LI()
now = 0
D = {0:-1}
for i in range(n):
    now = A[now] - 1
    if now in D:
        break
    D[now] = i


k -= (i + 1)
b = now
c = k % (i - D[now])
for j in range(c):
    b = A[b] - 1

print(b + 1)

Submission status: Wrong Answer

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

for _ in range(K):
    if A[loop[-1]]-1 not in loop:
        loop.append(A[loop[-1]]-1)
    else:
        break

l = len(loop)
print(loop[K % l]+1)

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

city_position = {}
step = 0
city = 0

while city not in city_position:
	city_position[city] = step
	city = a[city]
	step += 1

steps_before_cycle = city_position[city]
cycle_len = step - steps_before_cycle

k %= cycle_len

city = 0
for i in range(k):
	city = a[city]

print(city+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A
L = []
S = set()
i = 1
cnt = 0
while not i in S or cnt == K:
    S.add(i)
    L.append(i)
    cnt += 1
    i = A[i]

j = L.index(i)
ans = L[(j + (K - j) % (cnt - j)) % cnt]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:


n, k = map(int,input().split())
cities = list(map(lambda x:int(x)-1,input().split()))

footprint_L = list()
footprint_S = set()
i = 0
footprint_S.add(i)
footprint_L.append(i)
while cities[i] not in footprint_S:
    footprint_S.add(cities[i])
    footprint_L.append(cities[i])
    i = cities[i]

#print("terminal:", cities[i])


cyclestart = footprint_L.index(cities[i])
cyclelen = len(footprint_L) - cyclestart
#print(footprint_L)
#print(cyclestart, cyclelen)
#print((k-cyclestart) % cyclelen)
if k >= len(footprint_L):
    print(footprint_L[cyclestart+(k-cyclestart) % cyclelen]+1)
else:
    print(footprint_L[k])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
test_pos = 1
sub = 0
loop = 0
city = [0 for i in range(n)]

while True:
    test_pos = a[test_pos-1]
    city[test_pos-1] += 1
    sub += 1
    if city[test_pos-1] == 2:
        loop_start = test_pos
        break

while True:
    test_pos = a[test_pos-1]
    loop += 1
    if test_pos == loop_start:
        break

k -= sub
k %= loop
pos = loop_start

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

print(pos)

Submission status: Wrong Answer

Solution code:

def solve(N, K, As):
    visited = set()
    current = 1
    count = 0
    teleport = {i + 1 : v for i, v in enumerate(As)}
    dist = {}

    while current not in dist:
        visited.add(current)
        dist[current] = count
        count += 1
        current = teleport[current]

    loop_size = count - dist[current]
    K -= dist[current]
    K %= loop_size
    K += dist[current]

    current = 1
    while K > 0:
        current = teleport[current]
        K -= 1
    return current


if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    As = list(map(int, input().split(" ")))
    print(solve(N, K, As))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
x = [1]
y = [False]*n
i = 0
s = 0
t = 0
xa = x.append
while True:
  if y[i] == True:
    for j in range(len(x)):
      if i == x[j]-1:
        s = j
        t = len(x)-j-1
        break
    break
  else:
    y[i] = True
    xa(a[i])
    i = a[i]-1
x = x[s:-1]
print(x[(k-s)%t])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*A, = map(int, input().split())
# n, k = 6, 727202214173249351
# A = [6, 5, 2, 5, 3, 2]
# n, k = 2, 1
# A = [1, 3]
A = [a - 1 for a in A]
now = 0
V = [False for i in range(n)]
V[0] = 0
L = [now]
cnt = 0
while not V[A[now]]:
    cnt += 1
    now = A[now]
    L.append(now)
    V[now] = cnt
a = V[A[now]]
b = cnt - a + 1
print(L[(k - a) % b + a] + 1)

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
        break
    seen.add(here)
    route.append(here)
    # print(route)
# print(period)
idx = (K - start) % period + start
print(route[idx] + 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
cycle=[1]
flag=0
id=0
while flag==0:
    if a[id] in cycle:
        node=cycle.index(a[id])
        flag=1
    else:
        cycle.append(a[id])
        id=a[id]-1
K-=node
cycle=cycle[node::]
q=K%len(cycle)
print(cycle[q])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())

A=list(map(int,input().split()))
L=[1]
now=1
for _ in range(n):
        now=A[now-1]
        if now  in L:
                index=L.index(now)
                break
        L.append(now)
if k<=index:
        print(L[k+1])
        exit()
R=(k-index)%(len(L)-index)

print(L[index:][R])

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
    print(s[i+K%(len(s)-i)])

Submission status: Wrong Answer

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

kyori = [-1]*n

now = 0;kyori[now] = 0
jyunkan = 0;v = 0
for i in range(n):
    tugi = a[now] - 1
    if kyori[tugi] != -1:
        jyunkan = kyori[now] + 1
        v = tugi
        break
    kyori[tugi] = kyori[now] + 1
    if kyori[tugi] == k:
        print(tugi)
        sys.exit()
    now = tugi
k -= kyori[v]
mawaru = jyunkan - kyori[v]
amari = k % mawaru

now = v;cunt = 0
for i in range(amari):
    now = a[now] - 1
    
print(now + 1)

Submission status: Wrong Answer

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

x = 0
l = [0]
flag = False
for i in range(N):
    x = A[x]-1
    if x==0:
        break
    if l[-1]==x:
        flag = True
        break
    l.append(x)

if not flag:
    print(l[K%len(l)]+1)
else:
    if K>=len(l):
        print(l[-1]+1)
    else:
        print(l[K]+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
x=0
t=0
d=[0]*N
v=[-1]*N
while t==0 or x!=0:
    v[x]=t
    t+=1
    if v[A[x]-1]>=0:
        break
    x=A[x]-1
    d[t]=x
c=t-v[A[x]-1]
print(d[(K-t)%c+v[A[x]-1]]+1)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n
count[0]+=1

for i in range(k):
    if count[root[-1]-1]!=0:
        break
    else:
        root.append(As[root[-1]-1])
        count[As[root[-1]-1]-1]+=1
print(root[k%len(root)])

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
    ct = 1
    lis = [1]
    for i in range(N):
        po = A[po-1]
        if po == 1:
            break
        ct += 1
        lis.append(po)

    # print(lis, ct)

    K %= ct
    print(lis[K])


if __name__ == '__main__':
    main()

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]
    K//=2
    i+=1
print(p+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
al = [0 for i in range(n+1)]
al[1] = 1
now = 1
for i in range(n):
    nex = a[now-1]
    if al[nex] == 0:
        l.append(nex)
        now = nex
        al[nex] += 1
        if i == n-1:
            goal = nex
    else:
        goal = nex
        break
key = 0
count = 0
roop = []
mae = []
for i in l:
    if key == 0:
        if goal == i:
            key = 1
            roop.append(i)
        else:
            mae.append(i)
            count += 1
    else:
        roop.append(i)
if count >= k:
    print(mae[k-1])
else:
    k -= count
    rp = len(roop)
    k %= rp
    print(roop[k])

Submission status: Wrong Answer

Solution code:
import sys

n, k, *a = map(int, sys.stdin.read().split())
for i in range(n):
    a[i] -= 1

def main():
    r = k
    cnt = [None] * n
    cnt[0] = 0
    now = 0
    for i in range(1, n + 1):
        now = a[now]
        if cnt[now] is not None:
            r -= cnt[now]
            loop = i - cnt[now]
            break
        cnt[now] = i
    r %= loop 
    for _ in range(r):
        now = a[now]
    print(now + 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
flag = [0] * (N+1)
cnt = [0] * (N+1)
now = 0
i = 1
while(flag[i] == 0 and K >= now):
  flag[i] = 1
  cnt[i] = now
  i = A[i-1]
  now += 1
if(K < now):
  print(i)
else:
  loop = now-cnt[i]
  kn = (K-now)%loop
  for j in range(kn):
    i = A[i-1]
  print(i)

Submission status: Wrong Answer

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

seen = [0]
next = 0

while len(seen) < K:
    next = A[next]-1
    if next in seen:
        break
    seen.append(next)

next_ind = seen.index(next)

loop = seen[next_ind:]

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_ls = list(map(int, input().split()))

my_dict = dict()

ptr = 0
cnt = 0

while True:
    if ptr in my_dict:
        break
    else:
        my_dict[ptr] = cnt
        cnt += 1
        ptr = a_ls[ptr] - 1

loop = cnt - my_dict[ptr]
init = my_dict[ptr]

k = (k - init) % loop + init


ptr = 0

for i in range(k):
    ptr = a_ls[ptr] - 1

print(ptr + 1)

Submission status: Wrong Answer

Solution code:
import sys

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

k %= n
i = 0
for _ in range(k):
    i = nums[i] - 1

print(i + 1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    next = a[0]
    for i in range(k-1):
        print('next={}'.format(next))
        next = a[next-1]
    print(next)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
visited = [-1 for _ in range(N+1)]
path = [1]
visited[1] = 0
pos = 1
endi = -1
for i in range(1, N+1):
    pos = A[pos]
    if visited[pos] > -1:
        endi = i
        break
    visited[pos] = i
loop = endi - visited[pos]
ansi = (K-visited[pos])%loop
for i in range(ansi):
    pos = A[pos]
print(pos)

Submission status: Wrong Answer

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

warp = [-1] * (n+1)
warp[1] = 0

now = 1
for i in range(n):
    next_town = a[now-1]
    if warp[next_town] == -1:
        warp[next_town] = i+1
        now = next_town
    else:
        break

loop = (i+1) - warp[next_town]

rem = (k-warp[next_town])%loop

print(warp.index( rem + warp[next_town]))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
n = 1
B = [0]*N
for i in range(2*N+1):
    B[n-1] = i+1
    n = A[n-1]
    if B[n-1] != 0:
        t = i+2
        break
c = t-B[n-1]
s = (K+1-B[n-1])%c
for i in range(s):
    n = A[n-1]
print(n)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[i-1 for i in A]
c=A[A[0]]
if c==0:
  print(c+1)
  exit()
s=set()
s.add(0)
s.add(A[0])
while True:
  if c in s:
    break
  s.add(c)
  c=A[c]
K%=len(s)
ans=A[0]
for i in range(K):
  ans=A[ans]
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ans = [1]
gone = [0] * n
now = 0
lis = list(map(int,input().split()))
while True:
    gone[now] = 1
    if gone[lis[now]-1] == 0:
        gone[lis[now]-1] = 1
        now = lis[now]-1
        ans.append(now+1)
    else:
        num = len(ans)
        now = lis[now]
        first = ans.index(now)
        k -= first
        k %= (num-first)
  #         print(ans,now)
  #         print(first,k)
        print(ans[first+k])
        exit()

Submission status: Wrong Answer

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

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

cur = 0
while True :
  nex = A[cur]
  if visited[nex] == -1 :
    visited[nex] = visited[cur] + 1
    cur = nex
  else :
    # 一回目
    i = visited[nex]
    # 二回目
    j = visited[cur] + 1
    K -= i
    K %= (j - i)
    cur = nex
    break
    
while K > 0 :
  cur = A[cur]
  K -= 1
  
print(cur + 1)

Submission status: Wrong Answer

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

t=0
j=0
c=[]
for i in range(n):
    j=a[j]-1
    c.append(j + 1)
    if j+1==1:
        break


s=(k-1)%len(c)
print(c[s])

Submission status: Wrong Answer

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

loop_start=-1
visited={0}
cur=0
while True:
    cur=A[cur]
    if cur in visited:
        loop_start=cur
        break
    else:
        visited.add(cur)


assert loop_start!=-1
#print("start=>",loop_start)

before_loop=0
cur=0
while cur!=loop_start:
    cur=A[cur]
    before_loop+=1
#print("bef",before_loop)

loop_path=[loop_start]
cur=A[loop_start]
while cur!=loop_start:
    loop_path.append(cur)
    cur=A[cur]
#print("loop=>",loop_path)
#print("llop len=>",len(loop_path))

K-=before_loop
print(loop_path[K%len(loop_path)]+1)

Submission status: Wrong Answer

Solution code:


n, k = map(int,input().split())
cities = list(map(lambda x:int(x)-1,input().split()))

footprint_L = list()
footprint_S = set()
i = 0
footprint_S.add(i)
footprint_L.append(i)
while cities[i] not in footprint_S:
    footprint_S.add(cities[i])
    footprint_L.append(cities[i])
    i = cities[i]

#print("terminal:", cities[i])


cyclestart = footprint_L.index(cities[i])
cyclelen = len(footprint_L) - cyclestart
#print(footprint_L)
#print(cyclestart, cyclelen)
#print((k-cyclestart) % cyclelen)
print(footprint_L[cyclestart+(k-cyclestart) % cyclelen]+1)

Submission status: Wrong Answer

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

visited = [False]*(N)
visited[0] = True

dist = [0]*N
cur = 0
idx = 1
result = [0]
while True:
    nxt = A[cur]
    if visited[nxt]:
        amari = dist[nxt]
        MOD = idx - amari
        break
    else:
        visited[nxt] = True
        dist[nxt] = idx
        cur = nxt
        idx += 1
        result.append(cur)
        
K -= amari
result = result[amari:]
ans = result[K%MOD]+1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from random import choice,randint
inp=sys.stdin.readline
out=sys.stdout.write
flsh=sys.stdout.flush
 
sys.setrecursionlimit(10**9)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def MI(): return map(int, inp().strip().split())
def LI(): return list(map(int, inp().strip().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]
def LI_(): return [int(x)-1 for x in inp().strip().split()]
def LF(): return [float(x) for x in inp().strip().split()]
def LS(): return inp().strip().split()
def I(): return int(inp().strip())
def F(): return float(inp().strip())
def S(): return inp().strip()
def pf(s): return out(s+'\n')
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n,k=MI()
    a = LI()
    x = k%n
    # print(x)
    prev = 1
    for i in range(x):
        prev = a[prev]
    print(a[prev])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
inp=input
li=list
iinp=lambda : int(inp())
ent=lambda : map(int,inp().split())
lient=lambda : [int(i) for i in inp().split()]
li0= lambda x: [0 for i in range(x)]
livalor=lambda valor,x: [valor for i in range(x)]
stent=lambda : [i for i in inp()]
filldp=lambda valor,x,y: [[valor for i in range(y)] for i in range(x)]

n,k=ent()
cuidades=lient()
print(n-k%n+1)

Submission status: Wrong Answer

Solution code:
import sys

n, k, *aaa = map(int, sys.stdin.buffer.read().split())
shortest = [-1] * n
v = 0
d = 0
while shortest[v] == -1:
    shortest[v] = d
    v = aaa[v] - 1
    d += 1
    if d == k:
        print(v + 1)
        exit()

first = shortest[v]
loop = d - first
remaining = (k - first) % loop
print(v, first, loop, remaining + first, shortest)
ans = shortest.index(remaining + first) + 1
print(ans)

Submission status: Wrong Answer

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

cycle = []
pos = 1
for i in range(N):
    cycle.append(pos)
    pos = As[pos-1]
    if pos == 1:
        break

#print(cycle)

mod = K % len(cycle)
print(cycle[mod])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
n = 1
B = [0]*N
for i in range(N):
    B[n-1] = i+1
    n = A[n-1]
    if B[n-1] != 0:
        t = i+2
        break
c = t-B[n-1]
s = (K+1-B[n-1])%c
for i in range(s):
    n = A[n-1]
print(n)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a=list(map(int,input().split()))
cnt=[0]*(n+1)
x=1
cnt[x]=1
rep=1
for i in range(2*n):
    x=a[x-1]
    rep+=1
    if cnt[x]!=0:
        break 
    else:
        cnt[i+1]=x
print(cnt[k%rep+1])

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()
for i in range(n):
    now = a[now]
    if now in done:
        break
    done.add(now)
    cities.append(now)
idx_s = cities.index(now)
f = (len(cities) - idx_s)
d = k - idx_s
idx = d%f
ans = cities[idx + idx_s]
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n

for i in range(k):
    if count[root[-1]-1]!=0:
        break
    else:
        root.append(As[root[-1]-1])
        count[As[root[-1]-1]-1]+=1
print(root[k%len(root)])

Submission status: Wrong Answer

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

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

ansl = []
used = [0 for i in range(n)]
now = 0
while True:
    if used[now] == 1:
        break
    ansl.append(now)
    used[now] = 1
    now = al[now] -1

l = len(ansl)
last = ansl.index(now)
loop = ansl[last:]

if k < len(ansl):
    print(ansl[k])
else:
    k -= len(ansl)
    print(loop[k % len(loop)] + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
MOD = 10**9+7
# MOD = 998244353

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

visited = dict()
rvs = dict()
cur = 0
num = 0
while True:
    if cur in visited:
        # print(visited)
        # print(rvs)
        s = visited[cur]
        roop = num - s
        kk = (k-s)%roop
        # print(s, roop, kk, kk+s)
        print(rvs[kk+s]+1)
        exit()
    else:
        visited[cur] = num
        rvs[num] = cur
        cur = A[cur]-1
        num += 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


from collections import defaultdict


class Graph(object):
    def __init__(self, N):
        self.N = N
        self.E = defaultdict(list)

    def add_edge(self, f, t, w=1):
        self.E[f].append((t, w))


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

    # シミュレーションでループを検出
    g = Graph(N)
    for X, Y in zip(range(1, N+1), A):
        g.add_edge(X-1, Y-1)

    seen = [False]*N
    prev = []
    state = 0

    while True:
        seen[state] = True
        prev.append(state)
        for dest, _ in g.E[state]:
            state = dest
        if seen[state]:
            break
    amari = prev.index(state)
    period = len(prev)-amari
    # print(state, amari, period, prev)
    if K < amari:
        print(prev[K])
    else:
        print(prev[amari + (K-amari) % period]+1)
    return


if __name__ == '__main__':
    main()

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

visited = [0 for _ in range(n)]
i = 0
while visited[i] == 0:
    visited[i] = 1
    i = a[i]

cnt = 0
goal = i
i = goal
while not (i == goal and cnt > 0):
    i = a[i]
    cnt += 1

i = 0
while i != goal:
    k -= 1
    i = a[i]
k = k % cnt
i = goal

for cnt in range(k):
    i = a[i]

print(i+1)

Submission status: Wrong Answer

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

lastVisit = [None] * N
now = 0
n = 0
while lastVisit[now] is None:
    lastVisit[now] = n
    now = As[now]-1
    n += 1

for _ in range((K-lastVisit[now]) % (n-lastVisit[now])):
    now = As[now]-1
print(now+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A = [a-1 for a in A]  # 0-index

    cnt = 0
    i = 0
    for _ in range(N):
        i = A[i]
        cnt += 1

        if cnt == K:
            return i+1

        if i == 0:
            break

    # K > cnt
    rest = K % cnt
    i = 0
    for _ in range(rest):
        i = A[i]
    
    return i+1



print(main())

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
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:
	a=1
	for i in range(k):
		a=l[a-1]
	print(a)

Submission status: Wrong Answer

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

loop = [1]
now = 1
d = set([1])
for i in range(n):
    now = a[now]
    if now in d:
        for j in range(len(loop)):
            if now == loop[j]:
                ind = j
                break
    d.add(now)
    loop.append(now)

if len(loop) >= k:
    print(loop[k-1])

else:
    k -= ind
    loop = loop[ind:]
    print(loop[k%len(loop)])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a=list(map(int,input().split()))
cnt=[0]*(n+1)
x=1
cnt[x]=1
rep=1
for i in range(n):
    x=a[x-1]
    rep+=1
    if cnt[x]!=0:
        break 
    else:
        cnt[i+1]=x
print(cnt[k%rep+1])

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(lambda x: int(x)-1, input().split()))
    visited = [0]
    visited_set = {0}
    now = 0
    for i in range(2*n):
        now = a[now]
        if now in visited_set:
            break
        else:
            visited.append(now)
            visited_set.add(now)
    l = len(visited)
    v = visited.index(now)
    k -= v
    k %= (l-v)
    k += v
    print(visited[k]+1)


main()

Submission status: Wrong Answer

Solution code:
import sys


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


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

pos = A[1]
path = [1]
for _ in range(N):
    if pos == 1:
        break
    path.append(pos)
    pos = A[pos]

print(path[K % len(path)])

Submission status: Wrong Answer

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

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

M = K%N

town_now = 1

for _ in range(M):
  town_now = A[town_now-1]

print(town_now)

Submission status: Wrong Answer

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

loop = [1]
now = 1
d = set([1])
for i in range(n):
    now = a[now]
    if now in d:
        for j in range(len(loop)):
            if now == loop[j]:
                ind = j
        break
    d.add(now)
    loop.append(now)

if len(loop) >= k:
    print(loop[k-1])

else:
    k -= ind
    loop = loop[ind:]
    print(loop[k%len(loop)])

Submission status: Wrong Answer

Solution code:
(N, K) = [int(n) for n in input().split()]
A = [int(n)-1 for n in input().split()]
v = [-1 for _ in range(N)]

D = [0]
v[0] = 0

cur = A[0]
i = 1

while v[cur] == -1:
    D.append(cur)
    v[cur] = i
    

    i += 1
    cur = A[cur]

K -= v[cur]
K %= (i - v[cur])
    
print(D[v[cur] + K]+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd

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


res = 1
dic=dd(int)

now = 1
mita = set()
mita.add(now)
dic[now] = 1
start = 0
end = 0
# loop
while 1:
    next_town = a[now-1]
    # print(next_town)
    if next_town in mita:
        start = next_town
        end = now
        break
    else:
        res += 1
        now = next_town
        mita.add(next_town)
        dic[next_town] = res
    if res == K:
        print(next_town)
        exit()
# print(dic)

loop_num = dic[end] - dic[start] + 1
Kmod = (K-res)%loop_num
# print(K,res,loop_num,Kmod)
now = next_town
for i in range(Kmod):
    next_town = a[now-1]
    now = next_town
print(now)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().strip()
n,k = map(int,input().split())
k = k%n
a = list(map(int,input().split()))
cur = 0
while k:
    k-=1
    cur = a[cur]-1
print(cur+1)

Submission status: Wrong Answer

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

history = [None] * N

pos = 0
for idx in range(N):
    if history[pos] != None:
        offset = history[pos]
        step = idx - offset
        break
    else:
        history[pos] = idx
        pos = A[pos] - 1

n_step = (K - offset) // step
n_targ = K - (offset + n_step * step)

for _ in range(n_targ):
    pos = A[pos] - 1

print(pos + 1)