Problem p02684 (python)

Fixed submission:
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=1
cnt=1
zahyo=0
ans=0
kari.add(zahyo)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break

hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
count=len(kari)
cnt=len(hako)
kaisu=K-count
#print(zahyo)
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
#print(zahyo,kaisu,count,cnt)
#print(kari,hako)
print(zahyo+1)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=0
cnt=0
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
zahyo+=1
hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
    #print(zahyo,kaisu,count,cnt)
print(zahyo+1)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=0
cnt=0
zahyo=0
ans=0
kari.add(0)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
zahyo+=1
hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1
    print(zahyo,kaisu,count,cnt)
print(zahyo+1)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=1
cnt=0
zahyo=0
kari.add(0)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
kaisu=K-count
kaisu=kaisu//cnt+kaisu%cnt
for i in range(kaisu-1):
    zahyo=A[zahyo]
print(zahyo)

Buggy submission (verdict: Runtime Error):
from random import randint
import sys #追加
sys.setrecursionlimit(10**8)
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]
def saiki(B):
    if len(B)==K+1:
        ans.append(B)
        print(B[-1])
        return
    B.append(A[B[-1]-1])
    saiki(B)
saiki([1])
#print(ans)

Buggy submission (verdict: Runtime Error):
import sys #追加
sys.setrecursionlimit(500*500)
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]
def saiki(B):
    if len(B)==K+1:
        ans.append(B)
        print(B[-1])
        return
    B.append(A[B[-1]-1])
    saiki(B)
saiki([1])
#print(ans)

Buggy submission (verdict: Runtime Error):
from sys import
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]
def saiki(B):
    if len(B)==K+1:
        ans.append(B)
        print(B[-1])
        return
    B.append(A[B[-1]-1])
    saiki(B)
saiki([1])
#print(ans)