您的位置:首页 > 其它

hdu 2138 How many prime numbers

2015-02-17 16:58 302 查看

How many prime numbers

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


[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.

[align=left]Sample Input[/align]

3
2 3 4

[align=left]Sample Output[/align]
2

_____________________________
ACM STEPS里的...这题前面一道是求LCM....结果接下来就是这么一道。。。
朴素会超....筛法会爆....题目顺序真是按照难度来的?
于是想到 Miller-Rabin素数测试.......
这个方法是基于费马小定理
我的理解就是...
如果我要判断n是否为素数
只要取k个数 如果满足 a^(n-1)mod n =1 那么n就很可能为素数。
证明什么的...暂时还是算了吧...论文里貌似扯了一大堆
第一次用,竟然真的A了。。。。
感觉更好的办法也许是先打一个比较小的素数表,然后每次random选取若干个进行判断...那样应该更可靠些?
本来想WA掉之后再改的。。。没想到这么写就A掉了。。。。杭电数据略水?

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstring>

using namespace std;

typedef long long LL;
LL power(LL m,LL n,LL k)
{
int b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
bool judge(LL n)
{
LL i;
if (n<=3) return true;
for (i=2;i<=ceil(sqrt(n))+1;i++)
if (n %i==0) return false;
return true;
}

int main()
{
LL i,n,x;

while (scanf("%I64d",&n)!=EOF)
{   LL ans=0;
for (i=1;i<=n;i++)
{
scanf("%I64d",&x);
if ((power(61,x-1,x)==1)&&(power(11,x-1,x)==1)&&(power(31,x-1,x)==1)
&&(power(97,x-1,x)==1))
ans++;
}
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: