本文有嚴重紕漏, 請勿閱讀
MillerRabin 快速的素數概率判定法
1.作用:快速判斷單個數是否爲質數
2.原理:
介紹費馬小定理:對於每一個素數p,都有
但是不是對於每一個有
如果存在b滿足上述規則,那麼b有3/4的機率爲素數
MillerRabin通過多次隨機生成b並使用以上方法進行判斷,能把錯誤的機率降到
分母指數級擴大讓概率隨k的增大迅速趨近於0。當k達到10時判斷錯誤的機率已經降到百萬分之一,k達到20時幾乎不會出錯。
黑科技的力量啊 = =
typedef long long LL;
namespace millerrabin{
inline LL advpow(LL a,LL b,LL c){
LL ret=1;
while(b){
if(b&1)ret*=a,ret%=c;
a*=a;
a%=c;
b>>=1;
}
return ret;
}
int random(const int&mod){
srand(rand()*rand()*rand());
return rand()%mod;
}
bool jdPrime(const int&num,const int&eps){
if(num<=3)return true;
int cnt=0;
while(cnt<=eps){
int a=random(num)+2;
if(num%a==0)continue;
if(advpow(a,num-1,num)%num!=1)return false;
cnt++;
}
return true;
}
}