Pollard-Rho 模板

板题…没啥说的…

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

CODE

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
queue<int>arr;
inline LL multi(LL a, LL b, LL p) { //快速乘
LL re = a * b - (LL)((long double) a / p * b + 1e-8) * p;
return re < 0 ? re + p : re;
}
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
inline LL qpow(LL a, LL b, LL p) {
LL re = 1;
while(b) {
if(b&1) re = multi(re, a, p);
a = multi(a, a, p); b >>= 1;
}
return re;
}
inline LL Pollard_Rho(LL n, int sed) {
LL i = 1, k = 2, x = rand()%(n-1)+1, y = x;
while(true) {
x = (multi(x, x, n) + sed) % n;
LL p = gcd(n, (y-x+n)%n);
if(p != 1 && p != n) return p;
if(y == x) return n;
if(++i == k) y = x, k <<= 1;
}
}
LL x[100];
inline bool MR(LL n) {
if(n == 2) return 1;
int s = 20, t = 0; LL u = n-1;
while(!(u&1)) ++t, u>>=1;
while(s--) {
LL a = rand()%(n-2) + 2;
x[0] = qpow(a, u, n);
for(int i = 1; i <= t; ++i) {
x[i] = multi(x[i-1], x[i-1], n);
if(x[i] == 1 && x[i-1] != 1 && x[i-1] != n-1) return 0;
}
if(x[t] != 1) return 0;
}
return 1;
}
void find(LL n, int sed) {
if(n == 1) return;
if(MR(n)) { arr.push(n); return; }
LL p = n; int k = sed;
while(p == n) p = Pollard_Rho(p, sed--);
find(p, k);
find(n/p, k);
}
LL p, q, e, d, N, c, tmp, Z;
void exgcd(LL a, LL b, LL &x, LL &y, LL &Z) {
if(!b) { x = 1; y = 0; Z = a; return; }
exgcd(b, a%b, y, x, Z); y -= x*(a/b);
}
int main()
{
srand(19260817);
scanf("%lld%lld%lld", &e, &N, &c);
find(N, 107);
p = arr.front(), arr.pop();
q = arr.front(), arr.pop();
exgcd(e, (p-1)*(q-1), d, tmp, Z);
Z = (p-1)*(q-1)/Z;
d = (d % Z + Z) % Z;
printf("%lld %lld\n", d, qpow(c, d, N));
}

最新文章

  1. phpexcel读取输出操作
  2. MYSQL的常用命令和增删改查语句和数据类型
  3. 提取刷机包内system.new.dat文件
  4. hive中rcfile格式(收藏文)
  5. 2 个UserControl 的传值问题
  6. 动态链接库(VC_Win32)
  7. 认清Linux中标准输入和标准输出的双重含义
  8. MySQL压测中遇到的一些问题
  9. UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
  10. bzoj 1208 宠物收养所--splay
  11. Java泛型的基本应用
  12. java语言基础(变量和运算符)
  13. 201421123042 《Java程序设计》第10周学习总结
  14. 技术Leader相关文章和思考
  15. 微信小程序提交审核并发布详细流程
  16. 流媒体技术学习笔记之(十八)Ubuntu 16.04.3 如何编译 FFmpeg 记录
  17. Centos6安装FreeSWITCH 1.5时./configure问题解决记录
  18. [CQOI2017]老C的任务
  19. 在推送提交之后阻止Azure DevOps (TFS)持续集成
  20. [JSOI2007]文本生成器(AC自动机,DP)

热门文章

  1. jumpserver0.4.0与python3版本安装
  2. [转帖]【Oracle】详解Oracle中NLS_LANG变量的使用
  3. [转帖]SSH远程登录配置文件sshd_config详解
  4. 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0ax 2 +bx+c=0 的两个解。
  5. win10系统查看激活状态及是否永久激活
  6. Redis 消息队列 初体验
  7. dotnet跨平台 - 使用Nginx+Docker Compose运行.NETCore项目
  8. Spring切面编程Aspect之@Before和@Around用法
  9. ES6入门六:class的基本语法、继承、私有与静态属性、修饰器
  10. 让theano在windows下能进行GPU并行的配置步骤