POJ Prime Test
来源:互联网 发布:java将object转为date 编辑:程序博客网 时间:2024/05/20 00:37
模板题,代码留作以后用...
#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 10000;const int S = 20;LL factor[maxn];int tot;//返回(a * b) % c,a,b,c<2^63LL multi_mod(LL a, LL b, LL c){ a %= c; b %= c; LL ret = 0; while(b) { if (b & 1) { ret += a; if (ret >= c) ret -= c; } a <<= 1; if(a >= c) a -= c; b >>= 1; } return ret;}//返回x^n mod c ,非递归版LL pow_mod(LL x, LL n, LL mod){ LL ret = 1; while(n) { if(n & 1) ret = multi_mod(ret, x, mod); x = multi_mod(x, x, mod); n >>= 1; } return ret;}//以a为基,n-1=x*2^t,检验n是不是合数bool check(LL a, LL n, LL x, LL t){ LL ret = pow_mod(a, x, n), last = ret; for (int i = 1; i <= t; i++) { ret = multi_mod(ret, ret, n); if (ret == 1 && last != 1 && last != (n-1)) return 1; last = ret; } if (ret != 1) return 1; return 0;}/*素数测试*/bool Miller_Rabin(LL n){ LL 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 < S; k++) { LL a = rand()%(n-1) + 1; if (check(a, n, x, t)) { flag = 1; break; } flag = 0; } } if (!flag || n == 2) return 0; return 1;}LL gcd(LL a,LL b){ if (a == 0) return 1; if (a < 0) return gcd(-a, b); while (b) { LL t = a % b; a = b; b = t; } return a;}/*大整数素因子分解*/LL Pollard_rho(LL x, LL c){ LL i = 1, x0 = rand() % x, y = x0, k = 2; while(1) { i++; x0 = (multi_mod(x0, x0, x) + c) % x; LL d = gcd(y - x0, x); if (d != 1 && d != x) { return d; } if (y == x0) return x; if (i == k) { y = x0; k += k; } }}//递归进行质因数分解Nvoid findfac(LL n){ if (!Miller_Rabin(n)) { factor[tot++] = n; return; } LL p = n; while(p >= n) p = Pollard_rho(p, rand() % (n-1) + 1); findfac(p); findfac(n / p); return ;}int main(){ //freopen("aa.in", "r", stdin); //freopen("bb.out", "w", stdout); int T; LL n; cin >> T; while(T--) { cin >> n; if (!Miller_Rabin(n)) { printf("Prime\n"); continue; } tot = 0; findfac(n); //进行大整数的素因子分解 LL ans = factor[0]; for(int i = 1; i < tot; i++) if(factor[i] < ans) ans = factor[i]; cout << ans << endl; } return 0;}
- POJ 1811 Prime Test
- poj 1811 Prime Test
- POJ 1811 Prime Test
- POJ 1811 Prime Test
- poj 1811 Prime Test
- poj 1811 Prime Test
- POJ 1811 Prime Test
- POJ Prime Test
- poj 1811 (prime test)
- POJ 1811 Prime Test
- Prime Test POJ
- POJ 1811 Prime Test 笔记
- POJ 1811 Prime Test 素数测试
- poj 1811 Prime Test(素数判断)
- POJ 1811 Prime Test --- Miller 素数测试
- POJ 1811 Prime Test (miller_rabin + pollard_rho)
- [POJ]1811Prime Test 做题笔记
- POJ 1811 Prime Test (Miller-Robin+Pollard_rho)
- centos双网卡配置
- FatMouse' Trade
- 人人笔试题
- 文件系统和数据库系统的区别
- 研究 android apk安装卸载等 产生的 系统广播
- POJ Prime Test
- 不好好读书,将来连表都不认识
- UVa 10019 Funny Encryption Method (water ver.)
- 【MZ】hdu 2243 AC自动机 + 矩阵加速
- C++编译和链接详解
- cocos2dx左下角三行数值意义\去掉
- Android 抽屉效果实现
- n进制转换
- TMS32C64X DSP引起cache miss 的三种情况以及Cache Coherence一致性的解决办法