Pseudoprime numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15471 Accepted: 6696
Description
Fermat’s theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)
Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.
Input
Input contains several test cases followed by a line containing “0 0”. Each test case consists of a line containing p and a.
Output
For each test case, output “yes” if p is a base-a pseudoprime; otherwise output “no”.
Sample Input
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
Sample Output
no
no
yes
no
yes
yes
Source
Waterloo Local Contest, 2007.9.23
问题链接:UVA11559 Event Planning
问题简述:(略)
问题分析:
这个问题是验证伪素数问题。p是伪素数的条件是,p不是素数并且满足ap = a (mod p)。伪素数是数论中与费尔马小定理有关的一个重要概念。
这里的Miller_Rabin素性测试算法,没有经过改进,没有考虑Carmichael数(卡迈克尔数)的情况,需要另外写一个改进的Miller_Rabin素性测试算法。
程序说明:
函数isprime()不是一个真正意义上的素数判断函数,只进行奇数判定,对于本题条件是没有问题的。函数powmod()是模幂计算函数。
采用Miller_Rabin素性测试算法,TIME参数需要一个比较大的值才可以AC。另外,在POJ中编译需要采用C++,不可以用G++,否则运行会出错。
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* POJ3641 UVA11287 HDU1905 Pseudoprime numbers */
#include <iostream>
#include <cmath>
using namespace std;
typedef unsigned long long ULL;
// 试除法判断一个数是否为素数
bool isprime(ULL n)
{
ULL end2, i;
end2 = sqrt(n);
for(i=3; i<=end2; i+=2) {
if(n % i == 0)
break;
}
return i > end2;
}
// 模幂计算
ULL powermod(ULL a, ULL n, ULL m)
{
ULL res = 1LL;
while(n) {
if(n & 1LL) { // n % 2 == 1
res *= a;
res %= m;
}
a *= a;
a %= m;
n >>= 1;
}
return res;
}
int main()
{
ULL p, a;
while(cin >> p >> a && (p || a)) {
if(!isprime(p) && powermod(a, p, p) == a % p)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
AC的C++语言程序(改进Miller_Rabin素性测试算法)如下:
/* POJ3641 UVA11287 HDU1905 Pseudoprime numbers */
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
typedef unsigned long long ULL;
const int TIME = 5;
// 快速模幂计算
ULL powmod(ULL a, ULL n, ULL m)
{
ULL res = 1LL;
while(n) {
if(n & 1LL) { // n % 2 == 1
res *= a;
res %= m;
}
a *= a;
a %= m;
n >>= 1;
}
return res;
}
ULL random(ULL n)
{
return (ULL)((double) rand() / RAND_MAX * n + 0.5);
}
bool check(ULL a, ULL n)
{
ULL m = n - 1;
int j = 0;
while((m & 1) == 0) {j++; m >>= 1;}
ULL x = powmod(a, m, n);
if(x == 1 || x == n - 1)
return false;
while(j--) {
x = x * x % n;
if(x == n - 1)
return false;
}
return true;
}
bool Miller_Rabin(ULL n)
{
if(n < 2) return false;
if(n == 2) return true;
if((n & 1) == 0) return false;
ULL x = n - 1;
ULL t = 0;
while((x&1) == 0) {
x >>= 1;
t++;
}
for(int i = 0; i <= TIME; i++)
{
ULL a = random(n - 2) + 1;
if(check(a, n)) return false;
}
return true;
}
int main()
{
srand(time(NULL));
ULL p, a;
while(cin >> p >> a && (p || a)) {
if(Miller_Rabin(p))
cout << "no" << endl;
else {
ULL ans = powmod(a, p, p);
cout << (ans == a ? "yes" : "no") << endl;
}
}
return 0;
}
AC的C++语言程序(Miller_Rabin素性测试算法)如下:
/* POJ3641 UVA11287 HDU1905 Pseudoprime numbers */
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstdio>
using namespace std;
typedef unsigned long long ULL;
const int TIME = 1000;
// 快速模幂计算
ULL powmod(ULL a, ULL n, ULL m)
{
ULL res = 1LL;
while(n) {
if(n & 1LL) { // n % 2 == 1
res *= a;
res %= m;
}
a *= a;
a %= m;
n >>= 1;
}
return res;
}
ULL random(ULL n)
{
return (ULL)((double) rand() / RAND_MAX * n + 0.5);
}
bool Miller_Rabin(ULL p){
for(int i = 1; i <= TIME; i++) {
ULL a = random(p - 2) + 1;
if(powmod(a, p - 1, p) != 1)
return false;
}
return true;
}
int main()
{
srand(time(0));
ULL p, a;
while(cin >> p >> a && (p || a)) {
if(Miller_Rabin(p))
cout << "no" << endl;
else {
ULL ans = powmod(a, p, p);
cout << (ans == a ? "yes" : "no") << endl;
}
}
return 0;
}