結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー yuppe19 😺yuppe19 😺
提出日時 2020-07-29 10:26:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 729 ms / 9,973 ms
コード長 1,521 bytes
コンパイル時間 2,279 ms
コンパイル使用メモリ 202,988 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:32:49
合計ジャッジ時間 4,364 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

class Random {
	using T = uint64_t;
	mt19937_64 mt;
	random_device rd;

public:
	Random() {
		seed();
	}
	void seed() {
		mt.seed(rd());
	}
	void seed(T s) {
		mt.seed(s);
	}
	T operator()() {
		return mt();
	}
	T operator()(T r) {  // [0, r)
		uniform_int_distribution<T> u(0, 0 < r ? r - 1 : 0);
		return u(mt);
	}
	T operator()(T l, T r) {  // [l, r)
		uniform_int_distribution<T> u(l, max(l, r) - 1);
		return u(mt);
	}
	T dice() {
		return operator()(1, 7);
	}
	bool rand_bool() {
		return operator()(2);
	}
	bool rand_bool(double p) {
		bernoulli_distribution u(p);
		return u(mt);
	}
	template <class T> void shuffle(T& v) {
		std::shuffle(v.begin(), v.end(), mt);
	}
} rnd;

__uint128_t Powmod(__uint128_t a, __int128_t n, __int128_t m) {
	__uint128_t res = 1;
	while (n > 0) {
		if (n & 1)
			res = res * a % m, n--;
		else
			a = a * a % m, n >>= 1;
	}
	return res;
}

bool MillerRabin(const uint64_t n) {
	if (n == 2) {
		return true;
	} else if (n <= 1 || !(n & 1)) {
		return false;
	}
	uint64_t d = n - 1;
	while (!(d & 1)) {
		d >>= 1;
	}
	for (int _ = 20; _--;) {
		auto a = rnd(1, n);
		__int128_t t = d, y = Powmod(a, t, n);
		while (t != n - 1 && y != 1 && y != n - 1) {
			y = y * y % n;
			t <<= 1;
		}
		if (y != n - 1 && !(t & 1)) {
			return false;
		}
	}
	return true;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int32_t n;
	cin >> n;
	while (n--) {
		uint64_t x;
		cin >> x;
		cout << x << ' ' << MillerRabin(x) << '\n';
	}
}
0