【HDU2138】How many prime numbers
【题目大意】
给n个数判断有几个素数。(每个数<=2^32)
注意多组数据
【题解】
用Rabin_Miller测试跑得飞快。。。
1 /************* 2 HDU 2138 3 by chty 4 2016.11.5 5 *************/ 6 #include<iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<cstdlib> 10 #include<ctime> 11 #include<cmath> 12 #include<algorithm> 13 using namespace std; 14 typedef long long ll; 15 ll n,ans; 16 inline ll read() 17 { 18 ll x=0,f=1; char ch=getchar(); 19 while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();} 20 while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();} 21 return x*f; 22 } 23 ll fast(ll a,ll b,ll mod) {ll sum=1;for(;b;b>>=1,a=a*a%mod)if(b&1)sum=sum*a%mod;return sum;}//一行快速幂 24 bool Rabin_Miller(ll p,ll a) 25 { 26 if(p==2) return 1; 27 if(p&1==0||p==1) return 0; 28 ll d=p-1; 29 while(!(d&1)) d>>=1; 30 ll m=fast(a,d,p); 31 if(m==1) return 1; 32 while(d<p) 33 { 34 if(m==p-1) return 1; 35 d<<=1; 36 m=m*m%p; 37 } 38 return 0; 39 } 40 bool isprime(ll x) 41 { 42 static ll prime[9]={2,3,5,7,11,13,17,19,23}; 43 for(ll i=0;i<9;i++) 44 { 45 if(x==prime[i]) return 1; 46 if(!Rabin_Miller(x,prime[i])) return 0; 47 } 48 return 1; 49 } 50 int main() 51 { 52 freopen("cin.in","r",stdin); 53 freopen("cout.out","w",stdout); 54 while(~scanf("%lld",&n)) 55 { 56 ans=0; 57 for(ll i=1;i<=n;i++) 58 { 59 ll x=read(); 60 if(isprime(x)) ans++; 61 } 62 printf("%lld\n",ans); 63 } 64 return 0; 65 }