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: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
ai = list(map(int, input().split()))
for i in range(N):
    ai[i] -= 1
    
import math
dv = []
dv.append(ai)
for k in range(1, int(math.log2(K)) + 1):
    l = [0] * N
    dv.append(l)
    for n in range(N):
        dv[k][n] = dv[k - 1][dv[k - 1][n]]
        
#a = []
now = 0
for i in range(int(math.log2(K)) + 1):
    if K >> i & 1:
        #a.append(i)
        now = dv[i][now]
print(now + 1)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
an = list(map(int, input().split()))
for i in range(n):
    an[i] -= 1
    
import math
dv = []
dv.append(an)
K = k
N = n
for _ in range(1, int(math.log2(K)) + 1):
    l = [0] * N
    dv.append(l)

for k in range(1, int(math.log2(K)) + 1):
    for n in range(N):
        dv[k][n] = dv[k - 1][dv[k - 1][n]]
        
# N 回目を 2 ^ t で表すためにビット演算を用いる
# a : dv の何行目を用いるかを格納
a = []
for i in range(int(math.log2(K)) + 1):
    if K >> i & 1:
        a.append(i)
        
now = 0
for i in a:
    now = dv[i][now]
        
print(now + 1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()

x=0
x_list=[1]

for i in range(k):
  nx=a[x]

  if nx in x_list:
    inx=x_list.index(nx)
    loop=x_list[inx:]
    zan=k-i

    q=zan%len(loop)

    nx=loop[q-1]
    break
  else:
    x=nx-1
    x_list.append(nx)

print(nx)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
k = K.bit_length()
dv = [[0]*N for _ in range(k)]
dv[0] = [a-1 for a in A]
for turn in range(1,k):
    for town in range(N):
        dv[turn][town] = dv[turn-1][dv[turn-1][town]]

a = 0
for i in range(k):
    if (K>>i)&1:
        a = dv[i][a]
        
print(a+1)

Submission status: Time Limit Exceeded

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

next = [[0]*n for _ in range(60)]

for v in range(n):
    next[0][v] = a[v]-1
for d in range(60-1):
    for v in range(n):
        next[d+1][v] = next[d][next[d][v]]
        
v = 0
for d in range(60):
    if k>>d&1:
        v = next[d][v]
        
print(v+1)

Submission status: Time Limit Exceeded

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

memo = [1]
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in set(memo):
        memo.append(to)
        cnt += 1
    else:
        break
        
idx = memo.index(to)
kidx = ((k - len(memo))%(len(memo)-idx))
if cnt ==k:
    print(memo[-1])
else:
    print(memo[idx:][kidx])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
here = 1
b = [1]
for x in range(k):
    here = a[here-1]
    if here not in b:
        b.append(here)
    else:
        here_is = b.index(here)
        c = k - here_is + 1
        cycle = len(b) - here_is
        group = b[here_is:]
        d = c % cycle
        if d != 0:
            here = group[d-1]
        else:
            here = group[-1]
        break




print(here)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [a - 1 for a in A]

log_K = 1
while 1 << log_K < K:
    log_K += 1
# print(f'{log_K=}')
D = [[0] * N for _ in range(log_K)]
for i in range(N):
    D[0][i] = A[i]
for k in range(log_K - 1):
    for i in range(N):
        D[k + 1][i] = D[k][D[k][i]]
# print(f'{D=}')

now = 0
k = 0
while K > 0:
    if K & 1:
        now = D[k][now]
    K = K >> 1
    k += 1
print(now + 1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import numpy as np


# def input():
#     return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())

    warps = list(map(int, input().split()))
    warps = [0] + warps
    warps = np.array(warps, dtype=int)

    dp = np.zeros((k.bit_length() + 1, n + 1), dtype=int)

    dp[0, :] = warps

    for h in range(1, len(dp)):
        for j in range(len(dp[0] + 1)):
            dp[h][j] = dp[h - 1][dp[h - 1][j]]

    node = 1
    # for i in reversed(range(k.bit_length())):
    for i in range(k.bit_length(), -1, -1):
        if k >> i & 1:
            node = dp[i][node]

    print(node)


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import sys


def main():
    D = 60
    n, k = map(int, sys.stdin.buffer.readline().split())

    warps = list(map(int, sys.stdin.buffer.readline().split()))
    warps = [0] + warps

    dp = [[0 for j in range(n + 1)] for i in range(D)]
    # print(warps)
    for j in range(1, n + 1):
        dp[0][j] = warps[j]

    for i in range(D - 1):
        for j in range(1, n + 1):
            dp[i + 1][j] = dp[i][dp[i][j]]

    # for dd in dp:
    # print(dd)

    node = 1
    for i in range(D, -1, -1):
        if k >= (1 << i):
            # print(i)
            k -= 1 << i
            node = dp[i][node]

    print(node)


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


def main():
    D = 60
    n, k = map(int, input().split())

    warps = list(map(int, input().split()))
    warps = [0] + warps

    dp = [[0 for j in range(n + 1)] for i in range(D)]
    # print(warps)
    for j in range(1, n + 1):
        dp[0][j] = warps[j]

    for i in range(D - 1):
        for j in range(1, n + 1):
            dp[i + 1][j] = dp[i][dp[i][j]]

    # for dd in dp:
    # print(dd)

    node = 1
    for i in range(D, -1, -1):
        if k >= (1 << i):
            # print(i)
            k -= 1 << i
            node = dp[i][node]

    print(node)


main()

Submission status: Time Limit Exceeded

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

logk = k.bit_length()
doubling = []
for _ in range(logk):
  tmp = [0] * n
  doubling.append(tmp)

for i in range(n):
  doubling[0][i] = a[i] - 1

for i in range(1,logk):
  for j in range(n):
    doubling[i][j] = doubling[i-1][doubling[i-1][j]]

now = 0
pos = 0
while k:
  if k & 1:
    now = doubling[pos][now]
  k = k >> 1
  pos += 1

print(now+1)

Submission status: Time Limit Exceeded

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

dic = {}
list_town = []
town = 1

for i, v in enumerate(A):
    dic[i+1] = v

for _ in range(K):
    list_town.append(town)
    town = dic[town]
    if town in list_town:
        stop_twon = town
        break

if N <= K:
    list_first_split = list_town[:list_town.index(stop_twon)]
    list_second_split = list_town[list_town.index(stop_twon):]
    print(list_second_split[(K - (len(list_first_split))) % len(list_second_split)])
else:
    print(town)

Submission status: Time Limit Exceeded

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

c = []

for i in range(len(a)):
    if a[i] - 1 != i and (i + 1) in a:
        c.append(a[i])
#print(a)
c = list(set(c))
#print(c)
d = [0] * len(c)
e = []

flag = 0
pos = 1
for i in range(k):
    pos = a[pos - 1]
    if pos in c:
        flag = 1
        for j in range(len(c)):
            if c[j] == pos:
                d[j] = 1
                e.append(pos)
        if sum(d) == len(d):
            #print("loop", i)
            break
    else:
        flag = 0
        d = [0] * len(c)
        e = []
    #print(pos)


last_pos = (k - (i - len(c) + 1)) % len(c)
#print(e, i, last_pos)
print(e[last_pos - 1])

Submission status: Time Limit Exceeded

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


def fib_dynamic(n):
    # 結果を保持する辞書
    cal_result = {}

    # 初期値の設定
    cal_result[0] = 1

    ans = 0
    if n == 0:
        return 1

    for i in range(1,n+1):
        cal_result[i] = a[cal_result[i-1]-1]
        if i == k:
            ans = cal_result[i]

            break
        elif cal_result[i] == cal_result[i-1]:
            ans = cal_result[i]

            break
        elif i>1 and cal_result[i] == cal_result[i-2]:
            if (k-i) % 2 ==0:
                ans = cal_result[i]
            else:
                ans = cal_result[i-1]

            break
        elif i >2 and cal_result[i] == cal_result[i-3]:
            if (k-i)%3==0:
                ans = cal_result[i]
            elif (k-i)%3 ==1:
                ans = cal_result[i-2]
            else:
                ans = cal_result[i-1]
            break

        elif i >3 and cal_result[i] == cal_result[i-4]:
            if (k-i)%4 ==0:
                ans = cal_result[i]
            elif (k-i)%4 ==1:
                ans = cal_result[i-3]
            elif (k-i)%4 ==2:
                ans = cal_result[i-2]
            else:
                ans = cal_result[i-1]
            break

        elif i >4 and cal_result[i] == cal_result[i-5]:
            if (k-i)%5==0:
                ans = cal_result[i]
            elif (k-i)%5==1:
                ans = cal_result[i-4]
            elif (k-i)%5==2:
                ans = cal_result[i-3]

            elif (k-i)%5==3:
                ans = cal_result[i-2]

            elif (k-i)%5==4:
                ans = cal_result[i-1]
            break

    return ans

print(fib_dynamic(k))

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
count = 0
now = 1
city = [1]
while count < K:
    count +=  1
    now = A[now-1]
    if now in city:
        break
    else:
        city.append(now)
if count == K:
    print(now)
else:
    n = city.index(now)
    K -= n
    count -= n
    K %= count
    print(city[n+K])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, K = map(int, input().split())
As = list(map(int, input().split()))

# doubling
m = 0
while 2**m < K:
    m += 1
d = [[0] * n for i in range(m)]
for i in range(n):
    d[0][i] = As[i] - 1 

for k in range(m-1):
    for i in range(n):
        d[k+1][i] = d[k][d[k][i]]

now = 0
for k in range(m):
    if (K >> k) & 1:
        now = d[k][now]

print(now+1)

Submission status: Time Limit Exceeded

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


tel = np.zeros((63, n), dtype=np.int)
tel[0] = a
for i in range(62):
    for j in range(n):
        tel[i + 1][j] = tel[i][tel[i][j]]

now = 0
for i in range(62, -1, -1):
    if k >> i & 1:
        now = tel[i][now]
print(now + 1)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.insert(0,0)
cur = A[1]
T = 1
while cur!=1:
    cur = A[cur]
    T += 1
K = K%T
cnt = 0
while cnt<K:
    cur = A[cur]
    cnt += 1
print(cur)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))
l = []
i=0
while (a[i]-1) not in l:
    l.append(i)
    i = a[i]-1 
