結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ningenMeningenMe
提出日時 2020-09-05 05:24:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 9,973 ms
コード長 823 bytes
コンパイル時間 2,012 ms
コンパイル使用メモリ 203,528 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:34:24
合計ジャッジ時間 3,213 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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


// N <= 1e9 -> long long
// N > 1e9  -> int128
template<class T> int MillerRabin(const T N, int challenge = 10) {
	if (N <= 1) return 0;
	if (N == 2) return 1;
    if (N%2 == 0) return 0;
	T M = N - 1,cnt = 0;
	for (; M % 2 == 0; M /= 2,cnt++);
	for (T a:{2,3,5,7,11,13,17,19,23,29,31,37}) {
        if(N <= a) break;
        T r = 1;
		for (T K = M; K > 0; K >>= 1, (a *= a) %= N) if (K & 1) (r *= a) %= N;
		if (r == 1) continue;
		for (int j = 1; j < cnt && r != N - 1; j++) (r *= r) %= N;
		if (r != N - 1) return 0;
	}
	return 1;
}

int main() {
    long long N; cin >> N;
    vector<long long> A(N);
    for(int i = 0; i < N; ++i) cin >> A[i];
    for(int i = 0; i < N; ++i) {
        cout << A[i] << " " << MillerRabin<__int128_t>(A[i]) << endl;
    }
    return 0;
}
0