问题描述:素性测试兼质因子分解

解题关键: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大整数分解)的更多相关文章

  1. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  2. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  3. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

  4. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  5. Pollard Rho大质数分解学习笔记

    目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...

  6. 大整数分解质因数(Pollard rho算法)

    #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...

  7. 【HDU - 4344】Mark the Rope(大整数分解)

    BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每 ...

  8. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  9. POJ 1811 Prime Test( Pollard-rho整数分解经典题 )

    链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...

随机推荐

  1. android shareSDK 微博分享案例

    android shareSDK 微博分享案例 ShareSDK APP_KEY 219b1121fc68 腾讯微博 key 801517904 secret bfba83ae253c8f38dabe ...

  2. python 基础 3.1 打开文件 a a+ r+ w+ 详解

      一.python 访问文件   1.在python中要访问文件,首先要打开文件,也就是open ---open   r:  只读   w:  只写 ,文件已存在则清空,不存在则创建   a:追加 ...

  3. JS实现图片无缝滚动特效;附addEventListener()方法、offsetLeft和offsetWidth属性。

    一:html部分 <body> <input id="btn1" type="button" value="向左"> ...

  4. Java 学习 day04

    17-数组(概述-内存结构) 概念:同一种类型数据的集合,其实数组就是一个容器. 可以自动给数组中的元素从0开始编号,方便操作这些元素. int[] x = new int[3]; 01-数组(静态初 ...

  5. zookeeper_action

    连接串 从节点列表本地缓存主节点对未分配的任务,随机分配给从节点(不合理??)从节点保存一个本地待执行任务列表单独的线程对节点已分配任务进行循环 进程p为了获锁——>创建节点znode_/loc ...

  6. VC调用Delphi DLL

    别的没什么,是一定可以调用成功的.但是意外的是,ShowMessage函数在DLL里也可以轻易被调用.此外,Delphi里的var 相当于VC里的引用,需要在函数原型里正确标识,否则传递普通变量甚至常 ...

  7. 搭建vps***

    快速搭建ShadowSocks wget --no-check-certificate -O shadowsocks.sh https://raw.githubusercontent.com/tedd ...

  8. 关于SAP S4 HANA 的13个问题

    SAP S/4HANA的路线图是怎样的?价格是多少?下一步还将添加哪些新模块?莫不闻专业SAP问答平台结合SAP HANA及SAP HANA应用商务套件开发全球负责人Uwe Grigoleit帮大家整 ...

  9. Excel图表转成图片

    关于excel 图表转成图片 知识点:excel 生成的图表不是图片 尝试.    通过Java调用POI接口挺难把excel生成的图表转成图片导出来 ps.      其它生成图表的工具,如jfre ...

  10. c# XML-Object对象 序列化-反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...