POJ3641 UVA11287 HDU1905 Pseudoprime numbers【素數判定+快速模冪+Miller_Rabin素性測試算法】

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章