結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー pekempeypekempey
提出日時 2017-10-22 14:17:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 9,973 ms
コード長 1,007 bytes
コンパイル時間 1,134 ms
コンパイル使用メモリ 85,580 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:10:45
合計ジャッジ時間 2,497 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 142 ms
5,376 KB
testcase_05 AC 146 ms
5,376 KB
testcase_06 AC 50 ms
5,376 KB
testcase_07 AC 46 ms
5,376 KB
testcase_08 AC 44 ms
5,376 KB
testcase_09 AC 262 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cassert>
#include <cstring>
#include <random>
#include <ctime>

using namespace std;

bool miller_rabin(unsigned long long n) {
  if (n <= 1) return false;
  if (n == 2) return true;
  static mt19937 mt(time(NULL));
  using u64 = unsigned long long;
  using u128 = __uint128_t;
  u64 s = n - 1;
  int e = 0;
  for (; s % 2 == 0; s /= 2) e++;
  auto mul = [&](u64 a, u64 b, u64 m) { return u128(a) * b % m; };
  for (int ii = 0; ii < 8; ii++) {
    u64 x = std::uniform_int_distribution<u64>(2, n - 1)(mt);
    u64 r = 1;
    for (u64 i = s; i > 0; i >>= 1, x = mul(x, x, n)) {
      if (i & 1) r = mul(r, x, n);
    }
    if (r == 1) continue;
    for (int i = 1; i < e && r != n - 1; i++) {
      r = mul(r, r, n);
    }
    if (r != n - 1) return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;

  while (n--) {
    long long x;
    scanf("%lld", &x);
    printf("%lld %d\n", x, miller_rabin(x));
  }
}
0