#include<cstdio>
#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef __int64 LL;
LL prime[]={2,3,5,7,11,19,23,29};
LL mul(LL a,LL b,LL n){
LL s=0;a%=n;
while(b){
if(b&1){
s+=a;if(s>=n)s-=n;
}
a*=2;if(a>=n)a-=n;b/=2;
}
return s;
}
LL pow(LL a,LL b,LL n){
LL d=1;a%=n;
while(b){
if(b&1)d=mul(d,a,n);
a=mul(a,a,n);b/=2;
}
return d;
}
int m_r(LL a,LL n){
LL m=n-1;
int j=0;
while((m&1)==0){
j++;m=m>>1;
}
LL x=pow(a,m,n);
if(x==1)return 1;
for(int i=1;i<=j;i++){
if(x==n-1)return 1;
x=mul(x,x,n);
}
return 0;
}
int Isprime(int times,LL n){
if(n==2)return 1;
if(n<2||(n&1)==0)return 0;
for(int i=0;i<times;i++){
if(n==prime[i])return 1;
if(n%prime[i]==0)return 0;
if(!m_r(prime[i],n))return 0;
}
return 1;
}
米勒测试
最新推荐文章于 2018-12-17 21:54:18 发布