Pagini recente » Diferente pentru blog/meet-in-the-middle intre reviziile 86 si 87 | Diferente pentru pregatire-online-pentru-bacalaureatul-la-informatica intre reviziile 5 si 3 | Diferente pentru blog/meet-in-the-middle intre reviziile 28 si 123 | Atasamentele paginii Profil coolback | Diferente pentru blog/meet-in-the-middle intre reviziile 123 si 121
Nu exista diferente intre titluri.
Diferente intre continut:
== code(c) |
def 4sum(A):
sums = {}
S = {}
for a in A:
for b in A:
sums[a + b] = (a, b)
S[a + b] = (a, b)
for c in A:
for d in A:
if -(c + d) in sums:
print (sums[-(c + d)][0], sums[-(c + d)][1], c, d)
print (S[-(c + d)][0], S[-(c + d)][1], c, d)
return
print "No solution."
The naive solution goes through all possible values of k and takes $O(n)$ time.
The baby-step, giant-step algorithm solves the problem more efficiently using the meet in the middle trick.
Let's write k = $i[sqrt(n)] + j$
Let's write k = $i([sqrt(n)] + 1) + j$
Notice that $i <= sqrt(n)$ and $j <= sqrt(n)$.
Replacing k in the equality we get $p^(i[sqrt(n)] + j)^ = q (mod n)$.
Dividing by $p^j^$ we get $p^i[sqrt(n)]^ = qp^-j^ (mod n)$.
Replacing k in the equality we get $p^(i ([sqrt(n)] + 1) + j)^ = q (mod n)$.
Dividing by $p^j^$ we get $p^(i[sqrt(n)] + 1)^ = qp^-j^ (mod n)$.
At this point we can brute force through the numbers on each side of the equality and find a colision.
The algorithm takes $O(sqrt(n))$ space and $O(sqrt(n))$ time.
Diferente intre securitate:
Topicul de forum nu a fost schimbat.