poj 3641 快速幂+米勒罗宾判断大素数

题意:

判断一个数p是否满足:

1.p不是素数;

2.pow_mod(a, p, p) == a % p。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 1000001 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

LL pow_mod(LL a, LL n, LL mod)
{
    if (n == 0)
        return 1;
    LL x = pow_mod(a, n >> 1, mod);
    LL res = x * x % mod;
    if (n % 2)
        res = res * a % mod;
    return res;
}

bool Witness(LL a, LL n)
{
    LL t = 0, m = n - 1;
    while (!(m & 1))
    {
        t++;
        m >>= 1;
    }
    LL x = pow_mod(a, m, n);
    if (x == 1 || x == n - 1)
        return false;
    while (t--)
    {
        x = x * x % n;
        if (x == n - 1)
            return false;
    }
    return true;
}
const int Times = 11;
bool Miller_Rabin(LL n)
{
    if (n < 2)
        return false;
    if (n == 2)
        return true;
    if (!(n & 1))
        return false;
    for (int i = 1; i <= Times; i++)
    {
        LL a = rand() % (n - 1) + 1;
        if (Witness(a, n))
            return false;
    }
    return true;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    LL p, a;
    while (~scanf("%lld%lld", &p, &a))
    {
        if (!p && !a)
            break;
        if (!Miller_Rabin(p) && pow_mod(a, p, p) == a % p)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值