ZhenlongDai/ACPR huggingface dataset:
Problem: p03370
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x = list(map(int, input().split()))
m = []
for _ in range(n):
  m.append(int(input()))
m.sort()

def main():
  for i in range(n):
    if sum(m[0:i+1]) > x:
      print(i)
      return
  print(n)

main()


Failing test case input:
4 102
144
13
46
90

Expected output:
-11


Actual output:
2


Fixed submission (verdict: Accepted):
n, x = list(map(int, input().split()))
m = []
for _ in range(n):
  m.append(int(input()))
m.sort()

def main():
  for i in range(n):
    if sum(m[0:i+1]) > x:
      print(i)
      return
  print(n)

if sum(m) > x:
  main()
else:
  print(n + (x - sum(m))

Problem: p03370
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, x = map(int, input().split())
m = []
for i in range(n):
    m.append(int(input()))
x = x - sum(m)
c = n
min_m = min(m)
while (x - min_m > 0):
    c += 1
    x = x - min_m
print(c)

Failing test case input:
2 146
187
4
78
229

Expected output:
-10


Actual output:
2


Fixed submission (verdict: Accepted):
import math
n, x = map(int, input().split())
m = []
for i in range(n):
    m.append(int(input()))
x = x - sum(m)
c = n
min_m = min(m)
c += math.floor(x / min_m)
print(c)

Problem: p03370
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math

d = list(map(int,input().split(' ')))
N = d[0]
X = d[1]
min_m = 0

for i in range(N):
	mi = int(input())
	X = X - mi
	if min_m == 0 or min_m > mi:
		min_m = mi

print(N + math.floor(X/mi))

Failing test case input:
5 8327
50
166
24
47
100

Expected output:
335


Actual output:
84


Fixed submission (verdict: Accepted):
import math

d = list(map(int,input().split(' ')))
N = d[0]
X = d[1]
min_m = 0

for i in range(N):
	mi = int(input())
	X = X - mi
	if min_m == 0 or min_m > mi:
		min_m = mi

print(N + math.floor(X/min_m))

Problem: p03370
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, X = map(int, input().split())
gram = [int(input()) for i in range(N)]

dp = [0]*10**5
X = X - sum(gram)
print(N+(X/min(gram)))

Failing test case input:
3 1001
120
100
140

Expected output:
9


Actual output:
9.41


Fixed submission (verdict: Accepted):
N, X = map(int, input().split())
gram = [int(input()) for i in range(N)]

dp = [0]*10**5
X = X - sum(gram)
print(N+(X
Problem: p02468
Buggy submission (Python3, verdict: Wrong Answer):
import math
print(int(math.pow(*map(int,input().split()))))


Failing test case input:
7 24

Expected output:
497804128


Actual output:
191581231380566409216


Fixed submission (verdict: Accepted):
print(int(pow(*map(int,input().split()),10**9+7)))

Problem: p02468
Buggy submission (Python3, verdict: Wrong Answer):
m, n = map(int, input().split())
print(pow(m, n))

Failing test case input:
14 39

Expected output:
360828576


Actual output:
500026926136478545004035990584179037499817984


Fixed submission (verdict: Accepted):
m, n = map(int, input().split())
print(pow(m, n, 1000000007))

Problem: p02468
Buggy submission (Python3, verdict: Wrong Answer):
m,n=map(int,input().split())
print(pow,m,n,1000000007)


Failing test case input:
-7 11

Expected output:
22673271


Actual output:
<built-in function pow> -7 11 1000000007


Fixed submission (verdict: Accepted):
m,n=map(int,input().split())
print(pow(m,n,1000000007))
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):




def main(n, k, a):
    ck=[-1]*n
    tak=0
    c=0
    ck[0]=c
    for i in range(n):
        c+=1
        tak=a[tak]-1
        if ck[tak]!=-1:
            loop=c-ck[tak]
            lps=ck[tak]
            break
        ck[tak]=c
    print(ck, loop, lps)

    tak=0
    for i in range((k-lps)%(loop)+lps):
        tak=a[tak]-1

    print(tak+1)


if __name__ == '__main__':
    n,k=map(int, input().split())
    a=list(map(int, input().split()))
    main(n, k, a)


Failing test case input:
3 786427263574235267
2 1 1 5 2 2

Expected output:
2


Actual output:
[0, 1, -1] 2 0
2


Fixed submission (verdict: Accepted):




def main(n, k, a):
    ck=[-1]*n
    tak=0
    c=0
    ck[0]=c
    for i in range(n):
        c+=1
        tak=a[tak]-1
        if ck[tak]!=-1:
            loop=c-ck[tak]
            lps=ck[tak]
            break
        ck[tak]=c

    tak=0
    for i in range(min(k, (k-lps)%(loop)+lps)):
        tak=a[tak]-1

    print(tak+1)


if __name__ == '__main__':
    n,k=map(int, input().split())
    a=list(map(int, input().split()))
    main(n, k, a)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
def Ii():return int(sys.stdin.buffer.read())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))

