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=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0 
for i in range(k):
  now=a[now]
  if town[now]==1:
    memo=now 
    b=1
    break
  lst.append(now)
  town[now]=1
if b==1:
    i=lst.index(now)　
    l=len(lst)
    if memo!=0:
      lst=lst[i:]
      l-=len(lst)
      print(lst[(k-l)%len(lst)]+1)
    else:
         print(lst[k%l]+1)
else:
  print(now+1)


n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0 
for i in range(k):
  now=a[now]
  if town[now]==1:
    memo=now 
    b=1
    break
  lst.append(now)
  town[now]=1
if b==1:
  i=lst.index(now)　
  l=len(lst)
  if memo!=0:
    lst=lst[i:]
    l-=len(lst)
    print(lst[(k-l)%len(lst)]+1)
  else:
    print(lst[k%l]+1)
else:
  print(now)


n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0 
for i in range(k):
  now=a[now]
  if town[now]==1:
    memo=now 
    b=1
    break
  lst.append(now)
  town[now]=1
if b==1:
  i=lst.index(now)　
  l=len(lst)
  if memo!=0:
    lst=lst[i:]
    l-=len(lst)
    print(lst[(k-l)%len(lst)]+1)


n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0 
for i in range(k):
  now=a[now]
  if town[now]==1:
    memo=now 
    b=1
    break
  lst.append(now)
  town[now]=1
if b==1:
  i=lst.index(now)　
  l=len(lst)
  if memo!=0:
    lst=lst[i:]
    l-=len(lst)
    print(lst[(k-l)%len(lst)]+1)
  else:
    print(lst[k%l]+1)
else:
  print(now)

n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0
for i in range(k):
	now=a[now]
	if town[now]==1:
		memo=now
		b=1
		break
	lst.append(now)
	town[now]=1
if b==1:
	i=lst.index(memo)
	l=len(lst)
	if memo!=0:
		lst=lst[i:]
		l-=len(lst)
		print(lst[(k-l)%len(lst)]+1)
	else:
		print(lst[k%l]+1)
else:
	print(now+1)

Verdicts: Runtime Error

Runtime Error

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

h = set([])

c = 0
for i in range(2 * n):
  c = a[c] - 1
  if n < i:
    print("{} {}".format(i, c))
    if c in h:
      break
    else:
      h.add(c)


c = 0
d = 1
for i in range(n):
  c = a[c] - 1
  print("{} {}".format(i, c))
  if c in h:
    break
  else:
    d += 1

c = 0
if d >= len(a):
  for i in range(a):
    c = a[c] - 1
else:
  for i in range(d + (k % len(h))):
    print("{} {}".format(c, a[c]))
    c = a[c] - 1

print(c + 1)


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

h = set([])

c = 0
for i in range(2 * n):
  c = a[c] - 1
  if n < i:
    if c + 1 in h:
      break
    else:
      h.add(c + 1)

c = 0
d = 0
for i in range(n):
  c = a[c] - 1
  if c + 1 in h:
    break
  else:
    d += 1

c = 0
if d >= k:
  for i in range(k):
    c = a[c] - 1
else:
  for i in range(d + ((k - d) % len(h) + len(h))):
    c = a[c] - 1


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

h = set([])

c = 0
for i in range(2 * n):
  c = a[c] - 1
  if n < i:
    if c + 1 in h:
      break
    else:
      h.add(c + 1)

c = 0
d = 0
for i in range(n):
  c = a[c] - 1
  if c in h:
    break
  else:
    d += 1

c = 0
if d >= k:
  print("sss")
  for i in range(k):
    c = a[c] - 1
else:
  for i in range(d + (k % len(h))):
    print("{} {}".format(c, a[c]))
    c = a[c] - 1

print(c + 1)


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

h = set([])

c = 0
for i in range(2 * n):
  c = a[c] - 1
  if n < i:
    if c + 1 in h:
      break
    else:
      h.add(c + 1)

c = 0
d = 0
for i in range(n):
  c = a[c] - 1
  if c + 1 in h:
    break
  else:
    d += 1

c = 0
if d >= k:
  for i in range(k):
    c = a[c] - 1
else:
  for i in range(d + ((k - d) % len(h) + len(h))):
    c = a[c] - 1


print(c + 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):
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] 
f = [0] 
a=0
b = 0
f_l=0
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = i
        #  s_l_city=now_city
        
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)

if k>b:
    # now_city=s_l_city
    for _ in range((k-f_l)%sycle):
        
        now_city = s[now_city-1]
    print((k-f_l)%sycle)
    print(now_city)
else:
    
    now_city=1
    for _ in range(k):
        now_city=s[now_city-1]
    print(now_city)


n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] 
f = [0] 
a=0
b = 0
f_l=0
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = i
        #  s_l_city=now_city
         print("ループの最初の数字",now_city,"最初は"+str(f_l)+"回目","周期は",sycle)
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)

if k>b:
    # now_city=s_l_city
    for _ in range((k-f_l)%sycle):
        
        now_city = s[now_city-1]
    print((k-f_l)%sycle)
    print(now_city)
else:
    print("ループなし")
    now_city=1
    for _ in range(k):
        now_city=s[now_city-1]
    print(now_city)

n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] 
f = [0] 
a=0
b = 0
f_l=0
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = i
        #  s_l_city=now_city
        
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)

if k>b:
    # now_city=s_l_city
    for _ in range((k-f_l)%sycle):
        
        now_city = s[now_city-1]
    # print((k-f_l)%sycle)
    print(now_city)
else:
    
    now_city=1
    for _ in range(k):
        now_city=s[now_city-1]
    print(now_city)



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):
import sys

lines = sys.stdin.readlines()
N, M, X = map(int, lines[0].split())
A = []
C = []

for i in range(N):
    line = list(map(int, lines[i+1].split()))
    A.append(line[1:])
    C.append(line[0])

# return []
def full_search():
    costs = []
    for i in range(2**N):
        vals = [0] * M
        cost = 0

        
        for j in range(N):
            
            if ((i >> j) & 1):
                
                
                cost += C[j]

                
                for m in range(M):
                    vals[m] += A[j][m]

        combination_ok = True
        for m in range(M):
            if vals[m] >= X:
                continue
            else:
                combination_ok = False
                break

        if combination_ok:
            costs.append(cost)
    return costs

costs = full_search()
if len(costs) == 0:
    print(-1)
else:
    print(min(costs))


import sys

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

i = 1
l = [1] * N
s = set([1])
n = 1
while True:
    n = A[n-1]
    if n in s:
        l = l[:i]
        mod = i - l.index(n)
        init_len = l.index(n)
        break
    else:
        l[i] = n
        s.add(n)
        i += 1

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])


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()))
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 = 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)

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(l[k])

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):
sys.setrecursionlimit(10**7)

def teleport(city, end, teleportmap, count=1):

    next_city = teleportmap[city]

    if count == end:
        return next_city

    else:
        return teleport(next_city, end, teleportmap, count+1)

n, k = map(int,input().split())
a = list(map(int, input().split()))
teleportmap = dict(zip(range(1, len(a)+1), a))

if k <= n:
    print(teleport(1, n, teleportmap))

else:
    countmap = [0]*n
    countmap[0] = 1

    next_city =1
    for _ in range(1, n+1):
        next_city = a[next_city-1]
        countmap[next_city -1] += 1
        if countmap[next_city -1] == 2:
            start = next_city
            break

    roop = {}
    for j in range(1, n+1):
        roop[start] = teleportmap[start]
        start = roop[start]
    
    num_r = len(roop)

    num_until_r= 0
    for m in teleportmap.keys():
        if m not in roop and countmap[m-1] != 0:
            num_until_r += 1
    
    k_r = k - num_until_r
    num_end = k_r%num_r

    if num_end == 0:
        print(next_city)
        
    else:
        print(teleport(next_city, num_end, roop))

import sys
sys.setrecursionlimit(10**7)

def teleport(city, end, teleportmap, count=1):

    next_city = teleportmap[city]

    if count == end:
        return next_city

    else:
        return teleport(next_city, end, teleportmap, count+1)

n, k = map(int,input().split())
a = list(map(int, input().split()))
teleportmap = dict(zip(range(1, len(a)+1), a))

if k <= n:
    print(teleport(1, k, teleportmap))

else:
    countmap = [0]*n
    countmap[0] = 1

    next_city =1
    for _ in range(1, n+1):
        next_city = a[next_city-1]
        countmap[next_city -1] += 1
        if countmap[next_city -1] == 2:
            start = next_city
            break

    roop = {}
    for j in range(1, n+1):
        roop[start] = teleportmap[start]
        start = roop[start]
    
    num_r = len(roop)

    num_until_r= 0
    for m in teleportmap.keys():
        if m not in roop and countmap[m-1] != 0:
            num_until_r += 1
    
    k_r = k - num_until_r
    num_end = k_r%num_r

    if num_end == 0:
        print(next_city)
        
    else:
        print(teleport(next_city, num_end, roop))
            


    

        


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())
t=list(map(int,input().split()))
g=[1]
for i in range(N):
  gs=set(g)
  if t[g[i]-1] in gs:
    h=g.index(t[g[i]-1])
    s=i+1-h
    break
  g.append(t[g[i]-1])
if K>h:
  K=(K-h)%s+h
print(g[K])
print(s,h)

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

if K < N:
  while i < K:
  point = T_list[point]
  i += 1  

else:
  point = 0
  syuuki = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == 0:
      syuuki = i+1
      break
    i += 1

  second_point = point
  if syuuki != 0:
    L = K % syuuki

  else:
    j = 0
    while j < N:
      point = T_list[point]
      if point == second_point:
        syuuki = j+1
        break
      j += 1
    L = (K-i) % syuuki

  k = 0
  while k < L:
    point = T_list[point]
    k += 1
  
print(point+1)

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

for s_point in range(N):
  point = s_point
  count = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == s_point:
      count = i+1
      break
    i += 1
  count_list.append(count)

print(count_list)

point = 0
while count_list[point] == 0:
  point = T_list[point]
  count += 1

print(count,point)
L = K % count_list[point]
point = 0
i = 0
while i < L:
  point = T_list[point]
  i += 1
print(point+1)

N,K=map(int,input().split())
t=list(map(int,input().split()))
g=[1]
h={1}
for i in range(N-1):
  x=t[g[i]-1]
  g.append(x)
  if x in h:
    break
  h.add(x)
if K>len(g):
  a=g.index(g[-1])
  b=len(g)-a-1
  K=(K-a)%b+a
print(g[K])

Verdicts: Wrong Answer

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):
from bisect import bisect_right
from bisect import bisect_left

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

#n=6
#k=727202214173249351
#a=[6, 5, 2, 5, 3, 2]

ai=1
aj=[]
for i in range(min(n,k)):
    aim=ai
    ai=a[ai-1]
#    print(ai,aj)
    if ai in aj:
#        print(ai,aj)
        ic2=aim
        break
    else:
        aj.append(ai)
        

ic1=bisect_left(a,a[ic2])
icyc=ic2-ic1
    
k2=(k-ic1)%icyc+ic1
ai=1
for i in range(k2):
    ai=a[ai-1]
        
print(ai)


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

#n=6
#k=727202214173249351
#a=[6, 5, 2, 5, 3, 2]

#n=4
#k=5
#a=[3,2,4,1]

if n<k:
    ai=1
    aj=set()
    aj.add(1)
    for i in range(n):
        ai=a[ai-1]
        if ai in aj:
            ic2=i
            ai2=ai
            break
        else:
            aj.add(ai)

    ai=1
    for i in range(n):
        ai=a[ai-1]
        if ai==ai2:
            ic1=i
            break

    icyc=ic2-ic1
    k2=(k-ic1)%icyc+ic1
else:
    k2=k
    
ai=1
for i in range(k2):
    ai=a[ai-1]
        
print(ai)

import sys

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

for i in range(n):
    a[i]-=1
#print(a)

if k<=n:
    b=0
    for i in range(k):
        b=a[b]
    print(b+1)
    sys.exit()
    
b=0
c=[-1]*n
c[0]=0
for i in range(n):
#   print(b,a[b])
   b=a[b]
   if c[b]>=0:
#       print("i:",i)
       break
   else:
       c[b]=i+1

ki=c[b]+1
kc=(i+1)-c[b]
k2=(k-ki)%kc+ki
#print("ki:",ki,"kc:",kc,"k2:",k2)
b=0
for i in range(k2):
    b=a[b]
print(b+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):
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
  B[i]=A[B[i-1]-1]
#  if c==B[i]:break
#  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(B[-1])
#print(B.index(B[N-1]))
#print(d)
#print(B[-1])
#print(B,d)

if (K-d-1)%(N-d-1)==0:print(B[N])
else:print(B[d+1+(K-d-1)%(N-d-1)])


N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
B[0]=1
if B[0] in A:c=B[0]
for i in range(1,N):
  B[i]=A[B[i-1]-1]

d=B.index(B[-1])+1
if K<=N:print(B[K]);exit()
print(B[d-1+(K+1-d)%(N-d)])


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 = list(map(int, input().split()))
t = list(map(int, input().split()))

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val] or val == t[val - 1]:
        break
    visited[val] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(t[val - 1])
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)

# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    print('b', ans)
print(ans)

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val] or val == t[val - 1]:
        break
    visited[val] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)

# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    print('b', ans)
print(ans)

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val - 1] or val == t[val - 1]:
        break
    visited[val - 1] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
# print(hold)
if hold == 0:
    print(val)
    exit(0)
if val == t[val - 1]:
    print(val)
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)

# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    # print('b', ans)
print(ans)

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())
List = list(map(int, input().split()))

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

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

a=[]

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

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

a=[]

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

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

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 get_keys(d, val):
    return [k for k, v in d.items() if v == val]
    
def main():
    read = lambda: sys.stdin.readline().rstrip()
    
    N, K  = map(int,read().split())
    A = list(map(int,read().split()))
    
    g = collections.defaultdict(int)
    for i, val in enumerate(A):
        g[i + 1] = val
        
    v = collections.defaultdict(int)
    cnt = 0
    cur = 1
    p = 0
    mp = 0
    while True:
        if v[cur] > 0:
            p = cnt - v[cur]
            mp = v[cur]
            break
        v[cur] = cnt
        cnt += 1
        cur = g[cur]
    
    print(get_keys(v, (K-mp)%p + mp)[0] if K >= mp else get_keys(v, K)[0])


 
if __name__ == "__main__":
    main()

import sys
import collections
#import cython
def get_keys(d, val):
    return [k for k, v in d.items() if v == val]
    
def main():
    read = lambda: sys.stdin.readline().rstrip()
    
    N, K  = map(int,read().split())
    A = list(map(int,read().split()))
    
    g = collections.defaultdict(int)
    for i, val in enumerate(A):
        g[i + 1] = val
        
    v = collections.defaultdict(int)
    cnt = 0
    cur = 1
    p = 0
    mp = 0
    while True:
        if v[cur] > 0 or cnt > 2*10**5:
            p = cnt - v[cur]
            mp = v[cur]
            break
        v[cur] = cnt
        cnt += 1
        cur = g[cur]
    
    print(get_keys(v, (K-mp)%p + mp)[0] if K >= mp else get_keys(v, K)[0])


 
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=list(map(int,input().split()))
A=list(map(int,input().split()))
p = 1
B = [-1]*N
B[0] = 0
count = 1
while K < count :
	p = A[p-1]
	if B[p-1] != -1:
		break
	B[p-1] = count
	count += 1
if K < count:
	print(p)
	return
K -= count
K = K % (count-B[p-1])
for i in range(K):
	p = A[p-1]
print(p)

	


import sys
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
p = 1
B = [-1]*N
B[0] = 0
count = 1
while K >= count:
	p = A[p-1]
	if B[p-1] != -1:
		break
	B[p-1] = count
	count += 1
if K < count:
	print(p)
	sys.exit()
K -= count
K = K % (count-B[p-1])
for i in range(K):
	p = A[p-1]
print(p)

	


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

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


now = 0
next = -1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = [c[-1]] + c[c.index(next):-1]
        #print(d,e)
        break
    #print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    #print(" ",next)
    now = next

k = k - len(d)
k = k % len(e)
print(e[k])

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

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

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


now = 0
next = a[now] - 1
c = []
check = False
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        print(d,e)
        check = True
        break
    print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    print(" ",next)
    now = next

if check:
    k = k - len(d)
    k = k % len(e)
    print(e[k]+1)

else:
    k = k % len(c)
    print(c[k]+1)
        

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

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

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


now = 0
next = a[now] - 1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        print(d,e)
        break
    print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    print(" ",next)
    now = next

if k >= len(d):
    k = k - len(d)
    k = k % len(e)
    print(e[k]+1)

else:
    print(d[k]+1)
        

# coding: utf-8
# Your code here!
n,k = map(int,input().split())

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

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


now = 0
next = a[now] - 1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        #print(d,e)
        break
    #print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    #print(" ",next)
    now = next

if k >= len(d):
    k = k - len(d)
    k = k % len(e)
    print(e[k]+1)

else:
    print(d[k]+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):
from itertools import combinations
import numpy as np
N, M, X = map(int,input().split())
xx = np.array([X]*M)
C = {}
cs = []
for _ in range(N):
    c, *A = map(int,input().split())
    C[c] = A
    cs.append(c)
ans = float('inf')
for i in range(0, N+1):
    for comb in combinations(cs,i):
        tmp = 0
        x = xx.copy()
        for cos in comb:
            tmp += cos
            x -= np.array(C[cos])
        if len(x[x <= 0]) >= M:ans = min(ans,tmp)
if ans == float('inf'):ans = '-1'
print(ans)

N, K = map(int, input().split())
A = list(map(int, input().split()))
idx = 1
S = [idx]
ed = set([idx])

for i in range(1, K+1):
    idx = A[idx-1]
    if idx in ed:
        start = S.index(idx)
        end = len(S)
        n = start + (K - start) % (end - start)
        print(S[n])
        break
    else:
        S.append(idx)
        ed.add(idx)
        continue
else:
    print(idx)

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())
A = list(map(int, input().split()))


already = [False] * N
step = 0
now = 1
count = 1
while(already[now-1] != True):
    already[now - 1] = True
    now = A[now - 1]
    count += 1
    if count - 1 == K:
      print(now)
      exit(0)



if(now-1 == 0):
    step = 0
    loop = count
else:
    step = 0
    temp = 1
    while(temp != now):
        temp = A[temp - 1]
        step += 1
    cycle = count - step
    loop= (K-step) % cycle
    
temp = 1
for i in range(step + loop -1):
    temp = A[temp - 1]
print(temp)



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


already = [False] * N
step = 0
now = 1
count = 0
while(already[now-1] != True):
    already[now - 1] = True
    now = A[now - 1]
    count += 1
    if count == K: 
      print(now)
      exit(0)






loop = 0
cycle = 0
if(now == 1):
    step = 0
    cycle = count
    loop = K % cycle
else:
    step = 0
    temp = 1
    while(temp != now):
        temp = A[temp - 1]
        step += 1
    cycle = count - step
    loop= (K - step) % cycle

temp = 1
for i in range(step + loop):
    temp = A[temp - 1]
print(temp)

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 = [int(a) for a in input().split()]


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

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


# 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-1) % len(loop_visit)
    # print(c)
    print(loop_visit[c-1])




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

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


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

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


# 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)-1:
    print(first_visit[K])
else:
    l = K - len(first_visit)
    # print(l)
    c = (l) % len(loop_visit)
    # print(c)
    print(loop_visit[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):
import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
    else:
        A2[current_town] = i

print(current_town+1)


import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
#A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
    else:
        A2[current_town] = i

print(current_town+1)


import numpy as np
N, K=map(int,input().split())

A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0
 
 
 
i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - ((K-i) % cycle)
        break
    else:
        A2[current_town] = i
        t=i+1
        i=t

while i<K:
    current_town = A[current_town]
    t=i+1
    i=t

print(current_town+1)

import numpy as np
n, k=map(int,input().split())

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

 
s = list()
ord = np.full(n+1, -1)
c=1
l=0

v=1
while ord[v]==-1:
    ord[v]=len(s)
    s.append(v)
    v=a[v-1]
c=len(s)-ord[v]
l=ord[v]

if k<l:
    print(s[k])  
else:
    k-=l
    k%=c
    print(s[l+k])


Verdicts: Time Limit Exceeded

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())
lst = list(map(int, input().split()))

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break
print(i, now, visited)

k_bk = k - visited[now]
k_bk =  k_bk // i

lft = (k - visited[now]) - i* k_bk

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)

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

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break
        
k_bk = k - visited[now]
k_bk =  k_bk // (i - visited[now])

lft = k - k_bk * (i - visited[now])

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)

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

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break
        
k_bk = k - visited[now]
k_bk =  k_bk // i

lft = (k - visited[now]) - i* k_bk

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)


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

visited = [0] * n
i = 0
now = 0
while k > i:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break

lp = (i + 1) - visited[now]
st = i - lp
k_bk = k - st
k_bk = k_bk // lp
lft = k - (k_bk * lp + st)

if lft == 0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)


Verdicts: Runtime Error

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):
import numpy as np

def main():
    N,K = map(int,input().split())
    A = [0]
    A += list(map(int,input().split()))
    A = np.array(A)
    
    s_time =time.time()
    i = 1
    pas = np.zeros(N)
    pas[0] = i
    k = 1
    pass_set = set(pas)
    while k <= K:
        if A[i] in pass_set:
            rps = np.where(pas==(A[i]))[0]
            ans = A[i]
            break
        pas[k] = A[i]
        pass_set.add(A[i])
        ans = A[i]
        i = A[i]
        k += 1

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

main()

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

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


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

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

def main():
    N,K = map(int,input().split())
    A = [0] + list(map(int,input().split()))
    
    i = 1
    pas = [i]
    pas_set = set(pas)
    k = 1
    while k <= K:
        if A[i] in pas_set:
            rps = pas.index(A[i])
            ans = A[i]
            break
        pas.append(A[i])
        pas_set.add(A[i])
        ans = A[i]
        i = A[i]
        k += 1

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

main()

Verdicts: Runtime Error

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 = [int(x) for x in input().split()]
A = [int(x) - 1 for x in input().split()]

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break

print(T)

idx = (K - st) % (len(T) - st)
print(T[st + idx] + 1)





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

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break

# print(T)


idx = (K - st) % (len(T) - st)

if K > st:
    print(T[st + idx] + 1)
else:
    print(T[K] + 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(' ')))
loc=1
begin=0
end=0
d={1:0}
for i in range(n+1):
    loc=a[loc-1]
    if loc in d.keys():
        begin=d[loc]
        end=i+1
        break
    d[loc]=i+1
if k<=end:
    end=0
if end==begin+1:
    k=begin
    end=0
if end !=0:
    k=(k-begin+1)%(end-begin)+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)

n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
loc=1
begin=0
end=0
d={1:0}
for i in range(n+1):
    loc=a[loc-1]
    if loc in d.keys():
        begin=d[loc]
        end=i+1
        break
    d[loc]=i+1
if k<=end:
    end=0
if end==begin+1:
    k=begin
    end=0
if end != 0:
    x=(k-begin+1)%(end-begin)
    if x==0:
        x=end-begin
    k=x+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)

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(' '))

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start
#print(loop_end-loop_start,loop_start)
print(loop_start,loop_end,log)
if len(log) >= loop:
  if loop_end>K:
    print(now)
  else:
    print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)

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

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

if K<len(log):
  print(log[K]+1)
else:
  print(log[(K-loop_start)%loop+loop_start]+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):
# D

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

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)


if N > K:
    compute(1, K)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i

if loop_end - loop_start == 1:
    return city
div = (K - loop_start) // (loop_end - loop_start + 1)
# print(loop_start, loop_end, div, K % (loop_end - loop_start + 1))
compute(city, K % (loop_end - loop_start + 1) + loop_start + 1)

# D

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

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i


div = (K - loop_start) // (loop_end - loop_start)
if K - loop_start < 0 or div == 0:
    compute(1, K)
else:
    compute(city, K - loop_start - div * (loop_end - loop_start))

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

s=[]
d=[-1]*(n+1)

v=1
while(d[v]==-1):
    d[v]=len(s)
    s.append(v)
    v=a[v-1]
c=len(s)-d[v]
l=d[v]

if(k<l):print(s[k])
else:
    k-=l
    k%=c
    print(k+l)

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

s=[]
d=[-1]*(n+1)

v=1
while(d[v]==-1):
    d[v]=len(s)
    s.append(v)
    v=a[v-1]
c=len(s)-d[v]
l=d[v]

if(k<l):print(s[k])
else:
    k-=l
    k%=c
    print(s[k+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 = list(map(int, input().split()))
A = list(map(int, input().split()))

B = [0] * N
C = 0
D = 0
E = []
for i in range(K):
    if i == 0:
        B[i] += 1
        C = A[0]
        D += 1
        E.append(C)
        #print(i, C)
        
    else:
        if B[C - 1] == 0:
            B[C - 1] += 1
            C = A[C - 1]
            D += 1
            #E.append(C)
            #print(i, C)
            
        else:
            
            #print(D, K % D)
            #print(E)
            print(E[K % D])
            exit()

print(C)

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

mrk = [0] * n
plc = a[0] - 1
rp = 0 
rpc = -1
prc = 0
for i in range(1, k + 1):

    if rpc >= 0:
        rpc += 1
        if plc == rp:

            k = (k - prc) % rpc

            for i in range(k):
                plc = a[plc] - 1

            print(plc + 1)
            exit()
    if rpc == -1:
        prc += 1
    if mrk[plc] == 1 and rpc == -1:
        rp = plc
        rpc = 0
    mrk[plc] += 1
    if i < k:
        plc = a[plc] - 1


print(plc + 1)

Verdicts: Runtime Error

Accepted