結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 191 ms
5,376 KB
testcase_05 AC 186 ms
5,376 KB
testcase_06 AC 51 ms
5,376 KB
testcase_07 AC 48 ms
5,376 KB
testcase_08 AC 53 ms
5,376 KB
testcase_09 AC 365 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Prime{
	using int128 = __int128_t;
	inline static array<int128,12> a128 = {2,3,5,7,11,13,17,19,23,29,31,37};
	inline static array<long long,3> a64 = {2, 7, 61};
	inline static int miller_rabin64(const int128 N) {
		long long M = N - 1,cnt = 0;
		for (; M % 2 == 0; M /= 2,cnt++);
		for (int128 a: a128) {
			if(N <= a) break;
			int128 r = 1;
			for (long long 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;
	}
	inline static int miller_rabin32(const long long N) {
		long long M = N - 1,cnt = 0;
		for (; M % 2 == 0; M /= 2,cnt++);
		for (long long a: a64) {
			if(N <= a) break;
			long long r = 1;
			for (long long 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;
	}

	template<class T> static 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;
	}
public:
	inline static int is_prime(long long n) {
		if (n <= 1) return 0;
		if (n == 2) return 1;
		if (n%2 == 0) return 0;
		return MillerRabin<int128>(n);
		if (n < (1LL<<32)) return miller_rabin32(n);
		else return miller_rabin64(n);
	}
};


int main() {
	cin.tie(0);ios::sync_with_stdio(false);
    long long N; cin >> N;
	string ans = "";
    for(int i = 0; i < N; ++i) {
		long long A; cin >> A;
		ans += to_string(A) + " " + to_string(Prime::is_prime(A)) + "\n";
	}
	cout << ans;
    return 0;
}
0