結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー yuruhiyayuruhiya
提出日時 2020-07-28 22:12:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 875 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 200,268 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:05:05
合計ジャッジ時間 2,657 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
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;

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

bool MillerRabin(const __int128_t n) {
	assert(n > 0);
	if (n == 2) {
		return true;
	} else if (n == 1 || n % 2 == 0) {
		return false;
	}
	__int128_t d = n - 1;
	while (d % 2 == 0) {
		d /= 2;
	}
	for (__int128_t a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
		__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 % 2 == 0) {
			return false;
		}
	}
	return true;
}

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