結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2018-09-13 19:57:44 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,337 bytes |
| コンパイル時間 | 934 ms |
| コンパイル使用メモリ | 84,644 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-24 09:07:03 |
| 合計ジャッジ時間 | 2,519 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 WA * 1 |
ソースコード
#include <limits>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>
static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;
template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 2;
template< class T>
T pow_ (T x, T n, T M){
T u = 1;
if(n > 0){
u = pow_(x, n/2, M);
if (n % 2 == 0) u = (u*u) % M;
else u = (((u * u)% M) * x) % M;
}
return u;
};
bool suspect(__uint128_t a, __uint128_t s, __uint128_t d, __uint128_t n){
__uint128_t x = pow_(a, d, n);
if (x == 1) return true;
for (int r = 0; r < s; ++r) {
if(x == n-1) return true;
x = x * x % n;
}
return false;
}
template<class T>
bool miller_rabin(T m){
__uint128_t n = m;
if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
__uint128_t d = n - 1, s = 0;
while (!(d&1)) {++s; d >>= 1;}
static const __uint128_t v[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
for (auto &&p : v) {
if(p >= n) break;
if(!suspect(p, s, d, n)) return false;
}
return true;
}
int main() {
ll n;
cin >> n;
for (int i = 0; i < n; ++i) {
ll k;
cin >> k;
cout << k << " " << miller_rabin(k) << "\n";
}
return 0;
}