結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー pogyomopogyomo
提出日時 2024-04-03 05:06:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 260 ms / 9,973 ms
コード長 1,230 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 61,468 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 05:06:15
合計ジャッジ時間 1,974 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

template<class T> T pow_mod(T A, T N, T M) {
    T res = 1 % M;
    A %= M;
    while (N) {
        if (N & 1) res = (res * A) % M;
        A = (A * A) % M;
        N >>= 1;
    }
    return res;
}

bool MillerRabin(long long N, vector<long long> A) {
    long long s = 0, d = N - 1;
    while (d % 2 == 0) {
        ++s;
        d >>= 1;
    }
    for (auto a : A) {
        if (N <= a) return true;
        long long t, x = pow_mod<__int128_t>(a, d, N);
        if (x != 1) {
            for (t = 0; t < s; ++t) {
                if (x == N - 1) break;
                x = __int128_t(x) * x % N;
            }
            if (t == s) return false;
        }
    }
    return true;
}

bool is_prime(long long N) {
    if (N <= 1) return false;
    if (N == 2) return true;
    if (N % 2 == 0) return false;
    if (N < 4759123141LL)
        return MillerRabin(N, {2, 7, 61});
    else
        return MillerRabin(N, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		long long x;
		cin >> x;
		if (is_prime(x)) {
			cout << x << " " << 1 << endl;
		} else {
			cout << x << " " << 0 << endl;
		}
	}
}
0