結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー kya_skikya_ski
提出日時 2020-07-28 11:55:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,377 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 69,120 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:04:37
合計ジャッジ時間 1,856 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <array>
#include <cstdint>

struct miller_rabin {
	using u128 = __uint128_t;
	using u64 = std::uint_fast64_t;
	using u32 = std::uint_fast32_t;
private :
	static constexpr std::array<u64, 7> arr = {2, 325, 9375, 28178, 450775, 9780504, 1795265022ULL};

	void mul (u64 &x, const u64 &mod) {
		u128 buff = x;
		buff = (buff * buff) % mod;
		x = (u64)(buff);
	}

	bool test (u64 a, u64 n, u64 d, u32 s) {
		u64 cur = modpow(a, d, n);
		if (cur == 1) return false;
		for (u32 r = 0; r < s; r++) {
			if (cur == n - 1) return false;
			mul(cur, n);
		}
		return true;
	}

public :
	miller_rabin () = default;

	u64 modpow (u64 x, u64 exp, u64 mod) {
		u128 cur = 1, p = x;
		while (exp) {
			if (exp & 1) cur = cur * p % mod;
			p = p * p % mod;
			exp >>= 1;
		}
		return cur;
	}

	template<class T>
	bool operator() (T n) {
		if (n < 2) return false;
		if (n == 2) return true;
		if (not (n & 1)) return false;
		u64 d = n - 1;
		u32 s = 0;
		while (not (d & 1)) { d >>= 1; s++; }
		for (const u64 &a : arr) {
			if (n <= a) return true;
			if (test(a, n, d, s)) return false;
		}
		return true;
	}

};


#include <iostream>

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	
	int query;
	miller_rabin isprime;
	std::cin >> query;
	
	while (query--) {
		long long x;
		std::cin >> x;
		std::cout << x << ' ' << isprime(x) << '\n';
	}
	
	return 0;
}
0