結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー TlapesiumTlapesium
提出日時 2020-08-15 19:43:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 815 ms / 9,973 ms
コード長 1,242 bytes
コンパイル時間 3,120 ms
コンパイル使用メモリ 214,056 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:33:16
合計ジャッジ時間 5,248 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 451 ms
5,376 KB
testcase_05 AC 446 ms
5,376 KB
testcase_06 AC 160 ms
5,376 KB
testcase_07 AC 161 ms
5,376 KB
testcase_08 AC 154 ms
5,376 KB
testcase_09 AC 815 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'bool isprime_int64(ull)':
main.cpp:29:27: warning: 'd' may be used uninitialized [-Wmaybe-uninitialized]
   29 |                 if (modpow(a[i], d, N) == 1)continue;
      |                     ~~~~~~^~~~~~~~~~~~
main.cpp:17:20: note: 'd' was declared here
   17 |         ull s = 0, d;
      |                    ^

ソースコード

diff #

#pragma GCC optimize("O3")
//#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<setprecision(10)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

bool isprime_int64(ull N) {
	if (N == 2)return 1;
	if (N % 2 == 0 || N <= 2)return 0;
	ull s = 0, d;
	ull a[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,37 };
	for (ull i = N - 1; i % 2 == 0; i /= 2)s++, d = i / 2;
	auto modpow = [](__int128 x, __int128 k, __int128 mod) {
		__int128 res = 1;
		for (x %= mod; k; x = x * x % mod, k >>= 1)
			if (k & 1) res = res * x % mod;
		return res;
	};
	for (int i = 0; i < 12; i++) {
		bool flag = 1;
		if (N <= a[i])continue;
		if (modpow(a[i], d, N) == 1)continue;
		for (int j = 0; j < s; j++) if (modpow(a[i], d * (1ULL << j), N) == N - 1) {
			flag = 0;
			break;
		}
		if (flag)return 0;
	}

	return 1;
}

int main() {
	io_init;
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		ull x;
		cin >> x;
		cout << x << " " << isprime_int64(x) << endl;
	}

}
0