結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
Lanatus
|
| 提出日時 | 2022-12-12 22:36:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 278 ms / 9,973 ms |
| コード長 | 1,436 bytes |
| コンパイル時間 | 1,795 ms |
| コンパイル使用メモリ | 193,708 KB |
| 最終ジャッジ日時 | 2025-02-09 10:37:02 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
using namespace std;
template <class T, class U>
T power(T base, U exp, U mod) {
T res = 1;
base %= mod;
while (exp) {
if (exp & 1) (res *= base) %= mod;
(base *= base) %= mod;
exp >>= 1;
}
return res;
}
bool is_prime(int n) {
if (n < 2) return false;
int s = __builtin_ctz(n - 1);
int d = (n - 1) >> s;
for (int a : {2, 7, 61}) {
if (a % n == 0) continue;
long long cur = a;
cur = power(cur, d, n);
if (cur == 1) continue;
bool witness = true;
for (int r = 0; r < s; ++r) {
if (cur == n - 1) {
witness = false;
break;
}
(cur *= cur) %= n;
}
if (witness) return false;
}
return true;
}
bool is_prime(long long n) {
if (n < 2) return false;
int s = __builtin_ctzll(n - 1);
long long d = (n - 1) >> s;
for (long long a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (a % n == 0) continue;
__int128_t cur = a;
cur = power(cur, d, n);
if (cur == 1) continue;
bool witness = true;
for (int r = 0; r < s; ++r) {
if (cur == n - 1) {
witness = false;
break;
}
(cur *= cur) %= n;
}
if (witness) return false;
}
return true;
}
signed main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
cout << x << " " << is_prime(x) << endl;
}
return 0;
}
Lanatus