米勒羅賓素數測試
阿新 • • 發佈:2019-01-31
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 0x7fffffff
using namespace std;
typedef long long ll;
const int times=12;
ll random(ll n){
ll ans=(ll)((double)rand()/RAND_MAX*n+0.5);
return ans;
}
ll mulit(ll a,ll b,ll m){
ll ans=0 ;
while(b){
if(b&1){
ans=(ans+a)%m;
}
b>>=1;
a=(a<<1)%m;
}
return ans;
}
ll quick_mod(ll a,ll b,ll m){
ll ans=1;
a%=m;
while(b){
if(b&1) ans=mulit(ans,a,m);
b>>=1;
a=mulit(a,a,m);
}
return ans;
}
bool witness(ll a,ll n){
int m=n-1;
int j=0;
while(!(m&1)){
j++;
m>>=1;
}
ll x=quick_mod(a,m,n);
if(x==1||x==n-1) return false;
else{
while(j--){
x=mulit(x,x,n);
if(x==n-1)
return false;
}
}
return true;
}
bool miller(ll n){
if(n==2) return true;
if(n<2||!(n&1)) return false;
for(int i=0;i<times;i++)
if(witness(random(n-2)+1,n))
return false;
return true;
}
int main()
{
ll n;
while(cin>>n){
if(miller(n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}