結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2017-10-20 13:17:06 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 395 ms / 9,973 ms |
| コード長 | 1,428 bytes |
| コンパイル時間 | 502 ms |
| コンパイル使用メモリ | 59,100 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 22:33:54 |
| 合計ジャッジ時間 | 2,639 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:47:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
47 | int n; scanf("%d", &n);
| ~~~~~^~~~~~~~~~
main.cpp:50:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
50 | long long x; scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
ソースコード
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
using u64 = unsigned long long;
u64 mod_mul(u64 a, u64 b, u64 m) {
return __uint128_t(a) * b % m;
// https://github.com/wizykowski/miller-rabin/blob/master/mulmod64.h
//u64 res;
//__asm__("mulq %2;"
// "divq %3;"
// : "=&d"(res), "+%a"(a)
// : "rm"(b), "rm"(m)
// : "cc");
//return res;
}
u64 mod_pow(u64 x, u64 k, u64 m) {
if(k == 0) { return 1; }
if(k & 1) { return mod_mul(x, mod_pow(x, k-1, m), m); }
return mod_pow(mod_mul(x, x, m), k>>1, m);
}
bool is_prime(u64 n) {
if(n < 2) { return false; }
for(u64&& p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}) {
if(n < p * p) { return true; }
if(n % p == 0) { return false; }
}
u64 s = 0, d = n-1;
while(!(d & 1)) { ++s, d>>=1; }
for(u64&& a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
u64 x = mod_pow(a, d, n);
if(x == 1 || x == n-1) { continue; }
bool flag = false;
for(u64 loop=1; loop<s; ++loop) {
x = mod_mul(x, x, n);
if(x == n-1) { flag = true; break; }
}
if(!flag) { return false; }
}
return true;
}
int main(void) {
int n; scanf("%d", &n);
assert(1 <= n && n <= 10000);
for(int i=0; i<n; ++i) {
long long x; scanf("%lld", &x);
assert(1 <= x && x <= 9223372036854775807LL);
bool flag = is_prime(x);
printf("%lld %d\n", x, flag);
}
return 0;
}