結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー minamiminami
提出日時 2019-04-04 10:31:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,874 bytes
コンパイル時間 1,641 ms
コンパイル使用メモリ 167,760 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-29 13:11:26
合計ジャッジ時間 31,188 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 4 ms
5,376 KB
testcase_04 AC 6,689 ms
5,376 KB
testcase_05 AC 6,230 ms
5,376 KB
testcase_06 AC 1,618 ms
5,376 KB
testcase_07 AC 1,591 ms
5,376 KB
testcase_08 AC 1,586 ms
5,376 KB
testcase_09 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

// (a*b) % mod
template<typename T>
T modmul(T a, T b, T mod) {
	T x = 0, y = a % mod;
	while (b > 0) {
		if (b & 1)x = x + y % mod;
		y = y * 2 % mod;
		b >>= 1;
	}
	return x % mod;
}

// 累乗
// O(log e)
// mod^2 が T の最大値より大きければオーバーフローするので掛け算に modmul を使う
template<typename T>
T modpow(T a, T e, T mod) {
	T res = 1;
	while (e > 0) {
		if (e & 1)res = modmul(res, a, mod);
		a = modmul(a, a, mod);
		e >>= 1;
	}
	return res;
}


// 素数判定(Miller-Rabin primality test)
// 2^24程度から
// millerRabinPrimalityTest(n, 5)
template<typename T>
bool millerRabinPrimalityTest(T x, int iteration) {
	if (x < 2)return false;
	if (x != 2 && x % 2 == 0)return false;
	T s = x - 1;
	while (s % 2 == 0)s /= 2;
	for (int i = 0; i < iteration; i++) {
		T a = rand() % (x - 1) + 1, tmp = s;
		T mod = modpow(a, tmp, x);
		while (tmp != x - 1 && mod != 1 && mod != x - 1) {
			mod = modmul(mod, mod, x); // mod * mod % x;
			tmp *= 2;
		}
		if (mod != x - 1 && tmp % 2 == 0)return false;
	}
	return true;
}

using u128 = __uint128_t;

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	rep(_, 0, n) {
		long long x; cin >> x;
		cout << x << " " << millerRabinPrimalityTest(u128(x), 10) << endl;
	}
	return 0;
}
0