l.append(i)

pre = l.index(a[i]-1)  
loop = len(l)-pre
if pre+loop<k:
    k=(k-pre)%loop+pre
print(l[k]+1)

Submission status: Time Limit Exceeded

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

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i >= K:
    print(path[K])
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Submission status: Time Limit Exceeded

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

now = 1
path = [1]

i = 0
while i < N + 2:
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i >= K:
    print(path[K])
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Submission status: Time Limit Exceeded

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

now = 1
path = [1]
cnt = 1


i = 0
while i < min(N + 2, K):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i == K:
    print(now)
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Submission status: Time Limit Exceeded

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

now = 1
path = [1]
cnt = 1


i = 0
while i < min(N + 2, K):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i == K:
    print(now)
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

print(loop[(K - lo_start) % lo_len])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))
l = []
i=0
while (a[i]-1) not in l:
    l.append(i)
    i = a[i]-1 
l.append(i)

pre = l.index(a[i]-1)  
loop = len(l)-pre
if pre+loop<k:
    k=(k-pre)%loop+pre
i = 0
for _ in range(k):
    i = a[i]-1
print(i+1)

Submission status: Time Limit Exceeded

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

cnts = [None] * N

pos = 0
cnt = 0

while cnt < K:
    if cnts[pos] != None:
        loop_size = cnt - cnts[pos]
        cnt += ((K-cnt) // loop_size - 1) * loop_size
        cnts = [None] * N
    cnts[pos] = cnt
    pos = A[pos]
    cnt += 1

print(pos+1)

Submission status: Time Limit Exceeded

Solution code:
def input_li():
    return list(map(int, input().split()))


N, K = input_li()
A_LI = input_li()
"""
ループ回数調べる。
K % (ループ回数) で答え出せそう。
"""
town_list = []      # 通った街を格納する
town = 1
town_list.append(town)
marked_town = 0     # ループの起点となる街
first_cnt = 0
loop_li = []
for i in range(1, N + 1):
    town = A_LI[town - 1]
    if town in town_list:
        town_list.append(town)
        marked_town = town
        first_cnt = i
        break
    town_list.append(town)

if K <= first_cnt:
    print(town_list[K])
    exit()
K -= first_cnt
town = marked_town
loop_li.append(marked_town)
loop_cnt = 0
for i in range(N):
    town = A_LI[town - 1]
    if town == marked_town:
        loop_cnt = i + 1
        break
    loop_li.append(town)
print(loop_li[K % loop_cnt])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    N,K = map(int,input().split())
    A = [0]
    A += list(map(int,input().split()))
    A = np.array(A)
    
    i = 1
    pas = np.zeros(N)
    pas[0] = 1
    k = 1
    while k <= K:
        if A[i] in pas:
            rps = np.where(pas==(A[i]))[0] # roop start index
            ans = A[i]
            break
        pas[k] = A[i]
        ans = A[i]
        i = A[i]
        k += 1

    if k >= K:
        print(ans)
    else:
        rpnum = (K-rps[0])%(k-rps)
        print(int(pas[rps[0]+rpnum]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N,K = map(int,input().split())
    A = [0]
    A += list(map(int,input().split()))

    i = 1
    pas = [0]*N
    pas[0] = 1
    k = 1
    while k <= K:
        if A[i] in pas:
            rps = pas.index(A[i]) # roop start index
            ans = A[i]
            break
        pas[k] = A[i]
        ans = A[i]
        i = A[i]
        k += 1

    if k >= K:
        print(ans)
    else:
        rpnum = (K-rps)%(k-rps)
        print(pas[rps+rpnum])

main()

Submission status: Time Limit Exceeded

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

i = 1
pas = [i]
k = 1
while k <= K:
    if A[i] in pas:
        rps = pas.index(A[i]) # roop start index
        ans = A[i]
        break
    pas.append(A[i])
    ans = A[i]
    i = A[i]
    k += 1
    
if k >= K:
    print(ans)
else:
    rpnum = (K-rps)%(len(pas)-rps) # roop number
    print(pas[rps+rpnum])

Submission status: Time Limit Exceeded

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

i = 1
pas = [i]
k = 1
while k <= K:
    if A[i] in pas:
        rps = pas.index(A[i]) # roop start index
        ans = A[i]
        break
    pas.append(A[i])
    ans = A[i]
    i = A[i]
    k += 1
    
if k >= K:
    print(ans)
else:
    rpnum = (K-rps)%(len(pas)-rps) # roop number
    print(pas[rps+rpnum])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = map(int, input().split())
towns = [0] + list(map(int, input().split()))
checked = [0 for _ in range(N+1)]
next = towns[1]
count = 1
while(True):
  if checked[next] != 0:
    loop = count - checked[next]
    pos = 0
    if K > count:
      pos = checked[next] + (K-checked[next])%loop
    else:
      pos = K
    for i in range(1, N+1):
      if checked[i] == pos:
        print(i)
        exit()
  else:
    checked[next] = count
    count += 1
    next = towns[next]

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    da = [[None]*n for _ in range(64)]
    for i in range(n):
        da[0][i] = a[i]-1
    for i in range(1, 64):
        for j in range(n):
            da[i][j] = da[i-1][da[i-1][j]]
    ans = 0
    for i in range(64):
        p = k&1
        if p == 1:
            ans = da[i][ans]
        k//= 2
    print(ans+1)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.insert(0,0)
b=[1]
i=a[1]
while i!=1:
    b.append(i)
    i=a[i]
if a[1]==1:
    print(1)
else:
    print(b[k%len(b)])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.insert(0,0)
cnt=0
i=1
while cnt!=k:
    i=a[i]
    cnt+=1
print(i)

Submission status: Time Limit Exceeded

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

a = [i-1 for i in a]

p = 0
b = []
while p not in b:
    b.append(p)
    p = a[p]
avant = b.index(p)
loop_count = len(b) - avant
c = b[avant:]

if k <= avant:
    print(b[k]+1)
else:
    k -= avant
    k %= loop_count
    print(c[k]+1)

Submission status: Time Limit Exceeded

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

a = [i-1 for i in a]

p = 0
b = []
while p not in b:
    b.append(p)
    p = a[p]
loop_start = p
avant = b.index(p)
loop_count = len(b) - avant

if k <= avant:
    x = 0
else:
    k -= avant
    k %= loop_count
    x = loop_start

for _ in range(k):
    x = a[x]
print(x+1)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
dst = [1]
ans = 1
for _ in range(k):
    if a[ans - 1] not in set(dst):
        dst.append(a[ans - 1])
        ans = a[ans - 1]
    else:
        index = dst.index(a[ans - 1])
        ld = len(dst[:index])
        cyc = len(dst[index:])
        print(dst[(ld - 1) + (k + 1 - ld) % cyc] if (k + 1 - ld) % cyc != 0 else dst[-1])
        exit()
print(dst[-1])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
dst = [1]
ans = 1
for _ in range(k):
    if a[ans - 1] not in dst:
        dst.append(a[ans - 1])
        ans = a[ans - 1]
    else:
        index = dst.index(a[ans - 1])
        ld = len(dst[:index])
        cyc = len(dst[index:])
        print(dst[(ld - 1) + (k + 1 - ld) % cyc] if (k + 1 - ld) % cyc != 0 else dst[-1])
        exit()
print(dst[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

visited=[True]*N
visitednum=[]
now=0
cnt=0
while True:
    if visited[now]==False:
        break
    else:
        visited[now]=False
        visitednum.append(now)
        now=A[now]
        cnt+=1
loop=[]
while True:
    if now in loop:
        break
    loop.append(now)
    now=A[now]
if K<cnt:
    print(visitednum[K]+1)
else:
    t=(K-(len(visitednum)+len(loop)))%len(loop)
    print(loop[t]+1)

Submission status: Time Limit Exceeded

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

b=0
c=1

for _ in range(K):
    b=A[c-1]
    c=A[c-1]


print(b)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    A = [int(x) - 1 for x in input().split()]

    way = []
    now = 0
    for _ in range(K):
        if len(way) != len(set(way)):
            loop_start = way.index(now)
            loop = way[loop_start:]
            K -= len(way[:loop_start])
            now = loop[K % len(loop)]
            break
        way.append(now)
        now = A[now]
    print(now + 1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    A = [int(x) - 1 for x in input().split()]

    way = []
    now = 0
    for _ in range(K):
        if now in way:
            loop_start = way.index(now)
            loop = way[loop_start:]
            K -= len(way[:loop_start])
            now = loop[K % len(loop)]
            break
        way.append(now)
        now = A[now]
    print(now + 1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    A = [0] + [int(x) for x in input().split()]

    way = []
    now = 1
    for _ in range(K):
        if now in way:
            loop_start = way.index(now)
            K -= len(way[:loop_start])
            loop = way[loop_start:]
            K %= len(loop)
            now = loop[K]
            break
        way.append(now)
        now = A[now]
    print(now)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    A = [int(x) - 1 for x in input().split()]

    way = []
    now = 0
    for _ in range(K):
        if now in way:
            loop_start = way.index(now)
            K -= len(way[:loop_start])
            K %= len(way[loop_start:])
            for k in range(K):
                now = A[now]
            break
        way.append(now)
        now = A[now]
    print(now + 1)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n,k = map(int,input().split())
tele = {}
a = list(map(int,input().split()))
for i in range(1,n+1):
    tele[i] = a[i-1]
lis = deque([1])
now = 1
n = 1
while tele[now] not in lis:
    lis.append(tele[now])
    now = tele[now]
p = lis.index(tele[now])
q = len(lis) - p
if k <= p:
    print(lis[k])
else:
    print(lis[(k-p)%q + p])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
tele = {}
a = list(map(int,input().split()))
for i in range(1,n+1):
    tele[i] = a[i-1]
lis=[1]
now = 1
n = 1
while tele[now] not in lis:
    lis.append(tele[now])
    now = tele[now]
p = lis.index(tele[now])
q = len(lis) - p
if k <= p:
    print(lis[k])
else:
    print(lis[(k-p)%q + p])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
now = 0
visited = [now]
while A[now] not in visited:
    visited.append(A[now])
    now = A[now]
L = len(visited)
looplen = L-visited.index(A[now])
if K < L:
    print(visited[K]+1)
else:
    print(visited[L-looplen+(K-L)%looplen]+1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

table = np.zeros((64, n), dtype=np.int64)
table[0] = a

for i in range(61):
    for j in range(n):
        table[i + 1][j] = table[i][table[i][j]]

ans = 0
for i in range(61, -1, -1):
    if k >> i & 1:
        ans = table[i][ans]
print(ans + 1)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
now = 0
visited = [now]
while A[now] not in visited:
    visited.append(A[now])
    now = A[now]
looplen = len(visited)-visited.index(A[now])
if K < len(visited):
    print(visited[K]+1)
else:
    print(visited[visited.index(A[now])+(K-len(visited))%looplen]+1)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.insert(0,0)
B = [1]
x = A[1]
while x!=1:
    B.append(x)
    x = A[x]
T = len(B)
k = K%T
print(B[k])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
x = 0
y = 1
while x != k and b.count(y) != 2:
    x += 1
    y = a[y-1]
    b.append(y)
if x != k:
    x -= 1
    k -= x
    y = b.index(b[-1])
    k %= (x - y)
    c = b[y:x]
    print(c[k-1])
else:
    print(b[-1])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if a[posi] in way:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
if k < len(way):
    print(way[k] + 1)
else:
    print(way[(k-rest) % cycle + rest] + 1)

Submission status: Time Limit Exceeded

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

now=1
visited=[1]

for i in range(k):
  now=a[now-1]
  if now in visited:
    for j in range(1,len(visited)+1):
      if visited[-j]==now:
        t=j
        for i in range(((k-(len(visited)-j))%t)):
          now=a[now-1]
        print(now)
        exit()
  
  visited.append(now)
        
print(now)

Submission status: Time Limit Exceeded

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

now=1
visited=[1]

for i in range(k):
  now=a[now-1]
  if now in visited:
    for j in range(1,len(visited)+1):
      if visited[-j]==now:
        t=j
        for i in range((k-(len(visited)-j)%t)):
          now=a[now-1]
        print(now)
        exit()
  
  visited.append(now)
        
print(now)

Submission status: Time Limit Exceeded

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

town = List[0]
while K-1 > 0:
    town = List[town-1]
    K -= 1
print(town)

Submission status: Time Limit Exceeded

Solution code:
def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    A = [a-1 for a in A]

    L = 65
    D = [[0]*n for _ in range(L+1)]

    for i, a in enumerate(A):
        D[0][i] = a

    for i in range(L):
        for j in range(n):
            D[i+1][j] = D[i][D[i][j]]

    ans = 0
    for i in range(L):
        if k & (1 << i):
            ans = D[i][ans]
    print(ans+1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

L = 65
D = [[0]*n for _ in range(L+1)]

for i, a in enumerate(A):
    D[0][i] = a

for i in range(L):
    for j in range(n):
        D[i+1][j] = D[i][D[i][j]]

ans = 0
for i in range(L):
    if k & (1 << i):
        ans = D[i][ans]
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
def roop(now, a_list):
    return a_list[now-1]

if __name__ == '__main__':
    n, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    
    now = 1
    check = [1]
    for i in range(k):
        now = roop(now, a_list)
        if now not in check:
            check.append(now)
        else: 
            break

    point = (check.index(now))
    roop_len = len(check) - point

    if k <= len(check):
        print(check[k])
    else:
        k_ = k - point
        point_ = k_ % roop_len
        print(check[point+point_])

Submission status: Time Limit Exceeded

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

j=1
step=0
s=set([])
l=[]
loop_s=-1
loop_g=-1

while True:
	s.add(j)
	l+=[j]
	j=a_ls[j-1] #update
	if j in l:
		loop_s=l.index(j)
		loop_g=len(l)
		break


#print(l)
#print(loop_s)
#print(loop_g)

if K<loop_s:
	print(l[K])
elif (K+1-loop_s)%(loop_g-loop_s)!=0:
	print(l[loop_s+(K+1-loop_s)%(loop_g-loop_s)-1])
else:
	print(l[-1])

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")

# k: number of transfer
_,k = LI()
a = LI()
# 街の番号を0始まりに変更
a = list(map(lambda x:x-1,a))
# print(a)

# 再帰関数
def rec(visited,next):
    if next in set(visited):
        return visited,visited.index(next)
    else:
        visited.append(next)
        return rec(visited,a[next])

visited = [0]
# 訪れた街、初めて2度訪れた街のインデックスを取得
visited,loopStart = rec(visited,a[0])

# print(visited,loopStart)

if k <= len(visited):
    townIndex = visited[k]
else:
    visitedLoop = visited[loopStart:]
    # print(visitedLoop)
    # 残り回数を循環配列の長さで割った余り
    index = (k-(loopStart))%len(visitedLoop)
    # print(index)
    townIndex = visitedLoop[index]

print(townIndex+1)