一道裸的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 }

View Code

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

转载于:https://www.cnblogs.com/rausen/p/4263251.html

BZOJ3239 Discrete Logging相关推荐

  1. Discrete Logging POJ - 2417(BSGS)

    Discrete Logging POJ - 2417 题意:给P,B,N,求最小的L使得 BL≡N (mod P),其中P是素数. Baby Step Giant Step 1 #include & ...

  2. POJ 2417 Discrete Logging 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

  3. POJ2417 Discrete Logging | A,C互质的bsgs算法

    题目: 给出A,B,C 求最小的x使得Ax=B  (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...

  4. Discrete Logging hunnu10590 pku2417 fzu 1352 hit 1928 zoj 1898

    以下转自:http://hi.baidu.com/aekdycoin/blog/item/b317ca18bb24334942a9ad55.html [普通Baby Step Giant Step] ...

  5. poj 2417 Discrete Logging

    http://poj.org/problem?id=2417 BSGS,直接套之前那题.不过我在multiMod函数里进行了修改,这样这个代码如果输入只是32位有符号数,就不用怕溢出了! 代码如下: ...

  6. Discrete Logging ZOJ - 1898 (模板题大小步算法)

    就是求Ax三B(mod C)当C为素数时 #include<cstdio> #include<cstring> #include<cmath> #include&l ...

  7. BZOJ 3239 Discrete Logging Baby-Step-Giant-Step

    题目大意:给定P,B,N,求最小的L使B^L≡N (mod P) (P是质数) 裸的BSGS... 练练手吧- - #include <cmath> #include <cstdio ...

  8. 数论知识总结——史诗大作(这是一个flag)

    1.快速幂 计算a^b的快速算法,例如,3^5,我们把5写成二进制101,3^5=3^1*1+3^2*2+3^4*1 1 ll fast(ll a,ll b){ll ans=1;for(;b;b> ...

  9. 重走长征路---OI每周刷题记录---1月17日 2015

    总目录详见https://blog.csdn.net/mrcrack/article/details/84471041 做题原则,找不到测评地址的题不做.2018-11-28 重走长征路---OI每周 ...

最新文章

  1. mysql 1418 错误原因及解决
  2. mysql ogg_异构平台mysql-oracle(ogg)安装部署
  3. C语言那点事——如何从零学好C语言?
  4. [转载] Python中产生随机数
  5. Ubuntu上面安装Mongo
  6. SA / SAM 题目集
  7. promise重新认识
  8. putty GB2312 BIG5 中文字符集补丁
  9. 安全跑路指南2013之乌云版
  10. 修改SRVINSTW支持sys文件
  11. 小鹏汽车面试经验分享
  12. XRHT系列电钢琴实训室配置方案及清单
  13. Android音视频——Libyuv使用实战
  14. 计算机加法器原理,加法器原理
  15. ST7735 TFT显示屏 显示问题
  16. js每日一题(10)
  17. pdf编辑软件哪个好用?这个办法值得一试
  18. 公网IP/内网IP:
  19. Python入门(二)之Python的集成开发环境(IDE)
  20. 为什么你的IntelliJ IDEA没有别人的好用?差距在这...

热门文章

  1. Scala apply()方法用于创建伴生类对象
  2. MySQL ADDDATE(d,n) 计算起始日期 d 加上 n 天的日期
  3. tensorflow 按维度相加_人工智能 TensorFlow 必知必会编程概念整理
  4. 图说开源许可协议:GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
  5. 外部中断0——51程序
  6. 1在mysql进行定义操作系统_Mysql基础知识一
  7. LwIP 之一 源码目录文件详解及移植说明
  8. Qt 编译时报错“退出,退出代码2”的原因
  9. mysql字段名大写好处_数据库的表名字段名大小写问题
  10. Zynq-7000系统公共资源及特性