結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー firiexp
提出日時 2018-09-13 19:26:51
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,437 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 462 ms
コンパイル使用メモリ 86,484 KB
最終ジャッジ日時 2026-05-18 08:25:19
合計ジャッジ時間 1,158 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:35:14: error: 'uint64_t' was not declared in this scope
   35 | bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
      |              ^~~~~~~~
main.cpp:8:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    7 | #include <queue>
  +++ |+#include <cstdint>
    8 | 
main.cpp:35:26: error: 'uint64_t' was not declared in this scope
   35 | bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
      |                          ^~~~~~~~
main.cpp:35:26: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
main.cpp:35:38: error: 'uint64_t' was not declared in this scope
   35 | bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
      |                                      ^~~~~~~~
main.cpp:35:38: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
main.cpp:35:50: error: 'uint64_t' was not declared in this scope
   35 | bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
      |                                                  ^~~~~~~~
main.cpp:35:50: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
main.cpp:35:60: error: expression list treated as compound expression in initializer [-fpermissive]
   35 | bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
      |                                                            ^
main.cpp: In function 'bool miller_rabin(T)':
main.cpp:47:5: error: 'uint64_t' was not declared in this scope [-Wtemplate-body]
   47 |     uint64_t n = m;
      |     ^~~~~~~~
main.cpp:47:5: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
main.cpp:48:9: error: 'n' was not declared in this scope [-Wtemplate-body]
   48 |     if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
      |         ^
main.cpp:49:13: error: 

ソースコード

diff #
raw source code

#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;}
    static const uint64_t v[] = {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