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 = [int(v) for v in input().split(" ")]
Ai = [int(v) for v in input().split(" ")]
assert len(Ai) == N


path = [0]
while True:
  next_city = Ai[path[len(path) - 1]] - 1
  if next_city in path:
    break
  path.append(next_city)

for i, city in enumerate(path):
  if city == next_city:
    start = path[:i]
    circle = path[i:]

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

Submission status: Runtime Error

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

s = 1
g = 0
i = 1
history = [s]
flag = [False] * n
flag[0] = True

while True:
    g = a[s-1]
    if flag[g-1] == True:
        break
    else:
        flag[g-1] = True
        history.append(g)
        s = g
        i += 1


x = history.index(g)

if k < n:
    ans = history[k]
else:
    d = (k-x) % (i - x)

    ans = history[x + d]

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
town = 1
record = [1]
for i in range(K):
    town = A[town-1]
    if town in record:
        break
    record.append(town)
    
newrecord = record[record.index(town):]
roop = len(newrecord)
amari = K%roop

if K<=len(record):
    answer = record[amari]
elif amari-(len(record)-roop)>=0:
    answer = newrecord[amari-(len(record)-roop)]
else:
    answer = newrecord[(amari-(len(record)-roop))%3]
    
print(answer)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
town = 1
record = [1]
for i in range(K):
    town = A[town-1]
    if town in record:
        break
    record.append(town)
    
newrecord = record[record.index(town):]
roop = len(newrecord)
amari = K%roop

if K<=len(record):
    answer = record[amari]
else:
    answer = newrecord[amari-(len(record)-roop)]
print(answer)

Submission status: Runtime Error

Solution code:
import sys
N, K = map(int, input().split())
As = [0] + list(map(int, input().split()))
# print(As)
d = [-1]*(N+1)
i = 1
step = 1
while step <= K:
	# print("step: {}, idx: {}".format(step,i))
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i] - 1
		step = step + int((K - step) / freq)*freq
	d[next_i] = step
	step += 1
	i = next_i

print(i)

Submission status: Runtime Error

Solution code:
import sys
N, K = map(int, input().split())
As = list(map(int, input().split()))

d = [-1]*(N+1)
i = 1
step = 1
while step < K:
	print(d)
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i]
		step = step + int((K - step) / freq)
	d[next_i] = step
	step += 1
	i = next_i

print(i)

Submission status: Runtime Error

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

ans=0
now=1
i=1
result=[1]
while i<=k:
    now=a[now-1]
    if now in result:
        x=result.index(now)
        length=i-1-x
        ans=result[(k-x)%length+x]
    result.append(now)
    i+=1
    
if ans!=0:
    print(ans)
else:
    print(result[-1])

Submission status: Runtime Error

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

ans=0
now=1
i=1
result=[1]
while i<=k:
    now=a[now]
    if now in result:
        x=result.index(now)
        length=i-1-x
        ans=result[(k-x)%length+x]
    result.append(now)
    i+=1
    
if ans!=0:
    print(ans)
else:
    print(result[-1])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
cnt=1
for i in range(2*n):
    b.append(a[cnt-1])
    cnt=a[cnt-1]
#print(b)
b.sort()
cc=[]
for i in range(2*n):
    if i==0:
        cnt=b[i]
    else:
        if cnt==b[i]:
            cc.append(cnt)
        else:
            cnt=b[i]
#print(cc)
cc=list(set(cc))
#print(cc)
#print(d)
bb=[]
cnt=1
for i in range(2*n):
    bb.append(a[cnt-1])
    cnt=a[cnt-1]
for i in range(len(bb)-len(cc)):
    if bb[i]==bb[i+len(cc)]:
        ans=i
        break
    #print(b[i],b[i+len(cc)])
d=bb[ans:len(cc)]
if len(d)!=0 or k-ans<0:
    print(d[(k-ans)%len(cc)-1])
elif k-ans<0:
    print(bb[k-1])
else:
    print(bb[len(bb)-1])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
cnt=1
for i in range(2*n):
    b.append(a[cnt-1])
    cnt=a[cnt-1]
#print(b)
b.sort()
cc=[]
for i in range(2*n):
    if i==0:
        cnt=b[i]
    else:
        if cnt==b[i]:
            cc.append(cnt)
        else:
            cnt=b[i]
#print(cc)
cc=list(set(cc))
#print(cc)
#print(d)
bb=[]
cnt=1
for i in range(2*n):
    bb.append(a[cnt-1])
    cnt=a[cnt-1]
for i in range(len(bb)-len(cc)):
    if bb[i]==bb[i+len(cc)]:
        ans=i
        break
    #print(b[i],b[i+len(cc)])
d=bb[ans:len(cc)]
if len(d)!=0 or k-ans<0:
    print(d[(k-ans)%len(cc)-1])
elif k-ans<0:
    print(bb[k])
else:
    print(bb[len(bb)-1])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
cnt=1
for i in range(2*n):
    b.append(a[cnt-1])
    cnt=a[cnt-1]
#print(b)
b.sort()
cc=[]
for i in range(2*n):
    if i==0:
        cnt=b[i]
    else:
        if cnt==b[i]:
            cc.append(cnt)
        else:
            cnt=b[i]
#print(cc)
cc=list(set(cc))
#print(cc)
#print(d)
bb=[]
cnt=1
for i in range(2*n):
    bb.append(a[cnt-1])
    cnt=a[cnt-1]
for i in range(len(bb)-len(cc)):
    if bb[i]==bb[i+len(cc)]:
        ans=i
        break
    #print(b[i],b[i+len(cc)])
d=bb[ans:len(cc)]
print(d[(k-ans)%len(cc)-1])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
cnt=1
for i in range(2*n):
    b.append(a[cnt-1])
    cnt=a[cnt-1]
#print(b)
b.sort()
cc=[]
for i in range(2*n):
    if i==0:
        cnt=b[i]
    else:
        if cnt==b[i]:
            cc.append(cnt)
        else:
            cnt=b[i]
#print(cc)
cc=list(set(cc))
#print(cc)
#print(d)
b=[]
cnt=1
for i in range(2*n):
    b.append(a[cnt-1])
    cnt=a[cnt-1]
for i in range(len(b)-len(cc)):
    if b[i]==b[i+len(cc)]:
        ans=i
        break
    #print(b[i],b[i+len(cc)])
d=b[ans:len(cc)]
print(d[(k-ans)%len(cc)-1])

Submission status: Runtime Error

Solution code:
NK = list(map(int, input().split()))
N = NK[0]; K = NK[1]
A = [6,5,2,5,3,2]

rlist = [0]*N
flag = 1
location = 1
div = 0

while flag>0:
    location = A[location-1]
    if rlist[A[location-1]-1] == 0:
        rlist[A[location-1]-1] = rlist[location-1]+1
    else:
        div = rlist[location-1]+1-rlist[A[location-1]-1]
        flag = -1

cnt = (K-rlist[A[location-1]-1])%div + rlist[A[location-1]-1]

result = 0
for i in range(cnt):
    result = A[result]-1

print(result+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = [0]+list(map(int,input().split()))
visited = [False for _ in range(n+1)]
pos = 1
x = []
flg = False
for i in range(k):
    if visited[pos] == False:
        x.append(pos)
        visited[pos] = True
        pos = A[pos]
    else:
        flg = True
        pre = x.index(pos)
        loop = x[pre:]
        break
if not flg:
    print(pos)
else:
    print(loop[(k-pre)%len(loop)])
~

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
new_l= np.array([A[0]])
for i in range(K):
    r = i%N
    if A[new_l[r]-1] in new_l:
        target = A[new_l[r]-1]
        break
    new_l=np.append(new_l,A[new_l[r]-1])
    i+=1

index = np.where(new_l == target)
index=list(index)[0][0]
right = new_l[:index]
left  = new_l[index:]
pattern = K-len(right)
n = pattern % len(left)
print(left[n-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
new_l=[A[0]]
for i in range(K):
    r = i%N
    if A[new_l[r]-1] in new_l:
        target = A[new_l[r]-1]
        break
    new_l.append(A[new_l[r]-1])
    i+=1

index = new_l.index(target)
right = new_l[:index]
left  = new_l[index:]
pattern = K-len(right)
n = pattern % len(left)
print(left[n-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = [0]+list(map(int,input().split()))
visited = [False for _ in range(n+1)]
pos = 1
x = []
flg = False
for i in range(k):
    if visited[pos] == False:
        x.append(pos)
        visited[pos] = True
        pos = A[pos]
    else:
        flg = True
        pre = x.index(pos)
        loop = x[pre:]
        break
print(loop[(k-pre)%len(loop)] if flg else x[k])

Submission status: Runtime Error

Solution code:
print(kari)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [False] * N
B[0] = True


def loop(s):
    return A[s-1]


cnt = 0
cnt1 = 0
flag = False
L = []
a = A[0]
while K - cnt1 - cnt > 0:
    if flag:
        break

    cnt1 += 1
    if B[a-1] == True:
        c = a
        while K - cnt1 - cnt >= 0:
            c = loop(c)
            if c == a:
                flag = True
                break
            else:
                L.append(c)
                cnt += 1

    elif B[a-1] == False:
        B[a-1] = True
        a = loop(a)

if flag == False:
    print(c)
else:
    L.append(a)
    d = K - cnt1
    e = d % cnt
    print(L[e-1])

Submission status: Runtime Error

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

idx = a[0]
hist = [1]
flag = 0
for i, _ in enumerate(range(k), start=1):
    if idx in hist:
        hist.append(idx)
        flag = 1
        break
    hist.append(idx)
    idx = a[idx-1]
    
if flag:
    l_start2, l_end2 = hist.index(idx), i
    surplus = (k-l_start) % (l_end - l_start)
    print(hist[l_start+surplus])
else:
    print(idx)

Submission status: Runtime Error

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

idx = a[0]
hist = [1]
for _ in range(k):
    if idx in hist:
        hist.append(idx)
        break
    hist.append(idx)
    idx = a[idx-1]
    
l_start, l_end = [i for i, v in enumerate(hist) if hist[-1]==v]

surplus = (k-l_start) % (l_end - l_start)
print(hist[l_start+surplus])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [False] * N
B[0] = True


def loop(s):
    return A[s-1]


cnt = 0
cnt1 = 0
flag = False
L = []
a = A[0]
while K - cnt1 - cnt > 0:
    if flag:
        break

    cnt1 += 1
    if B[a-1] == True:
        c = a
        while K - cnt1 - cnt > 0:
            c = loop(c)
            if c == a:
                flag = True
                break
            else:
                L.append(c)
                cnt += 1

    elif B[a-1] == False:
        B[a-1] = True
        a = loop(a)

if flag == False:
    print(c)
else:
    L.append(a)
    d = K - cnt1
    e = d % cnt
    print(L[e-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=[p-1 for p in a]
l=[0]
i=a[0]
p=-1
q=-1
for x in range(n):
    if i in set(l):break
    l.append(i)
    i=a[i]
d=-1
for x in range(n):
    if a[x]==i:
        d=x
        break
f=l[:d]
b=l[d:]
print(l,f,b,d)
if len(f)==0 or len(b)==0:
    print(l[k%(n-1)]+1)
    exit()

if k<=d:
    print(l[k]+1)
    exit()

k-=len(f)
print(b[k%len(b)]+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=[p-1 for p in a]
l=[0]
i=a[0]
p=-1
q=-1
for x in range(n):
    if i in (l):break
    l.append(i)
    i=a[i]
d=-1
for x in range(n):
    if a[x]==i:
        d=x
        break
f=l[:d]
b=l[d:]
if d==0 or d==n-1 or d==-1:
    print(l[k%(n-1)]+1)
    exit()

if k<=d:
    print(l[k]+1)
    exit()

k-=len(f)
print(b[k%len(b)]+1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**18)
n,k = map(int,input().split())
a = list(map(int,input().split()))
def ku(i,pl):
    if i == k:
        return pl
    return ku(i+1,a[pl-1])
print(ku(0,1))

Submission status: Runtime Error

Solution code:
n,k  = map(int, input().split())
towns = list( map(int, input().split()))
town=0
num=0
for i in range(k):
    town = towns[town]-1
    if(town == 0):
        num=i+1
        break
for i in range(k%num):
    town = towns[town]-1
print(town+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
c=Counter()
c[1]+=1

cycle_num=-1
i=0
cnt=0
b=[]
while cnt<2*10**5+1:
  c[a[i]]+=1
  b.append(a[i])
  if c[a[i]]>1 or a[i]==1:
    cycle_num=a[i]
    break
  i=a[i]-1
  cnt+=1
# print(b)
# print(cycle_num)
cycle=[]
b=[1]+b
for i,bb in enumerate(b):
  if bb==cycle_num:
    cycle=b[i:]
    break
  else:
    if i!=0:
      k-=1
    if k==0:
      print(bb)
      exit(0)
# print(cycle)
# syuuki=len(cycle)-1
syuuki=len(cycle)
print(cycle[(k%syuuki)+1])

Submission status: Runtime Error

Solution code:
n,k,*a=map(int,open(0).read().split())
d=[2*k]*n
s=0
while k:s=a[s]-1;d[s]=k=~-k%(d[s]-k)
print(s+1)

Submission status: Runtime Error

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

NN = [-1] * N
NN[0] = 0

i = 0
num = 0

while True:
    num += 1
    i = A[i] - 1
    if NN[i] != -1:
        n1 = NN[i]
        break
    NN[i] = num

n_ = num - n1

if K > N:
    K -= n1
    t = NN.index(n1 + K % n_) + 1
    print(t)
else:
    t = NN.index(K)
    print(t) + 1

Submission status: Runtime Error

Solution code:
n,k,*a=map(int,open(0).read().split())
d=[2*k]*n
s=0
while k:k-=1;k%=d[s:=a[s]-1]-k;d[s]=k
print(s)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
s=[-1]*(n+1)
t=1
for i in range(1,k):
    d.append(t)
    s[t]=1
    if(s[a[t-1]]==1):
        break
    t=a[t-1] 
idx=d.index(a[t-1])
d=d[idx:]
print(d[(k-i)%len(d)])

Submission status: Runtime Error

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

a=[]

for i in range(N):
    a.append(-1)
#print(a)
b=1
for i in range(N):
    if a[b-1]==-1:
        a[b-1]=i+1
        b=List[b-1]
        
    else:
        c=i
        break
#print(a)
#print(b,c)

num=a[b-1]-1
T=c-num
#print(T)
e=1
if K<num:
    for i in range(K):
        e=List(e-1)
    print(e)
else:
    qw=(K-num)%T
    List_new=[]
    for i in range(T):
        List_new.append(b)
        b=List[b-1]
    print(List_new[qw])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 1
re = []
data = 0
count = 0

def teleport(now):
    re.append(now)
    return a[now-1]

for i in range(k):
    count +=1
    result = teleport(now)
    data = [int(i) for i in set(re) if re.count(i) > 1]
    if data:
        break
    now = result

Number = [x for x in set(re) if re.count(x) > 1]
roop = [i for i,x in enumerate(re) if x == Number[0]] #ループしている箇所のindexを取得
roopNumber = roop[1] - roop[0] #ループしているindexの差を計算
remin = (k+1) - len(re) #残テレポート数
fun = remin % roopNumber #残りのテレポート数から余りで答えを出す
ans = re[roop[0]:roop[1]]
print(ans[fun])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
t=1
for i in range(1,k):
    d.append(t)
    if(a[t-1] in d):
        break
    t=a[t-1] 
idx=d.index(a[t-1])
d=d[idx:]
print(d[(k-i)%len(d)])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
An = list(map(int,input().split()))
a = An[0]
b = []
counter = 0
for i in range(K-1):
    a = An[a-1]
    b += [a]
    counter += 1
    f = list({a,a}&set(b))
    if f == [a,a]:
        c = b[b.index(a):]
        d = (K - counter - b.index(a) ) % len(c)
        break
for i in range(d-1):
    a = An[a-1]
print(a)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[1]
t=a[0]
for i in range(1,k):
    d.append(t)
    if(a[t-1] in d):
        break
    t=a[t-1] 
idx=d.index(a[t-1])
d=d[idx:]
print(d[(k-i-1)%len(d)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
t=a[0]
for i in range(1,k):
    d.append(t)
    if(a[t-1] in d):
        # d.pop()
        break
    t=a[t-1] 
idx=d.index(a[t-1])
d=d[idx:]
print(d[(k-i-1)%len(d)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
t=a[0]
for i in range(1,k):
    d.append(t)
    if(a[t-1] in d):
        # d.pop()
        break
    t=a[t-1] 
idx=d.index(a[t-1])
idx=len(d[:idx])
print(d[(k-i-1)%len(d)-idx])

Submission status: Runtime Error

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

loop_checker = {}
before_loop_len = 0
loop_len = 0

count = 2
now = 0
while True:
    if A[now] in loop_checker.values():
        before_loop_len = loop_checker[A[now]]
        loop_len = count - before_loop_len - 1
        break
    else:
        loop_checker[count - 1] = A[now]
        now = A[now] - 1
        count += 1

if K <= before_loop_len:
    print(loop_checker[K])
else:
    checkK_prep = K - before_loop_len
    checkK = checkK_prep % loop_len
    print(loop_checker[before_loop_len + checkK])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [1]
x = 1

for i in range(n):
  x = a[x-1]
  if not(x in b):
    b.append(x)
  else:
    c = b.index(x)
    break
    
if len(b)=>n:
  print(b[n-1])
else:
  d = (k-c) % (len(b)-c)
  print( b[c+d] )

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [1]
x = 1

for i in range(n):
  x = a[x-1]
  if not(x in b):
    b.append(x)
  else:
    c = b.index(x)
    break
if len(b)-c=>n:
  print(b[n-1])
else:
  d = (k-c) % (len(b)-c)
  print( b[c+d] )

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = map(list(input().split()))         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = [1]                         # 訪れた町を ans_list に格納
while(1):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = []
        #print("ok")

    if k == 0:
        print(int(dum))
        break
    ans_list = ans_list.append(dum)
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
town = []
dic = {}
i = 0
j = 0
while i not in dic and j < K:
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1

if j == K:
    print(town[K] + 1)

else:
    num = dic[i]
    loop = j - num
    print(town[((K - num) % loop) + num] + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
List = list(map(int,input().split()))
Newlist = [1]
a=1
for i in range(N):
    a = List[a-1]
    if a==Newlist[i-1]:
        print(a)
        break
    elif a in Newlist:
        Cicle = Newlist[Newlist.index(a):Newlist[i-1]]
        print(Cicle[(K-i)%len(Cicle)-1])
        break
    else:
        Newlist.append(a)
else:
  print(a)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
town = []
dic = {}
i = 0
j = 0
while i not in dic and j < K:
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1

if j == K:
    print(town[K] + 1)

else:
    num = town.index(i)
    loop = j - num
    print(town[(K - num) % loop + num]+1)

Submission status: Runtime Error

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

a=[]
b=1
for i in range(N):
    if not b in a:
        a[i]=b
        b=List[b-1]
    else:
        c=i
        break
#print(a)
#print(b,c)
num=a.index(b)
#print(T,num)
T=c-num
if K<num:
    print(a[K])
else:
    print(a[num:][(K-num)%T])

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
List = list(map(int,input().split()))
Newlist = [1]
a=1
for i in range(N):
    a = List[a-1]
    if a==Newlist[i-1]:
        print(a)
        break
    elif a in Newlist:
        Cicle = Newlist[Newlist.index(a):Newlist[i-1]]
        print(Cicle[(K-i)%len(Cicle)-1])
        break
    else:
        Newlist.append(a)

Submission status: Runtime Error

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

a=[]

for i in range(N):
    a.append(-1)
print(a)
b=1
for i in range(N):
    if not b in a:
        a[i]=b
        b=List[b-1]
    else:
        c=i
        break
print(a)
print(b,c)
num=a.index(b)
print(T,num)
T=c-num
if K<num:
    print(a[K])
else:
    print(a[num:][(K-num)%T])

Submission status: Runtime Error

Solution code:
nk = input(' ')
n = int(nk.split()[0])
k = int(nk.split()[1])
alist = list(map(int,input('').split()))

loop_start=0
loop_end=0
root = []
next_val=1
for i in range(k):
    a = next_val
    next_val = alist[a-1]
    root.append(a)
    #print("{}: {}->{} [{}]".format(i,a,next,root))
    
    if next_val in root:
        loop_start=root.index(next_val)
        loop_end=len(root)
        break

#print("{} {}->{}".format(root,loop_start,loop_end))

step=k
if loop_end!= loop_start:
    step=(k-loop_start)%(loop_end-loop_start)
#print("step:({}-{})%({} - {})={}".format(k,loop_start,loop_end,loop_start,step))
#print("root[{}+{}]={}".format(loop_start,step,root[loop_start+step]))
if k < loop_start:
    print(root[k])
else:
    print(root[loop_start+step])

Submission status: Runtime Error

Solution code:
nk = input(' ')
n = int(nk.split()[0])
k = int(nk.split()[1])
alist = list(map(int,input('').split()))

loop_start=0
loop_end=0
root = []
next=1
for i in range(k):
    a = next
    next = alist[a-1]
    root.append(a)
    #print("{}: {}->{} [{}]".format(i,a,next,root))
    if next in root:
        loop_start=root.index(next)
        loop_end=len(root)
        break

#print("{} {}->{}".format(root,loop_start,loop_end))

step=(k-loop_start)%(loop_end-loop_start)
#print("step:({}-{})%({} - {})={}".format(k,loop_start,loop_end,loop_start,step))
#print("root[{}+{}]={}".format(loop_start,step,root[loop_start+step]))
if k < loop_start:
    print(root[k])
else:
    print(root[loop_start+step])

Submission status: Runtime Error

Solution code:
nk = input(' ')
n = int(nk.split()[0])
k = int(nk.split()[1])
alist = list(map(int,input('').split()))

loop_start=0
loop_end=0
root = []
next=1
for i in range(k):
    a = next
    next = alist[a-1]
    root.append(a)
#    print("{}: {}->{} [{}]".format(i,a,next,root))
    if next in root:
        loop_start=root.index(next)
        loop_end=len(root)
        break

#print("{} {}->{}".format(root,loop_start,loop_end))

step=(k-loop_start)%(loop_end-loop_start)
#print("step:({}-{})%({} - {})={}".format(k,loop_start,loop_end,loop_start,step))
#print("root[{}+{}]={}".format(loop_start,step,root[loop_start+step]))
if k < loop_start:
    print(root[k])
else:
    print(root[loop_start+step])

Submission status: Runtime Error

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

now = 0
a = [_-1 for _ in list(map(int,input().split()))]

past = []
offset = 0
cnt = 0
idx = 0
fl = False
for i in range(k):
  if fl:
    break
  past.append(now)
  now = a[now]
  try:
    idx = past.index(now)
    cnt = i-idx+1
    fl = True
    break
  except:
    pass

now = 0
for i in range(k % cnt):
  now = a[now]

print(offset + now+1)

Submission status: Runtime Error

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

now = 0
a = [_-1 for _ in list(map(int,input().split()))]

past = []
offset = 0
cnt = 0
idx = 0
for i in range(k):
    past.append(now)
    now = a[now]
    try:
      idx = past.index(now)
      cnt = i-idx+1
      break
    except:
      pass

now = 0
for i in range(k % cnt):
  now = a[now]

print(offset + now+1)

Submission status: Runtime Error

Solution code:
def isInt(x):
    try:
        float(x)
    except ValueError:
        return False
    else:
        return float(x).is_integer()

def main():
    try:
        str = input().split()
    except ValueError:
        return

    if isInt(str[0]):
        N = int(str[0])
    else:
        return

    if not( 2 <= N and N <= 2*10**5):
        return

    if isInt(str[1]):
        K = int(str[1])
    else:
        return

    if not( 1 <= K and K <= 10**18):
        return

    try:
        str = input().split()
    except ValueError:
        return

    A = [0] * N
    for i in range(N):
        if isInt(str[i]):
            ai = int(str[i])
            if 1 <= ai and ai <= N:
                A[i] = ai
            else:
                return
        else:
            return
        
    now = 1
    path = [1]
    for i in range(K):
        now = A[now-1]
        if now in path:
            bunki = path.index(now)
            break
        else:
            path.append(now)
            
    r = (K-bunki) % (len(path)-bunki)
    now = path[bunki + r]
    print(now)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def isInt(x):
    try:
        float(x)
    except ValueError:
        return False
    else:
        return float(x).is_integer()

def main():
    try:
        str = input().split()
    except ValueError:
        return

    if isInt(str[0]):
        N = int(str[0])
    else:
        return

    if not( 2 <= N and N <= 2*10**5):
        return

    if isInt(str[1]):
        K = int(str[1])
    else:
        return

    if not( 1 <= K and K <= 10**18):
        return

    try:
        str = input().split()
    except ValueError:
        return

    A = [0] * N
    for i in range(N):
        if isInt(str[i]):
            ai = int(str[i])
            if 1 <= ai and ai <= N:
                A[i] = ai
            else:
                return
        else:
            return
        
    now = 1
    path = [1]
    for i in range(K):
        now = A[now-1]
        if now in path:
            bunki = path.index(now)
            break
        else:
            path.append(now)
            
    r = (K-bunki) % (len(path)-2)
    now = path[bunki + r]
    print(now)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [int(input()) for _ in range(n)]

def dfs(i,pl):
    if i == k:
        return pl
    return dfs(i+1,a[pl-1])
print(dfs(0,1))

Submission status: Runtime Error

Solution code:
def dfs(i,pl):
    if i == k:
        return pl
    return dfs(i+1,a[pl-1])
print(dfs(0,1))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
order = [-1] * (n+1)
p = [0] * (n+1)
p[0] = 1
order[p[0]] = 0
for i in range (1, n+1):
    order[i] = A[p[i-1] - 1]
    if order[i] != -1:
        mae = order[p[i]]
        q = i - mae
        break
    order[i] = i
if k < mae:
    print(p[k])
else:
    print(p[k - mae % q + mae])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

    R = [False] * (N + 1)
    D = []

    p = 1

    while True:
        if R[p]:
            break
        D.append(p)
        R[p] = True
        p = A[p - 1]

    m = D.index(p)
    c = len(D) - m

    if K <= m:
        print(D[K])
    else:
        print(D[m + K - m] % c)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
change = list(input().split())
town = 1
j = 1
for i in range(k):
    j = town
    town = change[j - 1]
print(town)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
townchange = list(input().split())
town = 1
j = 1
for i in range(k):
    j = town
    town = townchange[j]
print(town)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
townchange = list(input().split())
town = 1
for i in range(k):
    j = town
    town = townchange[j]
print(town)

Submission status: Runtime Error

Solution code:
6 727202214173249351
6 5 2 5 3 2

Submission status: Runtime Error

Solution code:
import sys
N,K=map(int,(input().split()))
A=list(map(int,input().split()))
A.insert(0,0)
loc=1
log=[0]
loop_flg=0
i=1
while(i<=K):
  if(loc not in log):
    # print("ログに"+str(loc)+"を追加します")
    log.append(loc)
    # print(log)
    loc=A[loc]
    i+=1
  elif(loc in log and loop_flg==0):
    # print("ループを"+str(log.index(loc))+"番目から"+str(i-1)+"番目の間に発見しました")
    loop_longth=i-log.index(loc)
    K = ((K-(log.index(loc)-1))%loop_longth)
    if(K==0):
      K=i-1
    loc=log[K]
    print(log[loc])
    sys.exit()
  # print(str(loc)+" "+str(i)+" "+str(K))
print(loc)

Submission status: Runtime Error

Solution code:
import sys
N,K=map(int,(input().split()))
A=list(map(int,input().split()))
A.insert(0,0)
loc=1
log=[0]
loop_flg=0
i=1
while(i<=K):
  if(loc not in log):
    # print("ログに"+str(loc)+"を追加します")
    log.append(loc)
    # print(log)
    loc=A[loc]
    i+=1
  elif(loc in log and loop_flg==0):
    # print("ループを"+str(log.index(loc))+"番目から"+str(i-1)+"番目の間に発見しました")
    loop_longth=i-log.index(loc)
    K = ((K-(log.index(loc)-1))%loop_longth)
    # print(K)
    # print(log.index(loc)-1)
    loc=log[(log.index(loc)-1)+((K-log.index(loc))%loop_longth)]
    print(log[log[K]])
    sys.exit()
  # print(str(loc)+" "+str(i)+" "+str(K))
print(loc)

Submission status: Runtime Error

Solution code:
import sys
N,K=map(int,(input().split()))
A=list(map(int,input().split()))
A.insert(0,0)
loc=1
log=[0]
loop_flg=0
i=1
while(i<=K):
  if(loc not in log):
    # print("ログに"+str(loc)+"を追加します")
    log.append(loc)
    # print(log)
    loc=A[loc]
    i+=1
  elif(loc in log and loop_flg==0):
    # print("ループを"+str(log.index(loc))+"番目から"+str(i-1)+"番目の間に発見しました")
    loop_longth=i-log.index(loc)
    K = i+((K-(log.index(loc)-1))%loop_longth)
    # print(K)
    # print(log.index(loc)-1)
    loc=log[(log.index(loc)-1)+((K-log.index(loc))%loop_longth)]
    print(log[loc])
    sys.exit()
  # print(str(loc)+" "+str(i)+" "+str(K))
print(loc)