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):
from collections import deque

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

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

    loop=[]

    slack_que=deque([])
    flag=0
    t=0
    start=0
    counter=0
    while flag==0:
        slack_que.append(t)
        t=A[t]
        A[t]=-1
        if t==-1:
            start=counter
            flag=1
        counter+=1
    slack=list(slack_que)
    slack_num=start
    loop_num=len(slack)-slack_num

    # print(slack)
    # print(start)
    # print(slack_num)
    # print(loop_num)
    #
    loop=slack[start:]
    # print(loop)
    if (K-slack_num)<0:
        res=slack[K]+1
    else:
        tele_num=((K-slack_num)%loop_num)
        town=loop[tele_num]
        res=town+1

    print(res)

if __name__=="__main__":
    main()



from collections import deque

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

    A=list(map(lambda x:int(x)-1,input().split()))
    FootPrintA=A[:]

    loop=[]

    slack_que=deque([])
    flag=0
    t=0
    start=0
    while flag==0:
        slack_que.append(t)
        t_ = FootPrintA[t]
        FootPrintA[t]=-1
        if t_==-1:
            start=slack_que.index(t)
            flag=1
            slack_que.pop()
        t = A[t]

    slack=list(slack_que)
    slack_num=start
    loop_num=len(slack)-slack_num

    # print(slack)
    # print(start)
    # print(slack_num)
    # print(loop_num)
    #
    loop=slack[start:]
    # print(loop)
    if (K-slack_num)<0:
        res=slack[K]+1
    else:
        tele_num=((K-slack_num)%loop_num)
        town=loop[tele_num]
        res=town+1

    print(res)

if __name__=="__main__":
    main()



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):
from collections import deque

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

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
  
temp=1
trace=deque()
trace.append(1)
#trace=[1]
circle=[]
for i in range(K):
  temp=A[temp-1]
  ans=func1(trace,temp)
  if len(ans)==1:
    #print(temp)
    #print(trace.index(temp))
    #start=trace.index(temp)
    for _ in range(ans[0]):
      trace.popleft()
    #circle=trace[ans[0]:]
    circle.append(1)
    #print(circle)
    break
  trace.append(temp)
  #print("temp",temp)

if len(circle)!=0:
  amari=(K-i)%len(trace)
  print(trace[amari])
else:
  print(temp)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
  
temp=1
trace=[1]
circle=[]
for i in range(K):
  temp=A[temp-1]
  ans=func1(trace,temp)
  if len(ans)==1:
    circle.append(i-ans[0]+1)
    #print(circle)
    break
  trace.append(temp)
  #print("temp",temp)

if len(circle)!=0:
  amari=(K-i)%circle[0]+1
  for j in range(i+amari):
    temp=A[temp-1]

print(temp)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
check=[-1 for i in range(N)]
  
temp=1
circle=[]
for i in range(K):
  temp=A[temp-1]
  if check[temp-1]==-1:
    check[temp-1]=i
  else:
    circle.append(i-check[temp-1])
    #print(circle)
    break
  #print("temp",temp)

if len(circle)!=0:
  amari=(K-i)%circle[0]
  temp=1
  for j in range(i+amari):
    temp=A[temp-1] 

print(temp)

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(int, input().split()))
log = []
visited = [False for i in range(n)]
now = a[0]-1
loop = False
for i in range(k-1):
    if visited[now] == True:
        loop = True
        break
    log.append(now)
    visited[now] = True
    now = a[now]-1
else:
    print(now+1)
if loop:
    for i in range(len(log)):
        if log[i] == now:
            first = len(log[:i])
            log_list = log[i:]
            loop_num = len(log_list)
            break
    print(log)
    k -= first
    k %= loop_num
    print(log_list[k-1]+1)


n, k = map(int, input().split())
a = list(map(int, input().split()))
log = []
visited = [False for i in range(n)]
now = a[0]-1
loop = False
for i in range(k-1):
    if visited[now] == True:
        loop = True
        break
    log.append(now)
    visited[now] = True
    now = a[now]-1
