【题目】
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 个数中素数的个数,这些数在 int 范围内
其实呢,这道题用暴力(即用 的时间处理每个数)是可以 A 的,但是抱着认真严谨(实际上是找不到板题)的态度,我还是决定用 素数测试写一下这道题
然后就是模板了~
【代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int prime[10]={2,3,5,7,11,13,17,19,23,29};
int Quick_Multiply(int a,int b,int c)
{
long long ans=0,res=a;
while(b)
{
if(b&1)
ans=(ans+res)%c;
res=(res+res)%c;
b>>=1;
}
return (int)ans;
}
int Quick_Power(int a,int b,int c)
{
int ans=1,res=a;
while(b)
{
if(b&1)
ans=Quick_Multiply(ans,res,c);
res=Quick_Multiply(res,res,c);
b>>=1;
}
return ans;
}
bool Miller_Rabin(int x)
{
int i,j,k;
int s=0,t=x-1;
if(x==2) return true;
if(x<2||!(x&1)) return false;
while(!(t&1))
{
s++;
t>>=1;
}
for(i=0;i<10&&prime[i]<x;++i)
{
int a=prime[i];
int b=Quick_Power(a,t,x);
for(j=1;j<=s;++j)
{
k=Quick_Multiply(b,b,x);
if(k==1&&b!=1&&b!=x-1)
return false;
b=k;
}
if(b!=1) return false;
}
return true;
}
int main()
{
int n,i,x,ans;
while(~scanf("%d",&n))
{
ans=0;
for(i=1;i<=n;++i)
{
scanf("%d",&x);
if(Miller_Rabin(x))
ans++;
}
printf("%d\n",ans);
}
return 0;
}