Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
# 同じものが出てきた時
for i in range(10**5):
    if i == 0:
        tele = 1
        dp[1] = True
    else:
        tele = A[tele]
        if dp[tele] == False:
            dp[tele] = True
        else:
            finish = i
            tele1 = tele
            break
tele = 0
# 始めてループの数が現れる時
for i in range(10**5):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i
            break
loop = finish-start
if K<start:
    ans = 0
    for i in range(K+1):
        if i==0:
            ans = 1
        else:
            ans = A[ans]
else:
    ans = 0
    for i in range((K-start)%(finish-start)+1):
        if i == 0:
            ans = tele1
        else:
            ans = A[ans]
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
for i in range(10**5):
    if i == 0:
        tele = A[1]
        dp[1] = True
    else:
        tele = A[tele]
        if dp[tele] == False:
            dp[tele] = True
        else:
            finish = i+1
            tele1 = tele
            break
tele = 0
for i in range(10**5):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i+1
            break
ans = A[1]
if K<start:
    for _ in range(K-1):
        ans = A[ans]
else:
    ans = tele1
    for _ in range((K-start)%(finish-start)):
        ans = A[ans]
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
pl=list(map(int,input().split()))
cl=[0]*2*n
res=[]
next=0
for x in range 2*n:
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  if cl[next]==1:
    res.append(next+1)
    cl[next]=2
    next=pl[next]-1
  else:
    lroop=len(res)
    k-=count.cl(1)
    num=k%lroop
    print(res[num-1])
    break
else:
  print(next+1)

Submission status: Runtime Error

Solution code:
import sys
si = sys.stdin.readline


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(1, n+1):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 1, 0
    while k > 0:
        k -= 1
        cnt += 1
        i = l[i-1]
        if i == 1 or trace[i] != 0:
            cy = cnt-trace[i]
            break
        trace[i] = cnt
    k %= cy
    while k > 0:
        i = l[i-1]
        k -= 1
    print(i)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=set()
l.add(1)
t=1
ans=[]
if k==1:
    print(a[0])
    sys.exit()
while k>0:
    tt=a[t-1]
    k-=1
    if k==0:
        print(a[tt-1])
        sys.exit()
    if tt not in l:
        l.add(tt)
        t=tt
    elif tt in l:
        ans.append(tt)
        break
tmp=0
while k>0:
    if a[tt-1] not in ans:
        ans.append(a[tt-1])
        k-=1
        if k==0:
            print(a[tt-1])
            sys.exit()
        tt=a[tt-1]
        tmp+=1
    if a[tt-1] in ans:
        print(ans[k%tmp])
        sys.exit()

Submission status: Runtime Error

Solution code:
import sys
si = sys.stdin.readline


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(n):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 0, 0
    while True and k > 0:
        k -= 1
        cnt += 1
        i = l[i]-1
        if i == 0 or trace[i] != 0:
            cy = cnt-trace[i]
            break
        trace[i] = cnt
    k %= cy
    while k > 0:
        i = l[i]-1
        k -= 1
    print(i+1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF 1001001001
using ll = long long;
using dd = long double;

const int D = 60;
const int MAX_N = 20005;
int to[D][MAX_N];


int main() {
    int n;
    ll k;
    cin >> n >> k;
    rep(i,n) {
        cin >> to[0][i];
        to[0][i]--;
    }

    rep(i,D-1)rep(j,n) {
        to[i+1][j] = to[i][to[i][j]];
    }

    int v = 0;
    for (int i = D-1; i >= 0; --i) {
        ll l = 1ll<<i;
        if (l <= k) {
            v = to[i][v];
            k -= l;
        }
    }
    cout << v+1 << endl;
    
    return 0;
}

Submission status: Runtime Error

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

A.insert(0, 0)
Visited = [0] * (N + 1)
Visited[1] = A[1]
now = 1

while K != 0:
    last = now
    now = A[now] # テレポートを実行
    K -= 1
    if Visited[now] != 0: # テレポート先が訪問済みの場合
        
        K -= 
    else:
        Visited[now] = last

print(route_list)

Submission status: Runtime Error

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

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
for i in range(min(n, k)):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop):
    town = A[town]

print(town+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
isVisit = [False]*n
visits = []
visit = 0
for i in range(n):
    if isVisit[visit]:
        break
    else:
        isVisit[visit] = True
        visits.append(visit)
        visit = A[visit]

if len(visits)>=k:
    print(visits[k]+1)
else:
    k -= visits.index(visit)
    visits = visits[visits.index(visit):]
    print(visits[k%len(visits)]+1)

Submission status: Runtime Error

Solution code:
class UnionFind:
    # この時点でそれぞれのノードは自分を親としている
    # 初期化時に問題が0の頂点を認めるかに注意すること
    def __init__(self, n):
        self.N = n
        self.parent = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]

    # xの根を返す関数
    def root(self, x):
        visited_nodes = []
        while True:
            p = self.parent[x]
            if p == x:
                # 縮約
                for node in visited_nodes:
                    self.parent[node] = x
                return x
            else:
                visited_nodes.append(x)
                x = p

    # 木の結合を行う。親の配下に入る
    def unite(self, x, y):
        if not self.root(x) == self.root(y):
            if self.rank[x] > self.rank[y]:
                self.parent[self.root(y)] = self.root(x)
            else:
                self.parent[self.root(x)] = self.root(y)
                if self.rank[x] == self.rank[y]:
                    self.rank[self.root(y)] += 1

    def ifSame(self, x, y):
        return self.root(x) == self.root(y)

    # 木の根に到達すまでにたどるノードの配列を返す
    def printDebugInfo(self):
        print([self.root(i) for i in range(self.N)])


N, K = map(int, input().split())
A = [int(x) for x in input().split()]
tmp = [0 for _ in range(N)]
# bagに入る要素が多すぎてエラーが出ている

spot = 1
road = []
for _ in range(K):
    if not tmp[spot-1]:
        road.append(spot)
        tmp[spot-1] = 1
        spot = A[spot-1]
    else:
        break

# print(road)
till_loop = road.index(spot)
# print(till_loop)
loop = road[till_loop:]
# print(loop)
cycle_len = len(road) - road.index(spot)
# print(cycle_len)
answer = loop[(K-till_loop) % cycle_len]

print(answer)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
bag = set()

spot = 1
road = []
for _ in range(K):
    if not spot in bag:
        road.append(spot)
        bag.add(spot)
        spot = A[spot-1]
    else:
        break

# print(road)
till_loop = road.index(spot)
# print(till_loop)
loop = road[till_loop:]
# print(loop)
cycle_len = len(road) - road.index(spot)
# print(cycle_len)
answer = loop[(K-till_loop) % cycle_len]

print(answer)

Submission status: Runtime Error

Solution code:
import numpy as np
def mat_pow(a,n):
    cur=a
    ans=np.eye(len(a),dtype='uint64')
    while n>0:
        if n%2==1:
            ans=np.dot(ans,cur)
        cur=np.dot(cur,cur)
        n=n//2
    return ans
n,k=map(int,input().split())
a=list(map(int,input().split()))
A=np.zeros((n,n),dtype='uint64')
for i in range(n):
    A[a[i]-1][i]=1
res=mat_pow(A,k)
for i in range(n):
    if res[i][0]==1:
        ans=i+1
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 20 10:11:22 2020

@author: NEC-PCuser
"""

N, K = map(int, input().split())

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

tele_set = set()
tele_set.add(1)
junkai_town = -1 
junkai_first = -1
junkai_second = -1
for i in range(N):
    if A[i] not in tele_set:
        tele_set.add(A[i])
    else:
        junkai_town = A[i] - 1
        junkai_second = i
        break

town = 0
start_flg = False
junkaimae_list = []
junkai_list = []
for i in range(N):
    if town == junkai_town and start_flg == False:
        junkai_first = i
        start_flg = True
    elif town == junkai_town:
        break
    if start_flg == True:
        junkai_list.append(town)
    else:
        junkaimae_list.append(town)
    town = A[town] - 1

if K <= len(junkaimae_list):
    print(junkaimae_list[K - 1] + 1)
else:
    kaisu = K - len(junkaimae_list)
    print(junkai_list[kaisu % len(junkai_list)] + 1)

Submission status: Runtime Error

Solution code:
import sys
import time
st = time.perf_counter()
# ------------------------------

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break
if K - t > 0:
    amari = (K - t) % circle
    for _ in range(amari):
    now = A[now]
else:
    now = 0
    for i in range(K):
        now = A[now]
print(now + 1)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int, input().split()))
ns = [0] * (n+1)
tree = []

ns = [0] * n
ns[0] = 1
# max 2*10**5
# どこかでループしてる
nexti = 0
starti = 0
endi = 0
tree.append(nexti)

while  k > 0 :
    nexti = a[nexti] - 1
    if ns[nexti] == 1:
        starti = nexti
        break
    ns[nexti] = 1
    endi = nexti
    tree.append(nexti)

x = tree.index(starti) 
# y = tree[starti:]
y = len(tree[x:])

if k - x <= 0:
    print(tree[k-1])
else:
    k = k - x
    z = k % y
    if starti == 0:
        starti = -1
    temp = tree[starti+1:]

    print(temp[z]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int, input().split()))
ns = [0] * (n+1)
tree = []

ns = [0] * n
ns[0] = 1
# max 2*10**5
# どこかでループしてる
nexti = 0
starti = 0
endi = 0
tree.append(nexti)

while  k > 0 :
    nexti = a[nexti] - 1
    if ns[nexti] == 1:
        starti = nexti
        break
    ns[nexti] = 1
    endi = nexti
    tree.append(nexti)

x = tree.index(starti) 
# y = tree[starti:]
y = len(tree[x:])

if k - x <= 0:
    print(tree[k-1])
else:
    k = k - x
    z = k % y
    if starti == 0:
        starti = -1
    temp = tree[starti+1:]

    print(temp[z] + 1,temp,z)

Submission status: Runtime Error

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

p = 0

if n>=k:
    for i in range(k):
        p = a[p]
    print(p+1)

else:
    lst = [-1]*n
    lst[0] = 0
    
    for i in range(1,n+1):
        p = a[p]
        if lst[p] == -1:
            lst[p] = i
        else:
            loop = i - lst[p]
            k -= lst[p]
            extra = k % loop
            break

for i in range(extra):
    p = a[p]
print(p+1)

Submission status: Runtime Error

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

place = 0
memory = [0]

for i in range(n):
    place = a[place]-1
    if not place in memory:
        memory.append(place)
    else:
        if a[place] in memory:
            loopstart = memory.index(a[place])
            k -= loopstart
            extra = k % (len(memory) - loopstart)
            if extra == 0:
                ans = memory[-1]+1
            else:
                ans = memory[place+extra-1]+1
            break
        else:
            ans = a[place]
            break
print(ans)

Submission status: Runtime Error

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

A = [i-1 for i in map(int, input().split())]

used = [False] * N
loop = [0]  # startは0→町1
now = 0  # now+1が町
while not used[A[now]]:
    used[A[now]] = True
    loop.append(A[now])
    now = A[now]

ind = loop.index(A[now])
# new_loop = loop[(befo):]
if K < ind:
    print(loop[K]+1)
else:
    K -= ind
    K %= len[loop]-x

    print(loop[x+K]+1)

# print([i+1 for i in loop])
# print(new_loop[(K-befo) % (len(new_loop))]+1)

Submission status: Runtime Error

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

A = [i for i in map(int, input().split())]
# print(N, K)
# print(A)
loop = [0]  # startは0→町1
now = 0  # now+1が町
for i in range(N)
  now = A[now] - 1
   if now in loop:
        loop.append(now)

        break
    else:
        loop.append(now)

befo = loop.index(now)+1
new_loop = loop[(befo):]

# print([i+1 for i in loop])
print(new_loop[(K-befo) % (len(new_loop))]+1)

Submission status: Runtime Error

Solution code:
def main():
    n, k = map(int, input().split()) 
    al = list(map(int, input().split())) 
    city_l = [1]
    city_s = set([1])
    loop_city_l = []
    curr_city = 1
    cnt = 0
    for i in range(1,k+1):
        next_c = al[curr_city-1]
        if next_c in city_s:
            cnt = i
            first_city = city_l.index(next_c)
            loop_city_l = city_l[first_city:]
            break
        else:
            city_s.add(next_c)
            city_l.append(next_c)
        curr_city = next_c
    else:
        print(curr_city)

    # print(loop_city_l)
    remained = k-cnt
    last_pos = remained%len(loop_city_l)
    print(loop_city_l[last_pos])

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
    print(next+1)
amari=k%n
	ans=li[amari-1]
	print(ans)
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(n+10):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
  amari=k%n
  ans=li[amari-1]
  print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
    print(next+1)
amari=k%n
	ans=li[amari-1]
	print(ans)
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(n+10):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
	amari=k%n
	ans=li[amari-1]
	print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
	amari=k%n
    ans=li[amari-1]
	print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(n+10):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
	amari=k%n
    ans=li[amari-1]
	print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n)
res = []
next = 0
 
for i in range(n):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
	amari=k%n
    ans=li[amari-1]
	print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N, K = list(map(int, input().split()))
a_list = [0] + list(map(int, input().split()))

n = 1
roop = [1]
next_a = a_list[n]

roop_end = False
while next_a not in roop:
  roop.append(next_a)
  next_a = a_list[next_a]
last_a = next_a
roop_len = len(roop) - np.where(np.array(roop)==last_a)[0][0]
roop.pop(0)
K = K - (len(roop) - roop_len)
K = K % roop_len
roop = roop[-roop_len:]

print(roop[K-1])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=0
cnt=0
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
zahyo+=1
hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
    #print(zahyo,kaisu,count,cnt)
print(zahyo+1)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=0
cnt=0
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
zahyo+=1
hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
    print(zahyo,kaisu,count,cnt)
print(zahyo+1)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=1
cnt=0
zahyo=0
kari.add(0)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
kaisu=kaisu//cnt+kaisu%cnt
for i in range(kaisu-1):
    zahyo=A[zahyo]
print(zahyo)

Submission status: Runtime Error

Solution code:
import sys

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

N,K=(int(x) for x in input().split())



A=input().split()
A_int = [int(s) for s in A]
#print("A=" + str(A_int))
check=[]

for i in range(K+1):
  #print("i=:"+str(i))
  if i==0:
    check.append(1)
  elif i==1:
    check.append(A_int[0])
  else:
    check.append(A_int[check[i-1]-1])
    
  if has_duplicates(check)==True:
    break;

#print(check)
#print(has_duplicates(check))
#print(len(check))
range=len(check)-check.index(len(check)-1)
#print("range="+str(range))
head=len(check)-range-1
#print("head="+str(head))

#head+k*range
if K<=head:
  print(check[K-1])
else:
  ans=(K+1-head)%range
  #print("ans="+str(ans))
  if ans==0:
    print(check[len(check)-2])
  else:
    print(check[head+ans-1])

Submission status: Runtime Error

Solution code:
import sys

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

N,K=(int(x) for x in input().split())



A=input().split()
A_int = [int(s) for s in A]
#print("A=" + str(A_int))
check=[]

for i in range(K+1):
  #print("i=:"+str(i))
  if i==0:
    check.append(1)
  elif i==1:
    check.append(A_int[0])
  else:
    check.append(A_int[check[i-1]-1])
    
  if has_duplicates(check)==True:
    break;

#print(check)
#print(has_duplicates(check))
#print(len(check))
range=len(check)-check.index(len(check)-1)
#print("range="+str(range))
head=len(check)-range-1
#print("head="+str(head))

#head+k*range
if K<=head:
  print(check[K-1])
else:
  ans=(K+1-head)%range
  print("ans="+str(ans))
  if ans==0:
    print(check[len(check)-2])
  else:
    print(check[head+ans-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
l = [0 for _ in range(N)]

now = 0
l[now] = 1
count = 0
loop = []

while count < K:
    count += 1
    now = A[now]-1
    loop.append(now)
    if l[now] == 1:
        loop = loop[loop.index(now):-1]
        break
    else:
        l[now] = 1

if count == K:
    print(now + 1)
else:
    ans = loop[K % count]
    print(ans + 1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
i = 1
di = [-1]*10
di[0] = 0
if k >= 10**9+7:
  k%= 10**9+7
while True:
    if i == 1:
        a = ls[0]
        di[a-1] = i
        i += 1
        continue
    if i == k+1:
        print(a)
        break
    a = ls[a-1]
    if di[a-1] != -1:
        p = (k+1-di[a-1])%(i-di[a-1])
        s = i-di[a-1]
        if p ==0:
            q = di.index(i-1)+1
            print(q)
        else:
            q = di.index(i-s-1+p)+1
            print(q)
        break
    di[a-1] = i
    i += 1

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
i = 1
di = [-1]*10
di[0] = 0
while True:
    if i == 1:
        a = ls[0]
        di[a-1] = i
        i += 1
        continue
    if i == k+1:
        print(a)
        break
    a = ls[a-1]
    if di[a-1] != -1:
        p = (k+1-di[a-1])%(i-di[a-1])
        s = i-di[a-1]
        if p ==0:
            q = di.index(i-1)+1
            print(q)
        else:
            q = di.index(i-s-1+p)+1
            print(q)
        break
    di[a-1] = i
    i += 1
print(di)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
i = 1
di = [-1]*10
di[0] = 0
while True:
    if i == 1:
        a = ls[0]
        di[a-1] = i
        i += 1
        continue
    if i == k+1:
        print(a)
        break
    a = ls[a-1]
    if di[a-1] != -1:
        p = (k+1-di[a-1])%(i-di[a-1])
        s = i-di[a-1]
        if p ==0:
            q = di.index(i-1)+1
            print(q)
        else:
            q = di.index(i-s-1+p)+1
            print(q)
        break
    di[a-1] = i
    i += 1

Submission status: Runtime Error

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

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]
    
    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        l2 = l[l.index(num):]
        print(l2[k])

main()

Submission status: Runtime Error

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

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]

    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        print(l2[k])

main()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

# print(towns)

# score = {0: 0}
# current = 0
# move = 0
# while 1:
#   next_town = towns[current]
#   move += 1
#   # print(f"move {current} to {next_town}")
#   if score.get(next_town) is not None:
#     # print(f"find loop!")
#     loop_move = move - score.get(next_town)
#     loop_town = next_town
#     until_loop = score.get(next_town)
#     # print(f"loop start town : ", loop_town)
#     # print(f"cost to start to loop town : ", until_loop)
#     # print(f"loop length : ", loop_move)
#     break
#   score[next_town] = move
#   current = next_town

# if K < until_loop:
#   result = 0
#   for k in range(K):
#     result = towns[result]
# else:
#   result = loop_town
#   for k in range((K - until_loop) % loop_move):
#     result = towns[result]
# print(result + 1)

visited = set([])
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.add(current)

K = K % L
# print(K)
for _ in range(K):
  current = towns[current]
print(current + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

# print(towns)

# score = {0: 0}
# current = 0
# move = 0
# while 1:
#   next_town = towns[current]
#   move += 1
#   # print(f"move {current} to {next_town}")
#   if score.get(next_town) is not None:
#     # print(f"find loop!")
#     loop_move = move - score.get(next_town)
#     loop_town = next_town
#     until_loop = score.get(next_town)
#     # print(f"loop start town : ", loop_town)
#     # print(f"cost to start to loop town : ", until_loop)
#     # print(f"loop length : ", loop_move)
#     break
#   score[next_town] = move
#   current = next_town

# if K < until_loop:
#   result = 0
#   for k in range(K):
#     result = towns[result]
# else:
#   result = loop_town
#   for k in range((K - until_loop) % loop_move):
#     result = towns[result]
# print(result + 1)

visited = set([])
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.add(current)

K = K % L
print(K)
for _ in range(K):
  current = towns[current]
print(current + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

# print(towns)

# score = {0: 0}
# current = 0
# move = 0
# while 1:
#   next_town = towns[current]
#   move += 1
#   # print(f"move {current} to {next_town}")
#   if score.get(next_town) is not None:
#     # print(f"find loop!")
#     loop_move = move - score.get(next_town)
#     loop_town = next_town
#     until_loop = score.get(next_town)
#     # print(f"loop start town : ", loop_town)
#     # print(f"cost to start to loop town : ", until_loop)
#     # print(f"loop length : ", loop_move)
#     break
#   score[next_town] = move
#   current = next_town

# if K < until_loop:
#   result = 0
#   for k in range(K):
#     result = towns[result]
# else:
#   result = loop_town
#   for k in range((K - until_loop) % loop_move):
#     result = towns[result]
# print(result + 1)

visited = []
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.append(current)

K = K % L
for _ in range(K):
  current = towns[current]
print(current + 1)

Submission status: Runtime Error

Solution code:
visited = []
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.append(current)

K = K % L
for _ in range(K):
  current = towns[current]
print(current + 1)

Submission status: Runtime Error

Solution code:
from random import randint
import sys #追加
sys.setrecursionlimit(10**8)
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]
def saiki(B):
    if len(B)==K+1:
        ans.append(B)
        print(B[-1])
        return
    B.append(A[B[-1]-1])
    saiki(B)
saiki([1])
#print(ans)

Submission status: Runtime Error

Solution code:
import sys #追加
sys.setrecursionlimit(500*500)
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]
def saiki(B):
    if len(B)==K+1:
        ans.append(B)
        print(B[-1])
        return
    B.append(A[B[-1]-1])
    saiki(B)
saiki([1])
#print(ans)

Submission status: Runtime Error

Solution code:
from sys import
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]
def saiki(B):
    if len(B)==K+1:
        ans.append(B)
        print(B[-1])
        return
    B.append(A[B[-1]-1])
    saiki(B)
saiki([1])
#print(ans)

Submission status: Runtime Error

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

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

count=0
cycle[0]=count
town=0
mod=0
while True:
    count+=1
    K-=1
    town=A[town]
    if K==0:
        break
    if cycle[town]==-1:
        cycle[town]=count
    else:
        mod=count-cycle[town]
        break

K%=mod
while K>0:
    town=A[town]
    K-=1

print(town+1)