Problem p02684 (python)

Fixed submission:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

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

roop = [0]
for i in range(3*n):
    roop.append(a[roop[i]])
cnt =[0]*n
d = []
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=3 and cnt[roop[i]]<4:
        d.append(roop[i])
for i in range(len(roop)):
    if roop[i] == d[0]:
        check = i
        break
if check == 0:
    k%=len(d)
    print(d[k]+1)
else:
    if k<check + len(d):
        print(roop[k]+1)
    else:
        k-=check
        k%=len(d)
        print(d[k]+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

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

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break

if k<=len(s):
    print(a[k])
else:
    print(a[check +(k-check)%len(s)]+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

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

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break

if k<=len(s2):
    print(a[k-1]+1)
else:
    k-=check
    k%=len(s)
    print(int(s[k])+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

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

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break

if k<=len(s2):
    print(a[k-1]+1)
else:
    k-=check
    k%=len(s)
    print(a[check + k]+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

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

roop = []
pre = 0
for i in range(2*n):
    roop.append(pre)
    pre = a[pre]
cnt = [0]*n
s = ""
for i in range(len(roop)):
    cnt[roop[i]]+=1
    if cnt[roop[i]]>=2 and cnt[roop[i]]<3:
        s += str(roop[i])
ff = int(s[0])
s2 = s
for i in range(len(roop)):
    if roop[i]!=ff:
        s2  = str(roop[i]) + s2
    else:
        check = i
        break
k -= check
k%=len(s)
print(a[check+k]+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(len(a)):
    a[i] -= 1
pre = 0
d = []
for i in range(2*n+10):
    d.append(pre)
    pre = a[pre]
cnt = [0]*(n)
s = ""
for i in range(len(d)):
    cnt[d[i]]+=1
    if cnt[d[i]]>=2 and cnt[d[i]]<3:
        s += str(d[i])
ff = int(s[0])
check = 0
for i in range(len(d)):
    if ff == d[i]:
        check = i
        break
if check == 0:
    k %= len(s)
    print(a[k]+1)
else:
    k-= check
    k%=len(s)
    print(int(s[k])+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(len(a)):
    a[i] -= 1
pre = 0
d = []
for i in range(2*n+10):
    d.append(pre)
    pre = a[pre]
cnt = [0]*(n)
s = ""
for i in range(len(d)):
    cnt[d[i]]+=1
    if cnt[d[i]]>=3 and cnt[d[i]]<4:
        s += str(d[i])
ff = int(s[0])
for i in range(len(d)):
    if ff == d[i]:
        check = i
        break
if check == 0:
    k %= len(s)
    print(a[k]+1)
else:
    k-= check
    k%=len(s)
    print(int(s[k])+1)

Buggy submission (verdict: Runtime Error):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(len(a)):
    a[i] -= 1
pre = 0
d = []
for i in range(2*n+10):
    d.append(pre)
    pre = a[pre]
cnt = [0]*(n)
s = ""
for i in range(len(d)):
    cnt[d[i]]+=1
    if cnt[d[i]]>=3 and cnt[d[i]]<4:
        s += str(d[i])
ff = int(s[0])
for i in range(len(d)):
    if ff == d[i]:
        check = i
        break

k%=len(s)
print(a[k]+1)