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
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
"""
閉路をmodを取って出力
閉路になっていない(回帰するなら)ならそこを出力
"""
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))


visited = [-1]*n #modを取った時にそこにたどり着く
visited[0] = 0
now = 0
count = 1
mod = -1
lim = -1
while True:
    now = lst1[now] -1
    if visited[now] != -1: #閉路
        mod = count - visited[now]
        lim = visited[now]
        break
    if now == lst1[now]-1: #回帰
        lim = now
        visited[now] = -51
        mod = 0
        break
    visited[now] = count

    count += 1
    
if k <= lim:
    for i in range(n):
        if lst1[i] == k:
            print(i+1)
            exit()
else:
    if mod == 0:
        for i in range(n):
            if visited[i] == -51:
                print(i+1)
                exit() 
    else:
        res = (k-lim)%mod
        for i in range(n):
            if visited[i]-lim == res:
                print(i+1)
                exit()

Submission status: Wrong Answer

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

path = [0]*N
a = 0

for j in range(N+1):
    count = j
    a = A[a]-1
    if path[a]!= 0:
        c = j+1-path[a]
        break
    else:
        path[a] = j+1

mod = K%c

if count > K:
    print(path.index(K)+1)
else:
    print(path.index(mod)+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
kosuu = 1
tadoru = [1]
pin = 1
tmp = 1
while pin == 1:
    tmp = A[tmp-1]
    if tmp == 1:
        break
    else:
        tadoru.append(tmp)
        kosuu += 1
        if kosuu == N:
            break
mod = K % kosuu
print(tadoru[mod])

Submission status: Wrong Answer

Solution code:
import sys

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

visited = [False] * n
visited[0] = True
l = [1]
while True:
    x = A[l[-1]-1]
    if visited[x-1]:
        break
    l.append(x)
    visited[x-1] = True
# print(x)
# print(l)
# print(l[l.index(x):])
print(l[l.index(x):][(k-l.index(x)) % len(l[l.index(x):])])

Submission status: Wrong Answer

Solution code:
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

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

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

A = [A[i]-1 for i in range(len(A))]

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2

if tmp >= K:
    for _ in range(K):
        idx = A[idx]
else:
    e = (K - tmp) % count2

    for _ in range(e):
        idx = A[idx]

print(idx+1)

# idx is start

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(s):
    q = deque()
    q.append(s)
    dist[s] = 0
    while q:
        p = q.popleft()
        if dist[a[p]] == -1:
            dist[a[p]] = dist[p] + 1
            q.append(a[p])
        else:
            return [dist[a[p]], dist[p] + 1]


n, k = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)
dist = [-1] * (n + 1)
x = bfs(1)
y = [0, x[1] - x[0], k - x[0]]
l = y[2] % y[1]
ans = l + x[0]
for i in range(1, n + 1):
    if dist[i] == ans:
        print(i)
        exit()

Submission status: Wrong Answer

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

visited = list()
i = 0
cnt = 0
while True:
    if i in visited:
        break
    visited.append(i)
    i = a[i]
    cnt += 1

si = visited.index(i)
loop = visited[si:]
n_loop = len(loop)
left_k = k - cnt
print(loop[left_k % n_loop] + 1)

Submission status: Wrong Answer

Solution code:
import sys
import math
from  collections import defaultdict

# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

def solve(test):
	n, k = map(int, input().split())
	a = list(map(int, input().split()))
	fast = 1
	slow = 1
	fast = a[a[fast - 1] - 1]
	slow = a[slow - 1]
	while fast != slow:
		fast = a[a[fast - 1] - 1]
		slow = a[slow - 1]

	fast = 1
	count = 0
	while fast != slow:
		fast = a[fast - 1]
		slow = a[slow - 1]
		count += 1

	k -= count

	cycle = 1
	fast = a[fast - 1]
	while fast != slow:
		fast = a[fast - 1]
		cycle += 1

	k %= cycle

	while k:
		fast = a[fast - 1]
		k -= 1

	print(fast)







if __name__ == "__main__":
	test_cases = 1
	for t in range(1, test_cases + 1):
		solve(t)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
