結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー firiexp
提出日時 2018-09-13 19:14:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,430 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 83,160 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-24 09:07:01
合計ジャッジ時間 13,557 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 2;


template<class T>
T mul_ (T x, T y, T M){
    T res = 0;
    for (;y;y/=2) {
        if (y & 1) res = (res + x) % M;
        x = (x + x) % M;
    }
    return res;
}

template<class T>
T pow_ (T x, T n, T M){
    if (n == 0) return 1;
    T res = pow_(x, n/2, M);
    return mul_(mul_(res, res, M),(n & 1 ? x : 1), M);
};

bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
    uint64_t x = pow_(a, d, n);
    if (x == 1) return true;
    for (int r = 0; r < s; ++r) {
        if(x == n-1) return true;
        x = mul_(x, x, n);
    }
    return false;
}

template<class T>
bool miller_rabin(T m){
    uint64_t n = m;
    if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
    uint64_t d = n - 1, s = 0;
    while (!(d&1)) {++s; d >>= 1;}
    const uint64_t v[7] {2, 325, 9375, 28178, 450775, 9780504, 1795265022};

    for (auto &&p : v) {
        if(p >= n) break;
        if(!suspect(p, s, d, n)) return false;
    }
    return true;
}

int main() {
    ll n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        ll k;
        cin >> k;
        cout << k << " " << miller_rabin(k) << "\n";
    }
    return 0;
}
0