else:
    print(now+1)
if loop:
    for i in range(len(log)):
        if log[i] == now:
            first = len(log[:i])
            log_list = log[i:]
            loop_num = len(log_list)
            break
    k -= first
    k %= loop_num
    print(log_list[k-1]+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):
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

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

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

A = [A[i]-1 for i in range(len(A))]

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2
print(tmp, count2)

if tmp >= K:
    ans = 0
    for _ in range(K):
        ans = A[ans]
else:
    e = (K - tmp) % count2 + tmp
    ans = 0
    for _ in range(e):
        ans = A[ans]

print(ans+1)


from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

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

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

A = [A[i]-1 for i in range(len(A))]

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2

if tmp >= K:
    ans = 0
    for _ in range(K):
        ans = A[ans]
else:
    e = (K - tmp) % count2 + tmp
    ans = 0
    for _ in range(e):
        ans = A[ans]

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

leg = [ 0 ] * n
leg[ 0 ] = 1

p = 0
city_old = 1
for x in range( k ):
    city_new = a[ city_old-1 ]
    if leg[ city_new-1 ] == 0:
        leg[ city_new-1 ] += x + 1
    else:
        loop = x + 1 - leg[ city_new-1 ]
        modk = ( k - ( x + 1 ) ) % loop
        break
    city_old = city_new
    p += 1

if p == k:
    print( city_old )
    exit()


city_old = city_new
for x in range( modk ):
    city_new = a[ city_old-1 ]
    city_old = city_new

print( city_old )


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

leg = [ 0 ] * n
leg[ 0 ] = 0

p = 0
city_old = 1
for x in range( k ):
    city_new = a[ city_old-1 ]
    if leg[ city_new-1 ] == 0 and city_new != 1:
        leg[ city_new-1 ] = x + 1
    else:
        loop = x + 1 - leg[ city_new-1 ]
        modk = ( k - ( x + 1 ) ) % loop
#        print( loop, modk )
        break
    city_old = city_new
    p += 1

if p == k:
    print( city_old )
    exit()


city_old = city_new
for x in range( modk ):
    city_new = a[ city_old-1 ]
    city_old = city_new

print( city_old )


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()))
way = A[0]
List = [0 for n in range(N)]
List[way-1] += 1
count = 0
string = f"{way}"
#print(f"string:{string}")
#print(f"way:{way}")

for i in range(10**7):
    way = A[way-1]
    List[way-1] += 1 
    count += 1
    #print(f"way:{way}")
    #print(f"count:{count}")
    #print(List)
    if max(List) == 3:
        break
    else:
        string += f"{way}"
        #print(f"string:{string}")
        #print()

#print(List)
#print()
new_list = [i for i in List if i == 1]
length = len(new_list)
n_list = [j for j in List if j >= 2]
#print(new_list)
#print(n_list)
amari = (K - length)% len(n_list)

print(int(n_list[amari]))

N, K = map(int, input().split())
A = list(map(int, input().split()))
way = A[0]
List = [0 for n in range(N)]
List[way-1] += 1
count = 0
string = f"{way}"
#print(f"string:{string}")
#print(f"way:{way}")

for i in range(10**7):
    way = A[way-1]
    List[way-1] += 1 
    count += 1
    #print(f"way:{way}")
    #print(f"count:{count}")
    #print(List)
    if max(List) == 3:
        break
    else:
        string += f"{way}"
        #print(f"string:{string}")
        #print()

#print(List)
#print()
new_list = [i for i in List if i == 1]
length = len(new_list)
n_list = [j for j in List if j >= 2]
#print(new_list)
#print(n_list)
amari = (K - length)% len(n_list)

print(int(n_list[amari+1]))


N, K = map(int, input().split())
A = list(map(int, input().split()))# 0index
visi = [-1 for i in range(N)]
L = [1] 
visi[0] = 0
way = A[0] 
#l = []
count = 1

