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:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = {}
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.append(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    roop = memo[key:]
    print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Runtime Error

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

for i in range(200000):
    if a[l - 1] in m: #ループが始まったかどうか
        for j in range(len(m)):
            if m[j] == a[l - 1]:
                cnt = j #ループ開始までの端数
                loop = len(m) - cnt #ループの長さ
                break
    else:
        m.append(a[l - 1])
        l = a[l - 1]
        continue
    break


if (k - cnt)%loop == 0:
    l = loop + cnt
else:
    l = (k - cnt)%loop - 1 + cnt

print(m[l])

Submission status: Runtime Error

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1
 
elif N>=K:
    for i in range(K-1):
        c = A[b-1]
        b = c
    ans = b
else:   
    #print("a")
    for i in range(R+1):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i + 1
                #print(loop)
                break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    else:        
        #print(loop)        
        Y = K%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y-1-loop]
 
print(ans)

Submission status: Runtime Error

Solution code:
#D
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
nxt = 1
loop = []
tset = set()
final = -1
while 1:
    loop.append(nxt)
    tset.add(nxt)
    nxt = a[nxt-1]
    if nxt in tset:
        final = nxt #重複した町
        break
m = -1
for i in range(n):
    if loop[i]==final:
        m = i
        break
l = len(a)
if k < m:
    print(loop[m])
else:
    print(loop[(k+1-m)%(l-m)])
# print(loop[(k+1)%l])

Submission status: Runtime Error

Solution code:
n,k = list(map(int,input().split()))
A = list(map(int,input().split()))
tour=[0]
visited=[0]*n
visited[0]=1
next_town = 0
flag=False
for i in range(n):
    next_town=A[next_town]-1
    if i==k:
        flag = True
        ans = next_town
    if visited[next_town]>0:
        first_roop_idx = visited[next_town]
        break
    tour.append(next_town)
    visited[next_town]=i+1
#print(tour)
#print(visited)
#print(first_roop_idx)
if not flag:
    if k<=len(tour):
        ans = tour[k]
    else:
        k-=first_roop_idx
        #print(k)
        k=k%(len(tour)-first_roop_idx)
        #print(k)
        ans = tour[first_roop_idx+k]
print(ans+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A =[0]+list(map(int,input().split()))
l= 1
a = A[1]
C = [1]
d = [0]*(n+1)
d[1] += 1
d[A[1]] +=1
 
while True:
  C.append(a)
  a = A[a]
  l += 1
  d[a] += 1
  if d[a]==2:
    break
  
if k <= l:
  print(C[k])
 
else:
  o = C.index(a)
  p = l - o
  m = (k-o)%p + o
  print(C[m])

Submission status: Runtime Error

Solution code:
import sys
import os

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

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

    d=1
    li=[False]*N
    li[0]=True

    pre_d=0
    while True:
        pre_d=d-1
        d=A[d-1]
        if li[d-1]:
            break
        li[d-1]=True

    # print(pre_d)
    # print(d)
    roop=pre_d-d+1
    print(A[(K-d)%roop+d])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1:
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Submission status: Runtime Error

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

for i in range(10000):
    if a[l - 1] in m: #ループが始まったかどうか
        for j in range(len(m)):
            if m[j] == a[l - 1]:
                cnt = j #ループ開始までの端数
                loop = len(m) - cnt #ループの長さ
                break
    else:
        m.append(a[l - 1])
        l = a[l - 1]
        continue
    break


if (k - cnt)%loop == 0:
    l = loop + cnt
else:
    l = (k - cnt)%loop - 1 + cnt

print(m[l])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))
routs = {}
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i + 1] = tereporter[i]
present_location = 1
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if present_location = 1:
    if n == 0 or K % n == 0:
      break
    for i in range(K % n):
      present_location = routs[present_location]
    break

print(present_location)

Submission status: Runtime Error

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

visited = []
t = None

def rec(A, i):
  global t
  if i in visited:
    t = i
    return
  visited.append(i)
  rec(A, A[i-1])

rec(A, 1)

visited = np.array(visited)
idx = np.where(visited==t)
f1 = idx[0][0]
f2 = len(visited) - f1

roop = (K - f1)//f2

rest = K - (f1 + f2*roop)
#print(rest)
ans = visited[f1 + rest]

#print(f1, f2)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
pasta=[1]
memo=[0]*n
while True:
    if memo[a[i]-1]>0:
        roopy=pasta.index(a[i])
        roop=pasta[roopy:]
        break
    else:
        memo[a[i]-1]+=1
        pasta.append(a[i])
        i=a[i]-1
if k>len(pasta):
    k-=len(pasta)
    k%=len(roop)
    print(roop[k])
else:
    print(pasta[k])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Submission status: Runtime Error

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

otozureta = set([1])
otozureta_list = [1]


def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
#print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + ((K - loop_made_length) % loop_length)])

Submission status: Runtime Error

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

vis = set()
vis.add(1)
cnt = 0
cnt2town = {}
town2cnt = {}
cnt2town[0] = 1
town2cnt[1] = 0

for a in A:
    if a in vis:
        c = town2cnt[a]
        rem = K - cnt
        num_loop = cnt - c

        k = rem % num_loop
        print(cnt2town[c + k + 1])
        exit()

    if cnt == K:
        print(a)
        exit()

    cnt += 1
    cnt2town[cnt] = a
    town2cnt[a] = cnt
    vis.add(a)

Submission status: Runtime Error

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

b = 1

for i in range(n + 1):
    b = a[b]

print(b)

Submission status: Runtime Error

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

visited = [0]
x = 0
for i in range(n):
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  break
  else:
    
mod = (k-first) % T
print(a[first+mod]+1)

Submission status: Runtime Error

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

A_list = [int(a) for a in input().split()]

# print(N, K)
# print(A_list)

keiro = [1]
visit_town = [0]*N

town = 1
c = 1
while True:
    visit_town[town - 1] = c
    c += 1
    town = A_list[town - 1]
    # print('town', town)

    if len(keiro) > 1 and visit_town[town-1] != 0:
        break
    keiro.append(town)

# print(keiro)
# print(visit_town)
# print(town)


first_visit = keiro[:visit_town[A_list[keiro[-1]-1]-1]-1]
loop_visit = keiro[visit_town[A_list[keiro[-1]-1]-1]-1:]

# print(first_visit)
# print(loop_visit)

if K <= len(first_visit):
    print(first_visit[k - 1])
else:
    l = K - len(first_visit)
    # print(l)
    c = (l) % len(loop_visit)
    # print(c)
    print(loop_visit[c])

# print(keiro[len(first_visit) + (K - len(first_visit)) % len(loop_visit)])

Submission status: Runtime Error

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

otozureta = set([1])
otozureta_list = [1]


def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + (K % loop_length) - 1])

Submission status: Runtime Error

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

before = 1
townlist = [1]
flag = 1
for i in range(k):
    town = a[before - 1]
    #print(town)
    if flag and town not in townlist:
        townlist.append(town)
        #print(townlist)
        before = town
    else:
        num = i
        flag = 0
        break

        
if flag :
    print(town)
else:
    ind = townlist.index(town)
    k -= ind
    index = k % (num - ind)
    print(townlist[ind + index - 1])

Submission status: Runtime Error

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

t = [0]*(n+1)
t[1] = 1
q = [1]

for i in range(1,k):
    if i == k:
        print(s[i])
        exit()
    if t[s[i]] == 0:
        t[s[i]] = 1
        q.append(s[i])
    elif t[s[i]] != 0:
        ans = k%(len(q))
        print(q[ans])
        exit()

Submission status: Runtime Error

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

vis = set()
vis.add(1)
cnt = 0
cnt2town = {}
town2cnt = {}
cnt2town[0] = 1
town2cnt[1] = 0

for a in A:
    if a in vis:
        c = town2cnt[a]
        rem = K - cnt
        num_loop = cnt - c

        k = rem % num_loop
        print(cnt2town[c + k + 1])
        exit()

    if cnt == K:
        print(a)
        exit()

    cnt += 1
    cnt2town[cnt] = a
    town2cnt[a] = cnt
    vis.add(a)

Submission status: Runtime Error

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

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1


if i-1>k:
    print(replis[(k-st-len(replis))%len(replis)])
else :
    print(place[k-1])

Submission status: Runtime Error

Solution code:
import numpy as np

n,k = map(int,input().split())
l = list(map(int,input().split()))
s = 0
idx = 0
a = [0]
while(k!=0):
  ns = l[idx]-1
  if ns in a:
    ls = np.where(np.array(a) == ns)[0][0]
    mod = idx-ls
    k = (k-1) % mod
    print(a[ls+k]+1)
    exit(0)
  a.append(ns)
  idx = ns
  k -= 1
