zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        }
    } 
    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    HTML5中drag和drop使用
    E
    D
    杜教BM(解决线性递推式的模板)
    Myeclipse下载安装破解详细版
    D
    IDEA-连接MySQL连不上
    E
    C
    D. Ball(树状数组三维排序,求是否存在三个值都比自己大的人)
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11330791.html
Copyright © 2011-2022 走看看