[poj1811]Prime Test(Pollard-Rho大整数分解)
问题描述:素性测试兼质因子分解
解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用
期望复杂度:$O({n^{\frac{1}{4}}})$
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#define inf 1ll<<61
typedef long long ll;
using namespace std;
const int S=;
ll fac[],tol,mi;
ll mod_mul(ll a,ll b,ll p){
ll res=;
a%=p,b%=p;
while(b){
if(b&)res=(res+a)%p;
a=(a<<)%p;
b>>=;
}
return res;
}
ll mod_pow(ll x,ll n,ll p){
ll res=;
while(n){
if(n&)res=mod_mul(res,x,p);
x=mod_mul(x,x,p);
n>>=;
}
return res;
} bool check(ll a,ll n,ll x,ll t){//判断是否为合数
ll ret=mod_pow(a,x,n);
ll last=ret;
for(int i=;i<=t;i++){
ret=mod_mul(ret,ret,n);
if(ret==&&last!=&&last!=n-)return ;
last=ret;
}
if(ret!=)return ;//fermat测试
return ;
} bool Miller_Rabin(ll n){
if(n<)return ;
if(n==)return ;
if((n&)==)return ;
ll x=n-,t=;
while((x&)==)x>>=,t++;
for(int i=;i<S;i++){
ll a=rand()%(n-)+;
if(check(a,n,x,t))return ;//合数
}
return ;
} ll Pollard_Rho(ll n,ll c){//返回值n的因子
ll i=,j=,x=rand()%(n-)+,y=x;
while(){
i++,x=(mod_mul(x,x,n)+c)%n;
ll p=__gcd((y-x+n)%n,n);
if(p!=&&p!=n)return p;//p本身是合数,分解为本身就无意义了
if(y==x)return n;//循环节只有1,不符合条件,同时也判圈了
if(i==j)y=x,j<<=;//这里控制1步和2步
}
}
void find1(ll n,ll c){//找因子主体
if(n==) return;
if(Miller_Rabin(n)){
fac[tol++]=n;
mi=min(mi,n);
return;
}
ll p=n,k=c;
while(p>=n)p=Pollard_Rho(p,c--);//返回的是小于n但不一定为素数的因子
find1(p,k);
find1(n/p,k);
}
int main() {
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%lld",&n);
mi=n;
if(Miller_Rabin(n)) cout<<"Prime"<<endl;
else{
find1(n,);
cout<<mi<<endl;
}
}
return ;
}
[poj1811]Prime Test(Pollard-Rho大整数分解)的更多相关文章
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- Pollard Rho大质数分解学习笔记
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...
- 大整数分解质因数(Pollard rho算法)
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- 【HDU - 4344】Mark the Rope(大整数分解)
BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每 ...
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
随机推荐
- 再说WCF Data Contract KnownTypeAttribute
WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...
- AppICon设置
- visual studio 2010 c++ 打印 Hello world
由于好奇心驱使温习下c高级简化语言语言(个人解释可能不太准确).下面用visual studio 2010 实现 HelloWord 打印 第一步:visual studio 2010 打开.文件-- ...
- lombok插件安装
eclipse安装lombok插件 lombok注解介绍 记得最后,加入的配置文件中的jar包,最好写成相对路径,这样.eclipse移动位置后,不会报错.
- Entity Framework 4.1:多对多的关系
这篇文章讨论多对多的关系. 让我们从最简单的例子开始.我们让 EF4.1 来推断表的映射.我在订单和雇员之间建模多对多的关系. )] publicstring CustomerName { get; ...
- 九度OJ 1056:最大公约数 (GCD)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6278 解决:4075 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组输入,请 ...
- mac sublime text 3 add ctags plugin
https://www.smslit.top/2015/11/14/macSTctags-Develop/ ctags插件for sublime text项目和ctags源码项目都在github上.
- JAVA Exception处理
原文地址:http://blog.csdn.net/hguisu/article/details/6155636 1. 引子 try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用 ...
- 模拟登陆,selenium,线程池
一 . 模拟登陆案例(识别验证码) 1 . 打码平台 - 云打码 : www.yundama.com 使用步骤 : - 注册两个账户,普通用户和开发者用户 : - 登陆 普通用户查看余额 登陆开发 ...
- Ubuntu/CentOS下使用脚本自动安装 Docker
Ubuntu.Debian 系列安装 Docker 系统要求 Docker 支持以下版本的 Ubuntu 和 Debian 操作系统: Ubuntu Xenial 16.04 (LTS) Ubuntu ...