hdu 2138 How many prime numbers

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


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

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

 
 
 1 #include <iostream>

 2 #include <cmath>

 3 #include <stdio.h>

 4 #include <cstring>

 5  

 6 using namespace std;

 7  

 8 typedef long long LL;

 9 LL power(LL m,LL n,LL k)

10 {

11     int b = 1;

12     while (n > 0)

13     {

14           if (n & 1)

15              b = (b*m)%k;

16           n = n >> 1 ;

17           m = (m*m)%k;

18     }

19     return b;

20 }

21 bool judge(LL n)

22 {

23     LL i;

24     if (n<=3) return true;

25     for (i=2;i<=ceil(sqrt(n))+1;i++)

26         if (n %i==0) return false;

27         return true;

28 }

29  

30 int main()

31 {

32     LL i,n,x;

33  

34     while (scanf("%I64d",&n)!=EOF)

35     {   LL ans=0;

36         for (i=1;i<=n;i++)

37         {

38             scanf("%I64d",&x);

39           if ((power(61,x-1,x)==1)&&(power(11,x-1,x)==1)&&(power(31,x-1,x)==1)

40                 &&(power(97,x-1,x)==1))

41                  ans++;

42         }

43           printf("%I64d\n",ans);

44     }

45     return 0;

46 }

47  

48  

49  

50  

 

你可能感兴趣的:(number)