http://www.lydsy.com/JudgeOnline/problem.php?id=4522

题目:给你RSA密钥的公钥和密文,求私钥和原文,其中\(N=pq\le 2^{62}\),p和q为质数。

RSA加密算法:https://en.wikipedia.org/wiki/RSA_(cryptosystem)

N的范围较小,我们可以用pollard-rho算法在期望O(N^0.25)的时间把N分解出来,用exgcd求逆元之后直接代入。

因为懒,写了一个很短的模板。速度还行,进了第一页。

upd:更新了模板,在另一篇文章里面。

/**************************************************************
Problem: 4522
User: will7101
Language: C++
Result: Accepted
Time:392 ms
Memory:1292 kb
****************************************************************/ #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int MAXN = 100005;
ll mmul(ll a, ll b, ll m){ll r=0;for(;b;b>>=1,a=a+a>=m?a+a-m:a+a)if(b&1)r=r+a=>m?r+a-m:r+a;return r;}
ll mpow(ll a, ll b, ll m){ll r=1;for(;b;b>>=1,a=mmul(a,a,m))if(b&1)r=mmul(r,a,m);return r;}
ll gcd(ll a, ll b){return a?gcd(b%a,a):b;}
ll exgcd(ll a, ll b, ll &x, ll &y){
if(a==0) return x=0, y=1, b;
ll t=exgcd(b%a, a, y, x);
x-=y*(b/a); return t;
}
ll work(ll n, ll c)
{
ll x, y;
x=y=rand();
while(1){
x=(mmul(x,x,n)+c)%n;
y=(mmul(y,y,n)+c)%n;
y=(mmul(y,y,n)+c)%n;
if(x==y) return 1;
ll p=gcd(x>y?x-y:y-x, n);
if(p>1) return p;
}
}
int main()
{
srand(19260817);
ll e, n, C, p, q, c, r, x, y;
cin>>e>>n>>C;
c=1;
while((p=work(n, c))==1) c++;
q=n/p; r=(p-1)*(q-1);
exgcd(e, r, x, y); x=(x+r)%r;
cout<<x<<' '<<mpow(C,x,n);
return 0;
}

BZOJ 4522: [Cqoi2016]密钥破解的更多相关文章

  1. BZOJ 4522: [Cqoi2016]密钥破解 (Pollard-Rho板题)

    Pollard-Rho 模板 板题-没啥说的- 求逆元出来后如果是负的记得加回正数 CODE #include<bits/stdc++.h> using namespace std; ty ...

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

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

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

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

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

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

  5. BZOJ4522: [Cqoi2016]密钥破解

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

  6. [CQOI2016]密钥破解

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

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

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

  8. LibreOJ2045 - 「CQOI2016」密钥破解

    Portal Description 给出三个正整数\(e,N,c(\leq2^{62})\).已知\(N\)能表示成\(p\cdot q\)的形式,其中\(p,q\)为质数.计算\(r=(p-1)( ...

  9. Visio Premium 2010密钥+破解激活方法

    Visio Premium 2010密钥+破解激活方法: 在安装时能够使用下面密钥: GR24B-GC2XY-KRXRG-2TRJJ-4X7DC VWQ6G-37WBG-J7DJP-CY66Y-V27 ...

随机推荐

  1. JavaScript 面向对象 原型(prototype) 继承

    1.对象的概念:无需属性的集合,属性可以为数值,对象或函数,ECMAscript中没有类的概念,这点是javascript与其他面向对象(OO)语言不同的地方. //创建一个自定义对象 var per ...

  2. postgres(pl/pgsql)

    复制后期看 https://www.cnblogs.com/stephen-liu74/archive/2012/06/06/2312759.html https://www.cnblogs.com/ ...

  3. 【iOS开发】字典的快速赋值 setValuesForKeysWithDictionary

    前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件中 @property (nonatomic,copy)NSString *name; @property (nonato ...

  4. 【UML】类图介绍

    1.类图是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 2.类的关系有泛化(Generalization). ...

  5. 项目常用解决方案之SystemSetting.xml文件的修改与读取

    Winform及WPF项目中经常会用到类似SystemSetting.xml等类似的文件用于保存CLIENT的数据,比如登录过的用户名或密码以及其他设置.所以就想到一个解决方法,可以用到所有有此需求的 ...

  6. Object empty value key filter

    Object empty value key filter 过滤空值 Utils emptykeysFilter() "use strict"; /** * * @author x ...

  7. [剑指Offer] 37.数字在排序数组中出现的次数

    题目描述 统计一个数字在排序数组中出现的次数. [思路]因为是排序数组,所以可以用二分法搜索到要查找的值在数组中的一个位置,接着向两侧扫描,遇到不等的就停止. class Solution { pub ...

  8. JSP表单提交出现中文乱码的解决方法

    1)post方式 在servlet的doGet( )  doPost( )  中增加以下代码: response.setContentType("text/html;charset=utf- ...

  9. BZOJ1982 [Spoj 2021]Moving Pebbles 【博弈论】

    题目 Moving Pebbles Two players play the following game. At the beginning of the game they start with ...

  10. 【NOIP模拟赛】chess 建图+spfa统计方案数

    似乎弗洛伊德和迪杰斯特拉都干不了统计方案数,spfa的话就是不断入队就好. #include <cstdio> #include <cstring> #include < ...