結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
do2424_titech
|
| 提出日時 | 2019-06-17 19:13:51 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,396 bytes |
| コンパイル時間 | 1,613 ms |
| コンパイル使用メモリ | 167,732 KB |
| 実行使用メモリ | 6,816 KB |
| 最終ジャッジ日時 | 2024-11-18 16:43:00 |
| 合計ジャッジ時間 | 17,823 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 10 |
ソースコード
#include <bits/stdc++.h>
int sprp_base[] = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };
unsigned long long ll_product(unsigned long long a, unsigned long long b, unsigned long long r) {
unsigned long long ans = 0;
while (a != 0) {
if ((a & 1) == 1) {
ans = (ans + b) % r;
}
b = (b << 1) % r;
a >>= 1;
}
return ans;
}
unsigned long long ll_power(unsigned long long a, unsigned long long b, unsigned long long r) {
unsigned long long ans = 1;
while (b != 0) {
if ((b & 1) == 1) {
ans = ll_product(ans, a, r);
}
a = ll_product(a, a, r);
b >>= 1;
}
return ans;
}
bool primality_test(unsigned long long num) {
if (num == 2) {
return true;
}
if ((num & 1) == 0 || num == 1) {
return false;
}
unsigned long long d = (num - 1) >> 1;
while ((d & 1) == 0) {
d >>= 1;
}
for (int k = 0; k < 7; k++) {
unsigned long long a = sprp_base[k];
if (num > a) {
unsigned long long t = d, y = ll_power(a, t, num);
while (t != num - 1 && y != 1 && y != num - 1) {
y = ll_product(y, y, num);
t <<= 1;
}
if (y != num - 1 && (t & 1) == 0) {
return false;
}
}
}
return true;
}
int main() {
int n;
std::cin >> n;
for (int i = 0; i < n; i++) {
unsigned long long hoge;
std::cin >> hoge;
if (primality_test(hoge) == true) {
std::cout << "1" << std::endl;
}
else {
std::cout << "0" << std::endl;
}
}
return 0;
}
do2424_titech