POJ 1811 Prime Test 素性测试 分解素因子
题意:
给你一个数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 素性测试 分解素因子的更多相关文章
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- 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 ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- 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 ...
- POJ 1811 Prime Test
题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...
随机推荐
- linux usb 驱动详解
linux usb 驱动详解 USB 设备驱动代码通过urb和所有的 USB 设备通讯.urb用 struct urb 结构描述(include/linux/usb.h ). urb 以一种异步的方式 ...
- python使用get在百度搜索并保存第一页搜索结果
python使用get在百度搜索并保存第一页搜索结果 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用在意我的感受 #coding:utf-8 import ur ...
- OpenWrt修改
openwrt如何编译修改界面的顶部.底部信息.LOGO图片 2011-06-02 16:20:03 浏览次 以Atheros71xx为例,修改路径为:trunk/build_dir/target ...
- eclipse中误删了servers文件
Eclipse中误删了servers文件,需要重新添加tomcat服务器,这时就会遇到在New Server对话框中选择了Tomcat 6/7后却无法单击"Next"按钮的问题,如 ...
- 水池数目(DFS)
水池数目 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...
- log4net 使用与配置 每天一份log文件
1.下载 或 在nuget安装 log4net 2. web.config (app.config) <configuration> <configSections> < ...
- java File的getLastModified在不同操作系统以下存在差异
java对文件读取改动时间(getLastModified())在不同的操作系统下存在差异 //1.在windows下,返回值是毫秒级别,不存在问题 //2.在Linux下,返回的值是毫秒值,可是会 ...
- LoadRunner如何开展性能测试
最近一直想理清思路,特别是碰到一些业务复杂的项目,要求做性能测试,结果一时就不知道怎么下手了.因为之前面试的时候,也碰到很多面试官对性能测试知识方面的提问,面试多了,就有经验,现在写下来,脑子不会乱, ...
- 让man 显示中文
1.添加库函数手册 ubuntu默认是没有安装c语言的库函数man手册的,所以你在man perror 和sendto之类的函数时会显示没有相关文档的问题,这个问题让我郁闷了我好久.解决方法: sud ...
- html css js 框架
html css js 框架 Bootstrap http://www.bootcss.com/ http://www.cnblogs.com/aehyok/p/3404867.html ...