Bsgs This is mainly used to solve this problem:
A^x=b (mod c) (c is prime), are integers, known as a, B, and C for X.
In the specific topic, C is generally the total number of all possible events.
Solution:
Set m = ceil(sqrt(C))(ceil为上取整), x = i * m + j
,
So A^x = (A^m)^i * A^j, (0 <= i < m, 0 <= j < m)
.
You can then enumerate i
the O(sqrt(C))
levels of the enumeration.
For an enumeration, the i
D = (A^m)^i
present problem is translated into a request D * A^j ≡ B (mod C)
.
If it is A^j
regarded as a whole, then the EXGCD can be solved (and because C is a prime number, A is a multiple of C is easy to contract, besides, there must be
(D, C) = 1
, so there must be a solution): EXGCD to find the inverse, you can refer to: http://www.cnblogs.com/PJQOOO/p/3873654.html
Find out A^j
, now the question is how do I know how j
much?
The first O(sqrt(C))
time, will be A^j
all stored in the hash table inside. Then just look O(1)
up the table and know how much it is in the time j
.
Bsgs_baby steps giant Steps algorithm