HDU 2138 Miller-Rabin 模板题

求素数个数。

 

/** @Date    : 2017-09-18 23:05:15
  * @FileName: HDU 2138  miller-rabin 模板.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL fpow(LL a, LL n, LL mod)//快速幂 
{
    LL res = 1;
    while(n)
    {
        if(n & 1) res = (res * a) % mod;
        a = (a * a % mod + mod) % mod;
        n >>= 1;
    }
    return res;
}
bool Miller_Rabbin(int n,int a)//米勒拉宾素数测试 
{
    int r = 0, s = n - 1;
    if(!(n % a))
        return false;
    while(!(s & 1))
    {
        s>>=1;
        r++;
    }
    LL k = fpow(a, s, n);
    if(k == 1)
        return true;
    for(int j = 0; j < r; j++, k = k * k % n)
        if(k == n-1)
            return true;
    return false;
}
bool IsPrime(int n)//判断是否是素数 
{
    int tab[14] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41};
    for(int i = 0; i < 13; i++)
    {
        if(n == tab[i])
            return true;
        if(!Miller_Rabbin(n, tab[i]))
            return false;
    }
    return true;
}

int main()
{
	int n;
	while(cin >> n)
	{
		LL x;
		LL cnt = 0;
		for(int i = 0; i < n; i++)
			scanf("%ld", &x), cnt += (IsPrime(x)?1:0);
		printf("%lld\n", cnt);
	}
    return 0;
}

转载于:https://www.cnblogs.com/Yumesenya/p/7554032.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值