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:
# 初期入力
from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
connection ={}
for i,v in enumerate(A):
    connection[i+1] =v 

#巡回してるか
passed=[1]
nex =connection[1]
lap =[]
lap_num =0
pass_set =set()
for i in range(1,N+1):
    passed.append(nex)
    if nex not in pass_set:
        pass_set.add(nex)
        nex =connection[nex]
    else:
        #lap =deepcopy(passed)
        break
#巡回している部分の町の数    
indx_s =passed.index(nex) +1
indx_f =len(passed)
lap_num =indx_f -indx_s

mod =(K -indx_s) % lap_num
ans =passed[indx_s +mod]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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


time=1
route=[1]
x=A[0]
Loop=0
x_list=[0]*(N+1)
while x!=1:
    if x_list[x]==0:
        route.append(x)
        x_list[x]=1
        x=A[x-1]
        time+=1
        
    else:
        Loop=time-route.index(x)
        a=route.index(x)
        x=1
        
if Loop==0:
    mod=K%time
    print(route[mod])
    sys.exit()

else:
    mod=(K-a)%Loop
    print(route[mod+a])

Submission status: Wrong Answer

Solution code:

n, k = map(int, input().split(" "))
a = [-1] + list(map(int, input().split(" ")))

i = 1
head = []
used = {}
roop = []

inte=False
index = 0
for _ in range(k):
    index += 1
    i = a[i]
    if i in used:
        head = [ k for k, v in sorted(used.items(), key=lambda x:x[1]) if v < used[i]]
        roop = [ k for k, v in sorted(used.items(), key=lambda x:x[1]) if v >= used[i]]
        inte =True
        break
    else:
        used[i] = index

if inte:
    if len(head) >= k:
        print(head[k-1])
    else:
        x = (k - 1 - len(head)) % len(roop)
        print(roop[x])
else:
    print([ k for k, v in sorted(used.items())][k-1])

Submission status: Wrong Answer

Solution code:
def detectCycle(a):
    start, length = 0, 1
    p1, p2 = a[1], a[a[1]]
    while p1 != p2:
        p1, p2 = a[p1], a[a[p2]]
    
    p2 = 1
    while p1 != p2:
        p1, p2 = a[p1], a[p2]
        start += 1
    
    p2 = a[p1]
    while p1 != p2:
        p2 = a[p2]
        length += 1
    
    return start, length

n, k = (int(x) for x in input().split())
a = [-1] + [int(x) for x in input().split()]
start, length = detectCycle(a)

loc = 1
k = max(0,k-start)
for i in range(start):
    loc = a[loc]

k %= length
for i in range(k):
    loc = a[loc]
print(loc)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

s = 1
w = [1]
y = defaultdict(lambda: 0)
y[1] = 1

flag = 0
zen = 0
for i in range(k):
	print(s,a[s-1])
	if(y[a[s-1]] == 1):
		print("w",w,a[s-1])
		flag = 1
		if(a[s-1] == 1):
			break
		else:
			for j in range(len(w)):
				print("aa",w[j],a[s-1])
				if(w[j] == a[s-1]):
					zen = j
					w = w[j:]
					
					break
		break
	s = a[s-1]
	y[s] = 1
	w.append(s)

print(s,w)

if(flag==0):
	print(s)
else:
	print(w[((k - zen) % (len(w)))])
	print(w,zen,((k - zen) % (len(w))))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
tmp=0
tmplist=[0]
#loopしてない判定
loopinter=n
loopstart=0
for i in range(k):
    tmp=a[tmp]-1
    if tmp not in tmplist:
        tmplist.append(tmp)
    else:
        loopstart = tmplist.index(tmp)
        loopinter = len(tmplist)-tmplist.index(tmp)
        break
for i in range((k-loopstart)%loopinter):
    tmp=a[tmp]-1
print(tmp+1)

Submission status: Wrong Answer

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

l = [int(i) for i in input().split()]
a = [0]
s = 0
while(l[s] - 1 not in a):
	if l[s] == 1:
		flag = True
	s = l[s] - 1
	a.append(s)
	#print(l[s], a)
t = a.index(l[s] - 1)
#print('t', t, 'ls', l[s] - 1, a)
for _ in range(t):
	a.pop(0)
#print((k - t + 2) % (len(a)))
print(l[a[(k - t + 2) % (len(a))]])

Submission status: Wrong Answer

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

    l = [0]
    for i in range(n):
        b = a[l[i]]-1
        l.append(b)
        if l[-1] in l[:-2]:
            break
    
    for i in range(len(l)):
        if l[i] == l[-1]:
            loop = l[i:len(l)+1]
            break

    k -= (len(l)-1) - (len(loop)-1)
    ind = loop[k%len(loop)-1]
    
    return ind+1


print(solve())

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
visit = {0}
tmp = 0
a_list = [0]
ans = True
for i in range(k):
    tmp = a[tmp]
    if tmp in visit:
        left = a_list.index(tmp)
        ans = False
        print(a[left + (k - left) % (i + 1 - left)] + 1)
        break
    else:
        visit.add(tmp)
        a_list.append(tmp)
if ans:
    print(tmp + 1)

Submission status: Wrong Answer

Solution code:

n, k = map(int, input().split(" "))
a = [-1] + list(map(int, input().split(" ")))

i = 1
head = []
used = {}
roop = []

inte=False
index = 0
for _ in range(k):
    index += 1
    i = a[i]
    if i in used:
        head = [ k for k, v in used.items() if v < used[i]]
        roop = [ k for k, v in sorted(used.items(), key=lambda x:x[1]) if v >= used[i]]
        inte =True
        break
    else:
        used[i] = index

if inte:
    if len(head) >= k:
        print(head[k-1])
    else:
        x = (k - 1 - len(head)) % len(roop)
        print(roop[x])
else:
    print([ k for k, v in sorted(used.items())][k-1])

Submission status: Wrong Answer

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

Visit = [False for i in range(n+1)]

loop_st,loop_end,loop_len = 0,0,0
st = 1
releki = []
loop_list = []
for i in range(10000000000000):
    if Visit[st] == True:
        loop_key = st
        loop_st = releki.index(loop_key)
        loop_end = len(releki)-loop_st
        loop_list = releki[loop_st:]
        loop_len = len(loop_list)
        break
    releki.append(st)
    Visit[st] = True
    st = A[st-1]
if k <loop_st:
    print(releki[loop_st])
    exit()
k -= loop_st

print(loop_list[k%loop_len])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
Line = [int(s) for s in input().split()]

X = [-1]*N

# print(X)
ikisaki = Line[0]
X[0] = 0

# print(ikisaki)

for i in range(N**2):
    if X[ikisaki-1] == -1:
        X[ikisaki-1] = i + 1
        ikisaki = Line[ikisaki-1]
        warukazu = i+1 - X[ikisaki-1]
    else:
        warukazu = i+1 - X[ikisaki-1]
        saisyo = X[ikisaki-1]
        break
    # print(X)
# print(saisyo)
# print(warukazu)
Z = K%warukazu
# print(Z)
if saisyo >= K:
    print(X.index(K)+1)
else:
    print(X.index(Z)+1)

Submission status: Wrong Answer

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

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

cnt = len(loop)
if k>=len(know): k -= len(know)
ans = loop[k%cnt]


print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A = list(map(int, input().split()))
where = 1
loop=0
visited = []
for k in range(N):
    visited.append(where)
    where = A[where-1]

    if where in visited:
        end = k+1
        start = visited.index(where)
        loop = end -start
        break
loopbefore = K -end

loopafter = loopbefore%loop

where2 = visited[start:end]

if K < end:
    ans = visited[K-1]
else:
    K = K - end
    j = K % loop
    ans = where2[j]
 
print(ans)
'''
where2 = [where[end]]
for k in range(loopafter):
    where2.append(A[where2[k]-1])
print(where2[-1])
'''

Submission status: Wrong Answer

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

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

#print("通過0",now,limit,count,log)

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

if limit > 0:
    #input("stop")
    limit = limit % len(log[log.index(now) - 2:])
    #print("通過2",now,limit,count,log)
    while limit > 0:
        now = A[now - 1]
        limit -= 1
        count += 1
        #print("通過3",now,limit,count,log)

print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
f = 1
c = 1
ans = n
bal = 0
for e,i in enumerate(A,1):
    if dp[f-1]:
        ans = c-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(f)
        f = A[f-1]
        c+=1
if bal : bal-=1
k-= bal+1
# print(B,bal)
k = (k%ans+1)%ans
print(B[k+bal])

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
def solve(n, k, a):
    seen = set()
    cycle = []
    start = 1
    while start not in seen:
        seen.add(start)
        cycle.append(start)
        start = a[start-1]
    idx = cycle.index(start)
    pre = idx
    cycle_len = len(cycle)- idx
    k = (k-idx) % cycle_len
    
    return cycle[k+idx]
