結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
sekiya9311
|
| 提出日時 | 2017-10-22 19:47:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3,523 ms / 9,973 ms |
| コード長 | 2,144 bytes |
| コンパイル時間 | 1,831 ms |
| コンパイル使用メモリ | 167,628 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 23:03:21 |
| 合計ジャッジ時間 | 8,858 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using value_type = __uint128_t;
class XorShift {
private:
uint32_t x = 123456789;
uint32_t y = 362436069;
uint32_t z = 521288629;
uint32_t w;
public:
XorShift(uint32_t seed = 0)
: w(seed ? seed : 88675123) {this->random_loop();}
void random_loop() {
int loop_num = this->next_int() % 50;
while (loop_num--) this->next_int();
}
uint32_t next_int(bool heavy_ok = false) {
if (heavy_ok) this->random_loop();
uint32_t t = this->x ^ (this->x << 11);
this->x = this->y;
this->y = this->z;
this->z = this->w;
return this->w = (this->w ^ (this->w >> 19))
^ (t ^ (t >> 8));
}
uint64_t next_long(bool heavy_ok = false) {
return ((((uint64_t)this->next_int(heavy_ok)) << 32)
| this->next_int(heavy_ok));
}
uint32_t operator()(bool heavy_ok = false) {
return this->next_int(heavy_ok);
}
};
value_type pow_mod(value_type a, value_type p, value_type mod) {
if (p == 0) {
return 1;
} else if (p & 1) {
return a * pow_mod(a, p - 1, mod) % mod;
} else {
const value_type t = pow_mod(a, p >> 1, mod);
return t * t % mod;
}
}
XorShift rnd;
//random_device rnd;
bool miller_labin(value_type n, int loopNum = 100) {
if (n == 2) {
return true;
}
if (n < 2 || (!(n & 1))) {
return false;
}
value_type d = n - 1;
while (!(d & 1)) {
d >>= 1;
}
while (loopNum--) {
value_type a = (((((value_type) rnd()) << 32) | (value_type) rnd()) % (n - 2)) + 1;
value_type t = d;
value_type y = pow_mod(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = (y * y) % n;
t <<= 1;
}
if (y != n - 1 && (!(t & 1))) {
return false;
}
}
return true;
}
int main() {
int n;
scanf("%d", &n);
while (n--) {
long long x;
scanf("%lld", &x);
printf("%lld %d\n", x, (int) miller_labin(x));
}
return 0;
}
sekiya9311