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

【分析】
模板题
 /*
宋代谢逸
《踏莎行·柳絮风轻》
柳絮风轻,梨花雨细。春阴院落帘垂地。碧溪影里小桥横,青帘市上孤烟起。
镜约关情,琴心破睡。轻寒漠漠侵鸳被。酒醒霞散脸边红,梦回山蹙眉间翠。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
using namespace std;
typedef long long ll;
ll n, Ans; //快速乘
long long multi(long long a, long long b, long long c){
if (b == ) return ;
if (b == ) return a % c;
long long tmp = multi(a, b / , c);
if (b % == ) return (tmp + tmp) % c;
else return (((tmp + tmp) % c) + a) % c;
}
ll pow(ll a, ll b, ll p){
if (b == ) return a % p;
ll tmp = pow(a, b / , p);
if (b % == ) return (multi(tmp, tmp, p));
else return multi(multi(tmp, tmp, p), (a % p), p);
}
//二次探测
bool Sec_Check(ll a, ll p, ll c){
ll tmp = pow(a, p, c);
if (tmp != && tmp != (c - )) return ;//不通过
if (tmp == (c - ) || (p % != )) return ;
return Sec_Check(a, p / , c);
}
bool miller_rabin(ll n){
ll cnt = ;
while (cnt--){
ll a = (rand()%(n - )) + ;
if (!Sec_Check(a, n - , n)) return ;
}
return ;
}
//int f(int ) {return }
long long gcd(long long a, long long b){return b == ? a : gcd(b, a % b);}
long long BIGRAND() {return rand() * RAND_MAX + rand();}
long long pollard_rho(long long n, long long c){
long long x, y, d;
long long i = , k = ;
x = ((double)rand()/RAND_MAX*(n - )+0.5) + ;
y = x;
while(){
i++;
//注意顺序
x = (multi(x, x, n) % n + c) % n;
d = gcd(y - x + n, n);
if( < d && d < n) return d;
if(y == x) return n;
if(i == k){
y = x;
k <<= ;
}
}
}
//
void find(long long n, long long c){
if (n == ) return;
if (miller_rabin(n)) {
if (Ans == -) Ans = n;
else Ans = min(Ans, n);
return ;
}
long long p = n;
while (p >= n) p = pollard_rho(n, c--);
find(p, c);
find(n / p, c);
//return find(p, c) + find(n / p, c);
} int main(){
int T;
srand(time()); scanf("%d", &T);
while (T--){
scanf("%lld", &n);
if (n != && miller_rabin(n)) printf("Prime\n");
else {
Ans = -;
find(n, );
printf("%lld\n", Ans);
}
}
return ;
}

【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test的更多相关文章

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

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

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

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

  3. BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho

    BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho Description Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...

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

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

  5. 初学Pollard Rho算法

    前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...

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

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

  7. 浅谈 Miller-Robbin 与 Pollard Rho

    前言 $Miller-Robbin$ 与 $Pollard Rho$ 虽然都是随机算法,不过用起来是真的爽. $Miller Rabin$ 算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法, ...

  8. Pollard Rho 算法简介

    \(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...

  9. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

随机推荐

  1. jQuery 参考手册 - 效果

    (speed可选:规定动画的速度.默认是 "normal",可能的值:毫秒(比如 1500)"slow""normal""fast ...

  2. .net 加水印 图片变大很多 解决方法

    /// 给图片加水印 中国红木网          /// </summary>         /// <param name="originalImg"> ...

  3. 各种jee服务器的比较,tomcat, jboss, glassfish, websphere, weblogic

    tomcat, 开源,只是一个 servlet jsp 容器. jboss, 开源,是一个j2ee 应用服务器,容器支持 servlet, jsp, ejb,jms等. 稳定且实现了全部j2ee ap ...

  4. weekend110(Hadoop)的 第二天笔记

    (2015年1月11日) 课程目录 01-NN元数据管理机制 02-NN工作机制2 03-DN工作原理 04-HDFS的JAVA客户端编写 05-filesystem设计思想总结 06-hadoop中 ...

  5. /etc/init.d/syslog: No such file or directory

    centos 6 使用的是 rsyslog 而 centos5 使用 syslog ,兩個不同版本使用軟體不同.因此,你得要修訂 /etc/rsyslog.conf 才行!軟體也變成 /etc/ini ...

  6. Code Forces 711D Directed Roads

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Day 4 @ RSA Conference Asia Pacific & Japan 2016

    09.00 – 09.45 hrs Advanced Malware and the Cloud: The New Concept of 'Attack Fan-out' Krishna Naraya ...

  8. debug.keystore文件不存在解决办法

    重装系统之后,丢失了debug.keystore,找了很久都没有找到,根据网上所讲的只要重新运行一个android项目;就会在avd中生成一个新的debug.keystroe,此法也没解决,索性直接重 ...

  9. Hadoop-2.4.1学习之Map任务源代码分析(下)

    在Map任务源码分析(上)中,对MAP阶段的代码进行了学习,这篇文章文章将学习Map任务的SORT阶段.假设Reducer的数量不为0.则还须要进行SORT阶段.但从上面的学习中并未发现与MAP阶段运 ...

  10. Getting started with new I/O (NIO)--reference

    The new input/output (NIO) library, introduced with JDK 1.4, provides high-speed, block-oriented I/O ...