結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
Lanatus
|
| 提出日時 | 2022-12-13 04:27:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 693 ms / 9,973 ms |
| コード長 | 920 bytes |
| コンパイル時間 | 2,213 ms |
| コンパイル使用メモリ | 195,260 KB |
| 最終ジャッジ日時 | 2025-02-09 10:51:16 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = __int128_t;
ull modpow(ull a, ll n, ll m) {
if (n == 0) return 1;
if (n % 2) return a % m * modpow(a, n-1, m) % m;
ull t = modpow(a, n/2, m);
return t % m * t % m;
}
bool isPrime(ll x) {
if (x == 2) return true;
if (x == 1 || x % 2 == 0) return false;
ll m = x-1;
ll t = m;
ll s = 0;
while (t % 2 == 0) {
t /= 2;
++s;
}
ll d = m / (1LL << s);
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (a == x) return true;
ull n = a;
n = modpow(n, d, x);
int r = 0;
if (n == 1) continue;
while (n != m) {
n = modpow(n, 2, x);
++r;
if (r == s) return false;
}
}
return true;
}
int main(void) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
ll x;
cin >> x;
cout << x << " " << isPrime(x) << endl;
}
return 0;
}
Lanatus