print(ns+1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N, K = map(int, input().split())


mylist =  [0] + list(map(int,input().split()))
#print(mylist)

myflag = [0 for n in range(N+1)]
#print(myflag)

myorigine = []
mypos = 1
for n in range(1,N+1):
    if myflag[n] == 0:
        myflag[n] == 1
        myorigine.append(mypos)
        mypos = mylist[mypos]
    elif myflag[n] == 1:
        break
#print(myorigine)
    
mypos = 0
while myorigine[mypos] != myorigine[-1]:
    mypos += 1
#print(mypos)
origin1 = myorigine[:mypos]
origin2 = myorigine[mypos:]
#print(origin1, origin2[:-1])


mymod = K%N
mymod = len(origin1) -1
print(origin2[mymod-1])

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A =[0]
B=list(map(int, input().split()))
for m in range(N):
    A.append(B[m])
place=set([])
i=1
count=0
while i not in place:
    place.add(i)
    i=A[i]
    count+=1
if count>=K:
    print(place[K+1])
else:
    loop=[i]
    start=i
    j=A[i]
    count+=1
    while j!=start:
        loop.append(j)
        j=A[j]
        count+=1
    if count>=K:
        print(loop[K-len(place)])
    else:
        num=(K-count)%len(loop)
        print(loop[num])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
n, k = map(int,input().split())
a_list = list(map(int,input().split()))


visit_route = []#今までの道のり
visited_town = [0]*n#0=初めてきた、1=来たことがある
rooop = 0


def machi(x):
    global visit_route
    global visited_town
    global rooop
    if visited_town[x] == 1:
        rooop = x
        pass
    else:
        visited_town[x] += 1
        visit_route.append(x)
        machi(a_list[x]-1)

machi(0)


st = 0
for i in visit_route:
    st += 1
    if i == rooop:
        start = st
        break
tere = len(visit_route)

if k < tere:
    print(visit_route[k]+1)

elif k == tere:
    print(print(visit_route[start-1])+1)

else:
    loop = tere - start +1

    ans = (k - tere) % loop

    #print(a_list)
    #print(visit_route)
    #print(start,loop,tere)
    print(visit_route[start + ans-1]+1)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

#define pb insert
#define eb emplace_back
#define ff first
#define ss second
typedef long long int ll;
typedef long double ld;

#define all(a) a.begin(), a.end()
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;
typedef map<pp, ll> ipi;
typedef map<pp, char> ipc;
typedef map<ll, ll> ii;
typedef set<ll> sl;
typedef multiset<ll> msl;
typedef map<char, ll> ci;
typedef set<pair<ll, ll>> sp;
const ll mod = 1e9+7;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll a,b;cin>>a>>b;
    vl v(a);
    for(int i=0;i<a;i++){
        cin>>v[i];
    }
    
    ll i=0;
    int f=0;
    ll k=1;
    vl cnt(a+1,0);
    vl ans;
    
    while(i<a){
        ans.eb(k);
        if(cnt[k]) break;
        cnt[k]=i+1;
        k=v[k-1];
        i++;
    }
    ll p=i-cnt[k]+1;
    //cout<<k<<" "<<cnt[k]<<" "<<p<<"\n";
    /*for(auto i:ans) cout<<i<<" ";
    cout<<"\n";
    cout<<p<<" "<<b%p<<"\n";*/
    ll prev=cnt[k]-1;
    
    if(b<p){
        cout<<ans[b]<<"\n";
    }
    else{
        b-=ans.size()-1;
        cout<<ans[b%p+cnt[k]-1]<<"\n";
        
    }
	return 0;

}

Submission status: Runtime Error

Solution code:
##問題4
N,K= input().split()
tereports=list( int(x) for x in input().split())
visit_num=np.zeros(len(tereports)).astype(int)
print(visit_num)
i=0 #counter
townnum= 0
next_townnum=0 # tereports[terenum] = 次の街名
ans=1 #現在のテレポート先
while i< int(K)-1 :
  i+=1
  
  townnum=next_townnum
  
  if visit_num[townnum-1 ]!=0:
    syuuki=i-visit_num[townnum-1]
    nokori=(int(K)-1-i) % (syuuki)

    
    i= int(K)+1-nokori

  visit_num[townnum-1]=i
  next_townnum=tereports[townnum-1]
  
  
print(townnum)

Submission status: Runtime Error

Solution code:
import sys
import copy

