How many prime numbers
Time Limit: 3000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21497
Accepted Submission(s): 7287
Problem Description
Give you a lot of positive integers, just to find out how many prime numbers there are.
Input
There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.
Output
For each case, print the number of prime numbers you have found out.
Sample Input
3
2 3 4
Sample Output
2
题意:
给定的n个数有几个是素数。。。。。。
素数判定模板:
任务:
给定一个正整数N,判定N是否为素数。
说明:
Miller-Rabin测试:要测试N是否为素数,首先将N-1分解为
2sd
2
s
d
。在每次测试开始时,先随机选一个介于[1,N-1]的整数a,如果对所有的r
/
/
[0,s-1]都满足且
a2rdmodN!=−1
a
2
r
d
m
o
d
N
!
=
−
1
,N是合数。否则,N有3/4的几率为素数。为了提高测试的正确性,可以选择不同的a进行多次测试。
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll pow_mod(ll a, ll b, ll mod){
ll s = 1;
while(b){
if(b&1) s = (s*a)%mod;
a = (a*a)%mod;
b >>= 1;
}
return s;
}
bool test(int n, int a, int d){
if(n == 2) return true;
if(n == a) return true;
if((n&1) == 0) return false;
while(!(d&1)) d = d>>1;
int t = pow_mod(a,d,n);
while((d != n-1)&&(t != 1)&&(t != n-1)) {
t = (ll)t*t%n;
d = d<<1;
}
return (t == n-1||(d&1) == 1);
}
bool isPrime(int n){
if(n<2) return false;
int a[] = {2,3,61};//测试集;
for(int i = 0; i <= 2; i++)
if(!test(n,a[i],n-1)) return false;
return true;
}
int main()
{
int n,c;
while(scanf("%d",&n)!=EOF)
{
int s = 0;
while(n--)
{
scanf("%d",&c);
if(isPrime(c)) s++;
}
printf("%d\n",s);
}
return 0;
}