while visi[way-1] == -1: 
    visi[way-1] = count
    count += 1
    L.append(way)
    way = A[way-1]

l = count - visi[way-1]
l_pre = visi[way-1]
loop_L = L[l_pre:]



if K > l_pre:
    index = (K- l_pre)% l
    ans = loop_L[index]
else:
    ans = L[K]
print(ans)

Verdicts: Wrong Answer

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

memo = []
p = 1
vis = ['.']*n

while vis[p-1] == '.':
    memo.appened(p)
    vis[p-1] = 'v'
    p = alist[p-1]

loopstart = alist.index(p)
if k < len(alist):
    print(memo[k])
else:
    print(memo[loopstart+(k-loopstart)%(len(alist)-loopstart)])


from collections import deque
n,k = map(int,input().split())
alist = list(map(int,input().split()))


flag = ['.']*(n+1)
flag[1] = 'v'

been = [1]

for i in range(k):
    goto = alist[been[-1]-1]
    if flag[goto] == '.':
        been.append(goto)
        flag[goto] = 'v'
    else:
        loopstart = goto
        break
loop = been[been.index(loopstart):]
print(loop[(k-loopstart)%len(loop)])



n,k=list(map(int,input().split()))
m=list(map(int,input().split()))
a=[]
p=1
vis=[0]*n
while vis[p-1]==0:
    a.append(p)
    vis[p-1]=1
    p=m[p-1]

l=a.index(p)
print(k,l,a)
if k<len(a):
    print(a[k])
else:
    k-=l
    print(a[l+k%(len(a)-l)])


n,k = map(int,input().split())
alist = list(map(int,input().split()))
memo = []
p = 1
vis = ['.']*n
while vis[p-1] == '.':
    memo.append(p)
    vis[p-1] = 'v'
    p = alist[p-1]

loop = memo.index(p)
if k < len(memo):
    print(memo[k])
else:
    print(memo[loop+(k-loop)%(len(memo)-loop)])


Verdicts: Runtime Error

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 = [0]+list(map(int,input().split()))
visited = [0]*(n+1)
turn = [0]*(n+1)
visited[1] = 0
nxt = 1
i = 1
while True:
  if visited[a[nxt]]:
    nxt2 = a[nxt]
    break
  turn[a[nxt]] = i
  visited[nxt] = 1
  i += 1
  nxt = a[nxt]



loop = i-turn[nxt2]
if turn[nxt2] == 0:
  loop = i
  start = 0
start = turn[nxt2]
#print(turn,i,visited)
#print(start,loop)
if start == 0 and k % loop == 0:
  print(1)
  quit()

n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
visited = [0]*(n+1)
turn = [0]*(n+1)
visited[1] = 0
nxt = 1
i = 1
while True:
  if visited[a[nxt]]:
    nxt2 = a[nxt]
    break
  turn[a[nxt]] = i
  visited[nxt] = 1
  i += 1
  nxt = a[nxt]

loop = i-turn[nxt2]
if turn[nxt2] == 0:
  loop = i
  start = 0
start = turn[nxt2]
#print(turn,i)
#print(start,loop)
if start == 0 and k % loop == 0:
  print(1)
  quit()
if k < i:
  print(turn.index(k))
else:
  print(turn.index(((k-start)%loop)+start))

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

s = 1
place = [s]
first = set(place)

for i in range(n):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place(ans))
        break
    else:
        place.append(s)
        first.add(s)
else:
    print(s)

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

s = 1
place = [s]
first = set(place)

for i in range(1, k+1):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place[ans])
        exit()
    else:
        place.append(s)
        first.add(s)

print(s)

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

l=K%N
ans=1
for i in range(l):
  ans=A[ans-1]
  
print(ans)

N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
point=1
m,l=0,0
if K>N:
    for i in range(N):
        if B[point-1]==0:
            B[point-1]=i+1
            point=A[point-1]
        else:
            m=i+1-B[point-1]
            l=B[point-1]
            break
    if m!=0:
        m=(K-l)%m
    else:
        m=N
