結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー Suu0313Suu0313
提出日時 2021-04-03 01:01:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 801 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 69,116 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-25 15:29:44
合計ジャッジ時間 1,557 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

constexpr bool is_prime(long long n) {
  if(n <= 1) return false;
  constexpr long long witnesses[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
  for(long long a : witnesses) if(n == a) return true;
  if(~n & 1) return false;
  long long d = n-1;
  d >>= __builtin_ctzll(d);
  for(long long a : witnesses) {
    long long t = d, y = 1;
    {
      long long x = a%n, k = t, m = n;
      while(k){
        if(k & 1) y = y * x % m;
        x = x * x % m; k >>= 1;
      }
    }
    while (t != n-1 && y != 1 && y != n-1) {
        y = y * y % n;
        t <<= 1;
    }
    if (y != n-1 && ~t & 1) return false;
  }
  return true;
}

#include<iostream>
using namespace std;

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

  while(n--){
    long long x; cin >> x;
    cout << x << " " << is_prime(x) << endl;
  }
}
0