結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー LuzhiledLuzhiled
提出日時 2017-11-07 14:47:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 268 ms / 9,973 ms
コード長 830 bytes
コンパイル時間 1,498 ms
コンパイル使用メモリ 160,092 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:13:36
合計ジャッジ時間 2,939 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 158 ms
5,376 KB
testcase_05 AC 160 ms
5,376 KB
testcase_06 AC 75 ms
5,376 KB
testcase_07 AC 74 ms
5,376 KB
testcase_08 AC 75 ms
5,376 KB
testcase_09 AC 268 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

bool is_SPRP(uint64_t n, uint64_t a) {
  uint64_t s = __builtin_ctzll(n - 1), d = (n - 1) >> s, cur = 1;
  for (uint64_t pw = d; pw; pw >>= 1) {
    if (pw & 1) cur = ((__uint128_t)cur * a) % n;
    a = ((__uint128_t)a * a) % n;
  }
  if (cur == 1) return true;
  for (; s--; cur = ((__uint128_t)cur * cur) % n)
    if (cur == n - 1) return true;
  return false;
}

bool is_prime(uint64_t x) {
  if (x <= 1) return false;
  vector<uint64_t> bases{2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  if (x < 1ull << 32) bases = {2, 7, 61};

  for (const uint64_t base : bases)
    if (x != base && !is_SPRP(x, base)) return false;
  return true;
}

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

  while (q--) {
    long long n;
    cin >> n;

    cout << n << " " << is_prime(n) << endl;
  }
}
0