Miller_Rabin 素数测试算法

 HDU How many prime numbers

Give you a lot of positive integers, just to find out how many prime numbers there are.

根据费马小定理 要求 P 是质数 虽然不是充要条件 但实际上可以根据这个 来测试 素数

注意 a不能是p 的倍数

code:

//
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
long long n; 
long long a[6]={0,2,7,61};
ll mul(ll a,ll b,ll c)
{
    ll ans=0;
    while(b)
    {
        if(b&1) ans=(ans+a)%c;
        a=(a+a)%c;
        b>>=1;
    }
    return ans%c;
}
long long ksm(long long a,long long b ,long long c)
{
    long long ans=1;
    while(b)
    {
        if(b&1) ans=mul(ans,a,c);
        a=mul(a,a,c);
        b>>=1;
    }
    return ans;
}
int main()
{
    //freopen("data.txt","r",stdin);
    //freopen("myp.out","w",stdout);
    long long x=0,ans=0;
    while(~scanf("%lld",&n))
    {
        ans=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&x); 
        long long fla=0;
        for(int j=1;j<=3;j++)
        {
            if((a[j]%x!=0)&&ksm(a[j],x-1,x)!=1)
            {
                fla=1;
                break;
            }
        }
        if(fla==0) 
        {
        ans++;
        //cout<<x<<" ";
        }
        
    }
    printf("%lld\n",ans);
    }
} 

猜你喜欢

转载自www.cnblogs.com/OIEREDSION/p/11330791.html