poj 1811 Prime Test(大素数判定和素因子分解)
来源:互联网 发布:淘宝代销多个地点发货 编辑:程序博客网 时间:2024/05/17 02:59
http://poj.org/problem?id=1811
先用Miller_Rabin算法进行素数判断,再用Pollard_rho分解素因子。
Miller_Rabin素数测试
Miller_Rabin素数测试和素因子分解
今天做TC时,遇到一道大素数判定和质因子分解的模板题。想到了质因子分解,但没想到用这个模板。赛后,还是自己理解一遍,然后手敲吧。。
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)#define C 240#define S 20using namespace std;LL fac[100];int tol;LL gcd(LL a, LL b){if(a == 0)return 1;if(a < 0)return gcd(-a,b);if(b == 0)return a;return gcd(b,a%b);}LL MultMod(LL a, LL b, LL n){a %= n;b %= n;LL ret = 0;while(b){if(b&1){ret += a;if(ret >= n)ret -= n;}a <<= 1;if(a >= n)a -= n;b >>= 1;}return ret;}LL PowMod(LL a, LL n, LL m){LL ret = 1;a %= m;while(n){if(n&1)ret = MultMod(ret,a,m);a = MultMod(a,a,m);n >>= 1;}return ret;}/// 大素数判定bool Witness(LL a, LL n){LL t = 0;LL u = n-1;while(!(u&1)){t++;u = u/2;}LL x0 = PowMod(a,u,n);for(int i = 1; i <= t; i++){LL x1 = MultMod(x0,x0,n);if(x1 == 1 && x0 != 1 && x0 != (n-1) )return true;x0 = x1; //之前少了这一句}if(x0 != 1)return true;return false;}bool Miller_Rabin(LL n){if(n == 2)return true;if((n&1) == 0)return false;//srand(time(NULL));for(int i = 0; i < S; i++){LL a = rand()%(n-1) + 1;if(Witness(a,n))return false;}return true;}///质因子分解LL Pollar_Rho(LL n, LL c){LL i = 1,x = rand()%n, y = x,k = 2;while(1){i++;x = ( MultMod(x,x,n)+c)%n;LL d = gcd(y-x,n);if(d != 1 && d != n)return d;if(x == y)return n;if(i == k){y = x;k = k*2;}}}void get_small(LL n, LL c){if(n == 1)return;if(Miller_Rabin(n)){fac[tol++] = n;return;}LL p = n;while(p >= n)p = Pollar_Rho(p,c--);get_small(p,c);get_small(n/p,c);}int main(){int test;LL n;scanf("%d",&test);while(test--){scanf("%lld",&n);if(Miller_Rabin(n)){printf("Prime\n");continue;}tol = 0;get_small(n,C);LL ans = fac[0];for(int i = 1; i < tol; i++)ans = min(ans,fac[i]);printf("%lld\n",ans);}return 0;}
0 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(素数判定Miller-Rabin+素因子分解Pollard-rho)
- poj 1811 Prime Test(数论:大素数判定-分解)
- POJ 1811 Prime Test(大素数判定+质因数分解)
- POJ 1811 Prime Test(大素数判断+大合数素因子分解)
- poj 1811 Prime Test(大素数判定)
- (Relax 数论 1.5)POJ 1811 Prime Test(MillRabin模板题+Pollard模板题:判定大素数&&合数分解)
- POJ 1811 Prime Test 素性测试 分解素因子
- Poj 1811 Prime Test 素数测试 Miller-Rabin 与 整数的因子分解 Pollard rho
- POJ 1811 Prime Test(判断大素数&求最小质因子)
- POJ 1811 Prime Test 【快速判质+因子分解】【模板】
- poj 1811 Prime Test【 随机素数测试与大数分解】
- poj1811——Prime Test//素数判断+整数分解因子
- 大质数检测&&分解质因子 poj1811 Prime Test
- Java NIO 系列教程
- angularjs中directive声明scope对象时修饰符解释和用法
- String,StringBuffer和StringBuild 区别
- Linux内核的early_param原理追踪
- DOM元素
- poj 1811 Prime Test(大素数判定和素因子分解)
- mvc 跳转到另一个controler
- VS 2010 快捷键大全
- hdoj1262 寻找素数对
- 建联合主键报错DB2 Database Error: ERROR [42997] [IBM][DB2/AIX64] SQL0270N Function not supported (Reason c
- Vuforia Unity Camera Image Access
- 从模板生成代码
- 定时器
- 第二课:Android的事件处理(基于回调)