BZOJ 4522: [Cqoi2016]密钥破解 (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板题)的更多相关文章
- BZOJ 4522: [Cqoi2016]密钥破解
http://www.lydsy.com/JudgeOnline/problem.php?id=4522 题目:给你RSA密钥的公钥和密文,求私钥和原文,其中\(N=pq\le 2^{62}\),p和 ...
- BZOJ 4522: [Cqoi2016]密钥破解 exgcd+Pollard-Rho
挺简单的,正好能再复习一遍 $exgcd$~ 按照题意一遍一遍模拟即可,注意一下 $pollard-rho$ 中的细节. #include <ctime> #include <cma ...
- BZOJ4522: [Cqoi2016]密钥破解
pollard's rho模板题. 调参调到160ms无能为力了,应该是写法问题,不玩了. #include<bits/stdc++.h> using namespace std; typ ...
- LG4718 【模板】Pollard-Rho算法 和 [Cqoi2016]密钥破解
Pollard-Rho算法 总结了各种卡常技巧的代码: #define int long long typedef __int128 LL; IN int fpow(int a,int b,int m ...
- BZOJ4522:[CQOI2016]密钥破解(Pollard-Rho,exgcd)
Description 一种非对称加密算法的密钥生成过程如下: 1. 任选两个不同的质数 p ,q 2. 计算 N=pq , r=(p-1)(q-1) 3. 选取小于r ,且与 r 互质的整数 e ...
- [CQOI2016]密钥破解
嘟嘟嘟 这题我读了两遍才懂,然后感觉要解什么高次同余方程--然后我又仔细的看了看题,发现只要求得\(p\)和\(q\)就能求出\(r\),继而用exgcd求出\(d\),最后用快速幂求出\(n\). ...
- BZOJ 3932: [CQOI2015]任务查询系统 (主席树板题)
就是裸的主席树,差分之后排序插入主席树就行了. 注意主席树查询的时候叶子节点要特判,因为本身是有size的 还有要开longlong CODE #include <cctype> #inc ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- 【Luogu】P4358密钥破解(Pollard Rho)
题目链接 容易发现如果我们求出p和q这题就差不多快变成一个sb题了. 于是我们就用Pollard Rho算法进行大数分解. 至于这个算法的原理,emmm 其实也不是很清楚啦 #include<c ...
随机推荐
- 关于spring 事务 和 AOP 管理事务和打印日志问题
关于spring 事务 和 AOP 管理事务和打印日志问题 1. 就是支持事务注解的(@Transactional) . 可以在server层总使用@Transactional,进行方法内的事务管 ...
- win10现在安装redis
一.下载: 下载地址: https://github.com/MicrosoftArchive/redis/releases 根据系统下载的版本:以(64位为例) 下载后一般解压到根目录下:如(E:\ ...
- 【51nod】2027 期望问题
[51nod]2027 期望问题 %%%zsy 看不懂题解的垃圾选手在zsy大佬的讲解下终于知道了这道题咋做-- 先把所有\(a\)从大到小排序 设\(f_{i}\)为前\(i\)个数组成的排列的值, ...
- 洛谷 P2746 [USACO5.3]校园网Network of Schools
题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...
- 【数据结构】P1996 约瑟夫问题
[题目链接] https://www.luogu.org/problem/P1996 题目描述 n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数, ...
- ingress安装配置
Traefik Traefik 是一款开源的反向代理与负载均衡工具.它最大的优点是能够与常见的微服务系统直接整合,可以实现自动化动态配置.目前支持 Docker.Swarm.Mesos/Maratho ...
- Python Socket套接字编程
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- javascript——获取元素方式
//1:依据id //var element = document.getElementById("test"); console.log(element); //2:依据clas ...
- JS经典算法
寻找500以内能被5和7整除的数字:for(var num=1;num<=500&&num++;) if(num%7==0&&num%5==0){ consol ...
- vue路径中的#号
最近学习vue过程中,发现路径当中总是存在一个#号,比如这个: 这种情况是因为在入口js文件中,如果你不更改设置的话,vue会默认使用hash模式,该模式下回将路径格式化为 # 开头. 如果需要美化路 ...