n,k = Mi()
a = Li()
b = [1]
b.append(a[0])
flag = False
for i in range(n):
  for j in range(len(b)):
    if b[j] == a[b[-1]-1]:
      flag = True
      break
  if flag: 
    break
    
  b.append(a[b[-1]-1])

roop = len(b)-j
roopk = k%roop

print(b[roopk])

Failing test case input:
6 916742600949256759
6 5 2 4 2 2

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
import sys
def Ii():return int(sys.stdin.buffer.read())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))

n,k = Mi()
a = Li()
b = [1]
b.append(a[0])
flag = False
for i in range(n):b.append(a[b[-1]-1])
if len(b) > k:
  print(b[k])
  exit(0)
for j in range(len(b)):
  if b[j] == a[b[-1]-1]:
    break
    
roop = len(b)-j
roopk = (k-j)%roop
print(b[roopk+j])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, K = map(int, input().split())
A = [x-1 for x in list(map(int, input().split()))]

current = 0
d = dict()
d[current] = d.get(current, 0) + 1
pattern = [0]
cnt = K

while True:
    current = A[current]
    if d.get(current) == 2:
        break
    else:
        if d.get(current) == None:
            d[current] = d.get(current, 0) + 1
        else:
            d[current] = d.get(current) + 1
        pattern.append(current)
        cnt -= 1
        if cnt == 0:
            break

if cnt == 0:
    print(pattern[-1]+1)
else:
    loop = [key for key, value in d.items() if value == 2]
    res = len(pattern) % len(loop)
    pattern = pattern[res:]
    ans = len(pattern) % len(loop)

    print(pattern[ans]+1)

Failing test case input:
6 1122048832909353099
6 4 1 1 3 2

Expected output:
4


Actual output:
1


Fixed submission (verdict: Accepted):
n, K = map(int, input().split())
A = [x-1 for x in list(map(int, input().split()))]

current = 0
d = dict()
d[current] = d.get(current, 0) + 1
pattern = [0]
cnt = K

while True:
    current = A[current]
    if d.get(current) == 2:
        break
    else:
        if d.get(current) == None:
            d[current] = d.get(current, 0) + 1
        else:
            d[current] = d.get(current) + 1
        pattern.append(current)
        cnt -= 1
        if cnt == 0:
            break

if cnt == 0:
    print(pattern[-1]+1)
else:
    loop = len([key for key, value in d.items() if value == 2])
    not_loop = len([key for key, value in d.items() if value != 2])
    pattern = pattern[not_loop:]
    ans_position = (K - not_loop) % loop

    print(pattern[ans_position]+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,K = map(int,input().split())
A = list(map(int,input().split()))
visited = []
now = 1
next_town = A[now-1]
while(1):
    if next_town in visited:
        break
    visited.append(now)
    next_town = A[now-1]
    now = next_town

first_roop_city = A[visited[-1]-1]
first_roop_city_index = visited.index(first_roop_city)

if K <= len(visited):
    print(visited[K])
else:
    print(visited[K%(len(visited)-first_roop_city_index)])

Failing test case input:
6 279024949205837559
4 5 2 5 3 3

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
N,K = map(int,input().split())
A = list(map(int,input().split()))
visited = []
search = set()
now = 1
next_town = A[now-1]
count = 1
while(count<=N):
    if next_town in search:
        break
    visited.append(now)
    search.add(now)
    next_town = A[now-1]
    now = next_town
    count += 1

first_roop_city = A[visited[-1]-1]
first_roop_city_index = visited.index(first_roop_city)
if K < len(visited):
    print(visited[K])
else:
    visited = visited[first_roop_city_index:]
    L = len(visited)
    print(visited[(K-first_roop_city_index)%L])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    visited = [False for _ in range(N)]
    visited[0] = True
    while visited[A[current]] is False:
        current = A[current]
        routes.append(current)
        visited[current] = True
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    
    
    
    print(repeat[(K-leftlen)%len(repeat)]+1 if K >= len(routes) else routes[K])


if '__main__' == __name__:
    resolve()

Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    visited = [False for _ in range(N)]
    visited[0] = True
    while visited[A[current]] is False:
        current = A[current]
        routes.append(current)
        visited[current] = True
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    
    
    
    print(repeat[(K-leftlen)%len(repeat)]+1 if K > leftlen else routes[K]+1)


if '__main__' == __name__:
    resolve()

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
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)])


Failing test case input:
6 727202214173249351
6 5 1 5 2 2

Expected output:
5


Actual output:
727202214173249351 2 [1, 6, 2, 5]
5