print(solve(N,K,A))

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
seen=[0 for i in range(n)]
while True:
        now=way[-1]
        way.append(a[now])
        
        if seen[a[now]]==1:
                break
        seen[a[now]]=1


last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=k%step
if k>=st:
        ans=way[rest]+1

else:
        ans=way[k]+1
print(ans)

Submission status: Wrong Answer

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

    i=L[i]-1

    
S.append(i+1)
SS=S


S=S[S.index(L[i]):]

cnt=len(S)



print(SS[K%(cnt)])

Submission status: Wrong Answer

Solution code:
# A_1からスタートして、同じA_iに戻ってくるまでの[移動回数][到着場所]をリスト化

import sys
readline = sys.stdin.readline

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

route = [0]
visited = {0}
while True:
  nex = A[route[-1]] - 1
#  print(route[-1]," -> ",nex)
  if nex in visited:
#    print(nex,"is already visited")
    loopstart = route.index(nex)
#    print("loopstart =",loopstart)
    break
  else:
    visited.add(nex)
    route += [nex]
    
#print("route",route)

before_loop,one_loop = route[:loopstart],route[loopstart:]
#print("before_loop",before_loop)
#print("one_loop",one_loop)

if K <= len(before_loop):
  print(before_loop[K - 1] + 1)
else:
  K -= len(before_loop)
  K %= len(one_loop)
  print(one_loop[K] + 1)

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-start_point) % 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()))

count = 0
history = [1]
index = -1

while count <= K:
    current = history[-1]
    _next = A[current - 1]
    if _next in history:
        index = history.index(_next)
        break
    else:
        history.append(_next)
    count += 1

if index == -1:
    print(history[-1])
else:
    loop = history[index:]
    q, mod = divmod(K - count - (len(history) - index), len(loop))
    print(loop[mod - 1])

Submission status: Wrong Answer

Solution code:
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
c=0
count=0
init_city=0
tmp_city = [0] * (n+1)

while (1):
    count+=1
#     print(c)
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city[count-1]=tmp
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(count))%len(tmp_city[init_city:])])

Submission status: Wrong Answer

Solution code:
[N,K] = list(map(int,input().split()))
A = list(map(int,input().split()))
 
#ループする場合
flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        #del patt[-1]
        break
    flag[i]=1
    i=A[i]-1
 
 
srt = patt.index(patt[-1])
#print('srt',srt)
end = len(patt)-2
ptrn = patt[srt:end+1]
 
if K<len(ptrn):
    print(patt[K]+1)
else:
    B = (K-srt) % len(ptrn)
    #print('B',B)
 
    print(ptrn[B]+1)
    #print('0000000')
 
    #print('ptrm',ptrn)
    #print(patt)

Submission status: Wrong Answer

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

s,m=0,0
p1,p2=[],[]
while 1:
    if p1.count(str(a[s-1]))==0:
        p1.append(str(a[s-1]))
    elif p1.count(str(a[s-1]))==1:
        m=p1.index(str(a[s-1]))
        p2=p1[m:]
        break
    s=a[s-1]
if k<len(p1):
    print(p1[k-1])
else:
    k=(k-len(p1)-1)%len(p2)
    print(p2[k-1])
print(p1,p2)

Submission status: Wrong Answer

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

now = 1
loop_node = []
first_loop = 0
l = [0 for _ in range(N)]
while True:
    l[now - 1] += 1
    if l[now - 1] == 3:
        break

    if l[now - 1] == 2:
        loop_node.append(now)

    now = A[now - 1]
    first_loop += 1

loop_num = len(loop_node)
first_loop -= loop_num * 2

if K <= first_loop:
    now = 1
    for _ in range(first_loop):
        now = A[now - 1]
    print(now)
else:
    print(loop_node[(K - first_loop) % loop_num])

Submission status: Wrong Answer

