結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー minamiminami
提出日時 2019-04-04 09:59:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,892 bytes
コンパイル時間 1,655 ms
コンパイル使用メモリ 166,732 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 13:09:46
合計ジャッジ時間 13,507 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

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 
long long modmul(long long a, long long b, long long mod) {
	long long 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 が long long の最大値より大きければオーバーフローするので掛け算に modmul を使う
long long modpow(long long a, long long e, long long mod) {
	long long 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)
bool millerRabinPrimalityTest(long long x, int iteration) {
	if (x < 2)return false;
	if (x != 2 && x % 2 == 0)return false;
	long long s = x - 1;
	while (s % 2 == 0)s /= 2;
	for (int i = 0; i < iteration; i++) {
		long long a = rand() % (x - 1) + 1, tmp = s;
		long long 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;
}

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