您的位置:首页 > 其它

HDU 2138 How many prime numbers

2013-12-27 14:17 459 查看
米勒罗宾素数测试:

/*
if n < 1,373,653, it is enough to test a = 2 and 3.
if n < 9,080,191, it is enough to test a = 31 and 73.
if n < 4,759,123,141, it is enough to test a = 2, 7, and 61.
if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11.
*/
#include <cstdio>
long long power(long long m,long long n,long long k)
{
long long b = 1;
while (n>0)
{
if (n&1) b=(b*m)%k;
n=n>>1;
m=(m*m)%k;
}
return b;
}

bool Miller_Rabbin(long long t)
{
if (t==2) return true;
if (power(2,t-1,t)!=1) return false;
if (power(7,t-1,t)!=1) return false;
if (power(61,t-1,t)!=1) return false;
return true;
}

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