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:
def search(now, teleporter, limit):
    visit = [-1] * len(teleporter)
    cnt = 0
    
    while visit[now] == -1:
        if limit <= cnt:
            break
        
        visit[now] = cnt
        cnt += 1
        now = teleporter[now]
        
    
    return now, cnt, cnt - visit[now]
    

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

start, initial_cnt, repeat_num = search(0, a, 10**6)

#print(start, initial_cnt, repeat_num)

if k < initial_cnt:
    r = k
else:
    r = (k - initial_cnt) % repeat_num

#print(r)

ans, _, _ = search(start, a, r)

print(ans + 1)

Submission status: Wrong Answer

Solution code:
# N個の町, K個のテレポート
N, K = map(int, input().split())

# 町
array = list(map(int, input().split()))

while True:
  K = int(K/N)
  if K < N:
    print(array[array[K]])
    break

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
hako=[0]*N
for i in range(N):
    A[i]-=1
ichi=0
count=0
i=0
while True:
    if hako[A[i]]>2:
        break
    hako[A[i]]+=1
    i=A[i]
    count+=1
for i in range(K%count+count):
    ichi=A[ichi]
print(ichi+1)
#print(count)
#print(hako)

Submission status: Wrong Answer

Solution code:
import sys

N,K = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))

pos = 1
hist = [1]
count = 0

while True:
    pos = A[pos-1] # next
    if count > 0 and pos in hist:
        prefix = hist[:hist.index(pos)]
        cycle = hist[hist.index(pos):]
        break
    hist.append(pos)
    count += 1

# print(count)
# print(hist)
ans = (K - len(prefix)) % len(cycle)
print(cycle[ans])

Submission status: Wrong Answer

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

v=1
l=[]
for i in range(k):
    v=a[v-1]
    if v in l:
        index=l.index(v)
        l2=l[index:]
        print(l2[(k-index-1)%len(l2)])
        break
    else:
        l.append(v)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    As = list(map(int, input().split()))
    i = 1
    graph = []
    for _ in range(n):
        i = As[i-1]
        if i in graph:
            k = k - graph.index(i)
            start = graph.index(i)
            ans = graph[start:][k % (len(graph[start:])) - 1]
            break
        graph.append(i)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


while True:
    if not A[i]:
        start = i + 1
        break
    s.append(i + 1)
    dest = A[i]
    A[i] = None
    i = dest - 1
    length += 1



if start == s[0]:
    loop = K // length
    amari = K % length

    if amari - 1 == -1:
        print(s[length - 1])
    else:
        print(s[amari - 1])
else:
    idx = s.index(start)
    s2 = s[idx:]
    length2 = len(s2)
    tmp = length - length2

    if K < tmp:
        print(s[K])
    else:
        K -= tmp
        amari = K % length2

        if amari - 1 == -1:
            print(s2[length2 - 1])
        else:
            print(s2[amari - 1])

Submission status: Wrong Answer

Solution code:
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):
        x = A[x]
        if L[x] == 2:
            #print(L)
            lc = (K-i) % L.count(2)
            for _ in range(lc):
                x = A[x]
            print(A[x])
            break
        else:
            L[x] += 1
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
li=list(map(int,input().split()))
H=[0]*(n)
walk=[]
x=1
l=0
e=k
while e>=0:
    walk.append(x)
    if H[x-1]==0:
        H[x-1]=1
    else:
        dd=[i for i, j in enumerate(walk) if j == x ]
        l=1
        break
    x=li[x-1]
    e-=1
if l==1:
    ran=dd[1]-dd[0]
    k-=dd[0]
    kk=k%ran
    print(walk[dd[0]+kk])
else:
    print(x)

Submission status: Wrong Answer

Solution code:
a = raw_input().split()
n = int(a[0])
k = int(a[1])
a = raw_input().split()
m = []
i = 0
l=0
flag=0
while 1:
    if i in m:
        break
    m.append(i)
    l+=1
    if l==k:
        flag=1
        break
    i = int(a[i]) - 1
if flag:
    print m[-1]
else:
    ll = len(m)
    s = m.index(i)
    res = (k - s) % (l - s)
    print m[res + s] + 1

Submission status: Wrong Answer

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

N,K=map(int,input().split())
A=input().split()
A=[int(a) for a in A]

