Sample Input

2
5
10

Sample Output

Prime
2

模板学习:

判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解

miller : 通过费马小定理,若N为素数,a^(N-1) = 1 (mod N),

再利用二次判定:

若x为素数,0<x<p, x*x = 1(mod q)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <time.h>
#define N 10100
typedef long long ll;
using namespace std; const int S = 8; //随机判定次数 一般8 - 10 // a*b%c
ll mult_mod(ll a,ll b,ll c)
{
a %= c;
b %= c;
ll ret = 0;
ll temp = a;
while(b)
{
if(b&1)
{
ret += temp;
if(ret > c) ret -= c;
}
temp <<= 1;
if(temp > c) temp -= c;
b >>= 1;
}
return ret; } // (a^n)%mod
ll pow_mod(ll a,ll n,ll mod)
{
ll ret = 1;
ll temp = a%mod;
while(n)
{
if(n & 1)
ret = mult_mod(ret,temp ,mod);
temp = mult_mod(temp,temp,mod);
n>>= 1;
}
return ret;
}
//通过费马小定理,a^(n-1)=1(mod n) ;来判断n是否是素数
bool check(ll a,ll n,ll x,ll t)
{
ll ret = pow_mod(a,x,n);
ll last = ret;
for(int i = 1; i <= t; i++)
{
ret = mult_mod(ret,ret,n); //二次探测
if(ret == 1 && last != 1 && last != n-1) return true;
last = ret;
}
if(ret != 1) return true;
else return false;
} bool miller_rabin(ll n) //随机素数
{
if(n < 2) return false;
if(n == 2) return true;
if((n&1) == 0) return false; //偶数
ll x = n - 1;
ll t = 0;
while((x&1) == 0)
{
x>>= 1;
t++;
};
srand(time(NULL)); //G++时不要 for(int i = 0; i < S; i++)
{
ll a = rand()%(n - 1) + 1;
if(check(a,n,x,t))
return false;
}
return true;
} ll factor[100];
int tol; ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t = a;
a = b;
b = t % b;
}
if(a >= 0) return a;
else
return -a;
}
//找因子
ll pollard_rho(ll x,ll c)
{
ll i = 1,k = 2;
srand(time(NULL));
ll x1 = rand()%(x-1)+1;
ll y = x1;
while(1)
{
i++;
x1 = (mult_mod(x1,x1,x)+c)%x;
ll d = gcd(y-x1,x);
if(d!=1 && d!=x) return d;
if(y == x1) return x;
if(i == k)
{
y = x1;
k += k;
}
}
}
//对n进行分解,存入数组,
void findfac(ll n,int k) //大数分解
{
if(n == 1)
return ;
if(miller_rabin(n)) //判素
{
factor[tol++] = n;
return ;
}
ll p = n;
int c = k;
while( p>= n)
p = pollard_rho(p,c--); //防止死循环k,值变换
findfac(p,k);
findfac(n/p,k); } int main()
{
int t;
ll n;
scanf("%d",&t);
while(t--)
{
scanf("%I64d",&n);
if(miller_rabin(n)) printf("Prime\n");
else
{
tol = 0;
findfac(n,107);
ll ans = factor[0];
for(int i = 1; i < tol; i++)
ans = min(ans,factor[i]);
printf("%I64d\n",ans);
}
}
return 0;
}

  

poj 1811 随机素数和大数分解(模板)的更多相关文章

  1. POJ 1811 大素数判断

    数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...

  2. Pollard_Rho大数分解模板题 pku-2191

    题意:给你一个数n,  定义m=2k-1,   {k|1<=k<=n},并且 k为素数;  当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...

  3. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  4. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  5. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  6. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  7. poj 1811 Pallor Rho +Miller Rabin

    /* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...

  8. 数学:随机素数测试(Miller_Rabin算法)和求整数素因子(Pollard_rho算法)

    POJ1811 给一个大数,判断是否是素数,如果不是素数,打印出它的最小质因数 随机素数测试(Miller_Rabin算法) 求整数素因子(Pollard_rho算法) 科技题 #include< ...

  9. poj1181 大数分解

    //Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...

随机推荐

  1. velocity学习总结

    什么是velocity velocity是一个基于Java的模板引擎,它可以实现彻底的前后端,前端不允许像jsp那样出现Java代码,而是利用context容器传递变量,在java代码里面我们可以往容 ...

  2. 第一次PTA作业

    题目6-1拆分实数整数及小数部分 1设计思路 (1) 第一步:阅读题目要求及所给部分. 第二步:根据题意补全相应函数. (2)流程图 无 2.实验代码 #include <stdio.h> ...

  3. python全栈开发-json和pickle模块(数据的序列化)

    一.什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flat ...

  4. Python内置函数(48)——__import__

    英文文档: __import__(name, globals=None, locals=None, fromlist=(), level=0) This function is invoked by ...

  5. api-gateway实践(10)新服务网关 - OpenID Connect

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  6. angular2 学习笔记 ( Dynamic Component 动态组件)

    更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...

  7. Spring Security入门(1-9)Spring Security 的xml 命名空间配置

  8. SendMessage 遇到的神坑

    场景 两个进程A和B,需要从A中设置B中的文本框的内容 过程 x.x.x.x. 成功获取了B中的内容,惊喜,离成功更近异步 xxxx ***** ....... x.x.x.x. 大约查找了几百个网页 ...

  9. 复习HTML+CSS(2)

    n  项目符号嵌套编号思路 标签的内容(文本.项目符号.表格.图片等)必须放在最底层标记中. n  图片标记(行内元素,单边标记) l  语法:<img 属性 = "值"&g ...

  10. 愿奴胁下生双翼——— 详解cookie和session

    cookie和session都是基于web服务器的,不同的是cookie存储在客户端而session存储在服务器. 当用户浏览网站时,web服务器会在浏览器上存储一些当前用户的相关信息,在本地Web客 ...