結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー pyraninepyranine
提出日時 2021-01-02 08:34:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,014 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 167,012 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:47:14
合計ジャッジ時間 2,327 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using Int8 = int8_t;
using Int16 = int16_t;
using Int32 = int32_t;
using Int64 = int64_t;
using Int128 = __int128_t;

using Word8 = uint8_t;
using Word16 = uint16_t;
using Word32 = uint32_t;
using Word64 = uint64_t;
using Word128 = __uint128_t;

using Int = int_fast64_t;
using Word = uint_fast64_t;

using F32 = float;
using F64 = double;
using F80 = long double;

using VS = vector<string>;
using VVS = vector<vector<string>>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using VI = vector<Int>;
using VW = vector<Word>;
using VVI = vector<vector<Int>>;
using VVW = vector<vector<Word>>;
using PII = pair<Int,Int>;
using PWW = pair<Word,Word>;
using VPII = vector<pair<Int,Int>>;
using VPWW = vector<pair<Word,Word>>;

#define LOOP(n) for(Int _ipiewnsjiw=0; _ipiewnsjiw<(n); _ipiewnsjiw++)
#define REP(i,n) for(Int i=0, i##_len=(n); i<i##_len; ++i)
#define RANGE(i,a,b) for(Int i=(a), i##_len(b); i<=i##_len; ++i)
#define SZ(obj) ((Int)(obj).size())
#define UNIQUE(obj) (obj).erase(unique((obj).begin(),(obj).end()),(obj).end())
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()

Int mult(Int a, Int b, Int md) {
  Int ret = a * b - md * (Int) (1.L / md * a * b);
  return ret + md * (ret < 0) - md * (ret >= (Int)md);
}
Int power(Int a, Int b, Int md) {
  a %= md;
	Int ans = 1;
  while (b > 0) {
    if (b & 1) ans = mult(ans, a, md);
    a = mult(a, a, md);
    b >>= 1;
  }
	return ans;
}
int miller_rabin(Int n) {
  if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
  Int s = __builtin_ctzll(n - 1);
  Int d = n >> s;
  Int A[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  for (Int a : A) {
    Int p = power(a % n, d, n), i = s;
    while (p != 1 && p != n - 1 && a % n && i--) p = mult(p, p, n);
    if (p != n - 1 && i != s) return 0;
  }
  return 1;
}

int main() {
  int Q; cin >> Q;
  while (Q--) {
    Word64 x; cin >> x;
    cout << x << ' ' << miller_rabin(x) << '\n';
  }
  return 0;
}
0