結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2018-09-13 19:36:32 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,483 bytes |
| コンパイル時間 | 721 ms |
| コンパイル使用メモリ | 83,272 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-24 09:07:43 |
| 合計ジャッジ時間 | 17,779 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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 mul_ (T x, T y, T M){
T res = 0;
for (;y;y/=2) {
if (y & 1) res = (res + x) % M;
x = (x + x) % M;
}
return res;
}
template<class T>
T pow_ (T x, T n, T M){
if (n == 0) return 1;
T res = 1;
for (;n;n/=2) {
if (n & 1) res = mul_(res, x, M) % M;
x = mul_(x, x, M);
}
return res;
};
bool suspect(uint64_t a, uint64_t s, uint64_t d, uint64_t n){
uint64_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 = mul_(x, x, n);
}
return false;
}
template<class T>
bool miller_rabin(T m){
uint64_t n = m;
if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
uint64_t d = n - 1, s = 0;
while (!(d&1)) {++s; d >>= 1;}
static const uint64_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;
}