Problem p02684 (python)

Fixed submission:
looplenn,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)//2
        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]

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

#print(know,start,loop)

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


print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a

state = 1
connect = [1]
looplen= 10**18
for n in range(N):
    state = teleport[state] 
    print(state)
    print(connect)
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
    else:
        connect.append(state)
        
index = (K-loop_init)%looplen
end = connect[loop_init+index]
print(end)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a

state = 1
connect = [1]
looplen= 10**18
for n in range(N):
    state = teleport[state] 
    #print(state)
    #print(connect)
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
    else:
        connect.append(state)
        
index = (k-loop_init)%looplen
end = connect[loop_init+index]
print(end)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a

state = 1
connect = []
looplen= 10**18
for n in range(K):
    connect.append(state)
    state = teleport[state] 
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
index = (k-loop_init)%looplen
end = connect[index+loop_init]
print(end)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a
# teleport

state = 1
connect = []
looplen= 0
for n in range(K):
    connect.append(state)
    state = teleport[state] 
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
index = (k-loop_init)%looplen
end = connect[index+loop_init]
print(end)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a
# teleport

state = 1
connect = []
looplen= 0
for n in range(K):
    connect.append(state)
    state = teleport[state] 
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1   #0->0なら1
        #print(connect.index(state))
        #print(n)
        break
init
index = (k-init)%looplen
end = connect[index+init]
print(end)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a
# teleport

state = 1
connect = []
looplen= 0
for n in range(K):
    connect.append(state)
    state = teleport[state] 
    if state in connect:
        looplen = n-connect.index(state)+1
        # print(connect.index(state))
        # print(n)
        break
connect
index = k%looplen
end = connect[index]
print(end)