n, k = map(int, input().split())

flag = [0 for _ in range(n)]
toNext = [-1 for _ in range(n)]

tele = [int(i) for i in input().split()]

flag[0] = 1
loop = []
i = 0
loop_start = -1
cnt1 = 0
for i in range(len(tele)):
    toNext[i] = tele[i]-1
    cnt1 += 1
    if flag[tele[i]-1] == 1:
        loop_start = tele[i]-1
        break
    loop.append(tele[i] - 1)
    flag[tele[i]-1] = 1

#print("loop:", loop)
#print("loop_start:", loop_start)
if k <= len(loop):
    print(loop[k-1]+1)
    #print("aaaa")
    sys.exit()

if loop_start == 0:
    cut = 0
else:
    idx = 0
    cnt2 = 0
    while True:
        cnt2 += 1
        if toNext[i] == loop_start:
            break
    cut = cnt2 - cnt1
    loop = copy.deepcopy(loop[cut:])

idx = (k-cut)%len(loop)
#print("bbbb")
#print("idx:", idx)
print(loop[idx]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

print(B[k-1])

Submission status: Runtime Error

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

for i in range(10000):
    if a[l - 1] in m: #ループが始まったかどうか
        for j in range(len(m)):
            if m[j] == a[l - 1]:
                cnt = j #ループ開始までの端数
                loop = len(m) - cnt #ループの長さ
                break
    else:
        m.append(a[l - 1])
        l = a[l - 1]
        continue
    break

l = 0 #ループの始まり

l = m[(k - cnt)%(loop) - 1 + cnt]

print(l)

Submission status: Runtime Error

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

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1


if flag[p]-1>k:
    print(replis[(k-st-len(replis))%len(replis)])
else :
    print(place[k-1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
data = [-1]*100000

k -= 1
tmp = lst[0]
for i in range(1, k+1):
  #print(tmp, "t")
  if data[lst[tmp-1]-1] >= 1:
    num = (k - data[lst[tmp-1]-1])%(i-data[lst[tmp-1]-1])
    tmp = lst[tmp-1]
    break
  else:
    data[tmp-1] = i
    tmp = lst[tmp-1]


for j in range(num):
  #print(tmp)
  tmp = lst[tmp-1]
print(tmp)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(set)
def dfsutil(s,a):
    visited[s]=True
    for i in d[s]:
        if visited[i]==False:
            return dfsutil(i,a+1)
    return a,s,i
def dfsutil2(s,k,a):
    visited[s]=True
    if a==k:
        return s
    for i in d[s]:
        if visited[i]==False:
            return dfsutil2(i,k,a+1)
    
visited=defaultdict(bool)

for i in range(len(l)):
    d[i+1].add(l[i])
c=dfsutil(1,1)
# print(c)
if c[1] in d[c[1]]:
    if k>=c[0]:
        print(c[1])
    else:
        visited=defaultdict(bool)
        print(dfsutil2(1,k,0))   
else:
    if k<=c[0]:
        print(dfsutil2(1,k,0))
    if k>c[0]:
        visited=defaultdict(bool)
        c1=dfsutil(c[2],1)
        if c[2]!=1:
            
            k=k-(c[0]-c1[0])
            
        # visited=defaultdict(bool)
        # print(c)
        # print(c1)
        # print(k)
        k=k%c1[0]
        # print(k)
        # print(c1)
        # print(c1)
        
        # print(k)
        visited=defaultdict(bool)
        if k==0:
            print(c[2])
        else:
            print(dfsutil2(c[2],k,0))

Submission status: Runtime Error

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

li = [-1] * n
li[0] = 0
town = [0]
now = 0

for i in range(k):
    now = a[now] - 1
    if li[now] != -1:
        s = li[now]
        break
    else:
        li[now] = i + 1
        town.append(now)

town2 = town[s:]
m = len(town2)
l = len(town) - len(town2)

print(town2[(k - l) % m]+1)

Submission status: Runtime Error

Solution code:
As=input().split()
As=list(map(int,As))
As.insert(0,0)
temp=1
for j in As:
    dic[j]=As[j]

temp=As[1]
count=1
count_=1
lst=[]
while temp not in lst:
    count_+=1
    lst.append(temp)
    temp=dic[temp]
lst.insert(0,1)#代表循环到哪开始

tmp=As[temp]
list1=[temp,tmp]
while tmp!=temp:
    count+=1
    tmp=As[tmp]
    list1.append(tmp)
count_-=(count+1)
#count代表循环一次的个数，count_代表此前有多少次不循环

del list1[-1]
if lst[-1]!=1:
    for i in lst[:]:
        if i not in list1:
            lst.remove(i)
else:
    del lst[-1]
print(lst)

if count_==0:
    ret=lst[(K)%count]
else:
    ret=lst[(K-(count_))%count]

print(ret)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
n, k = map(int,input().split())
a_list = list(map(int,input().split()))


visit_route = []#今までの道のり
visited_town = [0]*n#0=初めてきた、1=来たことがある
rooop = 0


def machi(x):
    global visit_route
    global visited_town
    global rooop
    if visited_town[x] == 1:
        rooop = x
        pass
    else:
        visited_town[x] += 1
        visit_route.append(x)
        machi(a_list[x]-1)

machi(0)


st = 0
for i in visit_route:
    st += 1
    if i == rooop:
        start = st
        break
tere = len(visit_route)

if k < tere:
    print(visit_route[k]+1)

elif k == tere:
    print(print(visit_route[start-1])+1)
    
else:
    loop = tere - start +1

    ans = (k - tere) % loop

    #print(a_list)
    #print(visit_route)
    #print(start,loop,tere)
    print(visit_route[start + ans-1]+1)

Submission status: Runtime Error

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

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1


if flag[p]-1>k:
    print(replis[(k-st-len(replis))%len(replis)])
else :
    print(place[k])

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
sys.setrecursionlimit(410000)

x = inpl()
n = x[0]
k = x[1]
m = [0 for i in range(0, 300000)]
dic = [0 for i in range(0, 300000)]

A = inpl()

t = 0
i = 0
roop = 0
while True:
    if i == k:
        print(t + 1)
        break
    if m[t] == 0:
        m[t] = 1
        dic[t] = i
        t = A[t] -1
    else:
        roop = i - dic[t]
        break
    i += 1

for i in range(0, (k - i) % roop):
    t = A[t] - 1

print(t + 1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]

l = [int(i) for i in input().split()]
a = set()
flag = False
s = 0
while(l[s] - 1 not in a):
	if l[s] == 1:
		flag = True
		s = l[s] - 1
		a.add(s)
		break
	s = l[s] - 1
	a.add(s)
	print(l[s], a)
if flag:
	print(l[k % len(a)])
else:
	print(l[(k - 1) % len(a)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
now=1
s=set()
jnbn=[]
for x in range(n):
  now=l[now-1]
  s.add(now)
  jnbn.append(now)
l=len(s)
#繰り返しの暫定長さを求めた
if 1 in s:
  an=k%l
  print(jnbn[an-1])
else:
  nl=l-1
  an=k%nl
  jnbn.pop(0)
  print(jnbn[an-2])

Submission status: Runtime Error

Solution code:
import sys

n,k = (int(i) for i in input().split())

a = [int(i)-1 for i in input().split()]

hantei = [True]*(n+1)
otozure = [None]*(n+1)
mokuteki = a[0]
moto = k
kazoe = 0
otozure[0] = 0
while k>0:
        
    if hantei[mokuteki] == False: #二週目突入
       # print("break\nmokuteki= {}\nk = {}".format(mokuteki,k))
        otozure[kazoe+1] = mokuteki
        saigo = otozure
        break

    else:
        hantei[mokuteki]= False
        otozure[kazoe+1] = mokuteki#今いるところを記憶
       # print(otozure[kazoe])
        mokuteki = a[mokuteki]#次に行くところ
        kazoe+=1
    k-=1
else:
    print(otozure[kazoe+1]+1)
    sys.exit()
i = 0
while True:
    if otozure[kazoe+1] == otozure[kazoe-i]:
        break
    i+=1


nokori = k%(i+1)
#print(nokori)
i=0
while otozure[i] != mokuteki:
    i+=1

if a[0]==0:
    print("1")
else:
    print(otozure[i-1+nokori]+1)

Submission status: Runtime Error

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
N,K = map(int, input().split())		# 5 7 2
A = [0] + list( map(int, input().split()) )	# 1 2 3 4 5 ...


M = [ [0,0] for _ in range(N+1) ]			#=> [[], [], [], [], [], [], [], []]

p(M)

i = 1
x = y = 0
for k in range(1, K+1):
	x,y = M[i]
	
	if x==0:
		M[i][0] = A[i]
		M[i][1] = k
		i = A[i]
	
	else:
		
		break

else:
	ans = i
	print(ans)
	exit(0)


p(x,y,k)
p("M[1:]", M[1:])

k = k - y

j = k % (k-y)

for a in range(j):
	x,y = M[i]
	M[i][0] = A[i]
	M[i][1] = k
	i = A[i]

print(i)

Submission status: Runtime Error

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

a=1
Ans1=[]
Ans2=0

for k in range(K):
    a=A[a-1]
    if a in Ans1 :
        Ans4=a
        break
    else:
        if a==1:
            Ans1.append(a)
            Ans4=1
            break
        else:
            Ans1.append(a)

Ans5=Ans1.index(Ans4)

Ans3=[]
if Ans4==1:
    for num in range(len(Ans1)):
        Ans3.append(Ans1[num])
else:
    for num in range(Ans5,len(Ans1)):
        Ans3.append(Ans1[num])

if K < len(Ans1) :
    print(Ans1[K-1])
else:
    if len(Ans3)==0:
        K1=K//len(Ans1)
        K2=K-K1*len(Ans1)
        print(Ans1[K2-1])
    else:
        if Ans4==1:
            K2=K//len(Ans3)
            K3=K1-len(Ans3)*K2
            print(Ans3[K3-1])
        else:
            K1=K-Ans5
            K2=K1//len(Ans3)
            K3=K1-len(Ans3)*K2
            if K3==0:
                print(Ans3[len(Ans3)])
            else:
                print(Ans3[K3-1])

Submission status: Runtime Error

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

zyun = [a[0]]
alr = [False]*n
alr[0] = True
crr = a[0]

key = True

while key:
    if not alr[crr - 1]:
        alr[crr - 1] = True
        crr = a[crr - 1]
        zyun.append(crr)
        continue
    else:
        index1 = crr
        key = False

p = zyun[index1]
# print(zyun, p, zyun.index(p))

if zyun.index(p) - 1 == 0:
    zyun1 = zyun
    zyun2 = zyun
else:
    zyun1 = zyun[:zyun.index(p)]
    zyun2 = zyun[zyun.index(p):]

print(zyun1, zyun2)

if len(zyun1) >= k:
    ind = len(zyun1) % k
    if ind == 0:
        print(zyun1[-1])
    else:
        print(zyun1[ind - 1])
else:
    ind = (k - len(zyun1)) % len(zyun2)
    if ind == 0:
        print(zyun2[-1])
    else:
        print(zyun2[ind - 1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
s = set(i for i in range(2,n+1))
q = [1]
a = l[0]
key = -1

while True:
    if a in s:
        q.append(a)
        s.remove(a)
    else:
        key = a
        break
    a = l[a-1]
    
print(q)
print(key)
a = (q.index(key))
print(a)
if a >= k:
    print(q[k-1])
else:
    b = ((k-a) % key) + a
    print(q[b-1])

Submission status: Runtime Error

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, K = map(int, input().split())
    A_i = list(map(int, input().split()))
    B_i = [0 for i in range(N)]
    cycle_list = []
    
    cycle = 0
    diff = 0
    counter = 0
    machi = 1
    while counter < N:
        if B_i[machi] == 1:
            diff = cycle_list.index(machi)
            cycle = counter - diff
            cycle_list = cycle_list[diff:]
            break
        else:
            counter += 1
            B_i[machi] = 1
            cycle_list.append(machi)
            machi = A_i[machi - 1]
            
    mod_K = (K - diff) % cycle

    print(cycle_list[mod_K])

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
Town = map(int, input().split())
seq = map(int, input().split())
list3=[1]
for y in range(list(Town)[1]):
    for z in list3:
        list3.append(list(seq)[z-1])
print(list3)

Submission status: Runtime Error

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

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [0]*n
seen[0] = 1
#load = []
res = 0
flag = 0
for i in range(k-1):
    #print(seen)
    #print(nxt)
    if not(nxt in seen and i > n):
        seen[i+1] = nxt
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
def tele(i):
    ans = A[i-1]
    return ans
i = tele(1)
if N >= K:
    for j in range(K-1):
        i = tele(i)
        print(i)
else:
    l = [i]
    for j in range(N-1):
        i = tele(i)
        if i not in l:
            l.append(i)
    else:
        a = l.index(i)
        loop = len(l) - a
        ans = (K - a) % loop
        q = l[a + ans -2]
    print(q)