Give you a lot of positive integers, just to find out how many prime numbers there are..
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.
32-bit signed intege,最普通的肯定要超时,筛选法要超内存,开小的话就越界。
miller_rabin算法
一.费马小定里
if n is prime and gcd(a,n) equals one ,then a^(n-1) = 1 (mod n)
费马小定理只是个必要条件,符合费马小定理而非素数的数叫做Carmichael.
前3个Carmichael数是561,1105,1729。
Carmichael数是非常少的。
在1~100000000范围内的整数中,只有255个Carmichael数。
为此又有二次探测定理,以确保该数为素数:
二.二次探测定理
二次探测定理 如果p是一个素数,0<x<p,则方程x^2≡1(mod p)的解为x=1,p-1
根据以上两个定理,如到Miller-Rabin算法的一般步骤:
0、先计算出m、j,使得n-1=m*2^j,其中m是正奇数,j是非负整数
1、随机取一个b,2<=b
2、计算v=b^m mod n
3、如果v==1,通过测试,返回
4、令i=1
5、如果v=n-1,通过测试,返回
6、如果i==j,非素数,结束
7、v=v^2 mod n,i=i+1
8、循环到5
说明:
Miller-Rabin是随机算法
得到的结果的正确率为75%,所以应该多次调用该函数,使正确概率提高为1-(1/4)^s
How many prime numbers
Time Limit: 3000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2188 Accepted Submission(s): 653
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.
**********************************************************************************************
代码:
#include<stdio.h>
#include<stdlib.h>
bool witness(__int64 a,__int64 n)
{
__int64 t,u,x0,x1,b,i;
u=n-1;
t=0;
while(u%2==0)
{
u/=2;
t++;
}
b=a;
x0=1;
while(u)
{
if(u%2)
x0=x0*b%n;
u/=2;
b=b*b%n;
}
for(i=0;i<t;i++)
{
x1=x0*x0%n;
if(x1==1 && x0!=1 && x0!=n-1) return true;
x0=x1;
}
if(x0!=1) return true;
return false;
}
bool miller_rabin(__int64 n)
{
__int64 s[]={2,7,61};
int i;
if(n==2) return true;
if(n==1 || ((n&1)==0)) return false;
for(i=0;i<3;i++)
if(witness(s[i], n)) return false;
return true;
}
main()
{
int n,cnt;
__int64 a;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
while(n-->0)
{
scanf("%I64d",&a);
if(miller_rabin(a)) cnt++;
}
printf("%d\n",cnt);
}
system("pause");
}