Fixed submission (verdict: Accepted):
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)])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    D = [0] * (n+1)
    now = 1
    for i in range(1, k+1):
        tmp = A[now-1]
        if not D[tmp]:
            now = tmp
            D[now] += i
        else:
            mod = D[tmp] - 1
            loop = i - D[tmp]
            print(D.index((k - mod) % loop + mod) if loop > 1 else tmp)
            return
    print(now)
main()

Failing test case input:
6 165759462200935729
4 5 2 5 3 3

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    D = [0] * (n+1)
    now = 1
    for i in range(1, k+1):
        tmp = A[now-1]
        if not D[tmp]:
            now = tmp
            D[now] += i
        else:
            pre = D[tmp]
            loop = i - D[tmp]
            print(D.index((k - pre) % loop + pre) if loop > 1 else tmp)
            return
    print(now)
main()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

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

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


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

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

print(now)


Failing test case input:
6 237500909755776657
6 4 1 5 3 2

Expected output:
4


Actual output:
1


Fixed submission (verdict: Accepted):

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

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


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

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

print(now)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

N, K = map(int, input().split())
S = list(num-1 for num in map(int, input().split()))
loc = 0

arrived = [0]*N
ar = set()
cnt = 0
while not loc in ar:
    arrived[cnt] = loc
    ar.add(loc)
    loc = S[loc]
    cnt += 1

if K < cnt:
    print(arrived[K-1])
else:
    for i in range(cnt):
        if arrived[i] == loc:
            K -= i
            roop = cnt-i
            K %= roop
            print(arrived[i+K]+1)
            break

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
S = list(num-1 for num in map(int, input().split()))
loc = 0

arrived = [0]*N
ar = set()
cnt = 0
while not loc in ar:
    arrived[cnt] = loc
    ar.add(loc)
    loc = S[loc]
    cnt += 1

if K < cnt:
    print(arrived[K]+1)
else:
    for i in range(cnt):
        if arrived[i] == loc:
            K -= i
            roop = cnt-i
            K %= roop
            print(arrived[i+K]+1)
            break

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = 1
c = [0] * n
c[0] = 1
while c[b-1]<3 and k > 0:
	b = a[b-1]
	c[b-1] += 1
	k -= 1
n = k % n
for i in range(n):
	b = a[b-1]
print(b)

Failing test case input:
6 237500909755776657
2 4 1 5 3 2

Expected output:
4


Actual output:
1


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = 1
c = [0] * n
c[0] = 1
m = 0
while c[b-1] < 2 and k > 0:
	b = a[b-1]
	c[b-1] += 1
	k -= 1
while c[b-1] < 3 and k > 0:
	b = a[b-1]
	c[b-1] += 1
	m += 1
if k == 0:
	print(b)
else:
	o = k % m
	for i in range(o):
		b = a[b-1]
	print(b)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
cnt=[]
m=0
ans=0
for i in range(n):
  if a[m] in d:
    ans+=1
    cnt.append(a[m])
    if ans==k:
      print(a[m])
    break
  else:
    d[a[m]]=0
    cnt.append(a[m])
    ans+=1
    if ans==k:
      print(a[m])
      break
    m=a[m]-1
if ans!=k:
  print(cnt[(k-ans)%len(cnt)])

Failing test case input:
6 2190427306125562200
6 4 6 2 3 3

Expected output:
3


Actual output:
6


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
cnt=[]
m=0
ans=0
for i in range(2*n):
  if a[m] in d:
    ans+=1
    cnt.append(a[m])
    if ans==k:
      print(a[m])
    break
  else:
    d[a[m]]=0
    cnt.append(a[m])
    ans+=1
    if ans==k:
      print(a[m])
      break
    m=a[m]-1
if ans!=k:
  if cnt[-1]==cnt[-2]:
    print(cnt[-1])
  else:
    cnt=cnt[cnt.index(cnt[-1]):-1]
    print(cnt[(k-ans)%len(cnt)])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

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

visited = set()
visited.add(0)
trace = []
cur = 0
while True:
    trace.append(cur)
    if A[cur] in visited:
        cur = A[cur]
        break
    visited.add(A[cur])
    cur = A[cur]

pre = 0
for i, a in enumerate(trace):
    if a == cur:
        pre = i

loop = len(trace) - pre

if K <= pre:
    ans = trace[K - 1]
else:
    if loop == 0:
        ans = trace[pre::][0]
    else:
        r = (K - pre) % loop
        ans = trace[pre::][r]

print(ans + 1)


Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
4


Fixed submission (verdict: Accepted):
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

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

visited = set()
visited.add(0)
trace = []
cur = 0
while True:
    trace.append(cur)
    if A[cur] in visited:
        cur = A[cur]
        break
    visited.add(A[cur])
    cur = A[cur]

pre = 0
for i, a in enumerate(trace):
    if a == cur:
        pre = i

loop = len(trace) - pre

if K <= pre:
    ans = trace[K]
