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: Runtime Error

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

y = 1
list_y =[1]
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]
    list_y.append(y)
    
    if len(list_y) != len(set(list_y)):
        break    #重複要素で脱出
        
#roopを定義したい

roop = len(list_y) - list_y.index(list_y[len(list_y) - 1])

amari = len(list_y) - roop - 1
k = (k - amari) /roop + amari 

y = 1
for i in range(k):
    x = a[y - 1]
    y = a[x - 1]

print(y)

Submission status: Runtime Error

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

y = 1
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]
    
    
    if i > 10 ** 18
        break



print(y)

Submission status: Runtime Error

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

y = 1
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]
    if y == 1:
        r = i + 1
        break

k %= r
y = 1
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]

print(y)

Submission status: Runtime Error

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

y = a[1]
for i in range(k - 1):
    x = a[y]
    y = a[x]
    
print(y)

Submission status: Runtime Error

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

root = [1]
loop_start = 0
for i in range(n):
    pos = A[root[i] - 1]
    if pos not in root:
        root.append(pos)
    else:
        loop_start = pos
        break

idx = root.index(loop_start)
one_way, loop = root[: idx], root[idx: ]

print(one_way[k] if k <= len(one_way) else loop[(k - len(one_way)) % len(loop)])

Submission status: Runtime Error

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

visited = []
count, loop = 0, 0
pos = 1
for i in range(k):
    pos = A[pos - 1]
    count += 1
    if pos not in(visited):
        visited.append(pos)
    else:
        idx = visited.index(pos)
        visited = visited[idx: ]
        loop = len(visited)
        break

print(visited[(k - count) % loop])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int,input().split()))
flag = [True]*n
root = [1]
now = 0

while flag[now]:
    flag[now] = False
    now = A[now] -1
    root.append(now+1)

to_p = root.index(root[-1])
p2p = root[to_p:-1]
rooplen = len(p2p)

if k < rooplen:
    print(root[k])
else:    
    idx = (k-to_p)
    idx %= p2p_len
    print(p2p[idx])

Submission status: Runtime Error

Solution code:

n, k = map(int, input().split())
A = map(int,input().split())
flag = [True]*n
p = 0
rt = [1]

while flag[p]:
    flag[p] = False
    p = A[p] -1
    rt.append(p+1)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]
p2p_len = len(p2p)

if k < p2p_len:
    print(rt[k])
else:
    print(p2p[(k-to_p)%p2p_len])

Submission status: Runtime Error

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

array=map(int,input().split())

A=list(array)
A.insert(0,0)

visit=0
root=[]
visited=1
once=0
while True:
    if visit & (0b1<<visited):
        break
    root.append(visited)
    visit|=0b1<<visited
    once+=1
    visited=A[visited]

roop=root[root.index(visited):]
if K<=once:
    print(root[K])
else:
    order=K-once
    print(roop[order%len(roop)])

Submission status: Runtime Error

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

array=map(int,input().split())

A=list(array)
A.insert(0,0)

visit=0
root=[]
visited=1
once=0
while True:
    if visit & (0b1<<visited):
        break
    root.append(visited)
    visit|=0b1<<visited
    once+=1
    visited=A[visited]

roop=root[root.index(visited):]
print(roop)
if K<=once:
    print(root[K])
else:
    order=K-once
    print(roop[order%len(roop)])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限

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

#閉路を見つける
def rec(v,p):
    global cnt
    if seen[v] == -1:
        seen[v] = cnt
        cnt += 1
        rec(g[v],v)
    
    if seen[v] > 0 and seen[v] < seen[p]:
        s = seen[v]
        e = seen[p]
        c = e -s

seen = [-1]*(n+1)
seen[0] = 0
cnt = 1
rec(1,0)            
print(seen.index(l+(k-l)%c+1))

Submission status: Runtime Error

Solution code:

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

#閉路を見つける
def rec(v,p):
    global cnt
    if seen[v] == -1:
        seen[v] = cnt
        cnt += 1
        rec(g[v],v)
    
    if seen[v] > 0 and seen[v] < seen[p]:
        s = seen[v]
        e = seen[p]
        c = e -s

seen = [-1]*(n+1)
seen[0] = 0
cnt = 1
rec(1,0)            
print(seen.index(l+(k-l)%c+1))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
input = sys.stdin.readline

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

#閉路を見つける
def rec(v,p):
    global cnt
    if seen[v] == -1:
        seen[v] = cnt
        cnt += 1
        rec(g[v],v)
    
    if seen[v] > 0 and seen[v] < seen[p]:
        s = seen[v]
        e = seen[p]
        c = e -s

seen = [-1]*(n+1)
seen[0] = 0
cnt = 1
rec(1,0)            
print(seen.index(l+(k-l)%c+1))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
f_l=0
for i in range(n-1):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,k+1):
    now_city = s[now_city-1]
    t[now_city-1]+=1
    # f[now_city-1]=i
    for j in range(n):
        if t[j]==2:
        #  print(i,f[j])
         sycle=i-f[j]
         f_l=f[j]
         a = 1
         s_l_city=j+1
         break
    if a==1:
        break
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
now_city=s_l_city
for j in range((k-f_l)%sycle):
    now_city = s[now_city-1]
print(now_city)

Submission status: Runtime Error

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

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


A = []
for i in range(N):
    A.append(tmp[i]-1)

check = set([0])

now = 0

jun = [0]
for i in range(N):
    if A[now] not in check:
        check.add(A[now])
        jun.append(A[now])
        now = A[now]
    else:
        break
#print(jun.index(A[now]))
#print(jun)
print(jun[(K-jun.index(A[now])+1)%(len(jun)-jun.index(A[now])+1)]+1)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit
span=visit_flag[next_town-1][1]

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
#    if roop==0:
#        ans=next_town
    else:
        for i in range((K-span)%roop):
            next_town = TO[next_town-1]
            ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit
span=visit_flag[next_town-1][1]

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    if roop==0:
        ans=next_town
    else:
        for i in range((K-span)%roop):
            next_town = TO[next_town-1]
            ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit
span=visit_flag[next_town-1][1]

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    if roop==0:
        ans=ans=next_town
    else:
        for i in range((K-span)%roop):
            next_town = TO[next_town-1]
            ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit
span=visit_flag[next_town-1][1]

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-span)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop+1)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=1

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop+1)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=1

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=1
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=1

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=1
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=1

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    for i in range(K):
        next_town = 1
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import collections
from fractions import Fraction
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def ispow(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def pm(x):
    return x//abs(x)
def inputintlist(): 
    return list(map(int,input().split()))
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
######################################################################################################
N,K=inputintlist()
A=inputintlist()
for a in range(N):
    A[a]-=1
now=0
if K<10**6:
    for a in range(K):now=A[now]
else:
    history=[now]
    visited=defaultdict(lambda:False)
    for a in range(N):
        now=A[a]
        history.append(now)
        if visited[now]:break
        visited[now]=True
    heirostart=history.index(history[-1])
    heiro=history[heirostart:-1]
    llen=K-len(history[:heirostart])
    now=heiro[llen%len(heiro)]
now+=1
print(now)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
As = list(map(int, input().split()))
As = [A-1 for A in As]
Vs = [-1]*N
p = 0
p0 = -1
Rs = [p]
Vs[p] = p
for i in range(N):
  n = As[p]
  Rs.append(n)
  if not Vs[n] < 0:
    p0 = Vs[n]
    break
  p = n
  Vs[p] = p
#print(Rs, p0)
if K <= p0:
  r = Rs[K]
else:
  r = Rs[p0+(K-p0)%(len(Rs)-p0+1)]
print(r+1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
As = list(map(int, input().split()))
As = [A-1 for A in As]
Vs = [-1]*N
p = 0
p0 = -1
Rs = [p]
Vs[p] = p
for i in range(N):
  n = As[p]
  Rs.append(n)
  if not Vs[n] < 0:
    p0 = Vs[n]
    break
  p = n
  Vs[p] = p
#print(Rs, p0)
if K <= p0:
  r = Rs[K]
else:
  r = Rs[p0+1+(K-p0)%(len(Rs)-p0)]
print(r+1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if number==N+1:
  answer=K%N
  print(S[answer])
else:
  if K <= N:
    print(S[K - 1])
  else:
    z = K - c - 1
    y = z % (number - c)
    print(T[y])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= N:
    print(S[K - 1])
else:
    z = K - c - 1
    y = z % (number - c)
    print(T[y])

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10**7)

def teleport(city, end, teleportmap, count=1):

    next_city = teleportmap[city]

    if count == end:
        return next_city

    else:
        return teleport(next_city, end, teleportmap, count+1)

n, k = map(int,input().split())
a = list(map(int, input().split()))
teleportmap = dict(zip(range(1, len(a)+1), a))

if k <= n:
    print(teleport(1, n, teleportmap))

else:
    countmap = [0]*n
    countmap[0] = 1

    next_city =1
    for _ in range(1, n+1):
        next_city = a[next_city-1]
        countmap[next_city -1] += 1
        if countmap[next_city -1] == 2:
            start = next_city
            break

    roop = {}
    for j in range(1, n+1):
        roop[start] = teleportmap[start]
        start = roop[start]
    
    num_r = len(roop)

    num_until_r= 0
    for m in teleportmap.keys():
        if m not in roop and countmap[m-1] != 0:
            num_until_r += 1
    
    k_r = k - num_until_r
    num_end = k_r%num_r

    if num_end == 0:
        print(next_city)
        
    else:
        print(teleport(next_city, num_end, roop))

Submission status: Runtime Error

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_pow(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

def fast_mod(x, N):
  if N % 2 == 0:
    return x & (fast_pow(2, N) - 1)
  else:
    return x % N

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  if K > len(route) and len(once_route) > 0:
    # print("loop")
    loop_K = K - len(route)
    print(loop_route[fast_mod(loop_K, len(loop_route))] + 1)
    # print(loop_route[loop_K % len(loop_route)] + 1)
  else:
    # print("normal")
    # print(route[fast_mod(K, len(loop_route))] + 1)
    # print("K:", K)
    # print("K %% len(route):", K % len(route))
    print(route[fast_mod(K, len(route))] + 1)
    # print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_pow(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

def fast_mod(x, N):
  if N % 2 == 0:
    return x & fast_pow(2, N - 1)
  else:
    return x % N

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  if K > len(route) and len(once_route) > 0:
    # print("loop")
    loop_K = K - len(route)
    print(loop_route[fast_mod(loop_K, len(loop_route))] + 1)
    # print(loop_route[loop_K % len(loop_route)] + 1)
  else:
    # print("normal")
    # print(route[fast_mod(K, len(loop_route))] + 1)
    # print("K:", K)
    # print("K %% len(route):", K % len(route))
    print(route[fast_mod(K, len(route))] + 1)
    # print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
a = 0
n = A[0]
lis = [1,n]
 
def dupli(seq):
    return len(seq) != len(set(seq))
 
while a < K - 1:
    a = a + 1
    n = A[n-1]
 
    if dupli(lis) == True:
        break
    lis.append(n)
    #print(lis)
    
if K+1 > len(lis):
    t = (a - lis.index(lis[n]))
    num = (K-a) % t
 
    if num == 0:
        print(lis[a])
    else:
        #print("t:",t)
        #print("num:",num)
        print(lis[a-t+num])
else:
    print(lis[K])

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]

    if dupli(lis) == True:
        break
    lis.append(n)
    print(lis)
    
if K+1 > len(lis):
    t = (a - lis.index(lis[n]))
    num = (K-a) % t

    if num == 0:
        print(lis[a])
    else:
        print("t:",t)
        print("num:",num)
        print(lis[a-t+num])
else:
    print(lis[K])

Submission status: Runtime Error

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_mod(x, N):
  return (x * N) >> 32

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  print(route[fast_mod(K, len(loop_route))])
  # print(route[K % len(loop_route) + len(once_route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while next_town != 0:  # 最初の街に戻るまで
    route.append(town)
    next_town = A[town]
    town = next_town
  
  print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]

    if dupli(lis) == True:
        break
    lis.append(n)
    print(lis)
    
if K+1 > len(lis):
    t = (a - lis.index(lis[n]))
    num = (K-a) % t

    if num == 0:
        print(lis[a])
    else:
        print(lis[a-t+num])
else:
    print(lis[K])

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]

    if dupli(lis) == True:
        break
    lis.append(n)
    #print(lis)
    
if K+1 > len(lis):
    t = (a - lis.index(lis[n]))
    num = (K-a) % t

    if num == 0:
        print(lis[a-1])
    else:
        print(lis[a-t+num])
else:
    print(lis[K])

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]

    if dupli(lis) == True:
        break
    lis.append(n)
    print(lis)
if K+1 > len(lis):
    t = (a - lis.index(lis[n]))
    num = (K-a) % t

    if num == 0:
        print(lis[a-1])
    else:
        print(lis[a-t+num])
else:
    print(lis[K])

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]

    if dupli(lis) == True:
        break
    lis.append(n)
    print(lis)
    
if K+1 < len(lis):
    t = (a - lis.index(lis[n]))
    num = (K-a) % t

    if num == 0:
        print(lis[a-1])
    else:
        print(lis[a-t+num])
else:
    print(lis[K])

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]
    print(lis)
    if dupli(lis) == True:
        break
    lis.append(n)

t = (a - lis.index(lis[n]))

num = (K-a) % t

if num == 0:
    print(lis[a-1])
else:
    print(lis[a-t+num])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
a = 0
n = A[0]
lis = [1,n]
 
def dupli(seq):
    return len(seq) != len(set(seq))
 
while a < K - 1:
    a = a + 1
    n = A[n-1]
    #print(lis)
    if dupli(lis) == True:
        break
    lis.append(n)
 
num = (K-lis.index(A[n-1])-1) %(lis.index(A[n-1]) - lis.index(A[n])+1)
 
t = (lis.index(A[n-1]) - lis.index(A[n])+1)
 
lis[lis.index(A[n])-t+num+1]

Submission status: Runtime Error

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

a = 0
n = A[0]
lis = [1,n]

def dupli(seq):
    return len(seq) != len(set(seq))

while a < K - 1:
    a = a + 1
    n = A[n-1]
    #print(lis)
    if dupli(lis) == True:
        break
    lis.append(n)

num = (K-lis.index(A[n-1])-1) %(lis.index(A[n-1]) - lis.index(A[n])+1)

t = (lis.index(A[n-1]) - lis.index(A[n])+1)

print(lis[n-t+num])