Miller-Robin's test of prime numbers

#include<iostream>
#include<cstdio>
using namespace std;
typedef long  long LL;
 // 18th prime number: 154590409516822759
 // 19th prime number: 2305843009213693951 (Umemori prime number)
 // 19th prime number: 4384957924686954497 
LL prime [ 6 ] = { 2 , 3 , 5 , 233 , 331 };
LL qmul(LL x, LL y, LL mod) { // Multiplication prevents overflow, if p * p does not explode LL, it can be directly multiplied; O(1) multiplication or conversion to binary addition


    return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
    /*
    LL ret = 0;
    while(y) {
        if(y & 1)
            ret = (ret + x)% mod;
        x = x * 2 % mod;
        y >>= 1;
    }
    return ret;
    */
}
LL qpow(LL a, LL n, LL mod) {
    LL ret = 1;
    while(n) {
        if(n & 1) ret = qmul(ret, a, mod);
        a = qmul(a, a, mod);
        n >>= 1;
    }
    return ret;
}
bool Miller_Rabin(LL p) {
    if(p < 2) return 0;
    if(p != 2 && p % 2 == 0) return 0;
    LL s = p - 1;
    while(! (s & 1)) s >>= 1;
    for(int i = 0; i < 5; ++i) {
        if(p == prime[i]) return 1;
        LL t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1) {
            m = qmul(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1)) return 0;
    }
    return 1;
}
intmain ()
{
    LL n;
    while(cin>>n)
    {
        if(Miller_Rabin(n))
            cout<<true<<endl;
        else cout<<false<<endl;
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325079399&siteId=291194637