else:
    if loop == 0:
        ans = trace[pre::][0]
    else:
        r = (K - pre) % loop
        ans = trace[pre::][r]

print(ans + 1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]


print(L[K])




Failing test case input:
6 727202214173249351
6 5 1 5 4 2

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]


print(L[K + L.index(A[now - 1])])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
def resolve():
  N, K = map(int, input().split(" "))
  A = [int(x) - 1 for x in input().split(" ")]

  history = []
  current = 0
  while True:
    history.append(current)
    current = A[current]
    if current in history:
      loop_length = len( history[history.index(current):] )
      print(loop_length)
      if loop_length == 0:
        print(A[current] + 1)
        return True

      if loop_length == 1:
        print(A[current] + 1)
        return True

      length_to_loop = len( history[:history.index(current)] )
      additional_way = (K-length_to_loop)%loop_length
      print(A[length_to_loop + additional_way] + 1)
      return True

if __name__ == "__main__":
    resolve()

Failing test case input:
6 727202214173249351
6 5 4 5 5 2

Expected output:
5


Actual output:
1
5


Fixed submission (verdict: Accepted):
def resolve():
  N, K = map(int, input().split(" "))
  A = [int(x) - 1 for x in input().split(" ")]

  current = 0
  if K < N:
    for i in range(K):
      current = A[current]
    print(current + 1)
    return True
  else:
    history = [False] * N
    load_map = []
    while True:
      load_map.append(current)
      current = A[current]
      if history[current]:
        loop_length = len( load_map[load_map.index(current):] )
        if loop_length == 0:
          print(current + 1)
          return True

        length_to_loop = len( load_map[:load_map.index(current)] )
        additional_way = (K-length_to_loop)%loop_length
        print(load_map[length_to_loop + additional_way] + 1)
        return True
      else:
        history[current] = True

if __name__ == "__main__":
    resolve()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):








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

paths = {}

for i in range(N):
    paths[i+1] = A_i[i]





state = 1
steps = 0
flag = False
known_states = {}

while steps < K:
    known_states[state] = steps

    state = paths[state]
    steps += 1

    

    if state in known_states and not flag:
        K = K % (steps - known_states[state]) + 1
        steps = 0
        flag = True
        


print(state)


Failing test case input:
6 727202214173249351
6 5 1 5 3 2

Expected output:
6


Actual output:
2


Fixed submission (verdict: Accepted):
















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

paths = {}

for i in range(N):
    paths[i+1] = A_i[i]
















state = 1
steps = 0
flag = False
known_states = {}

while steps < K:
    known_states[state] = steps 

    state = paths[state] 
    steps += 1 

    

    if state in known_states and not flag: 
        loop_steps = steps - known_states[state]
        K = (steps - loop_steps) + (K - (steps - loop_steps))  % loop_steps
        
        
        
        state = 1
        steps = 0
        
        flag = True
        



print(state)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

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

flag = [0 for _ in range(N)]

tmp = 1
i = 1
flag[0] = 1
while(True):
    tmp = A[tmp-1]
    i += 1
    if (flag[tmp-1] != 0):
        break
    flag[tmp-1] = i

pre = flag[tmp-1] - 1
cyc = i - flag[tmp-1]
if (K >= pre):
    index = (K - pre) % cyc
    print(A[flag.index(pre+index)])
else:
    print(A[flag.index(K)])


Failing test case input:
4 9
3 2 4 1

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N, K = map(int,input().split())
A = list(map(int, input().split()))

flag = [0 for _ in range(N)]

tmp = 1
i = 1
flag[0] = 1
while(True):
    tmp = A[tmp-1]
    i += 1
    if (flag[tmp-1] != 0):
        break
    flag[tmp-1] = i

pre = flag[tmp-1] - 1
cyc = i - flag[tmp-1]
if (K >= pre):
    index = (K - pre) % cyc
    if (index == 0):
        print(A[flag.index(pre + cyc)])
    else:
        print(A[flag.index(pre+index)])
else:
    print(A[flag.index(K)])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int, input().split())
tele = list(map(int, input().split()))
move = [-1]*n
move[0] = 0
now = 0
cnt = 0

while True:
    cnt += 1
    now = tele[now] - 1 

    if move[now] != -1:
        cycle = cnt - move[now]
        break
    else:
        move[now] = cnt



if cnt-1 >= k:
    ans = 0
    for i in range(k):
        ans = tele[ans] - 1

else:
    loop = (k-(cnt-1))%cycle
    ans = cnt - 1 

    for i in range(loop+1):
        ans = tele[ans] - 1
print(ans+1)
        




Failing test case input:
6 237500909755776657
2 4 1 1 3 2

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n,k = map(int, input().split())
tele = [0] + list(map(int, input().split()))
move = [-1]*(n+1)
move[1] = 0
now = 1
cnt = 0

while True:
    cnt += 1
    now = tele[now] 

    if move[now] != -1:
        cycle = cnt - move[now]
        break
    else:
        move[now] = cnt

notCycle = move[now]

if notCycle > k:
    now = 1
    for i in range(k):
        now = tele[now]

else:
    loop = (k-notCycle)%cycle
    for i in range(loop):
        now = tele[now]
print(now)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys 
def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    p_list = [1]
    ne_p = a[0]
    p_bool = [False] * n
    p_bool[0] = True
    check = False
    step = 0
    for _ in range(n):
        p_bool[ne_p-1] = True
        p_list.append(ne_p)
        step = 0
        if step == k:
            
            sys.exit()
        ne_p = a[ne_p-1]
        
        
        
        if p_bool[ne_p-1]:
            check = True
            break
        
    
    
    if check:
        ind = p_list.index(ne_p)
    else:
        ind = 0
    k = k - ind
    
    
    
    
    print(p_list[ind + k%(len(p_list)-ind)])

solve()

Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:

Fixed submission (verdict: Accepted):
import sys 
def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    p_bool = [False] * n
    ne_p = 1
    p_bool[ne_p-1] = True
    step = 0
    for i in range(n):
        ne_p = a[ne_p-1]
        step += 1
        if p_bool[ne_p-1]:
            break
        else:
            p_bool[ne_p-1] = True
        if step == k:
            print(ne_p)
            sys.exit()
    
    p_list = []
    sta = ne_p
    for i in range(n):
        ne_p = a[ne_p-1]
        step += 1
        p_list.append(ne_p)
        if ne_p == sta:
            break
        if step == k:
            print(ne_p)
            sys.exit()

    len_l = len(p_list)
    left = k-step
    left -= (left
    print(p_list[left-1])
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    

solve()
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from sys import stdin
input = stdin.readline

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

graph = []

time = [-1] * N

for i in range(N):
    graph.append([i, A[i]])

now = 0
nex = 0
time[0] = 0
loop = 0

while True:
    now = nex
    nex = A[now] - 1

    if time[nex] == -1:
        time[nex] = time[now] + 1
    else:
        loop = nex
        break

if K > max(time):
    K %= (max(time) - time[loop] + 1)

for i in range(N):
    if time[i] == K:
        print(i + 1)


Failing test case input:
6 2190427306125562200
6 4 3 2 3 3

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
from sys import stdin
input = stdin.readline

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

graph = []

time = [-1] * N

now = 0
nex = 0
time[0] = 0
loop = 0

while True:
    now = nex
    nex = A[now] - 1

    if time[nex] == -1:
        time[nex] = time[now] + 1
    else:
        loop = nex
        break

if K > max(time):
    K -= time[loop]
    K %= (max(time) - time[loop] + 1)
    tmp = time[loop]
    for i in range(N):
        time[i] -= tmp

for i in range(N):
    if time[i] == K:
        print(i + 1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [0] + A
town = 1
visited = [0]*(N+1)

for i in range(1, K+1):
    town = A[town]
    if visited[town] == 0:
        visited[town] = i
    else:
        loop = i - visited[town]
        town = (K - visited[town]) % loop + visited[town]
        break
print(town)

Failing test case input:
6 165759462200935729
4 5 2 5 3 2

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
import sys

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [0] + A
town = 1
visited = [0]*(N+1)

for i in range(1, K+1):
    town = A[town]
    if visited[town] == 0:
        visited[town] = i
    else:
        loop = i - visited[town]
        place = (K - visited[town]) % loop + visited[town]
        print(visited.index(place))
        sys.exit()
print(town)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())

li = [0]
li.extend(list(map(int, input().split())))
tuita = [0]*(n+1)
cnt = 0
town = 1

def search(town, times):
  return times + 1
  if times == 0:
    return town
  return search(li[town], times - 1)

while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      print(search(1, k))
    elif k == tuita[town]:
      print(town)
    else:
      print(search(town, (k - tuita[town]) % (cnt - tuita[town])))
    break
 

  
    

Failing test case input:
4 5
3 2 4 1

Expected output:
4

Actual output:
2


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())

li = [0]
li.extend(list(map(int, input().split())))
tuita = [0]*(n+1)
cnt = 0
town = 1

lastTown = 1
while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      lastTown = 1
      times = k
    elif k == tuita[town]:
      lastTown = town
      times = 0
    else:
      lastTown = town
      times = (k - tuita[town]) % (cnt - tuita[town])
    break

town = lastTown
while times > 0:
  if times <= 0:
    break
  times -= 1
  town = li[town]
print(town)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())
button = list(map(int,input().split()))
visit = [-1]*n
hisitory = [-1]
push = 0
f = 0

for i in range(k):
    visit[push] = i+1
    hisitory.append(push)
    push = button[push] -1
    if(visit[push] != -1):  
        f = 1
        start = visit[push]
        last=push
        end = i+1
        break
if(f == 0):
    print(hisitory[-1]+1)
else:
    geta = start -1
    loopk = k - geta
    loop = end -start + 1
    modk = loopk%loop
    if(modk == 0):
        modk = loop
    print(button[hisitory[geta + modk]])
    
    


Failing test case input:
4 0
3 2 1 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
button = list(map(int,input().split()))
visit = [-1]*n
hisitory = [-1]
push = 0
f = 0

for i in range(k):
    visit[push] = i+1
    hisitory.append(push)
    push = button[push] -1
    if(visit[push] != -1):  
        f = 1
        start = visit[push]
        last=push
        end = i+1
        break
if(f == 0):
    print(button[hisitory[-1]])
else:
    geta = start -1
    loopk = k - geta
    loop = end -start + 1
    modk = loopk%loop
    if(modk == 0):
        modk = loop
    print(button[hisitory[geta + modk]])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):




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

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

    step = 0
    current = 1

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

    
    

    print(seq[n1-1+((K-(n1-1))%n2)])


if __name__ == '__main__':
    main()


Failing test case input:
6 727202214173249351
6 3 3 4 3 2

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):




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

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

    step = 0
    current = 1

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

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

    return


if __name__ == '__main__':
    main()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
def main():
    N, K = map(int, input().split())

    A    = [int(i)-1 for i in input().split()]
    visit_plan = [0]
    vpi        = 0
    loop_flg     = False
    B    = set(visit_plan)
    for i in range(N-1):
        a = A[vpi]
        if a in B:
            loop_flg = True
            break
        else:
            visit_plan.append(a)
            B.add(a)
            vpi = a


    if loop_flg:
        loop_start = visit_plan.index(a)
        loop_end   = len(visit_plan)
        loop_length = loop_end - loop_start
        last_index = (K-loop_start)%loop_length
        current_town = visit_plan[loop_start+last_index]
    else:
        current_town = a
    print(current_town+1)


if __name__ == '__main__':
    main()

Failing test case input:
6 237500909755776657
6 4 1 5 3 2

Expected output:
4


Actual output:
3


Fixed submission (verdict: Accepted):
def main():
    N, K = map(int, input().split())
    A    = [int(i)-1 for i in input().split()]

    visited_town = []
    current_town = 0
    loop_flg     = False
    visited_town.append(current_town)
    set_visted_town = set(visited_town)
    for i in range(K):
        current_town = A[current_town]
        if current_town in set_visted_town:
            visited_town.append(current_town)
            loop_flg = True
            break
        visited_town.append(current_town)
        set_visted_town.add(current_town)


    if loop_flg:
        loop_start = visited_town.index(current_town)
        loop_end   = len(visited_town) - 1
        loop_length = loop_end - loop_start
        last_index = (K-loop_start)%loop_length
        current_town = visited_town[loop_start+last_index]

    print(current_town+1)


if __name__ == '__main__':
    main()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k -= len(know)
    ans = loop[k%len(loop)]

print(ans)


Failing test case input:
6 2038098695786626292
6 4 1 1 3 4

Expected output:
4


Actual output:
[1, 6, 4] 1 [1, 6, 4]
4


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break



if k < len(know):
    ans = know[k]
else:
    k -= len(know)
    ans = loop[k%len(loop)]

print(ans)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-c)%len(X)])  
else: 
    print(i+1)

Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
3


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

check = [0]*N
i = 0
c = 1
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%len(X)])  
else: 
    print(i+1)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
h = 0
k = 0
check = []
for i in range(N[0]):
    count.append(0)
while h < N[0]:
    t = nums[k]
    if(count[t-1] == 1):
        break
    else:
        check.append(t)
        count[t-1] += 1
    k = t -1
    h+=1
s = check.index(t)
if(N[1] < s):
    print(check[N[1]])
else:
    ans = (N[1] - s)%(len(check)-s)
    ans1 = check.index(t) - 1 + ans
    print(check[ans1])

Failing test case input:
6 355145681216602327
4 5 1 5 2 3

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
k = 0
check = []
t = nums[0]
for i in range(N[0]):
    count.append(0)
while count[t-1] == 0:
    check.append(t)
    count[t-1] += 1
    k = t -1
    t = nums[k]

s = check.index(t) + 1
if(N[1] < s):
    print(check[N[1] - 1])
else:
    ans = (N[1] - s)%(len(check)+1-s)
    ans1 = check.index(t) + ans
    print(check[ans1])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
towns = [int(i) for i in input().split()]
arrivedList = [1]
loopCount = 0
previous = 0
for i in range(N):
    
    current = towns[arrivedList[-1]-1]
    arrivedList.append(current)

if K < N + 1:
    print(arrivedList[-1])
    exit()
loopStart = N - 1
while (arrivedList[N] != arrivedList[loopStart]):
    loopStart -= 1
