您的位置:首页 > 其它

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

2015-07-21 11:36 337 查看
题意:

判断一个数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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: