Код правильный, полного перебора обоих списков не производится.
def fastpower(base, exponent, mod=1):
res = 1
while(exponent > 0):
if exponent % 2 == 0:
exponent /= 2
base = mod > 1 and (base*base)%mod or base*base
else:
exponent-=1
res = mod > 1 and (res*base)%mod or res*base
return mod > 1 and res%mod or res
# searching X in A^X mod P = Y
def steps(a,p,y):
k = int (math.sqrt(p))
m = k + 2
child = [y%p,(y*a)%p]
for i in range (2, m):
t = fastpower(a,i)
t *= y
t %= p
child.append (int(t))
child.sort()
giant = []
for i in range (1, k + 1):
giant.append (int(fastpower(a,i*m,p)))
giant.sort()
c,g = 0,0
result = []
while True:
if child[c] is giant[g]:
x = (c+1) * m - (g+1)
result.append(x)
c += 1
g += 1
elif child[c] < giant[g]:
c += 1
elif child[c] > giant[g]:
g += 1
if c >= len(child)-1:
c = len(child)-1
for gp in range(g,len(giant)):
if child[c] is giant[gp]:
x = (c+1) * m - (gp+1)
result.append(x)
return result
if g >= len(giant)-1:
g = len(giant)-1
for cp in range(c,len(child)):
if child[cp] is giant[g]:
x = (cp+1) * m - (g+1)
result.append(x)
return result
return result