标签:
3 2 3 4
2
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL quickPow(LL a,LL d,LL n){ 5 LL ret = 1; 6 while(d){ 7 if(d&1) ret = (ret*a)%n; 8 d >>= 1; 9 a = (a*a)%n; 10 } 11 return ret; 12 } 13 bool check(int a,int d,int n){ 14 if(n == a) return true; 15 while(~d&1) d >>= 1; 16 LL t = quickPow(a,d,n); 17 while(d < n-1 && t != 1 && t != n-1){ 18 t = t*t%n; 19 d <<= 1; 20 } 21 return (d&1) || t == n-1; 22 } 23 bool isP(int n){ 24 if(n == 2) return true; 25 if(n < 2 || 0 == (n&1)) return false; 26 static int p[3] = {2,3,61}; 27 for(int i = 0; i < 3; ++i) 28 if(!check(p[i],n-1,n)) return false; 29 return true; 30 } 31 int main(){ 32 int n,cnt,tmp; 33 while(~scanf("%d",&n)){ 34 cnt = 0; 35 while(n--){ 36 scanf("%d",&tmp); 37 cnt += isP(tmp); 38 } 39 printf("%d\n",cnt); 40 } 41 return 0; 42 }
HDU 2138 How many prime numbers
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4497007.html