[CQOI2016]密钥破解
嘟嘟嘟
这题我读了两遍才懂,然后感觉要解什么高次同余方程……然后我又仔细的看了看题,发现只要求得\(p\)和\(q\)就能求出\(r\),继而用exgcd求出\(d\),最后用快速幂求出\(n\)。
再看看这个数据范围,用Pollard-Rho最适合不过了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
//const int maxn = ;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
ll e, N, c, r;
In ll mul(ll a, ll b, ll mod)
{
ll d = ((long double)a / mod * b + 1e-8);
ll r = a * b - d * mod;
return r < 0 ? r + mod : r;
}
In ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
In ll f(ll x, ll a, ll mod) {return (mul(x, x, mod) + a) % mod;}
int a[] = {2, 3, 5, 7, 11};
const int M = (1 << 7) - 1;
In ll find(ll n)
{
for(int i = 0; i < 5; ++i) if(n % a[i] == 0) return a[i];
ll x = rand(), y = x, a = rand() % (n - 2) + 2, t = 1;
for(int k = 2;; k <<= 1, y = x)
{
ll p = 1;
for(int i = 1; i <= k; ++i)
{
x = f(x, a, n);
p = mul(p, abs(x - y), n);
if(!(i & M))
{
t = gcd(p, n);
if(t > 1) break;
}
}
if(t > 1 || (t = gcd(p, n)) > 1) break;
}
return t;
}
In ll pollard_rho(ll x)
{
ll p = x;
while(p == x) p = find(x);
return p;
}
In void exgcd(ll a, ll b, ll& x, ll& y, ll& t)
{
if(!b) t = a, x = 1, y = 0;
else exgcd(b, a % b, y, x, t), y -= a / b * x;
}
In ll quickpow(ll a, ll b, ll mod)
{
a %= mod;
ll ret = 1;
for(; b; b >>= 1, a = mul(a, a, mod)) //别忘了这里也会爆long long
if(b & 1) ret = mul(ret, a, mod);
return ret;
}
int main()
{
srand(time(0));
e = read(), N = read(), c = read();
ll p = pollard_rho(N), q = N / p; ll r = (p - 1) * (q - 1);
ll d, y, t;
exgcd(e, r, d, y, t);
t = r / t;
d = (d % t + t) % t;
ll n = quickpow(c, d, N);
write(d), space, write(n), enter;
return 0;
}
[CQOI2016]密钥破解的更多相关文章
- 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 ...
- BZOJ 4522: [Cqoi2016]密钥破解
http://www.lydsy.com/JudgeOnline/problem.php?id=4522 题目:给你RSA密钥的公钥和密文,求私钥和原文,其中\(N=pq\le 2^{62}\),p和 ...
- BZOJ4522: [Cqoi2016]密钥破解
pollard's rho模板题. 调参调到160ms无能为力了,应该是写法问题,不玩了. #include<bits/stdc++.h> using namespace std; typ ...
- BZOJ 4522: [Cqoi2016]密钥破解 (Pollard-Rho板题)
Pollard-Rho 模板 板题-没啥说的- 求逆元出来后如果是负的记得加回正数 CODE #include<bits/stdc++.h> using namespace std; ty ...
- BZOJ 4522: [Cqoi2016]密钥破解 exgcd+Pollard-Rho
挺简单的,正好能再复习一遍 $exgcd$~ 按照题意一遍一遍模拟即可,注意一下 $pollard-rho$ 中的细节. #include <ctime> #include <cma ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- LibreOJ2045 - 「CQOI2016」密钥破解
Portal Description 给出三个正整数\(e,N,c(\leq2^{62})\).已知\(N\)能表示成\(p\cdot q\)的形式,其中\(p,q\)为质数.计算\(r=(p-1)( ...
- Visio Premium 2010密钥+破解激活方法
Visio Premium 2010密钥+破解激活方法: 在安装时能够使用下面密钥: GR24B-GC2XY-KRXRG-2TRJJ-4X7DC VWQ6G-37WBG-J7DJP-CY66Y-V27 ...
随机推荐
- nfs 提高传输速度
通常挂载 nfs 的命令为: mount -t nfs -o nolock 192.168.0.124:/home/admin/rootfs /mnt 之前我一直都是用这个命令来挂载,那个传输速度啊, ...
- windows上使用tensorboard
因为我的环境变量设置的不是python3.5,所以走了一些弯路. 启动tensorboard后,graphs里总是什么都没有 最后再stackoverflow里找到答案 https://stackov ...
- SQL Server 2008 management studio 无法连接到(local)解决方法
背景:安装VS2010之后,自带的 SQL Server 2008 Express 不具备management studio,于是下载了SQL Server 2008 Express 版本并全新安装 ...
- 【WebSocket No.1】实现服务端webSocket连接通讯
前言 现阶段socket通信使用TCP.UDP协议,其中TCP协议相对来说比较安全稳定!本文也是来讲解TCP为主(恕在下学艺不精). 下面是个人理解的tcp/ip进行通讯之间的三次握手! 1.客户端先 ...
- html 字符串 生成 pdf 完美解决中文不显示
//maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf< ...
- docker 安装jenkins
基于docker 进行安装 软件,首先需要有docker环境. 1.docker 下载 jenkins 镜像 指定版本 ,因为低版本的后面安装 软件会失败(亲测). docker pull jenki ...
- 对比学IT---路由器和linux流量统计的差别
1. 路由器使用MQC来统计端口入出方向,特定特征的数据流. 显示policy 的统计信息 配置policy: #traffic classifier vlan5traffic operator an ...
- Hexo + Github 个人博客设置以及优化
原文地址: Hexo + Github 个人博客设置以及优化 一.博客设置 分类.标签云.关于等页面 在站点目录下分别执行: hexo new page "categories" ...
- 慕学在线网0.3_四个model
1.四个model完整代码: # users/models.py from datetime import datetime from django.db import models from dja ...
- LeetCode题解之Binary Tree Level Order Traversal II
1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vecto ...