POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
2014-11-16 21:46
381 查看
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.
题目大意:给一个数n,问是不是素数,若是输出Prime,若不是输出其最小的非1因子。
思路:http://www.2cto.com/kf/201310/249381.html
模板盗自:http://vfleaking.blog.163.com/blog/static/1748076342013231104455989/
代码(375MS):
View Code
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.
题目大意:给一个数n,问是不是素数,若是输出Prime,若不是输出其最小的非1因子。
思路:http://www.2cto.com/kf/201310/249381.html
模板盗自:http://vfleaking.blog.163.com/blog/static/1748076342013231104455989/
代码(375MS):
#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 >= 0 ? res : res + mod; } LL mult(LL x, LL p, LL mod) { LL res = 0; while(p) { if(p & 1) res = modplus(res, x, mod); x = modplus(x, x, mod); p >>= 1; } return res; } LL power(LL x, LL p, LL mod) { LL res = 1; while(p) { if(p & 1) res = mult(res, x, mod); x = mult(x, x, mod); p >>= 1; } return res; } bool witness(LL n, LL p) { int t = __builtin_ctz(n - 1); LL x = power(p % n, (n - 1) >> t, n), last; while(t--) { last = x, x = mult(x, x, n); if(x == 1 && last != 1 && last != n - 1) return false; } return x == 1; } const int prime_n = 5; int prime[prime_n] = {2, 3, 7, 61, 24251}; bool isPrime(LL n) { if(n == 1) return false; if(find(prime, prime + prime_n, n) != prime + prime_n) return true; if(n % 2 == 0) return false; for(int i = 0; i < prime_n; i++) if(!witness(n, prime[i])) return false; return true; } LL getDivisor(LL n) { int c = 1; while (true) { int i = 1, k = 2; LL x1 = 1, x2 = 1; while(true) { x1 = modplus(mult(x1, x1, n), c, n); LL d = __gcd(modminus(x1, x2, n), n); if(d != 1 && d != n) return d; if(x1 == x2) break; i++; if(i == k) x2 = x1, k <<= 1; } 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())); } } }
View Code
相关文章推荐
- [POJ 1811 Prime Test] Miller_Rabin + Pollard_rho 大数质数判断/质因子分解模板
- POJ - 1811_Prime Test_miller-rabin模板&&polard_rho模板
- poj 1811 Prime Test——miller rabin+pollard rho
- POJ 1811 Prime Test (miller_rabin + pollard_rho)
- POJ 1811 Prime Test(素数判定Miller-Rabin+素因子分解Pollard-rho)
- POJ 1811 Prime Test (Miller-Robin+Pollard_rho)
- POJ1811- Prime Test(Miller–Rabin+Pollard's rho)
- POJ-1811 Prime Test(Miller_Rabin算法和Pollard_rho算法模板题)
- POJ1811 PrimeTest Miller-Robin+Pollard-Rho
- [POJ 1811]Prime Test---Miller-Rabin算法&Pollard-rho算法
- Poj 1811 Prime Test 素数测试 Miller-Rabin 与 整数的因子分解 Pollard rho
- poj 1811 Prime Test_Pollard_rho算法模板
- POJ 1811 Miller_Rabin+Pollard_Rho
- <模板>(Miller-Rabin和Pollard_rho算法)poj 2429 GCD & LCM Inverse (数论)
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
- POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
- POJ 1811 *** Prime Test(详解Miiler_Rabin算法与Pollard_Rho算法)
- poj 2429 GCD & LCM Inverse (pollard - rho && Miller-rabin)
- 【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test