結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー LanatusLanatus
提出日時 2022-12-12 22:36:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 9,973 ms
コード長 1,436 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 194,620 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-24 12:19:25
合計ジャッジ時間 3,109 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define all(v) begin(v), end(v)

using namespace std;

template <class T, class U>
T power(T base, U exp, U mod) {
  T res = 1;
  base %= mod;
  while (exp) {
    if (exp & 1) (res *= base) %= mod;
    (base *= base) %= mod;
    exp >>= 1;
  }
  return res;
}

bool is_prime(int n) {
  if (n < 2) return false;
  int s = __builtin_ctz(n - 1);
  int d = (n - 1) >> s;
  for (int a : {2, 7, 61}) {
    if (a % n == 0) continue;
    long long cur = a;
    cur = power(cur, d, n);
    if (cur == 1) continue;
    bool witness = true;
    for (int r = 0; r < s; ++r) {
      if (cur == n - 1) {
        witness = false;
        break;
      }
      (cur *= cur) %= n;
    }
    if (witness) return false;
  }
  return true;
}

bool is_prime(long long n) {
  if (n < 2) return false;
  int s = __builtin_ctzll(n - 1);
  long long d = (n - 1) >> s;
  for (long long a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
    if (a % n == 0) continue;
    __int128_t cur = a;
    cur = power(cur, d, n);
    if (cur == 1) continue;
    bool witness = true;
    for (int r = 0; r < s; ++r) {
      if (cur == n - 1) {
        witness = false;
        break;
      }
      (cur *= cur) %= n;
    }
    if (witness) return false;
  }
  return true;
}


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

  for (int i = 0; i < n; ++i) {
    long long x;
    cin >> x;

    cout << x << " " << is_prime(x) << endl;
  }

  return 0;
}
0