結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
aajisaka
|
| 提出日時 | 2020-04-29 01:52:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,740 bytes |
| コンパイル時間 | 2,050 ms |
| コンパイル使用メモリ | 197,236 KB |
| 最終ジャッジ日時 | 2025-01-10 03:13:05 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 WA * 1 |
ソースコード
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include<bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
The implementation of Miller-Rabin Test with C++
varified with ALDS1_1_C
http://joisino.hatenablog.com/entry/2017/08/03/210000
Copyright (c) 2017 joisino
Released under the MIT license
http://opensource.org/licenses/mit-license.php
*/
// 64bit support
// Verify: https://yukicoder.me/submissions/472283
struct Miller64 {
const vector<__uint128_t> v = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
__uint128_t modpow(__uint128_t x, __uint128_t k, __uint128_t m){
__uint128_t res = 1;
while( k ){
if( k & 1 ){
res = res * x % m;
}
k /= 2;
x = x * x % m;
}
return res;
}
// check if n is prime
bool check(__uint128_t n){
if( n < 2 ){
return false;
}
__uint128_t d = n - 1;
__uint128_t s = 0;
while( d % 2 == 0 ){
d /= 2;
s++;
}
for(__uint128_t a : v ){
if( a == n ){
return true;
}
if( modpow( a , d , n ) != 1 ){
bool ok = true;
for(__uint128_t r = 0; r < s; r++ ){
if( modpow( a, d * (1LL << r), n ) == n-1 ){
ok = false;
break;
}
}
if( ok ){
return false;
}
}
}
return true;
}
};
class TestMillerRabin {
public:
void solve(istream& cin, ostream& cout) {
SPEED;
int n; cin >> n;
Miller64 miller64;
while(n--) {
ll p; cin >> p;
cout << p << ' ';
cout << (miller64.check(p) ? 1 : 0) << '\n';
}
}
};
signed main() {
TestMillerRabin solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
aajisaka