BZOJ3239 Discrete Logging

一道裸的BSGS题目(叫baby step, giant step)

爱酱的blog里学来的,是一个很神的根号算法。

如果我们有hash的工具的话,就是O(sqrt(p))的,这里又用了一个map所以是O(sqrt(p) * log(sqrt(p)))

 1 /**************************************************************
 2     Problem: 3239
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:280 ms
 7     Memory:2932 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <cmath>
12 #include <map>
13
14 using namespace std;
15 typedef long long ll;
16
17 ll p, y, z, B;
18 map <ll, int> mp;
19
20 ll pow(ll a, ll x) {
21   a %= p;
22   ll res = 1, base = a;
23   while (x) {
24     if (x & 1) (res *= base) %= p;
25     (base *= base) %= p;
26     x >>= 1;
27   }
28   return res;
29 }
30
31 int main() {
32   ll i, now, base, tmp;
33   while (scanf("%lld%lld%lld", &p, &y, &z) != EOF) {
34     mp.clear();
35     y %= p;
36     if (!y) {
37       if (!z) puts("1");
38       else puts("no solution");
39       goto end_of_work;
40     }
41     B = (int) ceil(sqrt(p)), now = 1;
42     mp[1] = B + 1;
43     for (i = 1; i < B; ++i) {
44       (now *= y) %= p;
45       if (!mp[now]) mp[now] = i;
46     }
47     now = 1, base = pow(y, p - B - 1);
48     for (i = 0; i < B; ++i) {
49       tmp = mp[z * now % p];
50       if (tmp) {
51     if (tmp == B + 1) tmp = 0;
52     printf("%lld\n", i * B + tmp);
53     goto end_of_work;
54       }
55       (now *= base) %= p;
56     }
57     puts("no solution");
58   end_of_work:;
59   }
60   return 0;
61 }

(p.s. bz上开了O2,所以还不是很慢恩!)

时间: 2024-10-01 05:24:33

BZOJ3239 Discrete Logging的相关文章

Discrete Logging POJ - 2417(BSGS)

Discrete Logging POJ - 2417 题意:给P,B,N,求最小的L使得 BL≡N (mod P) Baby Step Giant Step 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #define ll long long 6 using namespace std; 7 const int maxn=76543;

Discrete Logging(poj2417)

Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5120   Accepted: 2319 Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, b

poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)

http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B,C,求x. 这里C是素数,可以用普通的baby_step. 在寻找最小的x的过程中,将x设为i*M+j.从而原始变为A^M^i * A^j = B(mod C),D = A^M,那么D^i * A^j = B(mod C ), 预先将A^j存入hash表中,然后枚举i(0~M-1),根据扩展欧几里得求出A^j,再去hash表中查找相应的j,那么x = i*M+j. 确定x是否有解,就是在循环i

POJ 2417 Discrete Logging ( Baby step giant step )

Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, b

POJ 2417 Discrete Logging BSGS

Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4011   Accepted: 1849 Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, b

POJ 2417 Discrete Logging 离散对数

链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 以下转载自:AekdyCoin [普通Baby Step Giant Step] [问题模型] 求解 A^x = B (mod C) 中 0 <= x < C 的解,C 为素数 [思路] 我们可以做一个等价 x = i * m + j  ( 0 <= i < m, 0 <=j < m) m = Ceil ( sqrt( C

BSGS算法+逆元 POJ 2417 Discrete Logging

POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accepted: 2211 Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarith

[poj 2417]Discrete Logging 数论 BSGS

Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3516   Accepted: 1651 Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P.

POJ2417 Discrete Logging

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the disc