Prime Test

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 35528   Accepted: 9479
Case Time Limit: 4000MS

Description

Given a big integer number, you are required to find out whether it's a prime number.

Input

The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 254).

Output

For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.

Sample Input

2
5
10

Sample Output

Prime
2

Source

 
 //2017-08-16
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long using namespace std; const int TIMES = ; ll random0(ll n){
return ((double)rand() / RAND_MAX*n + 0.5);
} //快速乘a*b%mod
ll quick_mul(ll a, ll b, ll mod){
ll ans = ;
while(b){
if(b&){
b--;
ans = (ans+a)%mod;
}
b >>= ;
a = (a+a) % mod;
}
return ans;
} //快速幂a^b%mod
ll quick_pow(ll a, ll n, ll mod){
ll ans = ;
while(n){
if(n&)ans = quick_mul(ans, a, mod);
a = quick_mul(a, a, mod);
n >>= ;
}
return ans;
} bool witness(ll a, ll n){
ll tmp = n-;
int i = ;
while(tmp % == ){
tmp >>= ;
i++;
}
ll x = quick_pow(a, tmp, n);
if(x == || x == n-)return true;
while(i--){
x = quick_mul(x, x, n);
if(x == n-)return true;
}
return false;
} bool miller_rabin(ll n){
if(n == )return true;
if(n < || n % == )return false;
for(int i = ; i <= TIMES; i++){
ll a = random0(n-)+;
if(!witness(a, n))
return false;
}
return true;
} //factor存放分解出来的素因数
ll factor[];
int tot; ll gcd(ll a, ll b){
if(a == )return ;
if(a < )return gcd(-a, b);
while(b){
ll tmp = a % b;
a = b;
b = tmp;
}
return a;
} ll pollard_rho(ll x, ll c){
ll i = , k = ;
ll x0 = rand()%x, x1 = x0;
while(){
i++;
x0 = (quick_mul(x0, x0, x)+c)%x;
ll d = gcd(x1-x0, x);
if(d != && d != x)return d;
if(x1 == x0)return x;
if(i == k){
x1 = x0;
k += k;
}
}
} //对n分解质因数
void decomposition_factor(ll n){
if(miller_rabin(n)){
factor[tot++] = n;
return;
}
ll p = n;
while(p >= n){
p = pollard_rho(p, rand()%(n-)+);
}
decomposition_factor(p);
decomposition_factor(n/p);
} int main()
{
int T;
scanf("%d", &T);
while(T--){
ll n;
scanf("%lld", &n);
if(miller_rabin(n))
printf("Prime\n");
else{
tot = ;
decomposition_factor(n);
ll ans = factor[];
for(int i = ; i < tot; i++)
if(factor[i] < ans)ans = factor[i];
printf("%lld\n", ans);
}
} return ;
}

POJ1811(SummerTrainingDay04-G miller-rabin判断素性 && pollard-rho分解质因数)的更多相关文章

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

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

  2. POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)

    http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接 ...

  3. miller——rabin判断素数

    我们首先看这样一个很简单的问题:判定正整数\(n\)是否为素数 最简单的做法就是枚举\(2\)到\(n\)的所有数,看是否有数是\(n\)的因数,时间复杂度\(O(n)\) 稍微优化一下发现只要枚举\ ...

  4. BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数

    BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sa ...

  5. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  6. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  7. Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法

    BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1044  Solved: 322[Submit][ ...

  8. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

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

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

  10. poj 1811 Pallor Rho +Miller Rabin

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

随机推荐

  1. cnetos6上实现nfs共享

    利用空余时间,做个nfs共享实验,岂不美滋滋!!! 系统测试环境: 主机ip 系统     主机名(服务) 192.168.241.130    centos6.5    hadoop1(nfs-sr ...

  2. win10+Theano+GPU

    1. cuda + cudnn 首先还是要先安装GPU库,具体和caffe安装中一样. 2. Theano 为防止下载速度慢,配置清华镜像 conda config --add channels ht ...

  3. 人工智能-机器学习之seaborn(读取xlsx文件,小提琴图)

    我们不止可以读取数据库的内容,还可以读取xlsx文件的内容,这个库有在有些情况还是挺实用的 首先我们想读取这个文件的时候必须得现有个seaborn库 下载命令就是: pip install  seab ...

  4. python学习笔记15-字符串 lsit set truple之间的相互转换

    import string #字符串转list str = 'abcde' list = list(str) #list转字符串 str_convert = ''.join(list) #字符串转se ...

  5. Python之tkinter中的askyescancel窗口返回值

    if messagebox.askokcancel(title="确认取消",message="您确认注册该账号吗?"): messagebox.showinf ...

  6. Java读取文件加锁代码Demo(利用Java的NIO)

    本博文部分转载于:http://blog.csdn.net/wangbaochu/article/details/48546717 Java 提供了文件锁FileLock类,利用这个类可以控制不同程序 ...

  7. Linux命令对应的英文全称

    su:Swith user  切换用户,切换到root用户cat: Concatenate  串联uname: Unix name  系统名称df: Disk free  空余硬盘du: Disk u ...

  8. Android get current Locale, not default

    he default Locale is constructed statically at runtime for your application process from the system ...

  9. 企业域的冗余设计①:DHCP冗余设计(上)

    在许多企业网络中,为了方便客户端IP地址的管理,通常采用的是自动获取的方式向DHCP服务器获得IP地址.为了保证DHCP服务器能够正常稳定地向客户端提供IP地址的租赁,DHCP服务器的冗余设计就显得格 ...

  10. Linux系列:Fedora虚拟机设置固定IP上网(配置IP、网关、DNS、防止resolv.conf被重写)

    首先声明:该方法在Fedora 17和18版本下有效,其它版本也许可行也许有所差异. 1.  虚拟机相关配置 如果不是虚拟机系统,则这步不需要,若是相关配置详细信息请看“Linux系列:Ubuntu虚 ...