結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
AC2K
|
| 提出日時 | 2023-03-04 12:48:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 594 ms / 9,973 ms |
| コード長 | 2,418 bytes |
| コンパイル時間 | 2,055 ms |
| コンパイル使用メモリ | 192,640 KB |
| 最終ジャッジ日時 | 2025-02-11 05:11:12 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#line 1 "main.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/3030"
#include<bits/stdc++.h>
#line 1 "math/mod_pow.hpp"
template <class T, class U = T>
U mod_pow(T base, T exp, T mod){
if(base==0)return 0;
T ans = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) {
ans *= base;
ans %= mod;
}
base *= base;
base %= mod;
exp >>= 1;
}
return ans;
}
///@brief mod pow(バイナリ法)
#line 7 "main.test.cpp"
using namespace std;
namespace prime {
namespace miller{
using i128 = __int128_t;
using u128 = __uint128_t;
using u64 = __uint64_t;
bool miller_rabin(u64 n,const u64 bases[],int siz) {
u64 d = n - 1;
u64 q = __builtin_ctz(d);
d >>= q;
for(int i=0;i<siz;i++){
u64 a = bases[i];
if (a == n) {
return true;
} else if (n % a == 0) {
return false;
}
if (mod_pow<u128>(a, d, n) != 1) {
bool flag = true;
for (u64 r = 0; r < q; r++) {
u64 pow = mod_pow<u128>(a, d * (1ll << r), n);
if (pow == n - 1) {
flag = false;
break;
}
}
if (flag) {
return false;
}
}
}
return true;
}
bool is_prime(u64 n){
static constexpr u64 bases_int[3] = {2, 7, 61}; // intだと、2,7,61で十分
static constexpr u64 bases_ll[7] = {2,325,9375,28178,450775,9780504,1795265022};
if (n < 2) {
return false;
} else if (n == 2) {
return true;
} else if (~n & 1) {
return false;
}
if(n<(1ul<<31)){
return miller_rabin(n, bases_int, 3);
} else {
return miller_rabin(n, bases_ll, 7);
}
}
};
};
using prime::miller::is_prime;
///@brief fast prime check(MillerRabinの素数判定法)
int main(){
int n;
cin >> n;
while(n--){
long long x;
cin>>x;
cout << x << ' ' << is_prime(x) << '\n';
}
}
AC2K