結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int unsigned long long
random_device rd;
mt19937 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);
}
long long xqrl(int a, int n) {
	int res = 1;
	a %= n;
	if (a ==n-1)return pow(-1,(n - 1) >> 1);
	if (a == 2) {
		int k = n % 8;
		return pow(-1, (k*k - 1) / 8);
	}
	if (!(a & 1))return xqrl(2, n)*xqrl(a / 2, n);
	int A = (a - 1) % 16, N = (n - 1) % 16;
	return pow(-1, A*N / 4)*xqrl(n, a);

}
bool isprime(int p) {
	if (p == 2 || p == 3 || p == 5)return true;
	if (p == 1 || p == 4)return false;
	for(int z=0;z<14;++z) {
		int b = mt() % (p - 2) + 2;
		if (gcd(b, p) > 1) {
			return false;
		}
		
		if (modpow(b, (p - 1)>>1, p) !=(xqrl(b,p)+p)%p) {
			return false;
		}
	}
	return true;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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