else:
    m=K
ans=1
for i in range(l+m):
    ans=A[ans-1]

print(ans)

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):
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A_cnt = [0] * N
    telepo = [1]
    i = 0
    A_cnt[i] = 1
    roop = 0
    while True:
        if A_cnt[i] > 1:
            roop = telepo.index(A[i]) - 1
            break
        A_cnt[A[i] - 1] += 1
        telepo.append(A[i])
        i = A[i] - 1
    K -= roop
    K %= len(telepo) - roop

    print(telepo[K + roop - 1])


if __name__ == "__main__":
    main()


#!/usr/bin/env python3

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    Acnt = [0] * N
    Acnt[0] = 1
    telepo = [1]
    i = 0
    while True:
        Acnt[A[i] - 1] += 1
        if Acnt[A[i] - 1] > 1:
            break
        telepo.append(A[i])
        i = A[i] - 1
    roop_s = telepo.index(A[i])
    roop = len(telepo) - roop_s

    ans = 0
    if K < roop_s:
        ans = K
    else:
        ans = K - roop_s
        ans %= roop
        ans += roop_s

    print(telepo[ans])


if __name__ == "__main__":
    main()


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

Town = []

rec = [-1] * (n+1)
idx = 1

while (rec[idx] == -1):
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

Exce = rec.index(idx)
Cycl = len(Town) - Exce

if Cycl > k:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[Exce+k])

def main():
  ## IMPORT MODULE
  #import sys

  
  #input=lambda :sys.stdin.readline().rstrip()

  #f_inf=float("inf")
  #MOD=10**9+7
  
  if 'get_ipython' in globals(): 
    ## SAMPLE INPUT
    n, k = 4, 5
    A = [3, 2, 4, 1]

  else:
    ##INPUT 
    #n = input()
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

  ## SUBMITION CODES HERE
  Town = []
  rec = [-1] * (n+1) # The town we haven't visited yet is -1
  idx = 1

  # Loop to the cycle
  while (rec[idx] == -1):
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

  # Pre-cycle
  Exce = rec[idx]
  # Intra-cycle
  Cycl = len(Town) - Exce

  if Exce > k:
    print(Town[k])
  else:
    Warp = (k - Exce) % Cycl
    print(Town[Exce+Warp])
  
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 = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 0
while j<=n:
    #print(town[i])
    warp.append(town[i])
    #print(warp,checklist)
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        #print(checklist[town[i]-1],j)
        #print("a",warp)
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1


m = len(warp)
print(warp[(k-s)%m])

n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1


m = len(warp)
print(warp[(k-s)%m])


n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 0
s = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1

m = len(warp)
print(warp[(k-s)%m])


n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 1
s = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        loop = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1
if k>=s:
    m = len(loop)
    print(loop[(k-s)%m])
else:
    print(warp[k])

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

d = 60
dp = [[-1]*N for _ in range(d+1)]
for i in range(n):
    dp[0][i] = a[i]-1

for i in range(d):
    for j in range(n):
        dp[i+1] = dp[i][dp[i][j]]

ans = 0

for i in range(d):
    if K & (1<<i):
        ans = dp[i][ans]

print(ans)




n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[]
o=(n+1)*[-1]
cycle,l=1,0
v=1
while o[v]==-1:
  o[v]=len(visited)
  visited.append(v)
  v=a[v-1]
cycle=len(visited)-o[v]
l=o[v]
if k<l:
  print(visited[k])
else:
  k-=l
  k%=cycle
  print(visited[l+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):
n, k = map(int, input().split())
a = list(map(int, input().split()))

t = [1]
for i in range(n):
    t.append(a[t[-1] -  1])

t_sort = sorted(t)
for i in range(n):
    if t_sort[i] == t_sort[i + 1]:
        b = t_sort[i]
        break

count = 0
for i in range(n):
    if t[i] == b:
        if count == 0:
            count += 1
            s_first = i
        else:
            s_second = i
            break
s = s_second - s_first

for i in range(n - s + 1):
    if t[i] == t[i + s]:
        break

if k < i:
    print(t[k])
else:
    print(t[(k - 1) % s + i])

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

t = [1]
for i in range(n):
    t.append(a[t[-1] -  1])

t_sort = sorted(t)
for i in range(n):
    if t_sort[i] == t_sort[i + 1]:
        b = t_sort[i]
        break

count = 0
for i in range(n):
    if t[i] == b:
        if count == 0:
            count += 1
            s_first = i
        else:
            s_second = i
            break
s = s_second - s_first

for i in range(n - s + 1):
    if t[i] == t[i + s]:
        break

if k < i:
    print(t[k])
else:
    print(t[(k - 1) % s + i - 1])

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

t = [1]
for i in range(n):
    t.append(a[t[-1] -  1])
print(t)

for i in range(n):
    for j in range(i + 1, n):
        if t[i] == t[j]:
            s = j - i
            break
    else:
        continue
    break

if k < i:
    print(t[k])
else:
    print(t[(k - i) % s + i])

n, k = map(int, input().split())
a_list = list(map(int, input().split()))
visit = [1]

for i in range(n):
    visit.append(a_list[visit[-1] - 1])

for i in range(n - 1):
    if visit[-1]  == visit[n - i - 1]:
        break
s = i + 1

for i in range(n - s):
    if visit[i] == visit[i + s]:
        break

if k < i:
    print(visit[k])
else:
    print(visit[i + (k - i) % s])

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

array=map(int,input().split())

A=list(array)
A.insert(0,0)

visit=0
root=[]
visited=1
once=0
while True:
    if visit & (0b1<<visited):
        break
    root.append(visited)
    visit|=0b1<<visited
    once+=1
    visited=A[visited]

roop=root[root.index(visited):]
print(roop)
if K<=once:
    print(root[K])
else:
    order=K-once
    print(roop[order%len(roop)])

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

array=map(int,input().split())

A=list(array)
A.insert(0,0)

visit=0
root=[]
visited=1
once=0
while True:
    if visit & (0b1<<visited):
        break
    root.append(visited)
    visit|=0b1<<visited
    once+=1
    visited=A[visited]

roop=root[root.index(visited):]
if K<once:
    print(root[K])
else:
    order=K-once
    print(roop[order%len(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 = list(map(int,input().split()))
A = list(map(int,input().split()))

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:break
  dp[t] = i
  
l = i - dp[t]
K = (k-dp[t])%l + dp[t]
print(dp)
for i in range(n):
  if dp[i] == K:exit(print(i+1))

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

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:break
  dp[t] = i
if k == 2:
  exit(print(t+1))
l = i - dp[t]
K = (k-dp[t])%l + dp[t]
print(dp)
for i in range(n):
  if dp[i] == K:exit(print(i+1))



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

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
flg = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:
    flg = 1
    break
  dp[t] = i
if flg == 0:
  exit(print(t+1))
elif k == 2:
  exit(print(t+1))

l = i - dp[t]
K = (k-dp[t])%l + dp[t]

for i in range(n):
  if dp[i] == K:exit(print(i+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())
l = list(map(int, input().split()))

keys = range(1,n+1)
values = [-1]*n
dic = dict(zip(keys,values))
dic[1] = 0
t = 1
i = 1

def inverse_lookup(d, x):
    for k,v in d.items():
        if x == v:
            return k

while i <= k:
    t = l[t-1]
    if dic[t] != -1:
        m  = dic[t]
        d = i-m
        r = (k-i+1)%d
        ans = inverse_lookup(dic, (m+r-1))
        print(ans)
        break
    dic[t] = i
    i += 1
if i == k+1:
    print(t)

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

keys = range(1,n+1)
values = [-1]*n
dic = dict(zip(keys,values))
dic[1] = 0
t = 1
i = 1

def inverse_lookup(d, x):
    for k,v in d.items():
        if x == v:
            return k

while i <= k:
    t = l[t-1]
    if dic[t] != -1:
        m  = dic[t]
        d = i-m
        r = (k-i+1)%d
        if r == 0:
            r = d
        ans = inverse_lookup(dic, (m+r-1))
        print(ans)
        break
    dic[t] = i
    i += 1
if i == k+1:
    print(t)

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 = [i for i in map(int, input().split(" "))]

hist = [0 for i in range(N)]
route = []
loop = []

jump = 1
cur = A[0] - 1
route.append(0)
hist[0] = jump
while hist[cur] == 0:
    jump = jump + 1
    route.append(cur)
    hist[cur] = jump
    cur = A[cur] - 1

loop = route[cur:]

if K < len(route):
    print(route[K] + 1)
else:
    K = K - len(route) - 1
    K = K % len(loop)
    print(loop[K] + 1)


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

hist = [-1 for i in range(N)]
route = []
loop = []

jump = 0
cur = A[0] - 1
route.append(0)
hist[0] = jump
while hist[cur] == -1:
    jump = jump + 1
    route.append(cur)
    hist[cur] = jump
    cur = A[cur] - 1

loop = route[hist[cur]:]

print(cur, hist, route, loop)

if K < len(route):
    print(route[K] + 1)
else:
    K = K - len(route)
    print(K)
    K = K % len(loop)
    print(K)
    print(loop[K] + 1)


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

hist = [-1 for i in range(N)]
route = []
loop = []

jump = 0
cur = A[0] - 1
route.append(0)
hist[0] = jump
while hist[cur] == -1:
    jump = jump + 1
    route.append(cur)
    hist[cur] = jump
    cur = A[cur] - 1

loop = route[hist[cur]:]

#print(cur, hist, route, loop)

if K < len(route):
    print(route[K] + 1)
else:
    K = K - len(route)
    
    K = K % len(loop)
    
    print(loop[K] + 1)


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()))
memo = [-1] * N
checked_num = {}

teleporter = A[0]
memo[0] = teleporter
repeat_point = -1
flag = False

for i in range(1, N):
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    checked_num[memo[i]] = None
    if memo[i] in checked_num and flag == False:
        repeat_point = i
        flag = True

start_point = memo.index(memo[repeat_point])
repeat = repeat_point - start_point


if K <= N:
    print(memo[K-1])
else:
    mod = K % repeat
    print(memo[mod-1])


N, K = map(int, input().split())
A = list(map(int, input().split()))
memo = [-1] * (N+1)
checked_num = {1:None}
memo[0] = 1

teleporter = A[0]
memo[1] = teleporter
checked_num[memo[1]] = None
repeat_point = -1
flag = False

for i in range(2, N+1):
    
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    if memo[i] in checked_num and flag == False:
        repeat_point = i
        flag = True
    checked_num[memo[i]] = None

# print(memo)

start_point = memo.index(memo[repeat_point])
repeat = repeat_point - start_point


if K <= N:
    print(memo[K])
else:
    mod = (K-start_point) % repeat
    
    print(memo[mod+start_point])


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()))
l = [1]
for i in range(n):  
    l.append(a[l[i]])
    
p = l.index(l[-1])
if k<=n:
  print(l[k])
  exit()
  
x = (k-n+1)%(n-p)
if x==0:
  print(l[n-1])
else:
  print(l[x+p-1])


n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
while True:  
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]
first = l.index(last)
loop = len(l)-first

