其他分享
首页 > 其他分享> > Miller Rabin 总结

Miller Rabin 总结

作者:互联网

费马小定理

\(a^p\equiv a\pmod p\)
在 p 是质数时成立,考虑 rand 一个 a 来判定
但是有一类数,满足费马小定理却又不是质数,如 561

二次探测定理

若 p 是质数且 \(b^2\equiv 1\pmod p,0<x<p\)
原式减一可得\(b^2-1\equiv 0\pmod p\),平方差\((b+1)(b-1)\equiv 0\pmod p\)
故 p 一定可以被 (b+1) 和 (b-1) 其中至少一个整除,所以 \(b=1,p-1\) 。若 b=1 ,可以继续探测

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef long double LD;
inline LL Abs(LL x) { return x<0?-x:x; }
inline LL Mul(uLL x,uLL y,LL p) { return (x*y-(uLL)((LD)x/p*y)*p+p)%p; }
inline LL Pow(LL x,LL y,LL p){
	register LL res=1;
	for(;y;y>>=1,x=Mul(x,x,p))
	if(y&1)res=Mul(res,x,p);
	return res;
}
inline bool Mr(LL n,LL p) {
    if(Pow(p,n-1,n)!=1)return 0;
    register LL q=n-1,o;
    while(!(q&1)) {
        q>>=1,o=Pow(p,q,n);
        if(o!=1 && o!=n-1)return 0;
        if(o==n-1)return 1;
    }
    return 1;
}
inline bool Prime(LL n) {
    if(n<2)return false;
    if(n==2||n==3||n==5||n==7||n==43)return true;
    return Mr(n,2)&&Mr(n,3)&&Mr(n,5)&&Mr(n,7)&&Mr(n,43);
}
int main() {
    srand(20080816);
    return 0;
}

标签:总结,return,pmod,Miller,LL,long,equiv,质数,Rabin
来源: https://www.cnblogs.com/KonjakLAF/p/14332068.html