您的位置:首页 > 其它

hdu 2138 How many prime numbers

2011-10-06 11:37 435 查看

How many prime numbers

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5099 Accepted Submission(s): 1615


[align=left]Problem Description[/align]
Give you a lot of positive integers, just to find out how many prime numbers there are.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each case, print the number of prime numbers you have found out.

#include <stdio.h>
#include <cmath>
#include <cstdlib>

bool witness(__int64 a,__int64 n)
{
__int64 t,d,x;
d=1;
int i=ceil(log(n-1.0)/log(2.0)) - 1;
for(;i>=0;i--)
{
x=d;  d=(d*d)%n;
if(d==1 && x!=1 && x!=n-1) return true;
if( ((n-1) & (1<<i)) > 0)
d=(d*a)%n;
}
return d==1? false : true;
}
bool miller_rabin(__int64 n)
{
if(n==2)    return true;
if(n==1 || ((n&1)==0))    return false;
for(int i=0;i<50;i++){
__int64 a=rand()*(n-2)/RAND_MAX +1;
if(witness(a, n))    return false;
}
return true;
}

main()
{
int n,cnt;
__int64 a;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
while(n--)
{
scanf("%I64d",&a);
if(miller_rabin(a))
{
cnt++;
// printf("%I64d ",a);
}
}
printf("%d\n",cnt);
}
}


[align=left]Sample Input[/align]

3
2 3 4

[align=left]Sample Output[/align]

2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: