TemplateMiller Rabin
Posted bingoyes
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TemplateMiller Rabin相关的知识,希望对你有一定的参考价值。
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #include<vector> 7 //#include<stack> 8 #include<bitset> 9 #define R(a,b,c) for(register int (a)=(b);(a)<=(c);++(a)) 10 #define nR(a,b,c) for(register int (a)=(b);(a)>=(c);--(a)) 11 #define Ii inline int 12 #define Iv inline void 13 #define Il inline long long 14 #define Ib inline bool 15 #define INF 0x7ffffff 16 #define re register 17 #define ll long long 18 #define Max(a,b) ((a)>(b)?(a):(b)) 19 #define Min(a,b) ((a)<(b)?(a):(b)) 20 #define Cmin(a,b) ((a)=(a)<(b)?(a):(b)) 21 #define Cmax(a,b) ((a)=(a)>(b)?(a):(b)) 22 #define Fill(a,b) memset((a),(b),sizeof((a))) 23 #define D_e_Line printf(" ------------- "); 24 #define D_e(x) printf(" ______%d_______ ",x) 25 #define Pause() system("pause") 26 using namespace std; 27 Ii read(){ 28 int s=0,f=1;char c; 29 for(c=getchar();c>‘9‘||c<‘0‘;c=getchar())if(c==‘-‘)f=-1; 30 while(c>=‘0‘&&c<=‘9‘)s=s*10+(c^‘0‘),c=getchar(); 31 return s*f; 32 } 33 template<class T>Iv print(T x){ 34 if(x<0)putchar(‘-‘),x=-x; 35 if(x>9)print(x/10); 36 putchar(x%10^‘0‘); 37 } 38 int base[3]={2,7,61}; 39 ll Pow(ll a,ll b,ll mod){ 40 ll s=1; 41 while(b){ 42 if(b&1)s=(s*a)%mod; 43 a=a*a%mod,b>>=1; 44 } 45 return s%mod; 46 } 47 Ib Miller_Rabin(ll n){ 48 if(n==2||n==7||n==61)return 1; 49 if(n<2||n%2==0||n%7==0||n%61==0)return 0; 50 R(i,0,2){ 51 int flag=0; 52 ll a=base[i]; 53 ll tmp=n-1;int k=0; 54 while(tmp%2==0) 55 ++k,tmp>>=1; 56 ll x=Pow(a,tmp,n); 57 if(x==1||x==n-1){flag=1;continue;} 58 while(k--){ 59 x=x*x%n; 60 if(x==n-1){ 61 flag=1;break; 62 } 63 } 64 if(!flag)return 0; 65 } 66 return 1; 67 } 68 int main(){ 69 int n=read(),T=read(); 70 while(T--){ 71 (Miller_Rabin(read())==1)? 72 printf("Yes "): 73 printf("No "); 74 } 75 return 0; 76 }
以上是关于TemplateMiller Rabin的主要内容,如果未能解决你的问题,请参考以下文章