[AtCoder] D - へんてこ辞書

D - へんてこ辞書

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
N,A=map(int,input().split())
K=input()
K=int(K)
arr = list(map(int,input().split()))

def solve(A, now, k):
vis = [0] * N
id = 1
while k > 0:
if vis[now] > 0:
cycle = id - vis[now]
if cycle == 0:
return now + 1
k %= cycle
if(k == 0):
return now + 1
else:
vis[now] = id
id = id + 1
now = A[now] - 1
k -= 1
return now + 1

print(solve(arr, A - 1, K))
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/26/PS/AtCoder/abc030-d/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.