Solution code:
N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=1
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
        print(s)
    print(s)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K <= key:
        print(lst_memo[K - 1] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
cnt = 0
loop_start = 0
loop_end = 0
now_town = 1
next_town = 0
l = [[0 for x in range(2)] for y in range(n)]
l[0] = [1,0]
while loop_end == 0:
    next_town = a[now_town-1]
    if l[next_town-1][0] == 1:
        loop_end = l[now_town-1][1]
        loop_start = l[next_town-1][1]
    else:
        cnt += 1
        now_town = next_town
        l[now_town-1][0] = 1
        l[now_town-1][1] = cnt
loop = loop_end-loop_start+1
passed = (k-loop_start)%loop
k = passed+loop_start
for i in range(n):
    if l[i][1] == k:
        print(i+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
temp = 1
count = 0
ans = 0

dict_num = {}
set_num = set()

for _ in range(K):
  count += 1
  temp = A[temp-1]
  if temp in set_num:
    dict_num[temp] += 1
    if dict_num[temp] == 3:
      break
  else:
    dict_num[temp] = 1
    set_num.add(temp)
    continue
else:
  ans = temp


if count == K:
  pass
else:
  count = count - list(dict_num.values()).count(1) -1
  mod = K % count
  for _ in range(mod + 1):
    temp = A[temp-1]
  else:
    ans = temp
  
print(ans)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K <= len(set_memo):
        print(lst_memo[K - 1] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

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

s = {0}
loop = [0]

while a[loop[-1]] not in s:
    loop.append(a[loop[-1]])
    s.add(loop[-1])

    
idx = loop.index(a[loop[-1]])

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

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

# print(next)
link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link) - len(link2)
# print(rep)
if (len(link2) == 0):
    p = k%rep
    # print('e')
    for i in range(p+rep):
        next = a[next-1]
elif(k > len(link2)):
    # print('f')
    p = (k-len(link2))%rep
    # print(p)
    # print(next)
    for i in range(p+rep):
        next = a[next-1]
else:
    # print('d')
    next = 0
    for i in range(k-1):
        next = a[next-1]

ans = next
# print(link,link2)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n, k = (int(x) for x in input().split())
seq = [int(x) for x in input().split()]
visited = [[False,0] for _ in xrange(n)]

t = 1
cnt = 0
while not visited[t-1][0]:
    visited[t-1][0] = True
    visited[t-1][1] = cnt
    t = seq[t-1]
    cnt += 1

cycle_len = cnt - visited[t-1][1]
for _ in xrange((k-visited[t-1][1]) % cycle_len):
    t = seq[t-1]
print(t)

Submission status: Wrong Answer

Solution code:
n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 1
s = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1


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

Submission status: Wrong Answer

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

rute = list()
visited = dict()
city = A[0]
i = 0
while city not in rute:
    rute.append(city)
    visited[city] = i
    city = A[city]
    i += 1

loopstart = visited[city]
loopend = len(rute) - 1

if K < loopstart:
    print(rute[K-1]+1)
else:
    K -= loopstart
    K %= loopend - loopstart + 1
    print(rute[loopstart + K - 1]+1)

Submission status: Wrong Answer

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

i = 1
head = []
used = {}
roop = []

inte=False
index = 0
for _ in range(k):
    index += 1
    i = a[i]
    if i in used:
        head = [ k for k, v in used.items() if v < used[i]]
        roop = [ k for k, v in sorted(used.items())]
        inte =True
        break
    else:
        used[i] = index

if inte:
    if len(head) >= k:
        print(head[k-1])
    else:
        x = (k - 1 - len(head)) % len(roop)
        print(roop[x])
else:
    print([ k for k, v in sorted(used.items())][k-1])

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split()) #N: 町の数, K: ちょうどこの回数でたどり着く町

visited = [False for _ in range(N)]

A = list(map(lambda n: int(n)-1, input().split()))
tmp = 0 #次に訪れる番号
visited[0] = True
cnt = 0 #テレポートの回数
log = [0] #テレポートの記録

while True:
    tmp = A[tmp]
    if cnt == K:
        print(tmp+1)
        sys.exit()
    if visited[tmp]: #一度訪れたことがあった場合
        start = log.index(tmp)
        break

    visited[tmp] = True
    log.append(tmp)
    cnt += 1

loop = log[start:cnt+1]
period = len(loop)

remain = K - cnt-1

print(loop[remain%period] + 1)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

print(next)
link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link) - len(link2)
print(rep)
if (len(link2) == 0):
    p = k%rep
    for i in range(p+rep):
        next = a[next-1]
elif(k > len(link2)):
    p = (k-len(link2))%rep
    for i in range(p+rep):
        next = a[next-1]
else:
    next = 0
    for i in range(k-1):
        next = a[next-1]

ans = next
print(ans)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K <= N:
        print(lst_memo[K - 1] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int, input().split()))
A.insert(0, 0)
root = [0 for i in range(N + 1)]
root[1] = 1
now = A[1]
loop = 0
getloop = 1
startloop = -1
mae_now = -1
for j in range(K):
    root[now] += 1
    if root[now] == 3:
        startloop = now
        break
    if root[now] == 2:
        loop += 1
    if root[now] == 1:
        getloop += 1
    mae_now = now
    now = A[now]
