Miller&&Pollard POJ 1811 Prime Test
题意:素性测试和大整数分解, N (2 <= N < 254)。
分析:没啥好讲的,套个模板,POJ上C++提交
收获:写完这题得到模板
代码:
/************************************************
* Author :Running_Time
* Created Time :2015-8-28 13:02:38
* File Name :POJ_1811.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int S = 20;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7; /*
素性测试,Miller_Rabin 随机算法
可以判断< 2^63的数
素数:true,合数:false
*/
const int S = 20; //随机算法判定次数,S越大,判错概率越小 //非递归写法,递归写法可能会爆栈
ll GCD(ll a, ll b) {
if (a == 0) return 1;
if (a < 0) a = -a;
while (b) {
ll c = a % b;
a = b; b = c;
}
return a;
} //计算 (a * b) % p,a,b是long long数,直接相乘可能会溢出
ll multi_mod(ll a, ll b, ll p) {
ll ret = 0;
a %= p; b %= p;
while (b) {
if (b & 1) {
ret += a;
if (ret >= p) ret -= p;
}
a <<= 1;
if (a >= p) a -= p;
b >>= 1;
}
return ret;
} //计算 a ^ x % p
ll pow_mod(ll a, ll x, ll p) {
ll ret = 1;
a %= p;
while (x) {
if (x & 1) ret = multi_mod (ret, a, p);
a = multi_mod (a, a, p);
x >>= 1;
}
return ret;
} /*
以a为基,n-1=x*2^t,a^(n-1) = 1(mod n) 验证n是不是合数
一定是合数返回true, 不一定返回false
*/
bool check(ll a, ll n, ll x, int t) {
ll ret = pow_mod (a, x, n);
ll last = ret;
for (int i=1; i<=t; ++i) {
ret = multi_mod (ret, ret, n);
if (ret == 1 && last != 1 && last != n - 1) return true; //合数
last = ret;
}
if (ret != 1) return true;
return false;
} bool Miller_Rabin(ll n) {
if (n == 2) return true;
if (n < 2 || ! (n & 1)) return false; //偶数或1
ll x = n - 1; int t = 0;
while (! (x & 1)) {
x >>= 1; t++;
}
for (int i=1; i<=S; ++i) {
ll a = rand () % (n - 1) + 1; //需要cstdlib头文件
if (check (a, n, x, t)) return false; //合数
}
return true;
} /*
大整数分解,Pollard_rho 随机算法
factorize ()保存质因数在vector
*/
ll Pollard_rho(ll x, ll c) {
ll i = 1, k = 2;
ll a = rand () % x;
ll b = a;
while (1) {
i++;
a = (multi_mod (a, a, x) + c) % x;
ll d = GCD (b - a, x);
if (d != 1 && d != x) return d;
if (b == a) return x;
if (i == k) b = a, k += k;
}
} void factorize(ll n, vector<ll> &ret) {
if (Miller_Rabin (n)) { //素数
ret.push_back (n); return ;
}
ll p = n;
while (p >= n) p = Pollard_rho (p, rand () % (n - 1) + 1);
factorize (p, ret);
factorize (n / p, ret);
} int main(void) {
srand (time (NULL));
int T; scanf ("%d", &T);
while (T--) {
ll n; scanf ("%I64d", &n);
if (Miller_Rabin (n)) puts ("Prime");
else {
if (n <= 1) {
puts ("-1"); continue;
}
vector<ll> ans;
factorize (n, ans);
sort (ans.begin (), ans.end ());
printf ("%I64d\n", ans[0]);
// for (int i=0; i<ans.size (); ++i) {
// printf ("%I64d%c", ans[i], (i == ans.size ()-1) ? '\n' : ' ');
// }
}
} return 0;
}
Miller&&Pollard 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 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- POJ 1811 Prime Test 素性测试 分解素因子
题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的 素数与素性测试 素因子分解利用 ...
- 数论 - 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(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测试用的红书模板,将测试 ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
随机推荐
- StringUtil内部方法差异
StringUtil 的 isBlank.isEmply.isNotEmpty.isNotBlank 区别 String.trim()方法: trim()是去掉首尾空格 append(Stri ...
- CentOS系统中常用查看系统信息和日志命令小结
转载:http://www.3lian.com/edu/2015/04-09/204628.html 进程 # ps -ef # 查看所有进程 # top # 实时显示进程状态(另一篇文章里面有详细的 ...
- Linux-百度云之AccleriderMini使用
打开命令行,然后输入: sudo apt-get install mono-devel 最后安装完成时,运行: 示例:mono xx.exe 实例:mono accleridaMini.exe 最后成 ...
- 有两个字符串a,b。假设a="ab",b="cd",判断字符串c="acbd"是属于a、b的组合。满足组合后a、b的内部顺序均不变。
#include<iostream> #include<string> using namespace std; int check(string a,string b,str ...
- Android开发系列(二十七):使用ProgressDialog创建进度对话框
进度对话框在寻常的应用中非经常见,比方下载的时候,打开页面的时候.转移文件等等.有环形的.有长条形的. 基本就这两种 创建进度对话框的两种方式: 1.创建ProgressDialog实例,然后调用Pr ...
- Nyquist–Shannon sampling theorem 采样定理
Nyquist–Shannon sampling theorem - Wikipedia https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_s ...
- LEA指令与MOV指令的区别——发现一本汇编好书
一.汇编语言中PTR的含义及作用mov ax,bx ;是把BX寄存器“里”的值赋予AX,由于二者都是word型,所以没有必要加“WORD”mov ax,word ptr [bx];是把内存地址等于“B ...
- 【java报错】CacheLoader returned null for key class
CacheLoader returned null for key class cmd mvn eclipse:clean eclipse:eclipse mvn install -Dmave ...
- SAP事务码 一
SE80 -- edit source code. SE24 -- class create or display. SFP -- created and maintained independent ...
- 【T^T 1736】【FJUTOJ 1077】排座位
http://59.77.139.92/problem.php?id=1077 水题,小心PE // <1736.cpp> - 11/12/16 17:17:52 // This file ...