if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
for i in range(10**18) 
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]

loop = len(l[l.index(last):-1])
first = l.index(last)
if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
while True:  
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]
first = l.index(last)
loop = len(l)-first

if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])
print(l.index(last))

n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
for i in range(n):  
    l.append(a[l[i]])
    
p = l.index(l[-1])
if k<=n:
  print(l[k])
  exit()
  
x = (k-n+1)%(n-p)
if x==0:
  print(l[n-1])
else:
  print(l[x+p-1])


n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
for i in range(n):  
    l.append(a[l[i]-1])
    
p = l.index(l[-1])
if k<=n:
  print(l[k])
  exit()
  
x = (k-n+1)%(n-p)
if x==0:
  print(l[n-1])
else:
  print(l[x+p-1])


Verdicts: Runtime Error

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):
import sys
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    l = [-1]*200010
    now = 0
    l[0] = 0
    ans = -100

    for i in range(n):
        k -= 1
        now = a[now] - 1 
        l[now] += 1
        if l[now] == 2: 
            loop = l.count(1) + 1
            k %= loop
        if k == 0:
            ans = now + 1
            break

    print(max(ans, -1))

main()

import sys
input = sys.stdin.readline

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

for i in range(10000):
    print(i)
    if a[l - 1] in m: 
        for j in range(len(m)):
            if m[j] == a[l - 1]:
                cnt = j 
                loop = len(m) - cnt 
                break
    else:
        m.append(a[l - 1])
        l = a[l - 1]
        continue
    break


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

print(m[l])

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

for i in range(10000):
    if a[l - 1] in m: 
        for j in range(len(m)):
            if m[j] == a[l - 1]:
                cnt = j 
                loop = len(m) - cnt 
                break
    else:
        m.append(a[l - 1])
        l = a[l - 1]
        continue
    break

l = 0 

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

print(l)

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

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

l = [-1]*n
l[0] = 0
order = 0
now = 0

while True:
    k -= 1
    now = a[now]
    order += 1
    if k == 0:
        break
    if l[now] != -1:
        cycle = order - l[now]
        k %= cycle
    if l[now] == -1:
        l[now] = order

print(now + 1)

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):
#N=int(input())
N,K = map(int,input().split())
#N=int(input())
A = list(map(int,input().split()))
#S=str(input())

now=1
flag=[0]+[-1]*(N-1)

for i in range(N):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        exit()
print(now)


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

now=1
flag=[0]+[-1]*(N-1)

for i in range(N-1):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans,now,flag,i)
        exit()

print(A[now-1])


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

now=1
flag=[0]+[-1]*(N-1)

for i in range(K):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans)
        exit()

print(now)


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=input().split()

l=len(a)

for i in range(0,l):
    a[i]=int(a[i])

p=1
h=[]
go=0
i=0
while(go==0):
    i+=1
    
    

    h.append(p)
    p=a[p-1]
    if i==k:
        break
    if p in h:
        l=len(h)
        for j in range(0,l):
            if h[j]==p:
                amari=(k-i-1)%(i-j)
                p=h[j+amari]
                go=1
                break



print(p)


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


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

ar=[-1 for i in range(n+1)]
p=1
h=[]
go=0


for i in range(1,k+1):
    
    
    
    

    ar[p]=i
    p=a[p-1]
    
    if ar[p]>0:
        j=ar[p]
        amari=(k-i-1)%(i-j)
        p=h[j+amari]
        go=1
        break

if go==1:
    p=ar.index(j+amari)
    p=a[p-1]


print(p)


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


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

ar=[-1 for i in range(n+1)]
p=1

go=0


for i in range(1,k+1):
    
    
    
    

    ar[p]=i-1
    p=a[p-1]
    
    if ar[p]>0:
        j=ar[p]
        amari=(k-i)%(i-j)
        
        go=1
        break

if go==1:
    p=ar.index(j+amari)
    
    


print(p)


Verdicts: Wrong Answer

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()))
town = [[] for _ in range(N)]
visited = [0]

for i in range(N):
  town[i] = [i,A[i] - 1]

def teleport(i,con):
  goal = town[i][1]
  if goal in visited or con == K:
    return goal
  else:
    visited.append(goal)
    return teleport(goal,con + 1)

l = teleport(visited[0],0)

print([i + 1for i in visited])

if K <= len(visited):
  print(visited[K] + 1)