remain = (K-loopStart) % (N - loopStart)
loopList = arrivedList[loopStart+1:]

print(loopList[remain-1])

Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):

N, K = map(int, input().split())
towns = [int(i) for i in input().split()]
arrivedList = [1]
for i in range(N * 2):
    arrivedList.append(towns[arrivedList[-1] - 1])
if K <= N:
    print(arrivedList[K])
    exit()

loop_end = N
loop_start = N - 1
while (arrivedList[loop_end] != arrivedList[loop_start]):
    loop_start -= 1

loop_len = loop_end - loop_start


K = K % loop_len
while K < N:
    K += loop_len

print(arrivedList[K])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))

ans=-1
now=0
i=1
result=[0]
while i<=k:
    now=a[now]
    if now in result:
        x=result.index(now)
        length=i-x
        ans=result[(k-i)%length+x]
        break
    result.append(now)
    i+=1
    
if ans!=-1:
    print(ans+1)
else:
    print(result[-1])

Failing test case input:
4 0
3 2 1 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))

ans=-1
l=[-1]*n
now=0
i=1
result=[0]
while i<=k:
    now=a[now]
    if l[now]!=-1:
        
        length=i-l[now]
        d=(k-i)%length+l[now]
        ans=l.index(d)
        break
    l[now]=i
    i+=1
    
if ans!=-1:
    print(ans+1)
else:
    print(now+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

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

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


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

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

print(now)


Failing test case input:
6 80635290846982474
6 4 1 5 3 2

Expected output:
5


Actual output:
1


Fixed submission (verdict: Accepted):

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

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


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

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

print(now)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = input().split()
A_MAP = {}
for i in range(N):
    A_MAP[str(i+1)] = A[i]

route_hash = {'1': 0}
current_pos = '1'
next_pos = ''
counter = 0
while counter <= K:
    next_pos = A_MAP[current_pos]
    counter+=1
    if next_pos in route_hash:
        break
    route_hash[next_pos] = counter 
    current_pos = next_pos

if counter >= K:
    print(current_pos)
    exit()

remain = K - counter
counter -= route_hash[next_pos]
remain = remain % counter

current_pos = next_pos
for i in range(remain):
    next_pos = A_MAP[current_pos]
    route_hash[next_pos] = counter 
    current_pos = next_pos
print(current_pos)


Failing test case input:
4 0
3 0 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = input().split()
A_MAP = {}
for i in range(N):
    A_MAP[str(i+1)] = A[i]

route_hash = {'1': 0}
current_pos = '1'
next_pos = ''
counter = 0
breaked = False
while counter < K:
    next_pos = A_MAP[current_pos]
    counter+=1
    if next_pos in route_hash:
        breaked = True
        break
    route_hash[next_pos] = counter 
    current_pos = next_pos

if not breaked:
    print(current_pos)
    exit()

remain = K - counter
counter -= route_hash[next_pos]
remain = remain % counter

current_pos = next_pos
for i in range(remain):
    next_pos = A_MAP[current_pos]
    route_hash[next_pos] = counter 
    current_pos = next_pos
print(current_pos)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a = [[ai,0] for ai in a]
i = 1
x = 0
ans = 0
while(True):
    ai = a[i-1]
    if ai[1]>=1:
        y=k%(x+1)
        for i in range(y):
            ai = a[ai[0]-1]
        ans = ai[0]
        break
    else:
        ai[1]+=1
        x+=1
    i = ai[0]
print(ans)

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
b = [bi-1 for bi in list(map(int,input().split()))]
a = 0

f = [0]*n
c = 1
for i in range(k):
    if f[a]:
        k %= c-f[a]
        for i in range(k):
            a = b[a]
        print(a+1)
        break
    f[a] = c
    k -= 1
    c += 1
    a = b[a]
else:
    print(a+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[i-1 for i in A]
B=[0]*n
cnt=[0]*n
m=0
for i in range(n):
    B[i]=m
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
C=[i for i,x in enumerate(B) if x==m]
if len(C)>1:
    c=abs(C[0]-C[1])
else:
    c=n
print(1+B[((k-B.index(m))%c + B.index(m))%c])

Failing test case input:
6 64871420930086488
6 2 1 5 3 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):

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

A=[int(i)-1 for i in A]
B=[]
cnt=[0]*n
m=0
for i in range(2*10**5):
    B.append(m)
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
c=len(B)-(B.index(m)+1)
print(1+B[(max(k-B.index(m),0))%c + min(B.index(m),k)])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))

x = 0
l = [0]
flag = False
for i in range(N):
    x = A[x]-1
    if x==0:
        break
    if l[-1]==x:
        flag = True
        break
    l.append(x)

if not flag:
    print(l[K%len(l)]+1)
else:
    if K>=len(l):
        print(l[-1]+1)
    else:
        print(l[K]+1)

