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. solr 加载 停用/扩展词典

    startup.bat 停止词典的效果

  2. sql 数据库日志收缩

    SQL2008 的收缩日志 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005)Backup Log DNName w ...

  3. spring-boot-starter-actuator

    首先在pom中添加依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  4. php -- 数据库信息

    ----- 023-dbinfo.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  5. h5实现输入框fixed定位在屏幕最底部兼容性

    1.问题由来 做h5 已经有很长一段时间了,现在做的工作h5比pc上的更多,曾经解决pc端IE各个版本的兼容性也是伤透脑筋,原以为h5的会更好,殊不知,还有更头疼的问题,当设计师要设计一个聊天窗口,把 ...

  6. docker cgroup技术之cpu和cpuset

    在centos7的/sys/fs/cgroup下面可以看到与cpu相关的有cpu,cpuacct和cpuset 3个subsystem.cpu用于对cpu使用率的划分:cpuset用于设置cpu的亲和 ...

  7. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(三):搭建开发环境

    生成项目模板 登录Spring Initializr生成Spring Boot项目模板,保存到本地. 地址:https://start.spring.io/ 导入Maven项目 使用IDE导入生成的M ...

  8. Excelbatis-一个将excel文件读入成实体列表、将实体列表解析成excel文件的ORM框架,简洁易于配置、可扩展性好

    欢迎使用Excelbatis! github地址:https://github.com/log4leo/Excelbatis Excelbatis的优点 和spring天然结合,易于接入 xsd支持, ...

  9. Linq 多表连接查询join

    在查询语言中,通常需要使用联接操作.在 LINQ 中,可以通过 join 子句实现联接操作.join 子句可以将来自不同源序列,并且在对象模型中没有直接关系(数据库表之间没有关系)的元素相关联,唯一的 ...

  10. Maven Jetty8

    <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...