miller_rabin hdu 2138 判断是否是素数

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <time.h>
#include <map>
#include <algorithm>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1000;
const int maxx=1e7+100;
const double EPS=1e-7;
const int mod=998244353;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template<class T> void Scan(T &x)
{
    static int CH;
    while((CH=getchar())<'0'||CH>'9');
    for(x=CH-'0';(CH=getchar())>='0'&&CH<='9';x=(x<<3)+(x<<1)+(CH-'0'));
}

const int s=10;
inline LL mul_mod(LL a, LL b, LL m)
{
    LL c = a*b-(LL)((long double)a*b/m+0.5)*m;
    return c<0 ? c+m : c;
}

LL fast_exp(LL a,LL x,LL m)
{
    LL b = 1;
    while (x)
    {
        if (x & 1)
            b = mul_mod(b, a, m);
        a = mul_mod(a, a, m);
        x >>= 1;
    }
    return b;
}

bool MR(LL n)
{
    if (!(n&1))
        return n == 2;
    LL t = 0, u;
    for (u = n-1; !(u&1); u >>= 1)
        ++t;
    for (int i = 0; i < s; ++i)
    {
        LL a = rand()%(n-2)+2, x = fast_exp(a, u, n);
        for (LL j = 0, y; x != 1 && j < t; ++j, x = y)
        {
            y = mul_mod(x, x, n);
            if (y == 1 && x != n-1)
                return false;
        }
        if (x != 1)
            return false;
    }
    return true;
}

int main()
{
    srand();
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        int ans=0;
        for(int i=1;i<=t;i++)
        {
            LL x;
            Scan(x);
            if(MR(x))
                ans++;
        }
        cout<<ans<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值