POJ1811米勒罗宾模板
来源:互联网 发布:企业员工监控软件 编辑:程序博客网 时间:2024/04/28 13:15
#include <ctime>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long int64;template<class T>inline bool updateMin(T& a, T b){ return a > b ? a = b, 1: 0; }template<class T>inline bool updateMax(T& a, T b){ return a < b ? a = b, 1: 0; }inline int nextInt() { int x; scanf("%d", &x); return x; }inline int64 nextI64() { int64 d; cin >> d; return d; }inline char nextChr() { scanf(" "); return getchar(); }inline double nextDbf() { double x; scanf("%lf", &x); return x; }inline int64 next64d() { int64 d; scanf("%I64d",&d); return d; }const int64 MaxN = 105;int64 gcd(int64 a, int64 b){ if (a == 0) return 1; if (a < 0) return gcd(-a, b); while (b) { int64 t = a; a = b; b = t % b; } return a;}int64 mul_mod(int64 a, int64 b, int64 m){int64 t = 0; a %= m; b %= m;while (b){if (b & 1) t += a, t = (t >= m)? t - m: t;a <<= 1; a = (a >= m)? a - m: a; b >>= 1;}return t;}int64 pow_mod(int64 a, int64 b, int64 m){int64 ans = 1; a %= m;while (b){if (b & 1) ans = mul_mod(ans, a, m);b >>= 1; a = mul_mod(a, a, m);}return ans;}bool test(int64 a, int64 n, int64 x, int64 t){ int64 ret = pow_mod(a, x, n); int64 last = ret; for (int i = 1; i <= t; i++) { ret = mul_mod(ret, ret, n); if (ret == 1 && last != 1 && last != n - 1) return true; last = ret; } if (ret != 1) return true; else return false;}bool isPrime(int64 n){ int64 x = n - 1, t = 0; while ((x & 1) == 0) { x >>= 1; t++; } bool flag = 1; if (t >= 1 && (x & 1) == 1) { for (int k = 0; k < 25; k++) { int64 a = rand() % (n - 1) + 1; if (test(a, n, x, t)) { flag = 1; break; } flag = 0; } } if (!flag || n == 2) return 1; return 0;}int64 Pollard_rho(int64 x, int64 c){ int64 i = 1, k = 2; int64 x0 = rand() % (x - 1) + 1; int64 y = x0; while (true) { i++; x0 = (mul_mod(x0, x0, x) + c) % x; int64 d = gcd(y - x0, x); if (d != 1 && d != x) return d; if (y == x0) return x; if (i == k) { y = x0; k += k; } }}int64 tot, result[MaxN];void findfac(int64 n){ if (n == 1) return; if (isPrime(n)) { result[tot++] = n; return; } int64 p = n; while (p >= n) p = Pollard_rho(p, rand() % (n - 1) + 1); findfac(p); findfac(n / p);}int t; int64 n;void solve(){ n = next64d(); if (isPrime(n)) puts("Prime"); else { tot = 0; findfac(n); int64 ans = result[0]; for (int i = 0; i < tot; i++) updateMin(ans, result[i]); printf("%I64d\n", ans); }}int main(){ srand(time(0)); t = nextInt(); while (t--) solve(); return 0;}
0 0
- POJ1811米勒罗宾模板
- (模板)米勒罗宾素数测试
- hdu4910(米勒罗宾)
- <模板>米勒拉宾素数判定 题目是hdu4910
- POJ1811
- poj1811
- 米勒-拉宾算法
- HDU5391米勒拉宾
- 拉宾米勒测试
- POJ 3641 : Pseudoprime numbers - 米勒罗宾素数测试
- poj 3641 快速幂+米勒罗宾判断大素数
- 米勒-拉宾素数测试
- 米勒拉宾素数测试
- poj1811(素数判断与合数分解模板)
- poj1811(大质数判定与大数分解的模板题)
- 【原创】数论模板-拓展欧几里得,质因数分解,快速幂快速加,欧拉函数筛法,欧拉函数求法,筛质数,卡塔兰数,筛逆元,第一二类斯特林数,米勒罗宾算法
- 米勒罗宾素性测试(Miller–Rabin primality test)
- [SinGuLaRiTy-1002] Miller Rabin Prime Judge 米勒·罗宾素数判定法
- Ubuntu下常用的APT命令参数(2)
- APP IOS6 适配iOS7开发计巧
- 51单片机实例收藏
- Android性能优化
- SVN服务器搭建和使用(二)
- POJ1811米勒罗宾模板
- centos 下安装jdk、tomcat 以及tomcat无法从外部访问的解决办法
- SVN服务器搭建和使用(三)
- 前言:apache虽然能运行,但是看见error.log中有httpd.exe: Could not reliably determine the server's fully qualified
- Mac下Android配置及unity3d的导出Android
- Matlab实例学习----数据传递
- linux v4l2 摄像头采集视频的方法
- jdk 源码分析
- 引用与指针