結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー RhoRho
提出日時 2019-10-19 02:43:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,145 bytes
コンパイル時間 1,723 ms
コンパイル使用メモリ 168,540 KB
実行使用メモリ 8,576 KB
最終ジャッジ日時 2024-04-29 13:44:10
合計ジャッジ時間 34,470 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int unsigned long long
random_device rd;
mt19937_64 mt(rd());
int modpro(int x, int n, int md) {//log^2 n
	int res = 0;
	for (; n; n >>= 1) {
		if (n & 1)res =(res+x)%md; x = (x + x) % md;
	}
	return res;
}
int modpow(int x, int n, int md) {//log n
	int res = 1;
	for (; n; n >>= 1) {
		if (n & 1)res = modpro(res, x, md);
		x = modpro(x, x, md);
	}
	return res;
}

int gcd(int a, int b) {
	if (!b)return a; return gcd(b, a%b);
}

bool isprime(int n) {
	if (n < 2)return false;

	int m = n - 1;
	int e = 0;
	while (!(m & 1)) {
		m /= 2; e++;
	}
	for(int z=0;z<7;z++) {
		int b = mt() % (n-1)+1;
		if (gcd(b, n) != 1)return false;
		bool ok = false;
		if (modpow(b, m, n) != 1) {
			for (int k = 0; k < e; k++) {
				if (modpow(b, m*(1ll << k), n) == n - 1) {
					ok = true; break;
				}
			}
		}
		else ok = true;
		if (!ok) {
			return false;
		}
	}
	return true;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	signed n; cin >> n;
	int p;
	for (int i = 0; i < n; i++) {
		cin >> p;
		cout << p;
		if (isprime(p))cout << " 1" << endl;
		else cout << " 0" << endl;
	}
}
0