Skip to content

Instantly share code, notes, and snippets.

@monhime
Created May 10, 2020 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monhime/08571eb19b785faaca43d5da1209cc5b to your computer and use it in GitHub Desktop.
Save monhime/08571eb19b785faaca43d5da1209cc5b to your computer and use it in GitHub Desktop.
ABC167 D問題 解答
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]
for _ in range(k):
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
else:
print(next_a)
sys.exit()
#print(route)
#rint(cunt,loop)
k-=cunt
k=k%loop
#print(k)
print(route[cunt+k])
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment