TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: 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.

Submission attempts (in order):
n, k = list(map(int,input().split()))
# a = list(map(int,input().split()))

tmp_city=[]
c=0
count=0
init_city=0

while (1):
    count+=1
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

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

now=1
count=0

cities = [0]

for i in range(1,k+1):
    tmp=a[cities[i-1]-1]
    if tmp in cities[:i]:
        init_city=cities.index(tmp)
        if init_city>k:
            print(cities[k])
        else:
            print(cities[init_city:i][(k-init_city)%(i-init_city)])
        break
    else:
        cities.append(tmp)
else:
    print(now)

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

now=1
count=0

cities = [0]

for i in range(1,k+1):
    tmp=a[cities[i-1]-1]
    if tmp in cities[:i]:
        init_city=cities.index(tmp)
        if init_city>k:
            print(cities[k])
        else:
            print(cities[init_city:i][(k-init_city)%(i-init_city)])
        break
    else:
        cities.append(tmp)
else:
    print(cities[k])

n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
#167
#d
n,k = map(int, input().split())
ln = list(map(int, input().split()))

# n,k = 10*4,10**18
# ln = list(range(2,n+1))
# ln.append(1)

visit = [0]*n 
now= 1 
before =[] 
roop =[] 
while visit[now -1] != 2:
    if visit[now -1] ==  0:
        before.append(now)
    elif visit [now-1] == 1:
        roop.append(now)
    visit[now -1]+= 1
    now = ln[now-1]
if k < len(before):
    print(before[k])
else :
    print(roop[((k-len(before))%len(roop)) -1])
    
    

#167
#d
n,k = map(int, input().split())
ln = list(map(int, input().split()))

# n,k = 10*4,10**18
# ln = list(range(2,n+1))
# ln.append(1)

visit = [0]*n 
now= 1 
before =[] 
roop =[] 
while visit[now -1] != 2:
    if visit[now -1] ==  0:
        before.append(now)
    elif visit [now-1] == 1:
        roop.append(now)
    
    visit[now -1]+= 1
    now = ln[now-1]


if k < len(before):
    print(before[k])
else :
    print(roop[((k-len(before))%len(roop)) ])
    

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n, k = map(int, input().split())

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

visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1
    if is_visited[now-1] == 0:
        is_visited[now-1] = 1
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)


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

visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1

    if is_visited[now-1] == 0:
        is_visited[now-1] = 1
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)


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

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

visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1
    if is_visited[now-1] == 0:
        is_visited[now-1] = 1
        visited.append(now)
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)


Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N,K = map(int,input().split())
A = list(map(int,input().split()))

pos = 0
visited = [0]*N
move = []
loop = []

while visited[pos]!=2:
  if visited[pos] ==0:
    move.append(pos)
  else:
    loop.append(pos)
  visited[pos] += 1
  pos = A[pos]-1

if len(move)>k:
  print(move[k]+1)
else:
  print(roop[(K-(len(move)-len(loop)))%len(loop)]+1)

N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())

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

cnt = [0 for _ in range(N)]
cnt[0] += 1

warp_rireki= [A[0]]
roop = []
# c = 0
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    warp_rireki.append(A[warp-1])
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = sum(cnt)-len(roop)
# print("root", roop)
# print("one", one_root)
if K < len(warp_rireki):
    print(warp_rireki[K])
else:
    print(roop[(K-one_root)%len(roop)-1])

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

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

cnt = [0 for _ in range(N)]
cnt[0] += 1

warp_rireki= [A[0]]
roop = []
# c = 0
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    warp_rireki.append(A[warp-1])
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = cnt.count(1)
# print("root", roop)
# print("one", one_root)
if K < len(warp_rireki):
    print(warp_rireki[K])
print(roop[(K-one_root)%len(roop)-1])

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

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

cnt = [0 for _ in range(N)]
cnt[0] += 1

warp_rireki= [A[0]]
roop = []
# c = 0
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    warp_rireki.append(A[warp-1])
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = cnt.count(1)
# print("root", roop)
# print("one", one_root)
if K < len(warp_rireki):
    print(warp_rireki[K])
else:
    print(roop[(K-one_root)%len(roop)-1])

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

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

ord = [-1 for _ in range(N)]
ord[0] = 0
v = A[0]-1
root = [0]

while ord[v] == -1:
    
    ord[v] = len(root)
        
    root.append(v)

    
    v = A[v]-1

# print(ord)
# print(root)



l = ord[v]

c = len(root) - l

if K < l:
    
    ans = root[K]
else:
    
    
    K -= l
    
    K %= c
    
    ans = root[l+K]
print(ans+1)



Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
#-------------------------------------------------------------------
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)

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

print(i)	

#-------------------------------------------------------------------
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)	

#-------------------------------------------------------------------
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(K):
	x,y = M[i]
	
	if x==0:
		M[i][0] = A[i]
		M[i][1] = k
		i = A[i]
		continue
	
	
	a = K - y
	b = a % (k-y)
	for _ in range(b):
		i = A[i]
	
	p(x,y,k)
	p("M[1:]", M[1:])
	
	break

ans = i
print(ans)



Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
tmp = input().split()
N = int(tmp[0])
K = int(tmp[1])

tmp = input().split()

Map = [0]*N
K = K%N
rt = [0]*K

for i in range(N):
    Map[i] = int(tmp[i])

def trip(self, steps):
    ntown = Map[self-1]
    if steps == K:
        return ntown
    rt[steps]=trip(ntown,steps+1)


    
Ft = trip(1,1)
print(rt[K-1])


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

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

Visited = [-1]*N
now = 1
Order = [0]*N

loopcount = 1
Order[0] = 1
Visited[0] = 1
for i in range(1,N):
    nt = Map[now-1]
    Order[i] = nt
    loopcount += 1
    if Visited[nt-1] == -1:
        Visited[nt-1] = loopcount
    else:
        break
    now = nt
    

ns = Visited[nt-1]
period = loopcount - ns

if period == 0:
    period = loopcount
    ns = 0

Kmod = K - ns
if Kmod > 0:
    Kmod %= period

print(Order[ns+Kmod])




Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
def main():
    N, K, *A = map(int, open(0).read().split())
    visited = [0]*N
    pos = A[0]
    visited[0] = visited[pos - 1] = 1
    while visited[pos - 1] < 2:
        pos = A[pos - 1]
        visited[pos - 1] += 1
    # print(pos)
    begin = pos
    step_to_begin = 0
    if begin != 0:
        pos = 0
        while pos != begin:
            pos = A[pos - 1]
            step_to_begin += 1
        step_to_begin += 1
    # print(begin, step_to_begin)

    if K <= step_to_begin:
        ans = 0
        for i in range(K):
            ans = A[ans - 1]
        print(ans)
        return
    order = 1
    pos = step_to_begin
    while A[pos - 1] != step_to_begin:
        pos = A[pos - 1]
        order += 1
    # print(order)
    remain = (K - step_to_begin) % order
    ans = step_to_begin
    for i in range(remain):
        ans = A[ans - 1]
    print(ans)

if __name__ == '__main__':
  main()


N,K,*A=map(int,open(0).read().split())
a=1
while K>0:
 if K&1:a=A[a-1]
 A=[A[A[i]-1] for i in range(N)]
 K>>=1
print(a)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n = input().split()
N = int(n[0])
K = int(n[1])

lista = []
listb = [1]
C = 1
A = list(map(int, input().split()))

for i in range(N):
    lista.append(int(A[i]))
                 
for j in range(20000000):
    C = lista[C-1]
    if C not in listb:
        listb.append(C)
    else:
        break
                 
for i in range(len(listb)):
    if listb[i] == C:
        D = C
        I = i

if K <= N:
    print(C)
elif K > N and C == N:
    print(listb[K % N -1])
else:
    if C-D == 1 or C-D == 0:
        print(C)
    else:
        M = (K-I) % (C-D)
        print(listb[I+M-1])

n = input().split()
N = int(n[0])
K = int(n[1])

listb = [1]
listc = [0]*N
C = 1
A = list(map(int, input().split()))
                 
for i in range(N):
    C = A[C-1]
    if listc[C-1] == 0:
        listb.append(C)
        listc[C-1] = 1
    else:
        D = listb.index(C)
        listb.append(C)
        break

L = len(listb) -1

if K <= N:
    print(listb[K])
else:
    if L-D == 1 or L-D == 0:
        print(C)
    else:
        M = (K-D) % (L-D)
        print(listb[(D+M)%L])

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k = map(int,input().split())
an = list(map(int, input().split()))
copyan = an[:]
i = copyan[0]
copyan[0]=10**6
movecount = 1
roopstart = 0
if i != 1:
    while True:
        memo = i
        i = copyan[i-1]
        copyan[memo-1]=10**6 + movecount
        
        
        if i >=30000:
            yokei = i%(10**6)
            
            break
        movecount += 1
        roopstart = i
else:
    print()
roopnum = movecount - yokei
x = (k-yokei)%roopnum 

count = 0
iti = an[roopstart]
while True:
    
    if x == 0:
        print(roopstart)
        exit()
    else:
        iti = an[iti-1]
        
        count += 1
        if count == x:
            print(iti+1)
            exit()









n,k = map(int,input().split())
an = list(map(int, input().split()))
copyan = an[:]
i = copyan[0]
copyan[0]=10**6
movecount = 1

if i != 1:
    while True:
        memo = i
        if k == movecount:
            print(i)
            
            exit()
        i = copyan[i-1]
        
        if i >=300000:
            yokei = i%(10**6)         
            break
        copyan[memo-1]=10**6 + movecount
        movecount += 1
        
else:
    print(1)
    exit()
roopnum = movecount - yokei
x = (k-yokei)%roopnum

ans = copyan.index(10**6+yokei+x) +1
print(ans)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
print(kari)

N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[1]           
seen={}
k=1
for i in range (K):
  if not k in seen.keys():
      seen[k]=i
      k=A[k-1]
      l.append(k)
  else:
    r_start=seen[k]
    roop=i-r_start
    K=(K-r_start)%roop+r_start
    break
ans=l[K]
print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n, k = map(int, input().split())
a = list(map(int, input().split()))

k += 1
pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[last[pos]:]

preCircle -= cnt

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)



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

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.insert(0, i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[:len(hist) - last[pos]]

preCircle -= cnt

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)



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

pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0

while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

cycle = hist[last[pos]:]
preCircle = last[pos]

if (k <= preCircle):
    print(hist[k] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)



Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
print(A)
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        # loop length
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        # print(current, tmp)
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)

N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        # loop length
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
seq=list(map(int,input().split()))
v=[False]*n
path=[]
i=seq.index(1)
while True:
        if v[seq[i]-1]==False:
                v[seq[i]-1]=True
                path.append(seq[i])
                i=seq[i]-1
                
        else:
                break
print(path[(k%len(path))])
print(path)
                


n,k=map(int,input().split())
seq=list(map(int,input().split()))
v=[False]*n
path=[1]
i=0
v[0]=True
while True:
        if v[seq[i]-1]==False:
                v[seq[i]-1]=True
                path.append(seq[i])
                i=seq[i]-1
                
        else:
                t=seq[i]
                break

ind=path.index(t)
path2=path[ind:]
path1=path[:ind]
x=len(path1)
if k>=x:
        k-=x
        #print(t,ind,path1,path2)
        print(path2[(k%len(path2))])

else:
        print(path1[k])
        


                


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
from collections import defaultdict
import math


def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def main():
    N, M, K = map(int, input().split())
    const = 998244353

    def hoge(k):
        return comb(N - 1, k) * M * ((M - 1) ** (N - k - 1))

    ans = sum([hoge(k) for k in range(K + 1)])

    print(ans % const)


if __name__ == '__main__':
    main()


import math
from collections import defaultdict


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

    visited = [-1 for _ in range(N)]
    visited[0] = 0

    current = 0
    th = -1
    per_loop = -1
    for i in range(1, N+1):
        current = A[current] - 1

        if visited[current] >= 0:
            
            per_loop = i - visited[current]
            th = visited[current]
            break
        else:
            visited[current] = i

    current = 0
    # print(th, per_loop)

    if K < th:
        num_rep = K
    else:
        num_rep = th + (K - th) % per_loop

    for i in range(num_rep):
        current = A[current] - 1

    print(current + 1)


if __name__ == '__main__':
    main()


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):

N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
loop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
    print(visit)
print(move)
print(loop)
if len(move)>K:
    print(move[K]+1)
else:
    print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)


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

visited = [0 for i in range(n+1)]
move_count = 0
position = 1
loop_dis = 0

while True:
    if visited[position] != 0:
        loop_dis = move_count - visited[position] + 1
        visited[position] = move_count
        break
    else:
        move_count += 1
        visited[position] = move_count
        position = towns[position-1]


rest_dis = (k - move_count) % loop_dis

move_count = 0

if rest_dis == 0:
    print(position)
else:
    for i in range(rest_dis):
        move_count += 1
        position = towns[position-1]
        print(position)


N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
loop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):

def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    u = [0]
    f = True
    loop_len = 1
    z = 0
    x = a[0]
    while f:
        if x in s:
            f = False
            z = s.index(x)
            s = s[z:]
            loop_len = len(s)
            
        else:
            s.append(x)
            x = a[x]
    y = (k-z) % loop_len
    print(s)
    print(s[y]+1)

    



if __name__ == "__main__":
    main()


def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    u = [0]
    f = True
    loop_len = 1
    z = 0
    x = a[0]
    while f:
        if x in s:
            f = False
            b=s.index(x)
            s = s[b:]
            loop_len = len(s) - b
            z = s.index(x)
        else:
            s.append(x)
            x = a[x]
    y = (k-z) % loop_len
    print(s)
    print(s[y]+1)

    



if __name__ == "__main__":
    main()

from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    s = []
    ord = [-1]*(n)
    u = 1
    while ord[u] == -1:
        ord[u] = len(s)
        s.append(u)
        u = a[u-1]
    l = len(s) - ord[u]
    if k < ord[u]:
        print(s[k])
    else:
        print(s[(k-ord[u])%l+ord[u]])


if __name__ == "__main__":
    main()

from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    s = []
    ord = [-1]*(n+1)
    u = 1
    while ord[u] == -1:
        ord[u] = len(s)
        s.append(u)
        u = a[u-1]
    l = len(s) - ord[u]
    if k < ord[u]:
        print(s[k])
    else:
        print(s[(k-ord[u])%l+ord[u]])


if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
def search(now, teleporter, limit):
    visit = [-1] * len(teleporter)
    cnt = 0
    
    while visit[now] == -1:
        if limit <= cnt:
            break
        
        visit[now] = cnt
        cnt += 1
        now = teleporter[now]
        
    
    return now, cnt, cnt - visit[now]
    

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

start, initial_cnt, repeat_num = search(0, a, 10**5 + 1)

#print(start, initial_cnt, repeat_num)

if k < initial_cnt:
    r = k
else:
    r = (k - initial_cnt) % repeat_num

#print(r)

ans, _, _ = search(start, a, r)

def search(now, teleporter, limit):
    visit = [-1] * len(teleporter)
    cnt = 0
    
    while visit[now] == -1:
        if limit <= cnt:
            break
        
        visit[now] = cnt
        cnt += 1
        now = teleporter[now]
        
    
    return now, cnt, cnt - visit[now]
    

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

start, initial_cnt, repeat_num = search(0, a, 10**6)

#print(start, initial_cnt, repeat_num)

if k < initial_cnt:
    start = 0
    r = k
else:
    r = (k - initial_cnt) % repeat_num

#print(r)

ans, _, _ = search(start, a, r)

print(ans + 1)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
A = list(map(int, input().split()))
e = [False] * (N+1)
idx = 1
move = []
while True:
  if e[idx]:
    break
  move.append(idx)
  e[idx] = True
  idx = A[idx-1]
m = move.index(idx)
c = len(move) - m

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

N, K = map(int, input().split())
A = list(map(int, input().split()))
e = [False] * (N+1)
idx = 1
move = []
while True:
  if e[idx]:
    break
  move.append(idx)
  e[idx] = True
  idx = A[idx-1]
m = move.index(idx)
c = len(move) - m

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


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
A = list(map(int, input().split()))

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = t_zero.index(t_zero[a] + k)
print(a)

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

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = t_zero.index(t_zero[a-1] + k) + 1
print(a)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import sys

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

roop = 0
queue = [1]

start = -1

for now_town in queue:
    if not town_list[now_town - 1] in queue:
        queue.append(town_list[now_town - 1])

    
    else:
        start = town_list[now_town - 1]
        break

if start == -1:
    print(queue[K])
    sys.exit()

before_len = queue.index(start)
roop = len(queue) - before_len

print(queue[before_len + (K - before_len) % roop])


import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(N):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(queue[now_town])
    sys.exit()

before_len = len(queue) - queue[start] - 1
roop = len(queue) - before_len

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])


import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(K):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(now_town)
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = queue[start]

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])


Verdicts: Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
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    
    key = lst_memo.index(i)
    roop = memo[key:]
    print(roop[(K - key) % len(roop)] + 1)    


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

def main(N, K, A):
    i = 0   
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    
    key = lst_memo.index(i)
    if K < key:
        print(lst_memo[K] + 1)
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    


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

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import collections

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

ex = collections.defaultdict(lambda:-1)
step=1
cur=0
ex[0]=0
while 1:
    if ex[a[cur]]!=-1:
        cycle = step-ex[a[cur]]
        k-=ex[a[cur]]-1
        #print(k%cycle)
        break
    else:
        ex[a[cur]]=step
        cur=a[cur]
    step+=1

print(ex,k%cycle)
for i in range(k%cycle):
    cur = a[cur]
print(cur+1)

import collections
 
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
a = [int(i)-1 for i in a]
 
ex = collections.defaultdict(lambda:-1)
step=1
cur=0
ex[0]=0
while 1:
    if ex[a[cur]]!=-1:
        cycle = step-ex[a[cur]]
        k-=ex[a[cur]]-1
        #print(k%cycle)
        break
    else:
        if step==k:
            print(a[cur]+1)
            exit()
        ex[a[cur]]=step
        cur=a[cur]
    step+=1

#print(ex,k%cycle)
for i in range(k%cycle):
    cur = a[cur]
print(cur+1)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
a=list(map(int,input().split()))
ok=set()
now=0
l=[]
while now not in ok and k:
    l.append(now)
    ok.add(now)
    now=a[now]-1
    k-=1
length=len(ok)-(h:=l.index(now))
l=l[h:]
print(l,h,length)
k=(k)%length

print(l[k]+1)

n,k=map(int,input().split())
a=list(map(int,input().split()))
ok=set()
now=0
l=[]
while now not in ok and k:
    l.append(now)
    ok.add(now)
    now=a[now]-1
    k-=1
if k==0:print(now+1);exit()
length=len(ok)-(h:=l.index(now))
l=l[h:]
print(l,h,length)
k=(k)%length

print(l[k]+1)

def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1
    for d in range(k.bit_length()):
        k,f=divmod(k,2)
        if f:now=l[now-1]
        l=tuple(l[i-1]for i in l)
    print(now)
if __name__ == "__main__":
    f()


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
a=list(map(int,input().split()))
next=a[0]
town=[0]*n
town[0]+=1
for i in range(n):
  if town[next-1]==0:
    town[next-1]+=1
    next=a[next-1]
  else:
    start=next
    break
next=a[start-1]
cnt=1
for i in range(n):
  if a[next-1]!=start:
    next=a[next-1]
    cnt+=1
  else:
    cnt+=1
    break
print(cnt)
k%=cnt
next=a[0]
for i in range(k-1):
  next=a[next-1]
print(next)

n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[0]*n
town[0]+=1
next=a[0]
for i in range(n):
  if town[next-1]==0:
    town[next-1]+=1
    next=a[next-1]
  else:
    break
re=next
next=a[re-1]
t2=0
for i in range(n):
  t2+=1
  if a[next-1]==re:
    t2+=1
    break
  else:
    next=a[next-1]
t1=0
next=a[0]
for i in range(n):
  t1+=1
  if a[next-1]==re:
    t1+=1
    break
  else:
    next=a[next-1]
if k<=t1:
  next=a[0]
  for i in range(k-1):
    next=a[next-1]
  print(next)
else:
  k-=t1
  k%=t2
  next=re
  for i in range(k):
    next=a[next-1]
  print(next)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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:
    print(cnts)
    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)

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)

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-1) // loop_size) * loop_size
        cnts = [None] * N
    cnts[pos] = cnt
    pos = A[pos]
    cnt += 1

print(pos+1)



Verdicts: Runtime Error

Time Limit Exceeded

Accepted
Problem: 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.

Submission attempts (in order):
n,k=(int(i) for i in input().split())
r=[0 for i in range(n)]
l=[int(i) for i in input().split()]
wazap=[]
count=1
saver=None
temp=0
while r[temp]==0:
  r[temp]=count
  wazap.append(temp)
  temp=l[temp]-1
start=wazap[-1]
for i in range(len(wazap)):
  if wazap[i]==start-1:
    left=i
    break
loop=len(wazap)-left
if k<left:
  print(wazap[k]+1)
else:
  print(wazap[(k-left)%loop+left]+1)

n,k=(int(i) for i in input().split())
r=[0 for i in range(n)]
l=[int(i) for i in input().split()]
wazap=[]
count=1
saver=None
temp=0
while r[temp]==0:
  r[temp]=count
  wazap.append(temp)
  temp=l[temp]-1
start=l[wazap[-1]]
left=None
for i in range(len(wazap)):
  if wazap[i]==start-1:
    left=i
    break
loop=len(wazap)-left
if k<left:
  print(wazap[k]+1)
else:
  print(wazap[(k-left)%loop+left]+1)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import numpy as np
N, K = (int(i) for i in input().split())  
A = [int(i) for i in input().split()] 

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

lis = [1]
idx = 0

check_l = [0] * N
for i in range(N):
  lis.append(A[idx])
  if check_l[A[idx]] != 1:
    lis.pop(-1)
    found = lis.index(A[idx])
    lis.append(A[idx])
    break
  check_l[A[idx]] == 1
  idx = A[idx]-1
  
print(lis[found:i+1][(K-found) % (i - found + 1)])

import numpy as np
N, K = (int(i) for i in input().split())  
A = [int(i) - 1 for i in input().split()] 
N = len(A)

def get_keys_from_value(d, val):
    return [k for k, v in d.items() if v == val][0]

now = 0
lis = []
p = {}
for i in range(N):
  now = A[now]
  if now in p:
    break
  
  p[now] = i
  lis.append(now)

if K < i:
  print(lis[K-1] + 1)
else:
  print(lis[(K-i-1) % (i - p[now]) + p[now]] + 1)
    
  

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    visited = [0] * (N+10)
    seq = []

    step = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)+1):
        print(current)
        seq.append(current)
        step += 1
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            print(n1, n2)
            print(n1-1, ((K-(n1-1))%n2))
            print(seq[n1-1+((K-(n1-1))%n2)])
            return
        else:
            visited[next_pos] = step
        current = next_pos

    print(seq[K])

    return


if __name__ == '__main__':
    main()


#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    visited = [0] * (N+1)
    seq = []

    step = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)):
        #print(visited)
        seq.append(current)
        step += 1
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            seq.append(next_pos)
            break
        current = next_pos

    #print(n1, n2)
    
    #print(seq)
    if n1+n2<=K:
        print(seq[n1-1+((K-(n1-1))%n2)])
    else:
        print(seq[K])

    return


if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted