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. centos7下 vsftpd初使用

    一. 安装 1. 命令: yum -y install vsftpd 2. 创建一个用户专门用来登录vsftpd #在根目录下创建一个文件夹ftpfile mkdir ftpfile  #创建用户ft ...

  2. linux定时重启tomcat服务的脚本学习

    要求:在linux中定时重启一个tomcat服务 一:shell脚本即Shell Script [1],Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便 ...

  3. [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ

    跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...

  4. Oracle的卸载过程步骤

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,那么怎么才能完全卸载Oracle呢?那就是直接注册表清除,步骤如下: 1. 开始->设置->控制面板-& ...

  5. 基于.Net + SqlServer的分库分表设计方案

    在说分库分表之前,先简单介绍下网站架构,这样有助于理解为何需要分库分表这种技术.因为所有的技术,大多都是因为业务的需要而产生的. 1.网站发展的第一阶段 大致架构如下,因为没有多少用户访问,所以单台服 ...

  6. 二、LINQ之查询表达式基础

    1.查询是什么? 查询是一组指令,描述要从给定数据源(或源)检索的数据以及返回的数据应具有的形状和组织.查询表达式和它所产生的结果不同.

  7. 一口一口吃掉Volley(二)

    欢迎访问我的个人博客转发请注明出处:http://www.wensibo.top/2017/02/17/一口一口吃掉Volley(二)/ 相信看了第一篇教程之后,你应该会对Volley有一个初步的了解 ...

  8. 微信小程序网络封装-简单高效

    废话引言 小程序虽然出世很久了,但一直没怎么接触到小程序开发.吉他兴趣班老师想弄一个小程序发布课程信息和打卡功能,作为IT一员就自愿加入了这个小程序开发小组中.虽然小程序面向的是前端工程师,但作为移动 ...

  9. 高可用Hadoop平台-运行MapReduce程序

    1.概述 最近有同学反应,如何在配置了HA的Hadoop平台运行MapReduce程序呢?对于刚步入Hadoop行业的同学,这个疑问却是会存在,其实仔细想想,如果你之前的语言功底不错的,应该会想到自动 ...

  10. Vue笔记:使用 VS Code 断点调试

    直接在 Chrome 的调试窗口中调试 Vue 代码有诸多不便, 好在 Visual Studio Code 中提供了 Debugger for Chrome 插件,能够通过配置直接在 VS Code ...