P=0
L=[0]*int(N)
D=[0]*int(N)
c=0
R=0
while(1):
    c+=1
    P=A[P]-1
    L[c]=P
    if D[P]>0: 
        R=D[P]
        break
    D[P]=c
    if P==0: break
    

#print(L)
K-=R
K%=(c-R)
#print(R,K)
ans=L[R+K]+1

print(ans)

Submission status: Wrong Answer

Solution code:
def next(n,k,arr):
	
	visited = [0]
	cur = arr[0]-1
	while True:

		if cur in visited:
			rep = visited.index(cur)
			break
		else:
			k -= 1
			visited.append(cur)
			cur = arr[cur] - 1
		
		if k == 0:
			print(cur + 1)
			return
	rep_visit = visited[rep:]
	rem = k%len(rep_visit)
	if rem == 0:
		print(rep_visit[-1]+1)
	else:
		print(rep_visit[rem-1]+1)
	return


    
#m = int(input())

#for t in range(m):
#	n = int(input())
a,b = map(int,input().split())
#s = input()
#t = input()

arr = list(map(int,input().split()))
next(a,b,arr)
'''
qarr = []
for i in range(b):
	qarr.append(int(input()))

#k = input()
#n = int(input())
Sol(a,b,arr,qarr)'''
#for i in range(n):
#	next(int(input()))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(" "))
A = [int(i)-1 for i in input().split(" ")]
check = [0]*N
here = 0
for _ in range(2*N):
    check[here] += 1
    here = A[here]
c = sum([1 for i in check if i>1])
c_s = sum([1 for i in check if i==1])
K -= c_s
here = 0
for _ in range(c_s + K%c):
    here = A[here]
print(here+1)

Submission status: Wrong Answer

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


y = x[cnt:-1]
if k>cnt:
    ind = (k-cnt)%len(y)
    print(y[ind]+1)
else:
    print(x[(k+1)%len(x)]+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A_list = list(map(int,input().split()))
# roopがあるときはその範囲

roop = N
first = 0
now = 1
already = [1]
for i in range(N):
  go_town = A_list[now-1]
  already.append(go_town)
  now = go_town

set_A = set(already)
for i in range(len(set_A)+1):
  try:
    set_A.remove(already[i])
  except KeyError:
    first = already.index(already[i])
    roop = already[first:i]
    break

real_K = (K - first) % len(roop)
print(roop[real_K])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
d = [0]*n
g = 0
d[0]=1
b1 = []
b = []
interval = 0
count = 0
if 2*n<k:
    for i in range(k):
        if count==0:
            if g in b1:
                interval = i #１周かかるのにどれくらいするのかを計算
                count += 1

            else:
                b1.append(g)
                g = a[g]
        else:
            if g in b:
                break
            else:
                b.append(g)
else:
    for i in range(k):
        if g in b:
            if interval==0:
                interval = i #１周かかるのにどれくらいするのかを計算
            else:
                break
        else:
            b.append(g)
            g = a[g]
        
index = k %len(b)
print(b[index]+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
iter_req = k+1
while town not in visited:
    visited[town] = times
    times += 1
    town = ls[town - 1]
if k>=times:
    period = times - visited[town]
    iter_req = visited[town]
    k -= visited[town]
    if k > period:
        k %= period
    iter_req += k

town = 1
ans = ls[town-1]

while iter_req >1:
            town = ans
            ans = ls[town - 1]
            k -= 1
            iter_req -= 1

print(ans)

Submission status: Wrong Answer

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

s = 0
seen = [False for _ in range(n)]
d = [0]
cy = -1
for _ in range(n):
    seen[s] = True
    s = a[s]-1
    if seen[s]:
        cy = s
        break
    d.append(s)

if cy == -1:
    print(d[k-1])
    exit()

noncyi = d.index(cy)
cyi = len(d) - noncyi
if k <= noncyi:
    print(d[k-1])
else:
    k -= noncyi
    print(d[noncyi + k%cyi]+1)

Submission status: Wrong Answer

Solution code:
nk = input('')
n = int(nk.split(' ')[0])
k = int(nk.split(' ')[1])

alist = input('').split(' ')
alist2 = []

next=1
loop_end=0
for i in range(len(alist)):
    next = int(alist[next-1])
    if next in alist2:
        loop_end=next
        break
    else:
        alist2.append(next)

head_size=alist2.index(loop_end)
loop_size=len(alist2) - head_size

k2=(k-head_size)%loop_size+head_size
pos=alist2[k2-1]

print(pos)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
L = list(map(int,input().split()))
L2 = []

t = 1
r = 0
i = 0
c = 0
for i in range(n*2):
    #print(L2)
    t = L[t-1]
    if ｔ in L2:
        i = L2.index(t)
        r = len(L2) - i
        break
    else:
        L2.append(t)
#print(r)
#print(i)

k = (k - i)%r
#print(k)
print(L2[i+k-1])

Submission status: Wrong Answer

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

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

print(C)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, k = map(int, input().split())
array = [int(x)-1 for x in input().split()]
count = [-1 for x in range(n)]

now = 0
count[now] = 0
queue = deque()
queue.append(now)
dag = False
dag_start = -1
dag_end = -1
while len(queue) != 0:
    now = queue.pop()
    next_index = array[now]
    if count[next_index] != -1:
        dag = True
        dag_start = next_index
        dag_end = now
        break
    count[next_index] = count[now] + 1
    queue.append(next_index)

if not dag:
    print(1)
    exit()
if count[dag_start] > k:
    for i in count:
        if i == k:
            print(i+1)
            exit()
now = dag_start
d = [now]
while now != dag_end:
    d.append(array[now])
    now = array[now]
print(d[(k - count[dag_start]) % len(d)] + 1)

Submission status: Wrong Answer

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

next_city = 1
visited = []
while not next_city in visited:
    visited.append(next_city)
    next_city = A[next_city-1]
idx = visited.index(next_city)
visited2 = visited[idx:]
mod = len(visited2)
x = (k%mod-idx)%mod
print(visited2[x])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().strip().split())
a = input().split()
list = []
position = 0
z = 0
for i in a:
    i = int(i)
    list.append(i)
for j in range(1, k+2):
    position = list[z-1]
    z = position
print(position)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

N = None
K = None
A = None

def check(start):
    global N,K,A
    slow = A[start]
    fast = A[A[start]]
    while slow != fast:
        slow = A[slow]
        fast = A[A[fast]]

    slow = start
    while slow != fast:
        slow = A[slow]
        fast = A[fast]

    cycle_start = slow

    cycle_length = 1
    slow = A[slow]
    fast = A[A[fast]]
    while slow != fast:
        slow = A[slow]
        fast = A[A[fast]]
        cycle_length += 1

    return cycle_start, cycle_length

def main():
    global N,K,A
    N,K = map(int, input().split())
    A = list(map(int, input().split()))
    A = [""] + A
    start,length = check(1)
    #print(start,length)
    current = 1
    if start is None:
        for _ in range(K):
            current = A[current]
        return current
    #print("Detect cyclic")
    count = 0
    while current != start:
        current = A[current]
        count += 1
    #print(current)
    #print(count)
    K = (K-count)%length
    for _ in range(K):
        current = A[current] 
    return current

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()));
a = list(map(int,input().split()));
i = 0;
count = 0;
while count<=n :
    i = a[i]-1;
    count += 1;
    if i == 0 :
        break;

amari = 0;
if count == n :
    ans = i;
else :
    amari = k%count;
    i = 0;
    for j in range(amari) :
        i = a[i]-1;
    ans = i+1;

print(ans);

Submission status: Wrong Answer

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

cur = 1
loop = -1
arr = []
for i in range(n): arr.append([False])

# ループする場所を調べる
while True:
    if arr[cur-1] == True:
        loop = cur
        break
    else:
        arr[cur-1] = True
        cur = a[cur-1]

cur = 1
cnt = 0
loop_cnt = 0
while True:
    cur = a[cur-1]
    cnt += 1
    if cur == loop and loop_cnt > 1: break
    if cur == loop: loop_cnt += 1
    if loop_cnt == 1: cnt = 0

if cur == 1:
    k %= cnt-1
else:
    k %= cnt
# print(f"cnt: {cnt}")
# print(f"k: {k}")
# print(f"cur: {cur}")
for i in range(k):
    cur = a[cur-1]

print(cur)

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]
sets = set(order)
while True:
  if len(order) == K:
    print(order[-1]+1)
    break
  if M[s] in sets:
    #print(order, M[s])
    K -= order.index(M[s])
    order = order[order.index(M[s]):]
    print(order[K%len(order)]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]

