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. ssh远程连接不上linux

    远程连接工具是:Xmanager Enterprise 5-->Xshell linux 发行版本是:CentOS-6.3-x86_64 问题:ssh一直都可以远程连接上linux,一段时间后突 ...

  2. 协程 coroutine

    参考链接: http://manual.luaer.cn/2.11.html http://www.cnblogs.com/riceball/archive/2008/01/03/1025158.ht ...

  3. 组件基础(参数校验和动态组件、v-once)—Vue学习笔记

    最最最后一点关于组件传值的问题. 提醒:本篇内容请使用Vue.js开发版!(附带完成的警告和提示) 1.组件的参数校验 父组件向子组件传值,子组件可以决定传值的一些限制. 比如,子组件指向接收Stri ...

  4. 深度学习环境配置:Ubuntu16.04下安装GTX1080Ti+CUDA9.0+cuDNN7.0完整安装教程(多链接多参考文章)

    本来就对Linux不熟悉,经过几天惨痛的教训,参考了不知道多少篇文章,终于把环境装好了,每篇文章或多或少都有一些用,但没有一篇完整的能解决我安装过程碰到的问题,所以决定还是自己写一篇我安装过程的教程, ...

  5. Smart/400开发上手3: 练习实践

    练习题 在2006年1月1日之前入职且在职的营销员,给予年资补贴2000元: 符合以上条件的,再按以下标准一次性发放职级补贴: 职级代码 简称 补偿金额 A1 AD 6000 B1 SBM 5000 ...

  6. (转)浅谈AIX下IPFilter防火墙

    1    序言 AIX操作系统发行至今,经历数个版本,功能不断增强,就安全方面IP Security也变化不少,如动作中增加了If等功能,但这次暂且讨论配置防火墙策略及防火墙的基本操作,其他高级功能待 ...

  7. vue中请求本地的json数据

    为什么要请求本地的数据?模拟后台的请求数据,验证页面的逻辑是否存在问题,抛开后台提前开发等. 常用的说来有:jq的方式 约等于 axios的方式,vuex状态管理的方式 个人认为最好用的就是jq的方式 ...

  8. mybatis 关联映射

    一对一 创建数据表 CREATE TABLE `tb_card` ( `id` int NOT NULL AUTO_INCREMENT , `code` varchar() NULL , PRIMAR ...

  9. 源码分析篇 - Android绘制流程(二)measure、layout、draw流程

    performTraversals方法会经过measure.layout和draw三个流程才能将一帧View需要显示的内容绘制到屏幕上,用最简化的方式看ViewRootImpl.performTrav ...

  10. 根据js来判断手机是操作系安卓还是ios

    平常开发很常见的功能,在移动端需要根据javaScript来判断手机是安卓还是ios进行应用市场跳转. 通过js提供的Navigator可以来判断手机是安卓还是ios系统,代码如下 1.js判断是否为 ...