#### 题解：

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;
}

#### 【数论】poj1811Prime Test

2015-08-01 21:14:03

#### poj 1811 rabinMiller素数测试

2014-10-09 19:10:12

#### POJ 1811

2012-12-09 14:42:04

#### POJ 1811 解题报告

2015-07-14 02:16:57

#### poj1811 + hdu4344 （素数测试及大数分解）

2013-07-24 23:12:01

#### POJ 1811 Prime Test【素数判定与大数分解】

2017-03-29 16:45:14

#### poj1811 Prime Test 数论专题

2016-09-21 21:04:19

#### poj1811(大质数判定与大数分解的模板题)

2016-09-28 20:56:51

#### poj1811(pollard-rho求一个大数(64位)的最小因子)

2017-03-09 19:24:58

#### Prime Test POJ - 1811 miller素数判断&pollar_rho大数分解

2017-08-25 11:09:58

poj 1811