题意:

给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子

解题思路:

自然数素性测试可以看看Matrix67的  素数与素性测试

素因子分解利用的是Pollard rho因数分解,可以参考 Pollard rho因数分解

存个代码~

/* **********************************************
Author : JayYe
Created Time: 2013-9-25 16:02:25
File Name : JayYe.cpp
*********************************************** */ #include <stdio.h>
#include <string.h>
#include <time.h>
#include <algorithm>
using namespace std;
#define Time 12 // Miller测试次数
typedef __int64 ll; const ll INF = 1LL << 61;
const int maxC = 240; ll big_mul(ll a, ll b, ll n) {
ll ret = 0;
a %= n;
while(b) {
if(b & 1) {
ret += a;
if(ret >= n) ret -= n;
}
a *= 2;
if(a >= n) a -= n;
b /= 2;
}
return ret;
} ll pow_mod(ll x, ll n, ll m) {
ll ret = 1;
x %= n;
while(n) {
if(n & 1) ret = big_mul(ret, x, m);
x = big_mul(x, x, m);
n /= 2;
}
return ret;
} // 以a为基对n进行Miller次测试并进行二次探测,返回true则是合数
bool Wintess(ll a, ll n) {
ll m = n-1;
int top = 0;
// n-1 = m*(2^top)
while(m % 2 == 0) {
m /= 2;
top++;
}
ll x = pow_mod(a, m, n), y;
for(int i = 0;i < top; i++) {
y = big_mul(x, x, n);
if(y == 1 && (x != 1 && x != n-1))
return true;
x = y;
}
if(y > 1) return true;
return false;
} // 对n进行ts次 Miller素性测试
bool Miller_Rabin(int ts, ll n) {
if(n == 2) return true;
if(n == 1 || n % 2 == 0) return false;
srand(time(NULL));
for(int i = 0;i < ts; i++) {
ll a = rand() % (n-1) + 1;
if(Wintess(a, n)) return false;
}
return true;
} ll ans; ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
} // 对n进行因式分解,找出n的一个因子,该因子不一定是最小的
ll Pollard(ll n, int c) {
srand(time(NULL));
ll i = 1, k = 2, x = rand()%n, y = x;
while(true) {
i++;
x = (big_mul(x, x, n) + c) % n;
ll d = gcd(y - x, n);
if(d > 1 && d < n) return d;
if(y == x) return n; // 如果该数已经出现过,直接返回
if(i == k) {
y = x; k <<= 1;
}
}
} // 找出所有素因子
void solve(ll n, int c) {
if(n == 1) return ;
// 判断是否为素数
if(Miller_Rabin(Time, n)) {
if(ans > n) ans = n;
return ;
}
ll m = n;
while(m == n) { // 找出n的一个因子
m = Pollard(n, c--);
}
solve(m, c);
solve(n/m, c);
} int main() {
int t;
ll n;
scanf("%d", &t);
while(t--) {
scanf("%I64d", &n);
if(Miller_Rabin(Time, n))
puts("Prime");
else {
ans = INF;
solve(n, maxC);
printf("%I64d\n", ans);
}
}
return 0;
}

POJ 1811 Prime Test 素性测试 分解素因子的更多相关文章

  1. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  2. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  3. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

  4. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  5. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  6. POJ 1811 Prime Test( Pollard-rho整数分解经典题 )

    链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...

  7. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  8. POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)

    Description Given a big integer number, you are required to find out whether it's a prime number. In ...

  9. POJ 1811 Prime Test

    题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...

随机推荐

  1. Unique Paths II 解答

    Question Follow up for "Unique Paths": Now consider if some obstacles are added to the gri ...

  2. Floyd 无向图模板

    这是无向图的 void Floyd() { memset(v, 0x3f, sizeof v); ; i <= n; i++) ; j <= n; j++) v[i][j] = map[i ...

  3. Safari HTML5 Canvas Guide: Creating Charts and Graphs

    Safari HTML5 Canvas Guide: Creating Charts and Graphs Bar graphs are similar to data plots, but each ...

  4. linux 之 tar 命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...

  5. Davinci开发板DM368 nandwrite.c简要分析

    #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <limits.h ...

  6. hdu 4611

    2013hdu多校联赛二的第一题,当时队友说两个盒子个数的最小公倍数是周期, 如果两个数的最小公倍数比较大的时候(最大是9999900000),如果遍历求的话肯定会超时 当时想找各种规律,都没找到,最 ...

  7. LDAP启动cacao提示Invalid file permission

    问题处理步骤: 1.LDAP实例停止 2.DSCC控制台启动,提示cacao已停止…… 3.启动caocaoroot@rusky bin]# ./cacaoadm startInvalid file ...

  8. 使用AsyncTask实现图片加载

    如上图所示:我们看到的就是使用PrograssDialog进度条和AsyncTask异步任务实现的效果(额,不要看应用名...).下面介绍一下具体的实现流程. 一.首先使用XML布局,布局很简单直接上 ...

  9. Entity Framework 6 Code First创建

    基本上我是DB先设计好的,所以就按现存在的table去写程式. 1.Web.config里配置Db连接字串,Connection String Name为DefaultConnection <c ...

  10. Shell学习之Shift的用法

        位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1 ...