How many prime numbers
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 12955 Accepted Submission (s): 4490
Problem Description Give you a lot of positive integers, just to find out how many prime numbers there is.
Input There is a lot of cases. In each case, there is a integer N representing the number of integers to find. Each of the integer won ' t exceed 32-bit signed integer, and each of the them won ' t is less than 2.
Output for each case, print the number of prime numbers you has found out.
Sample Input32 3 4
Sample Output2algorithm: Each input a number directly judge the violence Sqrt judge whether the number is prime, and then accumulate the number of primes, can also water over. but can also take this water problem that practice Miller_rabin judge Prime Method! Time complexity is low!
Code:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace Std;long long Pow_mod (long long A, long long I, long long N) {if (i==0) return 1%n;long long Temp=pow_mod (A, i>>1, N); temp = temp*temp%n;if (i&1) temp = (long long) Temp*a%n;return temp;} BOOL Test (int n, int a, int dd) {if (n==2) return true;if (n==a) return true;if ((n&1) ==0) return False;while (! ( dd&1)) Dd=dd>>1;int T=pow_mod (A, DD, n); Call Fast power function while ((dd!=n-1) && (t!=1) && (t!=n-1)) {T = (long Long) t*t%n;dd=dd<<1;} Return (T==n-1 | | (dd&1) ==1);} BOOL Miller_rabin_isprime (int n)//o (Logn) {if (n<2) return False;int a[]={2, 3, ","//for (int i=0; i<3; i++) if (!tes T (n, A[i], n-1)) return False;return true;} int main () {int n; while (scanf ("%d", &n)!=eof) {int dd; int cnt=0; while (n--) {scanf ("%d", &DD); if (Miller_rabin_isprime (DD)) cnt++;} printf ("%d\n", CNT); } return 0;}
HDU 2138 How many prime numbers (Miller_rabin method of judging prime numbers "* template" used fast power algorithm)