else:
  if l in visited:
    cs = visited.index(l)
    cycle = visited[cs:]
    K = K - len(visited) + len(cycle)
  print(cycle[K % len(cycle)] + 1)
  


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

A = list(map(int,input().split()))
town = [[] for _ in range(N)]
visited = [0]
flag = [-1] * N

for i in range(N):
  town[i] = [i,A[i] - 1]

f = 0

while flag[f] == -1:
  t = town[f][1]
  flag[f] = t
  f = town[t][0]
  visited.append(t)
  
l = visited[-1]
lv = len(visited)

if K <= lv:
  print(visited[K] + 1)
else:
  cs = visited.index(l)
  cycle = visited[cs:-1]
  lc = len(cycle)
  print(cycle[(K - lv + 1) % lc] + 1)
##i

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 = map(int,input().split())
    As = list(map(int,input().split()))
    route = [0]
    pre = 0
    i = 0
    r_append = route.append
    for _ in range(N):
        i = As[i]-1
        if i in route:
            pre = route.index(i)
            route = route[pre:]
            break
        r_append(i)
        len_route += 1
    # print(route)
    mod = len(route)
    print(route[(K-pre)%mod]+1)

if __name__ == "__main__":
    main()

N,K = map(int,input().split())
As = list(map(int,input().split()))
towns = [0]*N
route = [0]
pre = 0
i = 0
towns[0] = 1
for _ in range(K):
    i = As[i]-1
    if towns[i]>0:
        pre = route.index(i)
        route = route[pre:]
        break
    route.append(i)
    towns[i] = 1
mod = len(route)
print(route[(K-pre)%mod]+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):
from time import time
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [-1 for i in range(N)]

visited = []
now = 0
loopstarts = None
looplength = None
t0 = time()

for i in range(N):
    if L[now] != -1:
        break
    else:
        L[now] = i
        visited.append(now)
        now = A[now]-1

loopstarts = visited.index(now)
looplength = len(visited)-loopstarts

if K < loopstarts:
    print(visited[K]+1)
else:
    print(visited[loopstarts+(K-loopstarts)%looplength]+1)
print(time()-t0)


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

visited = []
now = 0
loopstarts = None
looplength = None
t0 = time()

for i in range(N):
    if L[now] != -1:
        break
    else:
        L[now] = i
        visited.append(now)
        now = A[now]-1

loopstarts = visited.index(now)
looplength = len(visited)-loopstarts

if K < loopstarts:
    print(visited[K]+1)
else:
    print(visited[loopstarts+(K-loopstarts)%looplength]+1)
# print(time()-t0)


Verdicts: Wrong Answer

Accepted
Problem: There are N blocks arranged in a row. Let us paint these blocks.  We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.  Find the number of ways to paint the blocks under the following conditions:    * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.   * There may be at most K pairs of adjacent blocks that are painted in the same color.  Since the count may be enormous, print it modulo 998244353.

Submission attempts (in order):
from math import factorial


#     return(factorial(n) // (factorial(n-r) * factorial(r)))

def comb(n,k,p):
    if n<0 or k<0 or n<k: return 0
    if n==0 or k==0: return 1
    a=factorial(n) %p
    b=factorial(k) %p
    c=factorial(n-k) %p
    return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
    if b==0: return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a*power_func(a,b-1,p ))%p


def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353 
N = 10**6
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

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

s = 0

for x in range(k+1):
    s += m * (power_func(m-1,n-1-x,998244353)% 998244353) * (cmb(n-1,x,998244353) % 998244353)
    s %= 998244353
print(s)


def power_func(a,b,p):
    if b==0: return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a*power_func(a,b-1,p ))%p


def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

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

for i in range( 2, n + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

s = 0

for x in range(k+1):
    s += (m * power_func(m-1,n-1-x,998244353) * cmb(n-1,x,998244353)) % 998244353
    s %= 998244353
print(s)

Verdicts: Time Limit Exceeded

Accepted