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));
}

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. poj2253(floyd变形)

    题目链接:https://vjudge.net/problem/POJ-2253 题意:给出n个点的坐标,求点1到点2的forg distance,其定义为点1到点2的所有路径中最长边的最小值. 思路 ...

  2. hdoj1561 The more, The Better (树形dp,分组背包)

    题目链接:https://vjudge.net/problem/HDU-1561 题意:给一个森林,每个结点有个权值,求选m个结点的最大权值和,并且选子结点前必须先选父结点. 思路: 把每颗树的树根连 ...

  3. [Cometoj#3 D]可爱的菜菜子_线段树_差分_线性基

    可爱的菜菜子 题目链接:https://cometoj.com/contest/38/problem/D?problem_id=1543 数据范围:略. 题解: 首先,如果第一个操作是单点修改,我们就 ...

  4. lua table 的操作(四)

    table在前面作过介绍,它是一种关联数组,这种关联指的是可以设置各类类型的key来存储值. 1.table 间的数据传递 -- 为 table a 并设置元素,然后将 a 赋值给 b,则 a 与 b ...

  5. vue中可以自定义动画的前缀

    vue中可以自定义动画的前缀1.只需在中加入name属性即可 <transition name="my"> <h6 v-if="flag2"& ...

  6. linux 下tomcat出现 Native memory allocation (malloc) failed to allocate 1915224064 bytes for committing reserved memory问题

    ## There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocat ...

  7. 小菜鸟之shell

    Linux shell编程 目录 什么是Shell 1 Shell脚本的执行方式 1 第一种:输入脚本的绝对路径或相对路径 1 第二种:bash或sh +脚本 1 Shell中的变量 2 定义变量 2 ...

  8. 【第一季】CH09_FPGA多路分频器设计

    [第一季]CH09_FPGA多路分频器设计 在第七节的学习中,笔者带大家通过一个入门必学的流水灯实验实现,快速掌握了VIVADO基于FPGA开发板的基本流程.考虑到很多初学者并没有掌握好Vivado ...

  9. redis 交集、并集、差集

    sinter .sunion .sdiff redis 支持 Set集合的数据存储,其中有三个比较特殊的方法: sinter key [key …] 返回一个集合的全部成员,该集合是所有给定集合的交集 ...

  10. java EE加载peoperties配置文件

    //加载配置文件 InputStream in = JedisUtils.class.getClassLoader().getResourceAsStream("redis.properti ...