POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
Description
Input
Output
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iterator>
#include <vector>
using namespace std; typedef long long LL; LL modplus(LL a, LL b, LL mod) {
LL res = a + b;
return res < mod ? res : res - mod;
} LL modminus(LL a, LL b, LL mod) {
LL res = a - b;
return res >= ? res : res + mod;
} LL mult(LL x, LL p, LL mod) {
LL res = ;
while(p) {
if(p & ) res = modplus(res, x, mod);
x = modplus(x, x, mod);
p >>= ;
}
return res;
} LL power(LL x, LL p, LL mod) {
LL res = ;
while(p) {
if(p & ) res = mult(res, x, mod);
x = mult(x, x, mod);
p >>= ;
}
return res;
} bool witness(LL n, LL p) {
int t = __builtin_ctz(n - );
LL x = power(p % n, (n - ) >> t, n), last;
while(t--) {
last = x, x = mult(x, x, n);
if(x == && last != && last != n - ) return false;
}
return x == ;
} const int prime_n = ;
int prime[prime_n] = {, , , , }; bool isPrime(LL n) {
if(n == ) return false;
if(find(prime, prime + prime_n, n) != prime + prime_n) return true;
if(n % == ) return false;
for(int i = ; i < prime_n; i++)
if(!witness(n, prime[i])) return false;
return true;
} LL getDivisor(LL n) {
int c = ;
while (true) {
int i = , k = ;
LL x1 = , x2 = ;
while(true) {
x1 = modplus(mult(x1, x1, n), c, n);
LL d = __gcd(modminus(x1, x2, n), n);
if(d != && d != n) return d;
if(x1 == x2) break;
i++;
if(i == k) x2 = x1, k <<= ;
}
c++;
}
} void getFactor(LL n, vector<LL> &ans) {
if(isPrime(n)) return ans.push_back(n);
LL d = getDivisor(n);
getFactor(d, ans);
getFactor(n / d, ans);
} int main() {
int T; LL n;
scanf("%d", &T);
while(scanf("%I64d", &n) != EOF) {
if(isPrime(n)) puts("Prime");
else {
vector<LL> ans;
getFactor(n, ans);
printf("%I64d\n", *min_element(ans.begin(), ans.end()));
}
}
}
POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)的更多相关文章
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- POJ1811- Prime Test(Miller–Rabin+Pollard's rho)
题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...
- 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 这 ...
- POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)
题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...
- POJ 1811 Prime Test 素性测试 分解素因子
题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的 素数与素性测试 素因子分解利用 ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
随机推荐
- ubuntu 64bit arm-linux-gcc: No such file or directory 解决
通过下面这个解决 ubuntu 64bit arm-linux-gcc: No such file or directory 安装 sudo apt-get install lsb-core解决 h ...
- 消息队列系列(一):.Net平台下的消息队列介绍
本系列主要记录最近学习消息队列的一些心得体会,打算形成一个系列文档.开篇主要介绍一下.Net平台下一些主流的消息队列框架. RabbitMQ:http://www.rabbitmq.com ...
- centos 安装 openerp
遇到问题:近日公司提出openerp的搭建,觉得openerp里的有些模块比较适合公司,openerp的运作,估计会有利于公司系统化的管理.于是我就去了解openrp,然后来搭建这套强大的系统. 解决 ...
- [LeetCode]题解(python):053-Maximum Subarray
题目来源 https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (c ...
- 配置 android环境
1.如上图,下载最新adt-bundle: http://developer.android.com/sdk/index.html 里面集成了 eclipse,SDK,SDK Manager. 2 ...
- Selenium2学习-024-WebUI自动化实战实例-022-网站不同分辨率下页面样式展示兼容性问题解决方案 -- 设置浏览器显示区域大小(无人值守,节约测试成本的福音,BOSS 最爱)
在 Web UI 自动化测试的过程中,通常会测试页面在不同分辨率下的显示效果,即在不同大小的显示器上全屏浏览器后的页面展示,此种测试方法需要购置不同大小的显示器,或者频繁的设置屏幕分辨率,不仅浪费了大 ...
- sell-- 英文网站产品显示404?
1. 简介: 通过在主页(header.jsp)查询B22212,在localhost本地, cn和us查询的结果search.jsp中显示都是没有找到! 但是在外网(www),cn能够查询到,并展示 ...
- sell - 配置service
1. 2. 注意value!
- C#中派生类调用基类构造函数用法分析
这里的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1.当基类中没有自己编写构造函数时,派生类默认的调用基类的默认构造函数例如: ? 1 2 3 4 5 6 7 8 9 10 11 ...
- JavaScript:通过id来进行元素的取得
每一个HTML元素都使用id来进行一个标注,随后可以通过document.getElementById(“ID名称”)取得指定的ID元素对象,取得元素对象之后就可以对其进行操作. 但是document ...