結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
👑 |
| 提出日時 | 2022-08-27 20:16:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 211 ms / 9,973 ms |
| コード長 | 1,337 bytes |
| コンパイル時間 | 564 ms |
| コンパイル使用メモリ | 29,952 KB |
| 最終ジャッジ日時 | 2025-01-31 06:31:33 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:36:19: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘int64_t*’ {aka ‘long int*’} [-Wformat=]
36 | scanf("%lld", &x);
| ~~~^ ~~
| | |
| | int64_t* {aka long int*}
| long long int*
| %ld
main.cpp:37:20: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
37 | printf("%lld %d\n", x, miller_rabin(x) ? 1 : 0);
| ~~~^ ~
| | |
| | int64_t {aka long int}
| long long int
| %ld
main.cpp:33:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
33 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
main.cpp:36:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
36 | scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
ソースコード
#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);
}
}