Failing test case input:
6 237500909755776657
6 4 1 5 6 4

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))

x = 0
l = [0]
s = set([0])
flag = False
for i in range(N):
    x = A[x]-1
    if x in s:
        break
    s.add(x)
    l.append(x)

for i in range(len(l)):
    if l[i]==x:
        break

y = len(l)
z = i

if K<len(l):
    print(l[K]+1)
else:
    
    
    
    
    
    print(l[z + (K-z)%(y-z)]+1)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]

route = [0]
seen = {0}
here = 0
for _ in range(K):
    here = A[here]

    if here in seen:
        start = route.index(here)
        period = len(route) - start
        
        r = (K - start + 1) % period
        if r == 0:
            idx = period + start - 1
        else:
            idx = r + start - 1
        print(route[idx] + 1)
        break
    seen.add(here)
    route.append(here)
else:
    print(route[-1])
    






Failing test case input:
4 0
3 2 1 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]

route = [0]
seen = set()
here = 0
for _ in range(N + 100):
    here = A[here]
    route.append(here)

if len(route) >= K:
    print(route[K] + 1)
else:
    for i in range(len(route)):
        if route[i] in seen:
            start = route.index(route[i])
            period = i - start
            
            r = (K - start + 1) % period
            if r == 0:
                idx = period + start - 1
            else:
                idx = r + start - 1
            print(route[idx] + 1)
            break
        seen.add(route[i])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0
t = 1
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
f = [0] * n
c = 0
d = 0
for i in range(k):
    if (f[t-1] == 1):
        d = 1
        break
    f[t-1] = 1
    t = a[t-1]
b = t
if(d == 1):
    for i in range(k):
        t = a[t-1]
        c = c + 1
        if t == b:
            break
    if c != 1:
        k = k%c
        t = 1
        for i in range(k):
            t = a[t-1]
print(t)

Failing test case input:
6 727202214173249351
4 3 2 3 3 3

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
t = 1
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
f = [0] * n
c = 0
d = 0
e = 0

for i in range(k):
    if (f[t-1] == 1):
        d = 1
        break
    f[t-1] = 1
    t = a[t-1]
    e = e + 1 


b = t
if(d == 1):
    for i in range(k):
        t = a[t-1]
        c = c + 1
        if t == b:
            break
    e = e - c 
    k = (k - e)%c 
    k = k + e 

t = 1
for i in range(k):
    t = a[t-1]
print(t)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

a = [0] + a
now = 1
cities = [now]
done = set()
for i in range(n):
    now = a[now]
    if now in done:
        break
    done.add(now)
    cities.append(now)
idx_s = cities.index(now)
f = (len(cities) - idx_s)
d = k - idx_s
idx = d%f
ans = cities[idx + idx_s]
print(ans)

Failing test case input:
6 80635290846982474
6 4 1 5 3 2

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

a = [0] + a
now = 1
cities = [now]
done = set()
done.add(now)
for i in range(n):
    now = a[now]
    if now in done:
        break
    done.add(now)
    cities.append(now)
idx_s = cities.index(now)

f = (len(cities) - idx_s)

if k > idx_s:
    d = k - idx_s
    idx = d%f
    ans = cities[idx + idx_s]
    print(ans)
else:
    print(cities[k])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
noappear=[i for i in range(n)]
while True:
        now=way[-1]
        way.append(a[now])
        
        if way[-1] in way[0:-1]:
                break


last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=k%step
if k>=en:
        ans=way[rest]+1

else:
        ans=way[k]+1
print(ans)

        

        



Failing test case input:
6 727202214173249351
6 0 3 4 3 5

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
seen=[0 for i in range(n)]
while True:
        now=way[-1]
        way.append(a[now])
        
        if seen[a[now]]==1:
                break
        seen[a[now]]=1


last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=(k-st+1)%step
if k>=st:
        if rest==0:
                ans=way[st-1+step]+1
        else:
                ans=way[st-1+rest]+1

else:
        ans=way[k]+1
print(ans)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N, K = lr()
    A = lr()
    vis = [-1]*(N+1)
    dis = [0]*(N+1)
    v = 1
    cnt = 0
    while vis[v] == -1:
        dis[cnt] = v
        vis[v] = cnt
        v = A[v-1]
        cnt += 1
    print(dis[K%(cnt-vis[v])])
resolve()

Failing test case input:
6 64871420930086488
6 2 1 5 3 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N, K = lr()
    A = lr()
    vis = [-1]*(N+1)
    dis = [0]*(N+1)
    v = 1
    cnt = 0
    while vis[v] == -1:
        dis[cnt] = v
        vis[v] = cnt
        v = A[v-1]
        cnt += 1
    if K <= vis[v]:
        print(dis[K])
    else:
        print(dis[(K-vis[v])%(cnt-vis[v])+vis[v]])
resolve()