poj 1811
来源:互联网 发布:java并发编程应用在哪 编辑:程序博客网 时间:2024/06/05 20:10
题目描述:
大质数(特别大)的判定质数和分解质数.
题解:
Miller_Rabin 算法进行素数测试 和 pollard_rho 算法进行质因素分解
重点:
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <ctype.h>#include <limits.h>#include <cstdlib>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#include <set>#include <bitset>#include <time.h>#define CLR(a) memset(a, 0, sizeof(a))#define REP(i, a, b) for(ll i = a;i < b;i++)#define REP_D(i, a, b) for(ll i = a;i <= b;i++)typedef long long ll;using namespace std;ll n;const ll S = 8;ll mul_mod(ll a, ll b, ll c){ a %= c; b %= c; ll res = 0; ll tmp = a; while(b) { if(b&1) { res = res + tmp; if(res >= c) res -= c; } tmp <<= 1; if(tmp >= c) tmp -= c; b >>= 1; } return res;}ll pow_mod(ll x, ll n, ll c){ ll res = 1; ll tmp = x%c; while(n) { if(n&1) { res = mul_mod(res, tmp, c); } n >>= 1; tmp = mul_mod(tmp, tmp, c); } return res;}ll check(ll a, ll n, ll x, ll t){ ll res = pow_mod(a, x, n); ll last = res; for(ll i=1; i<= t; i++) { res = mul_mod(res, res, n); if(res==1&&(last != 1 && last != n-1)) return 1; last = res; } if(res!=1) return 1; return 0;}ll miller_rabin(ll n){ if(n < 2) return 0; if(n==2) return 1; if((n&1)==0) return 0; ll x=n-1; ll t = 0; while((x&1)==0) { t++; x>>=1; } srand(time(NULL)); for(ll i=1; i<=S; i++) { ll a = rand()%(n-1)+1; if(check(a, n, x, t)) { return 0; } } return 1;}ll gcd(ll a, ll b){ if(a < 0) a= -a; if(b < 0) b = -b; //a = abs(a); //b = abs(b); if(b==0) return a; return gcd(b, a%b);}long long pollard_rho(long long x,long long c){ long long i = 1, k = 2; srand(time(NULL)); long long x0 = rand()%(x-1) + 1; long long y = x0; while(1) { i ++; x0 = (mul_mod(x0,x0,x) + c)%x; long long d = gcd(y - x0,x); if( d != 1 && d != x)return d; if(y == x0)return x; if(i == k) { y = x0; k += k; } }}ll factor[110], tot;void findfac(ll n, ll k){ if(n==1) return; if(miller_rabin(n)) { factor[tot++]= n; return; } ll p = n; ll c= k; while(p >= n) { p = pollard_rho(p, c); c--; } findfac(p, k); findfac(n/p, k);}void solve(){ if(miller_rabin(n)) { printf("Prime\n"); return; } else { tot= 0; findfac(n, 107); } sort(factor, factor + tot); printf("%I64d\n", factor[0]);}int main(){ // freopen("2Bin.txt", "r", stdin); //freopen("3Bout.txt", "w", stdout); ll t; scanf("%I64d", &t); while(t--) { scanf("%I64d", &n); solve(); } return 0;}
0 0
- POJ 1811
- poj 1811
- poj 1811
- poj 1811
- poj 1811, poj 2429 (pollard_rho算法)
- POJ 1811 Prime Test
- poj 1811 Prime Test
- poj 1811解题报告
- POJ 1811 Prime Test
- POJ 1811 Prime Test
- poj 1811 Prime Test
- POJ 1811 Miller_Rabin+Pollard_Rho
- poj 1811 Prime Test
- POJ 1811 Prime Test
- POJ 1811 解题报告
- [poj 1811]质数分解
- poj 1811 (prime test)
- POJ 1811 Prime Test
- LeetCode-String to Integer (atoi) -解题报告
- MySql 存储引擎的选取
- mysql循环方法总结
- LeetCode-Reverse Integer-解题报告
- Power of Two
- poj 1811
- 有道词典Demo(WebView)
- Leetcode #27 Remove Element
- addChildViewController相关
- OSGEarth嵌入Qt的QGraphicsView框架(一)
- 【Leetcode】Find the kth in the BST
- 49个权威的网上学习资源网站
- Teamcenter PSM(三)
- LeetCode-Median of Two Sorted Arrays-解题报告