結果
| 問題 | No.8030 ミラー・ラビン素数判定法のテスト |
| ユーザー |
|
| 提出日時 | 2022-08-27 20:16:42 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 39 ms / 9,973 ms |
| コード長 | 1,337 bytes |
| 記録 | |
| コンパイル時間 | 234 ms |
| コンパイル使用メモリ | 41,216 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-06-27 06:01:02 |
| 合計ジャッジ時間 | 1,090 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
In file included from main.cpp:2:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/cstdbool:50:6: warning: #warning "<cstdbool> is deprecated in C++17, remove the #include" [-Wcpp]
50 | # warning "<cstdbool> is deprecated in C++17, remove the #include"
| ^~~~~~~
ソースコード
#include <cstdio>
#include <cstdbool>
#include <cstdint>
const uint64_t bases[] = {2,325,9375,28178,450775,9780504,1795265022};
uint64_t modpow(uint64_t b, uint64_t p, uint64_t n) {
if (p == 2) { return (uint64_t)(((__uint128_t)b) * ((__uint128_t)b) % ((__uint128_t)n)); }
uint64_t r = ((p & 1) == 0) ? 1 : b;
for (p >>= 1; p != 0; p >>= 1) {
b = (uint64_t)(((__uint128_t)b) * ((__uint128_t)b) % ((__uint128_t)n));
if ((p & 1) == 1) { r = (uint64_t)(((__uint128_t)r) * ((__uint128_t)b) % ((__uint128_t)n)); }
}
return r;
}
bool miller_rabin(uint64_t n) {
if (n == 2) { return true; }
if (n < 2 || (n & 1) == 0) { return false; }
uint64_t n1 = n - 1, d = n - 1;
int s = 0;
while ((d & 1) == 0) { d >>= 1; s += 1; }
for (int i = 0; i < 7; ++i) {
uint64_t a = bases[i] % n;
if (a == 0) { continue; }
uint64_t t = modpow(a, d, n);
if (t == 1 || t == n1) { continue; }
for (int j = 1; j < s; ++j) { t = modpow(t, 2, n); if (t == n1) { goto nextbases; } }
return false;
nextbases: continue;
}
return true;
}
int main(int argc, char *argv[]) {
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
int64_t x;
scanf("%lld", &x);
printf("%lld %d\n", x, miller_rabin(x) ? 1 : 0);
}
}