【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test
Description
Input
Output
Sample Input
2
5
10
Sample Output
Prime
2
Source
/*
宋代谢逸
《踏莎行·柳絮风轻》
柳絮风轻,梨花雨细。春阴院落帘垂地。碧溪影里小桥横,青帘市上孤烟起。
镜约关情,琴心破睡。轻寒漠漠侵鸳被。酒醒霞散脸边红,梦回山蹙眉间翠。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
using namespace std;
typedef long long ll;
ll n, Ans; //快速乘
long long multi(long long a, long long b, long long c){
if (b == ) return ;
if (b == ) return a % c;
long long tmp = multi(a, b / , c);
if (b % == ) return (tmp + tmp) % c;
else return (((tmp + tmp) % c) + a) % c;
}
ll pow(ll a, ll b, ll p){
if (b == ) return a % p;
ll tmp = pow(a, b / , p);
if (b % == ) return (multi(tmp, tmp, p));
else return multi(multi(tmp, tmp, p), (a % p), p);
}
//二次探测
bool Sec_Check(ll a, ll p, ll c){
ll tmp = pow(a, p, c);
if (tmp != && tmp != (c - )) return ;//不通过
if (tmp == (c - ) || (p % != )) return ;
return Sec_Check(a, p / , c);
}
bool miller_rabin(ll n){
ll cnt = ;
while (cnt--){
ll a = (rand()%(n - )) + ;
if (!Sec_Check(a, n - , n)) return ;
}
return ;
}
//int f(int ) {return }
long long gcd(long long a, long long b){return b == ? a : gcd(b, a % b);}
long long BIGRAND() {return rand() * RAND_MAX + rand();}
long long pollard_rho(long long n, long long c){
long long x, y, d;
long long i = , k = ;
x = ((double)rand()/RAND_MAX*(n - )+0.5) + ;
y = x;
while(){
i++;
//注意顺序
x = (multi(x, x, n) % n + c) % n;
d = gcd(y - x + n, n);
if( < d && d < n) return d;
if(y == x) return n;
if(i == k){
y = x;
k <<= ;
}
}
}
//
void find(long long n, long long c){
if (n == ) return;
if (miller_rabin(n)) {
if (Ans == -) Ans = n;
else Ans = min(Ans, n);
return ;
}
long long p = n;
while (p >= n) p = pollard_rho(n, c--);
find(p, c);
find(n / p, c);
//return find(p, c) + find(n / p, c);
} int main(){
int T;
srand(time()); scanf("%d", &T);
while (T--){
scanf("%lld", &n);
if (n != && miller_rabin(n)) printf("Prime\n");
else {
Ans = -;
find(n, );
printf("%lld\n", Ans);
}
}
return ;
}
【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test的更多相关文章
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho
BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho Description Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...
- Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法
BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1044 Solved: 322[Submit][ ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 浅谈 Miller-Robbin 与 Pollard Rho
前言 $Miller-Robbin$ 与 $Pollard Rho$ 虽然都是随机算法,不过用起来是真的爽. $Miller Rabin$ 算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法, ...
- Pollard Rho 算法简介
\(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...
- Pollard Rho算法浅谈
Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...
随机推荐
- [GRYZ2014]最大连续子序列的和
求给定序列的最大连续子序列和. 输入:第一行:n(n<100000) 第二行:n个整数[-3000,3000]. 输出:最大连续子序列的和. 样例: 输入: 7 -6 4 ...
- WSAAsyncSelect模型
============================================== █ 异步选择(WSAAsyncSelect)模型是一个有用的异步 I/O 模型.利用这个模型,应用程序可在 ...
- Poj 2528-Mayor's posters 线段切割
题目:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total ...
- html自定义checkbox、radio、select —— checkbox、radio篇
前些日子,所在公司项目的UI做了大改,前端全部改用 Bootstrap 框架,Bootstrap的优缺点在此就不详述了,网上一大堆相关资料. 前端的设计就交给我和另一个同事[LV,大学同班同学,毕业后 ...
- 阅读STL源码剖析之list
首先,以我之愚见,觉得有两个地方可以优化一下,不知对否,有待商榷: 1.在list的结点定义中 template<typename T> struct __list_node { type ...
- Ajax 整理总结(入门)
Ajax 学习要点: 1.Ajax 概述 2.load()方法 3.$.get()和$.post() 4.$.getScript()和$.getJSON() 5.$.ajax()方法 6.表单序列化 ...
- [Webpack 2] Polyfill Promises for Webpack 2
If you're going to use code splitting with Webpack 2, you'll need to make sure the browser has suppo ...
- [D3] 4. d3.max
how to use d3.max to normalize your dataset visually within the specific bounds of a variable domain ...
- pcap的pcap_dump()保存的文件格式
(2009-09-01 20:36:49) 转载▼ 标签: 杂谈 分类: 专业 首先是tcpdump文件格式 当你在Windows或者Linux环境下用tcpdump命令抓取数据包时,你将得到如下格式 ...
- BaseAdapter优化深入分析
BaseAdapter是一个数据适配器,将我们提供的数据格式化为ListView可以显示的数据,BaseAdapter的优化直接影响到ListView的显示效率. 我们都知道,ListView自带有回 ...