C++米勒拉宾算法模板
//我也忘了从哪找来的板子,不过对于2^63级的数据请考虑使用java内置的米勒拉宾算法。
1 #include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
const int S=;
LL mult_mod(LL a,LL b,LL c){
a%=c;
b%=c;
long long ret=;
while(b){
if(b&){ret+=a;ret%=c;}
a<<=;
if(a>=c)a%=c;
b>>=;
}
return ret;
}
LL pow_mod(LL x,LL n,LL mod){
if(n==)return x%mod;
x%=mod;
LL tmp=x;
LL ret=;
while(n){
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=;
}
return ret;
}
bool check(LL a,LL n,LL x,LL t){
LL ret=pow_mod(a,x,n);
LL last=ret;
range(i,,t){
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;
last=ret;
}
if(ret!=) return true;
return false;
}
bool Miller_Rabin(LL n){
if(n<)return false;
if(n==)return true;
if((n&)==) return false;
LL x=n-;
LL t=;
while((x&)==){x>>=;t++;}
range(i,,S-){
LL a=rand()%(n-)+;
if(check(a,n,x,t))return false;
}
return true;
}
LL factor[];
int tol;
LL gcd(LL a,LL b){
if(a==)return ;
if(a<) return gcd(-a,b);
while(b){
long long t=a%b;
a=b;
b=t;
}
return a;
}
LL Pollard_rho(LL x,LL c){
LL i=,k=;
LL x0=rand()%x;
LL y=x0;
while(){
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k){y=x0;k+=k;}
}
}
void findfac(LL n){
if(Miller_Rabin(n)){
factor[tol++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
}
int main(){
long long n;
while(scanf("%lld",&n)!=EOF){
tol=;
/*
findfac(n);
for(int i=0;i<tol;++i)cout<<factor[i]<<" ";
printf("\n");
*/
if(Miller_Rabin(n))printf("Yes\n");
else printf("No\n");
}
return ;
}
C++米勒拉宾算法模板的更多相关文章
- HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...
- HDU2138 & 米勒拉宾模板
题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...
- Miller_Rabin (米勒-拉宾) 素性测试
之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- Miller_Rabin(米勒拉宾)素数测试
2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...
- csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 723 Solved: 198[Submit][Status][Web Bo ...
- Miller_Rabin(米勒拉宾)素数测试算法
首先需要知道两个定理: 1: 费马小定理: 假如p是素数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p). 2:二次探测定理:如果p是素数,x是小于p的正整数,且,那么要么x=1,要么x ...
- POJ 1811Prime Test(米勒拉宾素数测试)
直接套用模板,以后接着用 这里还有一个素因子分解的模板 #include <map> #include <set> #include <stack> #includ ...
- GCDLCM 【米勒_拉宾素数检验 (判断大素数)】
GCDLCM 题目链接(点击) 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some ...
随机推荐
- Oracle 学习----:ora-00054 资源正忙 ,但指定以nowait方式获取资源 ,或者超时失效---解决方法
1.查询被锁的会话ID: select session_id from v$locked_object;查询结果:SESSION_ID-------92.查询上面会话的详细信息: SELECT sid ...
- How to solve SyntaxError on autogenerated manage.py?
错误原因: 今天尝试使用pycharm中的Django. 使用的是基于python3.5的Django2.0.5. 按照官网教程https://docs.djangoproject.com/en/2. ...
- 记一下STL的一个题
A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- cfq调度器
cfq调度是block层最复杂的一个调度器,主要思想是是说每个进程平均享用IO带宽,实现方法是在时间上对进程进行划分,以此达到平均占用IO的目的.带着几个问题去看cfq 1)现在进程来了之后,是插入到 ...
- objective-c runtime 开发详情
目录 概述 对象与类的实质 id与class 继承关系与isa 总结 C函数创建一个OC类 OC类与runtime NSObjectProtocol NSObject NSProxy 一.概述 Obj ...
- NSOperation 开发
目录 1.简介 2.Operation对象 3.自定义Operation对象 4.通过Operation Queues运行Operation对象 5.手动运行Operation对象 一.简介 Coco ...
- [洛谷P4151][WC2011]最大XOR和路径
题目大意:给你一张$n$个点$m$条边的无向图,求一条$1->n$的路径,使得经过路径值的异或值最大(重复经过重复计算) 题解:某条路$k$被重复走了两次,那么它的权值对答案的贡献就是$0$,但 ...
- 【距离GDOI:131天】 后缀数组完毕
用了近两周的时间,终于把罗神那篇后缀数组应用看完了,题目也写了一遍,T了无数次...详见前几篇博文... 后缀数组很重要的是那个height数组,可以用来做各种奇奇怪怪的东西...常用方法去是去二分, ...
- A Dangerous Maze (II) LightOJ - 1395(概率dp)
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
- 2018超详细sublime text3+python3.x安装配置教程(附常用插件安装教程)
导读 本文是关于2018年7月最新版sublime text3+pythin3.x下载及安装配置教程,sublime text3版本为3176,python版本为3.7,安装环境是基于windows1 ...