if startloop == -1:
    print(mae_now)
if startloop != -1:
    nokoriloop = K % loop + getloop
    now = A[1]
    for i in range(nokoriloop):
        now = A[now]
    print(now)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = map(int, input().split())
    A = tuple(map(int, input().split()))
    path = [1]
    for i in range(2*N):
        cur = A[path[-1]-1]
        path.append(cur)

    if K < N:
        print(path[K])
        exit()

    end = path[N]
    si = N-1
    while path[si] != end:
        si -= 1
    dist = end - si +1
    K = K % dist
    while K < N:
        K += dist

    print(path[K])

if __name__ == '__main__':
    resolve()

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

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

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np
n, k = map(int, input().split())
An = list(map(int, input().split()))
root = deque([1])
check = np.zeros(n, np.int)
check[0] = 1
i = 0
dup = 1
cnt = 1
for _ in range(n):
    temp = An[i]
    if check[temp-1] == 1:
        dup = temp
        break
    root.append(temp)
    check[temp-1] = 1
    i = temp - 1
    cnt += 1
index = root.index(dup)
if index == cnt - 1:
    print(dup)
else:
    print(root[(k-index) % (cnt-index) + index])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
f = 1
c = 1
ans = n
bal = 0
for e,i in enumerate(A,1):
    if dp[f-1]:
        ans = e-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(f)
        f = A[f-1]
        c+=1
bal-=1
k-= bal+1
# print(B,bal)
k = (k%ans+1)%ans
print(B[k+bal])

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)%(stop-start)
print(P[Z+start])

Submission status: Wrong Answer

Solution code:

def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K < key:
        print(lst_memo[K - 1] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#4問目
#まず最初に何回で一周するか調べてみる
#すでにいったところに戻ってきた場合も
N, K = map(int, input().split())
A = list(map(int, input().split()))

already = [0] * N

start = A[0]
now = A[A[0] - 1]
count = 1
while(now != start and already[now-1] != True):
    count += 1
    already[now - 1] = True
    now = A[now - 1]
count += 1
if(now != start):
    #ループに入るまで何回ステップがあったか調べる
    step = 1
    temp = A[A[0] - 1]
    while(temp != A[now - 1]):
        temp = A[temp - 1]
        step += 1

    cycle = count - step
    amari = K % cycle
    temp = A[A[0] - 1]
    
    for i in range(step + amari -1):
        temp = A[temp - 1]
    print(temp)
else:
    temp = K % count
    now = A[0]
    for i in range(N):
        now = A[now - 1]
    print(now)

Submission status: Wrong Answer

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

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

from collections import defaultdict

now = 0
tele = [0]
tele2 = defaultdict(lambda:N+1)
tele2[0] = 1

i = 1
while True:
  now = A[now] - 1
  i += 1
  if tele2[now] != N+1:
    num = tele2[now] -1
    epoch = i - num - 1
    e_list = tele[num:]
    break
  tele2[now] = i
  tele.append(now)
roop = int((K-num)/epoch)
amari = (K-num)%epoch
print(e_list[amari] + 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

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

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

#解
if K < onewayLen:
    town = 0
    for i in range(K):
        town = A[town]-1
    print(town)
else:
    loopMod = (K-onewayLen) % loopLen
    town = loopPoint
    for i in range(loopMod):
        town = A[town]-1
    print(town+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np
n,k = map(int,input().split())
a = list(map(int,input().split()))
roop_find = deque([0])
i = 0
count = 0 
for i in range(n):
    if a[i]-1 in roop_find:
        break
    else:
        roop_find.append(a[i]-1)
        i = a[i] - 1
        count += 1
b = roop_find.index(a[i]-1)

if k < b:
    print(list(roop_find)[k]+1)
else:
    k-=b
    e = k % (count+1-b)
    print(list(roop_find)[b+e]+1)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K < key or key > len(lst_memo):
        print(lst_memo[K - 1] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

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

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
for i in range(n+1):
    if bin(know, now): break
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

cnt = len(loop)
if k>=len(know): k -= len(know)
ans = loop[k%cnt]


print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
bl = [False]*n
now = 1
bl[now - 1] = True
ct = 1
res = [1]
ky = int()
for i in range(n):
    if a[i] == i:
        print(i)
        exit()
while True:
    if bl[a[now - 1] - 1]:
        ky = a[now - 1]
        break
    now = a[now - 1]
    bl[now - 1] = True
    res.append(now)
    ct += 1
roop = ct - res.index(ky)
x = ct - roop
k -= x
print(res[x + k % roop])