結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ninoinuininoinui
提出日時 2020-07-18 11:04:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 9,973 ms
コード長 678 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 202,244 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 09:31:17
合計ジャッジ時間 2,959 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 118 ms
6,940 KB
testcase_05 AC 119 ms
6,944 KB
testcase_06 AC 47 ms
6,940 KB
testcase_07 AC 48 ms
6,940 KB
testcase_08 AC 48 ms
6,944 KB
testcase_09 AC 230 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

bool isp(long n) {
  if (n < 2) return false;
  vector<long> bases {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  long s = __builtin_ctzll(n - 1), d = n >> s;
  for (auto b : bases) {
    long p = 1, i = s, j = d, k = b;
    while (j > 0) {
      if (j & 1) p = (__int128) p * k % n;
      k = (__int128) k * k % n;
      j >>= 1;
    }
    while (p != 1 && p != n - 1 && b % n && i--) p = (__int128) p * p % n;
    if (p != n - 1 && i != s) return false;
  }
  return true;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  long N, X;
  cin >> N;
  while (cin >> X) cout << X << " " << isp(X) << "\n"; 
}
0