state=[1]
for _ in range(n+1):
    state.append(A[state[-1]-1])
count={}
for i in range(n+1):
    if state[i] in count:
        if i<n and state[i+1]in count:
            start=count[state[i+1]]
            end=i+1
        else:
            start=count[state[i]]
            end=i
    else:
        count[state[i]]=i
k -= start
k%=(end-start)
print(state[k+start])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

N, K = read_ints()

A = read_ints()
B = []
for a in A:
    B.append(a - 1)

# ループするまで回してみる?
# 閉路が知りたい
# 閉路の起点をメモ
# K←（K-起点までの回数）
# K%閉路の長さ
# のこりは愚直に起点から再スタート

nums_to_arrive = [-1] * N
nums_to_arrive[0] = 0
now = 0
while True:
    to = B[now]
    if nums_to_arrive[to] == -1:
        nums_to_arrive[to] = nums_to_arrive[now] + 1
        if nums_to_arrive[to] == K:
            print(to)
            exit()
    else:
        break
    now = to
n1 = nums_to_arrive[to]
n2 = nums_to_arrive[now] + 1 - n1
base = to  # 起点
# print(base)


K = (K - n1) % n2  # 残り移動回数
# print(K)
now = base  # 起点をもとに開始する
while K:
    to = B[now]
    K -= 1
    now = to
print(now + 1)
# print(to + 1)

Submission status: Wrong Answer

Solution code:
import bisect
import collections
import heapq
import math
import operator as op
from collections import Counter
from functools import reduce

import numpy as np


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def main():

    N, K = get_inputs(int)
    As = get_inputs(int)
    As = [None, ] + As

    prev = 1
    n_teles = 0
    history = {}
    history[1] = 0
    for n_teles in range(1, K+1):
        cur = As[prev]
        if cur in history:
            # Returned to start
            len_cyc = n_teles - history[cur]
            for _ in range((K - n_teles) % len_cyc):
                cur = As[cur]
            print(cur)
            return
        # elif cur == prev:
        #     # Induction
        #     print(cur)
        #     return

        else:
            prev = cur
            history[cur] = n_teles

# def main():
#     N, M, X = get_inputs(int)
#     Cs = np.empty(N, dtype=np.int)
#     A_mat = np.empty((N, M), dtype=np.int)
#     for i in range(N):
#         CAs = get_inputs(int)
#         Cs[i] = CAs[0]
#         for j in range(M):
#             A_mat[i, j] = CAs[j+1]

#     if (np.sum(A_mat, axis=0) >= X).all():
#         ans = float("inf")


#     else:
#         print(-1)


#     # N, M, X = get_inputs()
#     # CAs_arr = []
#     # for _ in range(N):
#     #     CAs = get_inputs(int)
#     #     CAs_arr.append(CAs)

#     # Sums = [0 for _ in range(M)]
#     # for i in range(M):
#     #     for j in range(N):
#     #         Sums[i] += CAs_arr[j][i+1]

#     # if all([Sum >= X for Sum in Sums]):
#     #     pass
#     # else:
#     #     print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

p_list = [1]
ne_p = a[0]
for _ in range(n):
    p_list.append(ne_p)
    ne_p = a[ne_p-1]
    if ne_p in p_list:
        ind = p_list.index(ne_p)
        break
k = k - ind
#print(ind)
#print(k)
#print(p_list)
#print(k%(len(p_list)-ind))
print(p_list[ind + k%(len(p_list)-ind)])

Submission status: Wrong Answer

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

a.insert(0, 0)
s=1
for i in range(k):
    s=a[s]
    if(s not in roop):
        roop.append(s)
    else:
        s_first=roop.index(s)
        roop=roop[s_first:]
        k-=(s_first+1)
#        print(k,roop,s_first)
#        print(roop[k%len(roop)])
        break

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

fidx = root.index(root[-1])
lidx = len(root)-1
lp   = lidx-fidx

cnt = (k-fidx)%lp

if cnt==0:
  print(root[-1]+1)
else:
  que.append(root[-1])
  for i in range(cnt):
    v = que.pop()
    que.append(li[v])
  print(que.pop()+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
arr = list(map(int, input().split()))
check = [0]*N
index = 0
cnt = 0
while True:
    if cnt == K:
        print(index+1)
        exit()
    if check[index] == 1:
        break
    check[index] = 1
    index = arr[index]-1
    cnt += 1

index2 = 0
first_cnt = 0
while True:
    if index2 == index:
        break
    index2 = arr[index2]-1
    first_cnt += 1

if first_cnt == 0:
    ans_cnt = K % cnt
    index = 0
    cnt = 0
    while True:
        if cnt == ans_cnt:
            break
        index = arr[index]-1
        cnt += 1
    print(index+1)
else:
    ans_cnt = (K-cnt) % first_cnt
    index = 0
    cnt = 0
    while True:
        if cnt == first_cnt:
            break
        index = arr[index]-1
        cnt += 1
    print(index+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = [0]+list(map(int, input().split()))
num = [0] * (n+1)
num[1] = 1
now = 1
for i in range(2,300000):
  if num[a[now]]:
    start = num[a[now]]
    cycle = i - num[a[now]]
    v = (k-start+1) % cycle + start
    ans = num.index(v)
    break
  else:
    num[a[now]] = i
    now = a[now]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

sys.setrecursionlimit(10 ** 7)

n, k = na()
a = na()

proc = [0] * n
def rec(i, cnt):
    if proc[i]:
        return (cnt, i)
    proc[i] = 1
    return rec(a[i] - 1, cnt + 1)

def rec2(i, arr):
    if proc[i] >= 2:
        return arr
    proc[i] += 1
    arr.append(i)
    return rec2(a[i] - 1, arr)

cnt, i = rec(0, 0)
arr = rec2(i, [])

k -= cnt - len(arr)
print(arr[k % len(arr)] + 1)

Submission status: Wrong Answer

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

visited = [-1] * n
cnt = 0
n = 0
while True:
    visited[n] = cnt
    nx = As[n] - 1
    cnt += 1
    # ループ入った場合
    if visited[nx] != -1:
        first = visited[nx]
        a = (k-first)%(cnt-first)
        loop = [v-first for v in visited]
        for i,x in enumerate(loop): 
            if x == a:
                print(i+1)
                exit()
        break
    n = nx

Submission status: Wrong Answer

Solution code:
import sys
from _collections import deque
N,K = map(int,input().split())
A = list(map(int,input().split()))
for i in range(len(A)):
    A[i] = A[i]-1
used = [0]*N
m = 0
c = 0
used[m] = 1

while(True):
    m = A[m]
    c += 1
    if c == K:
        print(m)
        sys.exit()
    if used[m] == 1:
        break
    else:
        used[m] = 1

loop = m
h = K-c
ans = deque([loop])
c = 0
p = loop
while(True):
    p = A[p]
    c += 1
    if p == loop:
        break
    ans.append(p)

print(ans[h%c]+1)

Submission status: Wrong Answer

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

def input_one_number():
    return int(input())

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

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


N,K = input_multiple_number()
A = input_multiple_number_as_list()

isVisit = [-1]*N

now = 1
cnt = 0
looper = 0
while True:
	if isVisit[now-1] >= 0:
		looper = cnt - isVisit[now-1]
		break

	isVisit[now-1] = cnt
	now = A[now-1]
	cnt += 1

K -= cnt

K %= looper

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

print(now)

Submission status: Wrong Answer

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

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	n, k = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	here = 1
	patarrn = []
	dist = [0]*n
	while 1:
		here = a[here-1]
		if dist[here-1]:
			break
		dist[here-1] = 1
		patarrn.append(here-1)
	log.print(here, patarrn)
	b = patarrn.index(here-1)
	patarrn = patarrn[b:]
	log.print(patarrn, b, patarrn[(k-b) % len(patarrn) - 1])
	print(patarrn[(k-b) % len(patarrn) - 1]+1)


main()

Submission status: Wrong Answer

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

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

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

    his = []
    s = 1
    cnt = 1
    while not(s in his):
        s = A[s]
        his.append(s)
        cnt += 1

    cnt -= A.index(A[s])

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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def binarySearch (arr, l, r, x): 
  
    # Check base case 
    if r >= l: 
  
        mid = l + (r - l) // 2
  
        # If element is present at the middle itself 
        if arr[mid] == x: 
            return mid 
          
        # If element is smaller than mid, then it  
        # can only be present in left subarray 
        elif arr[mid] > x: 
            return binarySearch(arr, l, mid-1, x) 
  
        # Else the element can only be present  
        # in right subarray 
        else: 
            return binarySearch(arr, mid + 1, r, x) 
  
    else: 
        # Element is not present in the array 
        return -1
n,k=map(int,input().split( ))
s=input().split( )
p=[]
g=0
while binarySearch(p,0,len(p)-1,int(g))==-1:
    p.append(int(g))
    g=int(s[int(g)])-1
k+=1
x=p.index(g)
y=len(p)
if k>=y:
    u=p[x:y]
    k-=x
    v=(k)%(y-x)
    print(u[v-1]+1)
else:
    print(p[k])

Submission status: Wrong Answer

Solution code:
#2020-05-10
n, k = map(int,input().split())
lst = list(map(int,input().split()))
ans = 1
a = k % n
for i in range(a):
    ans = lst[ans - 1]
print(ans)

Submission status: Wrong Answer

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

F=[-1]*N
l=0
prev=0
while True:
  if F[prev]!=-1:
    break
  
  
  F[prev]=l
  l+=1
  prev=A[prev]-1
  #print(l,prev)
l = (K-F[prev])%(l-F[prev])
#print(l)
for i in range(l):
  prev=A[prev]-1
print(prev+1)

Submission status: Wrong Answer

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

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

    data = []
    visited = set()
    data.append(1)
    visited.add(1)
    ai = 1
    while True:
        ai = A[ai - 1]
        if ai in visited:
            break
        data.append(ai)
        visited.add(ai)
    d_index = data.index(ai)
    rep_data = data[d_index:]
    rep_len = len(rep_data)
    K -= d_index
    print(rep_data[K - K // rep_len * rep_len])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

vst = [False for i in range(N)]
plc = [0]

for i in range(N):
    mm = plc[-1]
    mm2 = A[mm]-1
    if vst[mm2]:
        break
    vst[mm] = True
    plc.append(A[mm]-1)

li = []
flg = False
for i in range(len(plc)):
    if plc[i]==mm2:
        flg = True

    if not(flg):
        continue

    li.append(plc[i])

# for i in A:
#     print(i-1," ",end="")


ans = (K-len(plc))%len(li)
print(li[ans]+1)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
d = {}
for i in range(N):
    d[i+1] = A[i]
    
al = [1]
for i in range(N):
    ne = d[al[-1]]
    if ne in al:
        tar = al.index(ne)+1
        break
    al.append(ne)
    
ne = i+2
if K<=len(al):
    print(al[K-1])
else:
    K-=(tar-1)
    K%=(ne-tar)
    print(al[tar-1+K])

Submission status: Wrong Answer

Solution code:
import math,sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))

def case():
    n,k=li()
    a=li()
    d=defaultdict(int)
    i=0
    d[1]=1
    c=0
    ans=[1]
    while(a[i] not in d):
        d[a[i]]=1
        c+=1 
        ans.append(a[i])
        i=a[i]-1
    #print(ans)
   # print(i)
    if(k<=len(a)):
        print(ans[k%len(ans)])
        return
    for j in range(len(ans)):
        if(ans[j]==a[i]):
            ans=ans[j:]
            k-=j
            break 

    #print(ans,k)
    print(ans[(k)%len(ans)])
    




for _ in range(1):
    case()

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(2*n):
  if a[m] in d:
    ans+=1
    cnt.append(a[m])
    if ans==k:
      print(a[m])
    break
  else:
    d[a[m]]=0
    cnt.append(a[m])
    ans+=1
    if ans==k:
      print(a[m])
      break
    m=a[m]-1
if ans!=k:
  print(cnt[(k-ans)%len(cnt)])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as d 

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

s = '1'
i = 1
b = 0
while True:
	if b == 2 * 10**5:
		break
	s += str(arr[i - 1])
	i = arr[i - 1]
	b += 1
y = (k) % len(s)
print(s[y])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
s = 1
cnt = 0
b = []
while True:
    if s in b:
        rep = b.index(s)
        t = cnt-rep
        num = (k-rep)%t
        print(b[rep+num])
        break
    else:
        b.append(s)
        s = a[s-1] 
        cnt +=1

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    if A[0]==1:
        print(1)
        sys.exit()
    cur_a=1
    cunt=0
    visited=[False]*n
    visited[0]=True
    route=[1]
    while True:
        next_a=A[cur_a-1]
        if visited[next_a-1]:
            loop=len(route)-route.index(next_a)
            cunt-=loop-1
            break
        cur_a=next_a
        cunt+=1
        route.append(cur_a)
        visited[cur_a-1]=True
    k-=cunt
    k=k%loop
    print(route[cunt+k])
    

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

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

p = 1
count = 1
while num[p] == 0:
    num[p] = count
    inv.append(p)
    p = A[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:
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
cnt = 0
cur = 1
memo = []
kumi = set([])
for i in range(2 * (10 ** 5) + 5):
    cur = A[cur]
    cnt += 1
    memo.append(cur)
    if cur in kumi:
        break
    kumi.add(cur)
k -= len(memo)
for j in range(cnt):
    if memo[j] == cur:
        start = j
        break
loop = memo[start:]

loop_num = i - start
print(loop[k % loop_num])

Submission status: Wrong Answer

Solution code:
# Contest No.: ABC167
# Problem No.: D
# Solver:      JEMINI
# Date:        20200510

import sys

def main():
    n, k = map(int, sys.stdin.readline().split())
    nums = [None]
    nums += list(map(int, sys.stdin.readline().split()))
    visited = [1]
    visitedSet = set([1])
    temp = nums[1]
    while temp not in visitedSet:
        visited.append(temp)
        visitedSet.add(temp)
        temp = nums[temp]
    frontCnt = visited.index(temp)
    k -= frontCnt
    cnt = k % (len(visited) - frontCnt)
    print(visited[frontCnt + cnt])
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    cnt = 0
    used = [-1] * N
    used[0] = 0
    cur = 0
    loop_cnt = 0
    start = 0
    for i in range(1, N+1):
        cur = A[cur] - 1
        if used[cur] == -1:
            used[cur] = i
            continue
        else:
            loop_cnt = i
            start = cur
            break
    K -= loop_cnt
    loop_cnt -= used[start]
    x = K % loop_cnt
    for i in range(N):
        if used[i]-used[start] == x:
            print(i+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

while True:
    
    go = a[res[-1]]
    if i > n+1 or go in res:break
    res.append(go)
    i += 1

for i in range(len(res)):
    if res[i] == go:
        ind = i
        break
v = res[ind:]

m = len(v)

k -= ind
ans = v[k%m]
print(ans)

Submission status: Wrong Answer

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

go = dict()
res = []

go[0] = True
res = [0]

now = a[0]
while True:
    if now in go:
        break
    res.append(now)
    go[now] = True
    now = a[now]

# 1
# [0, 1, 2, 3]
# (5 - 1) % (xunhuan)
amari = res.index(now)
xunhuan = res[amari:]
#print(res, xunhuan, amari)
print(xunhuan[ (k - amari) % len(xunhuan)] + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(i)-1 for i in input().split()]
l = [0]
d = {0: 0}
p = A[0]
while p not in d:
    d[p] = len(l)
    l.append(p)
    p = A[p]
K -= d[p]
K %= len(l) - d[p]
print(l[K + d[p]] + 1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

def main():
  N, K = map(int, input().split())
  towns = tuple(map(int, input().split()))

  j = 0
  rem = K%N
  for i in range(N+rem):
    j = towns[j]-1
  print(j+1)

if __name__ == '__main__':
  main()

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]
l = []
used = [0 for _ in range(n)]
used[0] = 0

c = 0
while 1:
    l.append(c)
    if used[c] == 1 and l:
        break
    used[c] = 1
    c = a[c]


end = l[-1]
p = 0
for i in range(len(l)):
    if l[i] == end:
        l = l[i:]
        p = i
        break

l=l[:-1]
b = (k-p) % len(l)
print(l[b]+1)

Submission status: Wrong Answer

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

kaisuu = k % n

ans = a[kaisuu]
print(a[ans])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
t=0
j=0
c=[]
b={}
for i in range(n):
    j=a[j]-1


    if j+1 in b:

        break
    c.append(j + 1)
    b[j + 1] = 1
m=c.index(a[c[-1]-1])

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

Submission status: Wrong Answer

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

city = [-1 for i in range(0,N,1)]#0~N-1
count = 0
nex_city = 0
city[0]=0
while True:
    city[nex_city] = count
    nex_city = A[nex_city]-1
    count+=1
    if city[nex_city] >= 0:
        break

X = nex_city
Y = count - city[nex_city]

nex_city = 0
for i in range(0,X+(K-X)%Y,1):
    nex_city = A[nex_city]-1

print(nex_city+1)

Submission status: Wrong Answer

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

roop = [0] 
flag = 1
i = 1
for i in range(len(A)):
    if A[roop[i]] - 1 == 0:
        break
    else:
        roop.append(A[roop[i]] - 1)

s = len(roop)
t = K % s
print(roop[t] + 1)

Submission status: Wrong Answer

Solution code:
from sys import stdin

INF = int(1e10)
MOD = int(1e9 + 7)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A = [0] + A
    la = []
    now = 1
    visited = [False] * (N + 1)
    last = None
    while True:
        if visited[now]:
            last = now
            break
        la.append(now)
        visited[now] = True
        now = A[now]
    period = len(la) - la.index(last)
    

    lai = la.index(last)
    if lai > 0:
        K -= lai
        # la = la[lai:]
    rem = K % period
    print(la[lai + rem])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

path = [0]*N
a = 0

for j in range(N+1):
    a = A[a]-1
    if path[a]!= 0:
        c = j+1-path[a]
        break
    else:
        path[a] = j+1

mod = K%c

print(path.index(mod)+1)

Submission status: Wrong Answer

Solution code:
import math

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

routetemp=[]
routetemp.append(0)
while True:
    if a[routetemp[-1]] in routetemp:
        roop=len(routetemp)-routetemp.index(a[routetemp[-1]])
        pre=routetemp.index(a[routetemp[-1]])
        break
    routetemp.append(a[routetemp[-1]])
#print(routetemp)
#print(a)
#print(roop)
#print(pre)
num=(k-pre)%roop
#print(num)
print(routetemp[pre+num]+1)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [False for _ in range(N)]
state = 0
heiro = [0]
visited[0] = True
while True:
    state = A[state]-1
    heiro.append(state)
    # print(state)
    if visited[state]:
        break
    visited[state] = True
cnter = dict(Counter(heiro))
for k in cnter.keys():
    if cnter[k] == 2:
        v = k
        break
idx = heiro.index(v)
K -= idx
ans = heiro[idx:][K % (len(heiro) - idx - 1)] + 1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  b=a[0]
  if b==1:
    print(b)
    exit()
  root=[1]
  c=1
  while b not in root and c<n+1:
    root.append(b)
    b=a[b-1]
    c+=1
  start=root.index(b)
  end=len(root)
  #print(start,end)
  k=start+(k-start)%(end-start)
  b=a[0]
  for _ in range(1,k):
    b=a[b-1]
  print(b)

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
route = [1]
dest = A[0]
while dest not in route:
    route.append(dest)
    dest = A[dest - 1]
ind = route.index(dest)
cycle = route[ind:]
print(ind, cycle)
if k >= ind:
    ans = (k - ind) % len(cycle)
    print(cycle[ans])
else:
    print(route[k])

Submission status: Wrong Answer

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

while True:
    
    go = a[res[-1]]
    if i > n+1 or go in res:break
    res.append(go)
    i += 1

for i in range(len(res)):
    if res[i] == go:
        ind = i
v = res[ind:]

m = len(v)

k -= ind
ans = v[k%m]
print(ans)