Submission status: Wrong Answer

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

already = [False for i in range(N)]
already[0] = True
tel = [0]

while True:
    tmp = A[tel[-1]]
    tel.append(tmp)
    
    if already[tmp]:
        tel.pop()
        break
        
    already[tmp] = True

for i in range(len(tel)):
    if tel[i] == tmp:
        s = i
        break
if K - s >= 0:
    ans = (K - s) % (len(tel) - s) 
    print(tel[ans + s] + 1)
else:
    print(tel[K + 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
v = []
now = 0
for i in range(K):
  p = A[now]
  now = p-1
  if p in v:
    j = v.index(p)
    df = i-j
    Kn = (K-i-1) % df
    p = v[j] + Kn
    break
  else:
    v.append(p)
print(p)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    lst = list(map(int, input().split()))
    ma = [l-1 for l in lst]
    
    visited = [False for _ in range(n)]
    history = []
    now = 0
    while True:
        if visited[now]:
            history.append(now)
            break
        
        visited[now] = True
        history.append(now)
        
        now = ma[now]
    if k <= len(history)-1:
        print(history[k])
        return
    
    last = history[-1]
    for i in range(n):
        if history[i] == last:
            roopstart = i
            break
    syokilst = history[:roopstart]
    rooplst = history[roopstart:]
    syokilen = len(syokilst)
    rooplen = len(rooplst)
    resi = k - (syokilen-1)
    mod = resi%rooplen
    print(rooplst[mod])
        
main()

Submission status: Wrong Answer

Solution code:
N,K= map(int, input().split())
A = list(map(int,input().split()))
P =[]
p = 1
P.append(p)
start,stop = 0, 0
for i in range(N):
    p = A[p-1]
    if p in P:
        start = P.index(p)
        P.append(p)
        break
    P.append(p)

stop = len(P)-1
Z = (K-start-1)%(stop-start)
print(P[Z+start+1])

Submission status: Wrong Answer

Solution code:
from itertools import permutations


def resolve():
    n, k = map(int, input().split())
    a = list(map(lambda x :int(x)-1, input().split()))
    visited = [0 for i in range(n)]
    route = [0 for i in range(n)]

    now = 0
    cnt = 0
    while visited[now] == 0 and cnt < k:
        visited[now] = cnt + 1
        route[cnt] = now
        now = a[now]
        cnt += 1
    route[cnt] = now


    loop = cnt - visited[now] + 1
    tt = (k - visited[now] + 1) % loop
    print(route[visited[now] - 1 + tt] + 1)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=tuple(map(int,input().split()))
flag=[-1]*n
flag[0]=0
x,count=1,0
for i in range(1,n+1):
  x=a[x-1]
  if flag[x-1]<0:
    flag[x-1]=i
  else:
    l=i-flag[x-1]
    count=flag[x-1]
    break
k=(k-count)%l
for _ in range(k):
  x=a[x-1]
print(x)

Submission status: Wrong Answer

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

exist = [1]
#exist.append(a[0])
ex = {}
ex[1] = 0
#ex[a[0]] = 0
if k == 1:
    print(1)
    exit()

cnt = 1
while(True):
    if a[exist[-1]-1] in ex:        
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    ex[a[exist[-1]-1]] = 0
    exist.append(a[exist[-1]-1])
    
    cnt += 1
    if cnt >= k:
        print(exist[-1])
        exit()


#k = k-idx
d = (k-idx) % (cnt-idx)

print(exist[d+idx])

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
city=list(map(int,input().split()))
ne=[0]*(a*2)
ne[0]=1
for i in range(a*2-1):
    ne[i+1]=city[ne[i]-1]
rpp=[]
nrp=[]
for i in ne[1:a]:
    if i in ne[a+1:a*2]:
        rpp.append(i)
for i in ne[0:a]:
    if i not in ne[a+1:a*2]:
        nrp.append(i)
if len(nrp)>=b-1:
    print(ne[b])
else:
    print(rpp[((b-len(nrp)-1)%len(rpp))])

Submission status: Wrong Answer

Solution code:
def main():
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  b=1
  seen=[-1]*n
  c=0
  while seen[b-1]==-1:
    seen[b-1]=c
    b=a[b-1]
    c+=1
  start=seen[b-1]
  end=c
  k=start+(k-start)%(end-start)
  print([i+1 for i,s in enumerate(seen) if s==k][0])
if __name__=='__main__':
  main()

Submission status: Wrong Answer

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

s,r1,r2=0,0,0
p1,p2=[str(a[s])],[]
for i in range(k+1):
    if p1.count(str(a[s-1]))==0:
        p1.append(str(a[s-1]))
        r1+=1
    elif p2.count(str(a[s-1]))==1:
        break
    elif p1.count(str(a[s-1]))==1:
        p2.append(str(a[s-1]))
        r2+=1
    s=a[s-1]
if k<len(p1):
    print(p1[k-1])
else:
    k=(k-len(p1))%len(p2)
    print(p2[k-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
li=list(map(int,input().split()))
H=[0]*(n)
walk=[]
x=1
l=0
e=k
while e>=0:
    walk.append(x)
    if H[x-1]==0:
        H[x-1]=1
    else:
        dd=[i for i, j in enumerate(walk) if j == x ]
        l=1
    x=li[x-1]
    e-=1
if l==1:
    ran=dd[1]-dd[0]
    k-=dd[0]
    kk=k%ran
    print(walk[dd[0]+kk])
else:
    print(x)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
loc = 0
lst = [False]*n
lst[0] = True
while True:
    if lst[a[loc]]:
        x = a[loc]
        break
    loc = a[loc]
    lst[loc] = True
cnt = 0
loc = 0
if x != 0:
    while True:
        loc = a[loc]
        cnt += 1
        if loc == x: break
cnt2 = 0
while True:
    loc = a[loc]
    cnt2 += 1
    if loc == x: break
if k >= cnt:
    k -= cnt
    k %= cnt2
    loc = x
    while k > 0:
        loc = a[loc]
        k -= 1
else:
    loc = 0
    while cnt > 0:
        loc = a[loc]
        cnt -= 1
print(loc+1)

Submission status: Wrong Answer

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

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

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 1
x = 0
ss = set([1])
#ss = set([now-1])
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+1:
        x = 1
        break
if x == 1:
    aa = k%(len(ss)+1)
    for i in range(aa-1):
        now = a[now-1]
print(now)

Submission status: Wrong Answer

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

A = list(map(int, input().split()))
vis=[]
now=1
while True:
    if now in vis:
        ropmade = vis.index(now)
        rop = len(vis) - ropmade
        break

    vis.append(now)
    now = A[now-1]


print(vis[(K-ropmade)%rop+ropmade])

Submission status: Wrong Answer

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

if k > n:
    idx = (k%n)-1
else:
    idx = k
print(A[A[idx] - 1])

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    As = list(map(int, input().split()))
    i = 1
    graph = []
    for _ in range(n):
        i = As[i-1]
        if i in graph:
            k = k - graph.index(i)
            start = graph.index(i)
            ans = graph[start:][k % (len(graph[start:])) - 1]
        graph.append(i)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

    town = defaultdict(int)
    cn = defaultdict(int)
    

    visited = set()
    visited.add(0)
    cnt = 0
    tmp = A[0]-1
    town[cnt] = 0

    while True:
        if(tmp not in visited):
            visited.add(tmp)
            cnt += 1
            cn[tmp] = cnt
            town[cnt] = tmp
            tmp = A[tmp]-1
        else:
            break
      
    ii = cn[tmp]

    if(ii != 0):
        K -= ii
        ans = town[(K%(len(town)-ii))+ii]+1
    else:    
        ans = town[K%(len(town))]+1
    print(ans)
main()

Submission status: Wrong Answer

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

citys = [False] * n
now = 0
citys[0] = True
costt = 0
while True:
    now = a[now] - 1
    if citys[now]:
        costt += 1
        break
    else:
        citys[now] = True
        costt += 1

if costt > k:
    i = 0
    while i != k:
        now = a[now] - 1
        i += 1
    print(now+1)
    quit()

citys = [False] * n
citys[now] = True
check = [0] * n
check[now] = 1
cost = 0
while True:
    now = a[now] - 1
    if citys[now]:
        cost += 1
        break
    else:
        citys[now] = True
        cost += 1
        check[now] = cost + 1

costt -= cost
d = (k - costt) % cost + 1
ans = check.index(d)
print(ans+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
List = list(map(int,input().split()))
Newlist = [1]
a=1
for i in range(K):
  a=List[a-1]
  if a==List[a-1]:
    print(List[a-1])
    break
  if a in Newlist:
    b=Newlist.index(a)
    c=len(Newlist)-b
    NEW=Newlist[b:len(Newlist)]
    print(NEW[(K-i-1)%len(NEW)])
    break 
  Newlist.append(a)

Submission status: Wrong Answer

Solution code:
n,k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a = [0]+a
l = [0]*(n+1)
place = 1
loop = False

for i in range(1,k+1):
    if i == k-1:
        print(place)
        exit()
    if l[place] != 0:
        loop = i
        break
    else:
        l[place] = a[i]
        place = a[i]

if loop:
    k = k % loop
    print(l[l[k+1]])

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import Counter
import itertools
import fractions
#import numpy as np
#from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

# ###########################################

N,K=MI()
A=LI()

loopStartPoint=0
current=A[0]
keiyu=[]
keiyu.append(current)
for i in range(N):
    current=A[current-1]
    if current in keiyu:
        loopStartPoint=current
        break
    keiyu.append(current)

loopCount=len(keiyu)-keiyu.index(loopStartPoint)

if K<=keiyu.index(loopStartPoint):
    print(keiyu[K])
else:
    iti = K%loopCount
    print(keiyu[iti-1])

Submission status: Wrong Answer

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

count= 0
now=1
flag = 0
if(K == 0):
  print(1)
else:
  while(True):
    now = A[now-1]
    for i in range(len(B)):
      if(now == B[i]):
        flag = 1
        count = i
        break
    if(flag == 1):
      break
    B.append(now)
  print(B[(K-count)%(len(B)-count)+count-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
total=0
b[0]+=1
next=a[0]
if a[0]==1:
  print(1)
else:
  flag=0
  while True:
    total+=1
    if a[next-1]==next:
      flag=1
      break
    if b[next-1]==0:
      b[next-1]+=1
      c[next-1]+=total
      next=a[next-1]
    else:
      k-=(c[next-1])
      total-=c[next-1]
      start=next-1
      break
  if flag==1:
    print(next)
  else:    
    k%=total
    for i in range(k):
      start=a[start]-1
    print(start+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]*n
for i in range(n):
    b=[0,0]
    b[0]+=i+1
    b[1]+=a[i]
    l.append(b)
c=0
ne=0
l1=[0]*n
roop_act=0
while True:
    ne+=-ne+l[ne][1]-1
    c+=1
    if l1[ne]==0:l1[ne]+=1
    else:
        roop_act+=ne
        break

roop_c=0
while True:
    if l1[ne]==1:l1[ne]+=1
    else:break
    ne+=-ne+l[ne][1]-1
    roop_c+=1

re=(k-c)%roop_c
if re==0:print(l[ne][0])
else:
    for i in range(re):
        if i==re-1:print(l[ne][1])
        ne+=-ne+l[ne][1]-1

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
cnt = 1
B = [0]*n
B[0] = cnt
i = A[0]-1
while True:
  if B[i] >= 1:
    break
  else:
    #print(i)
    cnt += 1
    B[i] = cnt    
    i = A[i]-1
    
loop = cnt - B[i] + 1

j = A[i]
for i in range((k-cnt)%loop):
  j = A[j] - 1   
  
print(A[j])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
T={}
T[1]=1
t=1
for i in range(2,N+1):
    a=A[t]
    t=a
    K-=1
    if a in T:
        l=list(T.keys())[T[a]-1:]
        print(l[K%len(l)])
        break
    else:
        T[a]=i   
else:
    print(a)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))

x = A[0]
visited = [0] * N
visited[0] = 1
visited_num = [1]
for i in range(N):
    if visited[x - 1] == 1:
        b = i + 1
        break
    visited[x - 1] = 1
    visited_num.append(x)
    x = A[x - 1]
    
for j, p in enumerate(visited_num):
    if p == x:
        a = j
    
n = (K - a) % (b - a)
ans = visited_num[a + n]
print(ans)

Submission status: Wrong Answer

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

# print(a_list)

next = 1
for _ in range(N + K % N):
    next = a_list[next-1]

print(next)