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

vislist=[1]
visset={1}
loopflag=True
endflag=False
donek=0
ima=1

while loopflag:
    ima=alist[ima-1]
    if ima in visset:
        loopflag=False
        if ima == alist[ima-1]:
            endflag=True
    else:
        vislist.append(ima)
        visset.add(ima)

        donek+=1

        if donek == k:
            loopflag=False
            endflag=True

if endflag:
    print(ima)
    sys.exit()

loopstartindex=vislist.index(ima)
looplength=len(visset)-loopstartindex
if (k+1-loopstartindex)%looplength == 0:
    print(vislist[loopstartindex])
else:
    print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A_s = list(map(int, input().split()))
# それぞれの街に訪れた時のカウント
cache = [0]*N
# cacheの逆引き辞書
MAP = {}
# 町カーソル
i=0
# テレポーターのカウント
cnt=1
# 閉路のサイズ
rplng = 0
while(True):
    if(cache[i]!=0):
        rplng = cnt - cache[i]
        break
    MAP[cnt] = i+1
    cache[i] = cnt
    i = A_s[i]-1
    cnt+=1
ans = MAP[(K-cnt+1)%rplng+cache[i]]
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [A[i]-1 for i in range(N)]
k = []
now = 0
while True:
    if now in k:
        p = k.index(now)
        interval = len(k) - p
        t = (K-p)%interval
        ans = k[p+t]+1
        print(ans)
        break
    else:
        k.append(now)
        now = B[now]

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
for i in range(min(k, int(3 * n + 1))):
    now = a[now - 1]
print(now)

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

city_l=[]
city_l.append(1)
city_c=[0]*(N+1)
city_c[1]+=1
n_city=A[0]
flag=True
r_start=-1
r_end=-1
while(flag):
    city_c[n_city]+=1
    city_l.append(n_city)
    if(city_c[n_city]>1):
        r_start=city_l.index(n_city)+1
        r_end=len(city_l)-1
        flag=False
    n_city=A[n_city-1]

if(K<r_end):
    print(A[city_l[K-1]])

else:
    K2=K-r_end
    K2=K2%(r_end-r_start+1)
    print(A[city_l[r_start+K2-2]-1])

Submission status: Wrong Answer

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

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

loc = 1
rec[loc] = 1

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

    if rec[next_loc] == 1:
        finished = False
        break

    costs[next_loc] = i
    rec[next_loc] = 1
    loc = next_loc

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

    cs = tuple(map(lambda x: x - loop_index_cost, costs))

    final_loc = cs.index((K - loop_index_cost) % loop_cost)
    print(final_loc)

Submission status: Wrong Answer

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

now=1
root=[1]
for i in range(N):
    now=A[now-1]
    if now in root:
        index=root.index(now)
        num=len(root[index:])
        root=root[index:]
        break
    else:
        root.append(now)

an = (K-i) - (K-i)//num * num
print(root[an-1])

Submission status: Wrong Answer

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()

nokori=(K-startat) % loopcount

print(startat)
print(nokori)
print(loopcount)

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=1
l=list()
for i in range(n):
    l.append(s)
    s=a[s-1]
    if len(set(l))!=len(l):
        for i in range(len(l)):
            m=l.index(l[-1])
            M=len(l)-1
            break
k=(k-m)%(M-m)
print(l[k+m])

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= n + 1):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count <= n + 1:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    k = k % n
    print(ans_list[k + 1] + 1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
arr=list(map(int,input().split()))
p=arr[0]
l=len(arr)-1
count=0
while l>0:
    p=arr[p-1]
    l-=1
    count+=1
turn=k%count
p=arr[0]
turn-=1
if turn ==0:
    print(arr[p-1])
else:
    while turn>0:
        p=arr[p-1]
        turn -=1
    print(p)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
already_went = []
until_loop = 0
loop = 0
currentPlace = 1
for _ in range(1, n):
    until_loop += 1

    # まず一回きたか検査
    if currentPlace in already_went:
        firstIndex = already_went.index(currentPlace)
        already_went.append(currentPlace)
        secondIndex = already_went.index(already_went[-1])
        loop = secondIndex - firstIndex + 1
        break
    else:
        # 記録して
        already_went.append(currentPlace)
        # 更新
        currentPlace = a[currentPlace]

# loops left after going back to first position

if loop != 0:
    loopsLeft = (k - until_loop) % loop
else:
    loopsLeft = k - until_loop
for _ in range(loopsLeft):
    currentPlace = a[currentPlace]
print(currentPlace)

Submission status: Wrong Answer

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

kouho = [1]
ikisaki = listA[0]
while ikisaki not in kouho:
    kouho.append(ikisaki)
    ikisaki = listA[ikisaki-1]

index = kouho.index(ikisaki)
loop = len(kouho) - index
amari = (k-index)%loop
print(kouho[index + amari])

Submission status: Wrong Answer

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

roopStart = -1
roopCount = 0
firstMove = 0
now = 0
trace = [0] * n

for i in range(2 * n):
    if trace[now] == 1 and roopStart == -1:
        roopStart = now

    if trace[now] == 2:
        break

    if roopStart != -1:
        roopCount += 1
    else:
        firstMove += 1

    trace[now] += 1
    now = a[now] - 1

firstMove -= roopCount

now = 0
for i in range(firstMove):
    now = a[now] - 1

k -= firstMove
k %= roopCount
for i in range(k):
    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()))

isPassed = [False] * N
town = 0
#2度同じところに行きつくまで繰り返す
while True:
    if isPassed[town]==False:
        isPassed[town] = True
        town = A[town]-1
    else:
        loopPoint = town
        break

#ループまでの長さを測る
wayLen = 0
town = 0
while town!=loopPoint:
    town = A[town]-1
    wayLen += 1

#ループ自体の長さを測る
loopLen = 1
town = A[loopPoint] - 1
while town!=loopPoint:
    town = A[town]-1
    loopLen += 1

if K <= wayLen:
    town = 0
    for i in range(K):
        town = A[town]-1
    print(town)
else:
    loopMod = (K-wayLen) % loopLen

    for i in range(loopMod):
        town = A[town]-1
    print(town+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
a.insert
count = 0
current = 0
for i in range(k):
    n = a[current]
    current = n
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(points):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
else:
    print(loop[(M-ll)%l]+1)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x) for x in stdin.readline().rstrip().split()]
    
    X=1
    K = K % N
    for i in range(K):
        X = A[X-1]
    
    print(X)
    
main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = []
check = {}
key = -1
p = 0
while True:
    ans.append(p)
    check[p] = 0
    p = a[p]-1
    if p in check:
        key = ans.index(p)
        break
r = ans[key:]
mod = (k-key)%len(r)
print(r[mod])

Submission status: Wrong Answer

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

limit = K
now = 1
log = []
count = 1

while limit > 0 and now not in log:
    if 1 not in log:
        log.append(1)
    #print("通過1",now,limit,log)
    now = A[now - 1]
    log.append(now)
    limit -= 1
    count += 1

if limit > 0:
    #print("通過2",now,limit,log)
    log2 = log[log.index(now):]
    limit = limit % len(log2)

while limit > 0:
    #print("通過3",now)
    now = A[now - 1]
    limit -= 1

now = A[now - 1]

print(now)

Submission status: Wrong Answer

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

currTown = 1
visited = [1]
const = 0
mod = 0

for i in range(N):
    nextTown = A[currTown-1]
    if((nextTown in visited) == False):
        visited.append(nextTown)
        currTown = nextTown
    else:
        const = visited.index(nextTown)
        mod = len(visited) - const
        break

for i in range(const):
    visited.pop(0)

print(visited[(K - const) % mod])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
board = [-1 for _ in range(N)]
now = 1
if K == 1:
    print(A[0])
    exit()
for i in range(1, N+1):
    if board[now-1] == -1:
        board[now-1] = i
        now = A[now-1]
    else:
        i -= 1
        break
loop = i - board[now-1] + 1
init = board[now-1] - 1
K -= init
f = True
if K < 0:
    K += init
else:
    f = False
    K = K % loop
for i in range(N):
    if f:
        if board[i] == K:
            print(i+1)
            break
    else:
        if board[i] - init == K + 1:
            print(i+1)
            break

Submission status: Wrong Answer

Solution code:
# D

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

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i


div = (K - loop_start) // (loop_end - loop_start)
if K - loop_start - div * (loop_end - loop_start) < 0:
    compute(1, K)
else:
    compute(city, K - loop_start - div * (loop_end - loop_start))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
memo = [-1] * N
checked_num = {}

teleporter = A[0]
memo[0] = teleporter
checked_num[memo[0]] = None
repeat_point = -1
flag = False

for i in range(1, N):
    # print(i, checked_num)
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    if memo[i] in checked_num and flag == False:
        repeat_point = i
        flag = True
    checked_num[memo[i]] = None

# print(memo)

start_point = memo.index(memo[repeat_point])
repeat = repeat_point - start_point
# print(memo, start_point, repeat_point)

if K <= N:
    print(memo[K-1])
else:
    mod = K % repeat
    # if mod == 0:
    #     mod = repeat_point
    print(memo[mod-1])

Submission status: Wrong Answer

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

route = [1]
current_town = 1
loop = False
while not loop:
    current_town = A[current_town-1]
    if current_town in route:
        route.append(current_town)
        loop = True
    else:
        route.append(current_town)  
        
start_index = route.index(route[-1])
loop_cnt = len(route) - (start_index+1)
K -= route.index(route[-1])
add_num = K % loop_cnt

print(route[start_index+add_num])

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
 
import numpy as np
 
 
 
Nstock = np.zeros(N)
 
cpos = 1
Nstock[cpos - 1] = 1
stock = [cpos]
loop = []
 
idx = 0
for i in range(K):
    cpos = A[cpos-1]
    if Nstock[cpos - 1]  == 1:
        idx = stock.index(cpos)
        loop = stock[idx:]
        break
    Nstock[cpos - 1] = 1
    stock.append(cpos)
    
if loop != []:
    v = (K - idx) 
    a = v %len(loop)
    print(loop[a])
else:
    print(stock[K-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

li = [0]
li.extend(list(map(int, input().split())))
tuita = [0]*(n+1)
cnt = 0
town = 1

def search(town, times):
  return times + 1
  if times == 0:
    return town
  return search(li[town], times - 1)

while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      print(search(1, k))
    elif k == tuita[town]:
      print(town)
    else:
      print(search(town, (k - tuita[town]) % (cnt - tuita[town])))
    break

Submission status: Wrong Answer

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

town = 1
numberlist = []
count = 0
count2 = 0
for i in range(k):
    town = alist[town-1]
    if town in numberlist:
        break
    else:
        numberlist.append(town)
        count += 1
for i in numberlist:
    if i == town:
        break
    else:
        numberlist.remove(i)
        count2 += 1
k = k - count2
x = k % len(numberlist)
print(numberlist[x-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = list(map(int, input().split()))
a = [0, ] + list(map(int, input().split()))
stack = deque([1, ])
visited = [0] * (n + 1)
hist = []
open = []
loop = []
while stack:
    now = stack.popleft()
    if visited[now] == 1:
        b = hist.index(now)
        open = hist[:b]
        loop = hist[b:]
        break
    next = a[now]
    visited[now] = 1
    hist.append(now)
    stack.append(next)
print(open, loop)
if k < len(open):
    print(open[k])
else:
    print(loop[(k - len(open)) % len(loop)])

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)
  b=a[0]
  for _ in range(k-1):
    b=a[b-1]
  print(b)
  #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 = list(map(int, input().split()))
route = []
roop = []

pos = 1
while k > 0:
    pos = a_list[pos-1]
    if pos in route:
        if len(roop) > 0 and pos == roop[0]:
            k -= k % max(1, (k * (k // len(roop))))

        roop.append(pos)
    else:
        roop = []

    route.append(pos)
    k -= 1

print(pos)

Submission status: Wrong Answer

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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    visited = [0] * (N+10)
    seq = []

    step = 1
    current = 1

    n1 = n2 = 0
    while(step<=len(A)):
        #print(current)
        seq.append(current)
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            break
        else:
            visited[next_pos] = step
        step += 1
        current = next_pos

    #print(n1, n2)
    #print(n1-1, ((K-(n1-1))%n2))

    print(seq[n1-1+((K-(n1-1))%n2)])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N, K = map(int, readline().split())
A = list(map(lambda x: x - 1, map(int, readline().split())))
visited = [False for i in range(N)]

cur = 0
cycle = []
start = 0
while True:
    if visited[cur]:
        start = cur + 1
        break

    visited[cur] = True
    cycle.append(cur + 1)
    cur = A[cur]

print(cycle)
cycle_start_idx = 0
for i in range(len(cycle)):
    if cycle[i] == start:
        cycle_start_idx = i
        break

if K <= cycle_start_idx:
    print(cycle[K])
    exit()


cycle_len = len(cycle) - cycle_start_idx
idx = (K - cycle_start_idx) % cycle_len
print(cycle[idx + cycle_start_idx])

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
for _ in range(N + 100):
    here = A[here]

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

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count <= n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    k = k % n
    print(ans_list[k - 1] + 1)

Submission status: Wrong Answer

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

roopStart = -1
roopCount = 0
firstMove = 0
now = 0
trace = [0] * n

for i in range(2 * n):
    if trace[now] == 1 and roopStart == -1:
        roopStart = now

    if trace[now] == 2:
        break

    if roopStart != -1:
        roopCount += 1
    else:
        firstMove += 1

    trace[now] += 1
    now = a[now] - 1

firstMove -= roopCount

now = 0
for i in range(firstMove):
    now = a[now] - 1

k -= firstMove
k %= roopCount
for i in range(k - 1):
    now = a[now]
print(now + 1)

Submission status: Wrong Answer

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

edge=[0]*n

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

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

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

Submission status: Wrong Answer

Solution code:
n,K=list(map(int,input().split()))
a = list(map(int,input().split()))
b = []
c = [0]*len(a)
I=0
check=0
k=0
if a[k]==1:
    print(1)
else:
    for i in range(n):
        b.append(a[k])
        tes=a[k]
        k=a[k]-1
        if a[k]==1:
            b.append(1)
            check=-1
            break
        else:
            if c[k]==0:
                c[k]=i
            else:

                b.pop(-1)
                I = c[k]
                break
    if check==-1:
        e=K%len(b)
        print(b[e-1])
    else:
        d=len(b)-I
        e=(K-I)%d
        print(b[I+e-1])

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in b:
        b.append(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break

e=b.index(e)

b=b[e:c+1]
f=len(b)
if 1 not in b:
    ans=(e-1+k)%f
    print(b[ans])
else:
    ans=k%f
    print(b[ans])

Submission status: Wrong Answer

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

print(tmp)

Submission status: Wrong Answer

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

N = NK[0]
K = NK[1]
town= 1
list = []
for i in range(N+1):
  town = A[town-1]
  list.append(town)
  if i > 0 and town == list[0]:
    break
list1 = list[:-1]
#print(list1)  
d = K % len(list1)
#print(d)
print(list1[d-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li=[0]+list(map(int,input().split()))
m=1
loop={1}
s="1"
mkr=0
for i in range(1,n+1):
    m=li[m]
    if m in loop:
        loopend=i-1
        break
    if i==k:
        mkr=1
        break
    loop|={m}
    s+=str(m)
if mkr==1:
    print(m)
else:
    loopstart=s.index(str(m))
    ind=(k-loopstart)%(loopend-loopstart+1)
    print(int(s[loopstart+ind]))

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0)
if count <= n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    k = k % n
    print(ans_list[k + 1] + 1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

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

n,k=LI()
a=LI()
b=[1]
done=[False]*(n+1)
done[1]=0 #1ははじめにいた

#シュミレートでもとまる
if k<=n:
    for i in range(1,k+1):#i回目
        nxt=a[b[-1]-1]
        # print(i,nxt) #i回目にnxtに達した
        b.append(nxt)
        print(nxt)
        exit()

#周期を求める
else:
    for i in range(1,k+1):#i回目
        nxt=a[b[-1]-1]
        b.append(nxt)
        if done[nxt]==False:
            done[nxt]=i
        else:
            m=i-done[nxt]
            break
    # print(m,k%m+m+1)
    k=k%m+m
    b=[1]
    for i in range(1,k+1):#i回目
        nxt=a[b[-1]-1]
        b.append(nxt)
    print(nxt)
    exit()

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
lis=""
llis=[[] for i in range(a)]
c=[int(i) for i in input().split()]
j=0
while True:
    i=c[j]
    if len(llis[i-1])!=0:
        ans=lis.index(str(i))
        break
    lis+=str(i)
    llis[i-1].append(1)
    j=i-1

lis=lis[ans:]
print(lis[((b-ans)%len(lis))-1])

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 M[s] in sets:
    #print(order, M[s])
    
    if K < order.index(M[s]):
      print(order[K-1]+1)
    else:
      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(int, input().split()))
memo = [-1] * N
checked_num = {}

teleporter = A[0]
memo[0] = teleporter
checked_num[memo[0]] = None
repeat_point = -1
flag = False

for i in range(1, N):
    # print(i, checked_num)
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    if memo[i] in checked_num and flag == False:
        repeat_point = i
        flag = True
    checked_num[memo[i]] = None

# print(memo)

start_point = memo.index(memo[repeat_point])
repeat = repeat_point - start_point
# print(memo, start_point, repeat_point)

if K <= N:
    print(memo[K-1])
else:
    mod = K % repeat
    if mod == 0:
        mod = repeat_point
    print(memo[mod-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [0]+list(map(int, input().split()))
ans = [1]
while True:
    b = a[ans[-1]]
    if b in ans:
        idx = ans.index(b)
        mod = len(ans)-idx
        break
    ans += [b]
if len(ans)-mod==0:
    print(ans[k%len(ans)])
else:
    print(ans[(k-idx)%mod+idx])

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))
if K == 1:
    print(A[0])
    sys.exit()
now = A[0]
kiseki = [1]
count = 0
while now not in kiseki:
    kiseki.append(now)
    count += 1
    now = A[now - 1]
    if count == K:
        print(now)
        sys.exit()
ind = kiseki.index(now)
roop = len(kiseki) - ind
nokori = (K - ind) % roop
ans = kiseki[ind + nokori]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_l = list(map(int, input().split()))
a_m = dict()
tmp = list()  # startまでの距離を計測用

f = 1

while True:
    if f in a_m:
        # すでにfrom-toが登録済みであればループのstartを登録終了
        start = f
        pre = tmp.index(f)
        break
    tmp.append(f)
    t = a_l[f-1]  # to
    a_m[f] = t  # from-toを登録
    f = t

cycle_len = len(a_m) - pre
k_mod = (k - pre) % cycle_len

result = start

for _ in range(k_mod):
    result = a_m[result]

print(result)

Submission status: Wrong Answer

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

if K >= 2*N:
    point = 0
    L = [0 for _ in range(N)]
    loop = []
    for i in range(N+1):
        L[point] +=1
        T = 1
        if L[point] == 2:
            loop.append(point)
            p = point
            for j in range(N+1):
                point = A[point]
                if point != p:
                    loop.append(point)
                else:
                    T = 0
                    break
        if T == 1:
            point = A[point]
        else:
            break
    c = 0
    p = 0
    for i in range(N):
        if p != loop[0]:
            c+-1
            p = A[p]
        else:
            break
    J = K-c
    n = len(loop)
    k = J%n
    print(loop[(k+1)%n]+1)
else:
    point = 0
    for i in range(K):
        point = A[point]
    print(point+1)

Submission status: Wrong Answer

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

list_order = []
already = [1]
idx = 0
for i in range(1, k + 1):
    next_city = a_list[idx]
    list_order.append(next_city)
    idx = next_city - 1
    if next_city in already:
        break
    already.append(next_city)
    if idx == 0:
        break
print(list_order[(k % i) - 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = input().split()
A.insert(0,0)
position = 1
B = []

for i in range(N):
  if (int(A[position]) in B):
    B.append(position)
    C = B[B.index(int(A[position])):]
    repeat = len(C)
    break
  else:
    B.append(int(position))
    position = int(A[position])

x = (K - len(B)) % repeat

print(C[x])

Submission status: Wrong Answer

Solution code:
def main():
  n, k = map(int, input().split())
  a_list = list(map(int, input().split()))

  count=0
  now_point = 1
  root_list = [a_list[0]]
  while True:
    now_point = a_list[now_point - 1]
    count += 1
    if a_list[now_point - 1] in root_list:
      loop_list = root_list[root_list.index(a_list[now_point - 1]):]
      first_num = len(root_list[:root_list.index(a_list[now_point - 1])])
      loop_num = len(loop_list)
      break
    else:
      root_list.append(a_list[now_point - 1])

  num = (k - first_num) % loop_num
  print(loop_list[num-1])

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    def main():
        k = map(int, input().split())
        a = list(map(int, input().split()))
        ans = []*k
        for i in [0] * (k+1):
            if i <= k:
                ans.append(a[a[i-1]])
            print(ans[-1])

Submission status: Wrong Answer

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

portals = [0] + list(map(int, input().split()))

before = 1

keiro = [1]

for _ in range(N):
    target_index = portals[before]
    keiro.append(target_index)
    before = target_index
    if keiro.count(target_index) != 1:
        break

loop = keiro[keiro.index(keiro[-1]) : -1]
mae = keiro[: keiro.index(keiro[-1])]

amari = (K - len(mae)) % (len(loop))

print(loop[amari])

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
c = 1
s = 0
b = [0]*(n+1)
b[1] = 1
d = [0]*(n+1)
d[1] = 0

for i in range(n*10):
    c = a[c-1]
    b[c] += 1
    if b[c] == 2:
        s = i+1 - d[c]
        f = d[c]
        break
    d[c] = i+1

e = c
S = (k-f)%s
for i in range(S):
    e = a[e-1]
    #print(e)
#print(s)
#print(c)
#print(f)
print(e)

Submission status: Wrong Answer

Solution code:



arr1 = list(map(int,list(input().split())))
arr2 = list(map(int,list(input().split())))
arr2.insert(0,0)

N=arr1[0]
K=arr1[1]

cnt=0

x=1
times=0
for _ in range(N):
	times+=1
	x=arr2[x]
	if(x==1):
		break
#print(times)

last=K%times
if (last>0):
	for _ in range(last):
		x=arr2[x]

print(x)