Pollard-Rho 模板

板题…没啥说的…

求逆元出来后如果是负的记得加回正数

CODE

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. queue<int>arr;
  5. inline LL multi(LL a, LL b, LL p) { //快速乘
  6. LL re = a * b - (LL)((long double) a / p * b + 1e-8) * p;
  7. return re < 0 ? re + p : re;
  8. }
  9. LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
  10. inline LL qpow(LL a, LL b, LL p) {
  11. LL re = 1;
  12. while(b) {
  13. if(b&1) re = multi(re, a, p);
  14. a = multi(a, a, p); b >>= 1;
  15. }
  16. return re;
  17. }
  18. inline LL Pollard_Rho(LL n, int sed) {
  19. LL i = 1, k = 2, x = rand()%(n-1)+1, y = x;
  20. while(true) {
  21. x = (multi(x, x, n) + sed) % n;
  22. LL p = gcd(n, (y-x+n)%n);
  23. if(p != 1 && p != n) return p;
  24. if(y == x) return n;
  25. if(++i == k) y = x, k <<= 1;
  26. }
  27. }
  28. LL x[100];
  29. inline bool MR(LL n) {
  30. if(n == 2) return 1;
  31. int s = 20, t = 0; LL u = n-1;
  32. while(!(u&1)) ++t, u>>=1;
  33. while(s--) {
  34. LL a = rand()%(n-2) + 2;
  35. x[0] = qpow(a, u, n);
  36. for(int i = 1; i <= t; ++i) {
  37. x[i] = multi(x[i-1], x[i-1], n);
  38. if(x[i] == 1 && x[i-1] != 1 && x[i-1] != n-1) return 0;
  39. }
  40. if(x[t] != 1) return 0;
  41. }
  42. return 1;
  43. }
  44. void find(LL n, int sed) {
  45. if(n == 1) return;
  46. if(MR(n)) { arr.push(n); return; }
  47. LL p = n; int k = sed;
  48. while(p == n) p = Pollard_Rho(p, sed--);
  49. find(p, k);
  50. find(n/p, k);
  51. }
  52. LL p, q, e, d, N, c, tmp, Z;
  53. void exgcd(LL a, LL b, LL &x, LL &y, LL &Z) {
  54. if(!b) { x = 1; y = 0; Z = a; return; }
  55. exgcd(b, a%b, y, x, Z); y -= x*(a/b);
  56. }
  57. int main()
  58. {
  59. srand(19260817);
  60. scanf("%lld%lld%lld", &e, &N, &c);
  61. find(N, 107);
  62. p = arr.front(), arr.pop();
  63. q = arr.front(), arr.pop();
  64. exgcd(e, (p-1)*(q-1), d, tmp, Z);
  65. Z = (p-1)*(q-1)/Z;
  66. d = (d % Z + Z) % Z;
  67. printf("%lld %lld\n", d, qpow(c, d, N));
  68. }

BZOJ 4522: [Cqoi2016]密钥破解 (Pollard-Rho板题)的更多相关文章

  1. BZOJ 4522: [Cqoi2016]密钥破解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4522 题目:给你RSA密钥的公钥和密文,求私钥和原文,其中\(N=pq\le 2^{62}\),p和 ...

  2. BZOJ 4522: [Cqoi2016]密钥破解 exgcd+Pollard-Rho

    挺简单的,正好能再复习一遍 $exgcd$~ 按照题意一遍一遍模拟即可,注意一下 $pollard-rho$ 中的细节. #include <ctime> #include <cma ...

  3. BZOJ4522: [Cqoi2016]密钥破解

    pollard's rho模板题. 调参调到160ms无能为力了,应该是写法问题,不玩了. #include<bits/stdc++.h> using namespace std; typ ...

  4. LG4718 【模板】Pollard-Rho算法 和 [Cqoi2016]密钥破解

    Pollard-Rho算法 总结了各种卡常技巧的代码: #define int long long typedef __int128 LL; IN int fpow(int a,int b,int m ...

  5. BZOJ4522:[CQOI2016]密钥破解(Pollard-Rho,exgcd)

    Description 一种非对称加密算法的密钥生成过程如下: 1. 任选两个不同的质数 p ,q 2. 计算 N=pq , r=(p-1)(q-1) 3. 选取小于r ,且与 r 互质的整数 e  ...

  6. [CQOI2016]密钥破解

    嘟嘟嘟 这题我读了两遍才懂,然后感觉要解什么高次同余方程--然后我又仔细的看了看题,发现只要求得\(p\)和\(q\)就能求出\(r\),继而用exgcd求出\(d\),最后用快速幂求出\(n\). ...

  7. BZOJ 3932: [CQOI2015]任务查询系统 (主席树板题)

    就是裸的主席树,差分之后排序插入主席树就行了. 注意主席树查询的时候叶子节点要特判,因为本身是有size的 还有要开longlong CODE #include <cctype> #inc ...

  8. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  9. 【Luogu】P4358密钥破解(Pollard Rho)

    题目链接 容易发现如果我们求出p和q这题就差不多快变成一个sb题了. 于是我们就用Pollard Rho算法进行大数分解. 至于这个算法的原理,emmm 其实也不是很清楚啦 #include<c ...

随机推荐

  1. P5441 【XR-2】伤痕

    Luogu5441 有 \(n\) 个点 ( \(n\) 为奇数 , \(n \le 99\) ) 的完全图 , 其中可以有最多 \(n\) 条无向边 , 其他都是有向边 . 如果对于某四个点不经过这 ...

  2. Flutter、Weex、RN,Native对比

  3. Javascript性能优化阅读笔记

    第一章 加载和执行 大多数浏览器都是用单一进程处理UI界面的刷新和JavaScript的脚本执行,所以同一时间只能做一件事,Javascript执行过程耗时越久,浏览器等待响应的时间就越长. 所以,H ...

  4. K60工程

    使用arm-none-eabi-objcopy工具将elf文件转换为hex文件 "D:/ELF/arm-none-eabi-objcopy.exe" -O ihex "D ...

  5. 利用神器BTrace 追踪线上 Spring Boot应用运行时信息

    概述 生产环境中的服务可能会出现各种问题,但总不能让服务下线来专门排查错误,这时候最好有一些手段来获取程序运行时信息,比如 接口方法参数/返回值.外部调用情况 以及 函数执行时间等信息以便定位问题.传 ...

  6. SysInternals提供了一个工具RamMap,可以查看内存的具体使用情况

    SysInternals提供了一个工具RamMap,可以查看内存的具体使用情况.如果发现是Paged Pool和Nonpaged Pool占用过大,可以用另一个工具poolmon来查看占用内存的驱动T ...

  7. springboot application.properties配置大全

    springboot application.properties配置大全 官方文档 https://docs.spring.io/spring-boot/docs/current/reference ...

  8. JS基础_立即执行函数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. [NOIP2018模拟10.15]比赛报告

    闲扯 昨晚又颓到好晚,Yali的降智光环感觉持续至今... 题面好评 T1T3都玩过 逃) T1没看多久就开始写二分+并查集 然后T3看着眼熟想了一个多小时...结果啥都没想出来 赶紧看T2发现还是没 ...

  10. ssh下known_hosts的作用

    原文地址:http://blog.csdn.net/yasaken/article/details/7348441 在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁 ...