結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
nonamae
|
| 提出日時 | 2022-07-15 19:31:28 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,985 bytes |
| コンパイル時間 | 1,821 ms |
| コンパイル使用メモリ | 195,368 KB |
| 最終ジャッジ日時 | 2025-01-30 07:14:39 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 6 |
コンパイルメッセージ
main.cpp: In member function ‘RuntimeMontgomeryModint64 RuntimeMontgomeryModint64::operator<<(u64) const’:
main.cpp:83:97: warning: no return statement in function returning non-void [-Wreturn-type]
83 | RuntimeMontgomeryModint64 operator<<(u64 y) const { RuntimeMontgomeryModint64(*this) <<= y; }
| ^
main.cpp: In member function ‘RuntimeMontgomeryModint64 RuntimeMontgomeryModint64::operator>>(u64) const’:
main.cpp:84:97: warning: no return statement in function returning non-void [-Wreturn-type]
84 | RuntimeMontgomeryModint64 operator>>(u64 y) const { RuntimeMontgomeryModint64(*this) >>= y; }
| ^
main.cpp: In function ‘int main()’:
main.cpp:122:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
122 | u64 Q; scanf("%lu", &Q);
| ~~~~~^~~~~~~~~~~
main.cpp:124:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
124 | u64 x; scanf("%lu", &x);
| ~~~~~^~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
int jacobi_symbol(i64 a, u64 n) {
u64 t;
int j = 1;
while (a) {
if (a < 0) {
a = -a;
if ((n & 3) == 3) j = -j;
}
int s = __builtin_ctzll(a);
a >>= s;
if (((n & 7) == 3 || (n & 7) == 5) && (s & 1)) j = -j;
if ((a & n & 3) == 3) j = -j;
t = a, a = n, n = t;
a %= n;
if (u64(a) > n / 2) a -= n;
}
return n == 1 ? j : 0;
}
struct RuntimeMontgomeryModint64 {
private:
using m64 = u64;
public:
inline static m64 one, r2, n, md;
static void set_mod(u64 m) {
md = m;
one = u64(-1ull) % m + 1;
r2 = u128(i128(-1)) % m + 1;
u64 nn = m;
for (int _ = 0; _ < 5; ++_) nn *= 2 - nn * m;
n = nn;
}
static m64 reduce(u128 a) {
u64 y = (u64(a >> 64)) - (u64((u128(u64(a) * n) * md) >> 64));
return i64(y) < 0 ? y + md : y;
}
RuntimeMontgomeryModint64() : x(0) { }
RuntimeMontgomeryModint64(u64 x) : x(reduce(u128(x) * r2)) { }
u64 val() const {
return reduce(u128(x));
}
m64 x;
RuntimeMontgomeryModint64 &operator+=(RuntimeMontgomeryModint64 y) {
x += y.x - md;
if (i64(x) < 0) x += md;
return *this;
}
RuntimeMontgomeryModint64 &operator-=(RuntimeMontgomeryModint64 y) {
if (i64(x -= y.x) < 0) x += 2 * md;
return *this;
}
RuntimeMontgomeryModint64 &operator*=(RuntimeMontgomeryModint64 y) {
x = reduce(u128(x) * y.x);
return *this;
}
RuntimeMontgomeryModint64 &operator<<=(u64 y) {
x <<= y;
return *this;
}
RuntimeMontgomeryModint64 &operator>>=(u64 y) {
x >>= y;
return *this;
}
RuntimeMontgomeryModint64 operator+(RuntimeMontgomeryModint64 y) const { return RuntimeMontgomeryModint64(*this) += y; }
RuntimeMontgomeryModint64 operator-(RuntimeMontgomeryModint64 y) const { return RuntimeMontgomeryModint64(*this) -= y; }
RuntimeMontgomeryModint64 operator*(RuntimeMontgomeryModint64 y) const { return RuntimeMontgomeryModint64(*this) *= y; }
RuntimeMontgomeryModint64 operator<<(u64 y) const { RuntimeMontgomeryModint64(*this) <<= y; }
RuntimeMontgomeryModint64 operator>>(u64 y) const { RuntimeMontgomeryModint64(*this) >>= y; }
bool operator==(RuntimeMontgomeryModint64 y) const { return (x >= md ? x - md : x) == (y.x >= md ? y.x - md : y.x); }
bool operator!=(RuntimeMontgomeryModint64 y) const { return not operator==(y); }
RuntimeMontgomeryModint64 pow(u64 k) {
RuntimeMontgomeryModint64 y = 1, z = *this;
for ( ; k; k >>= 1, z *= z) if (k & 1) y *= z;
return y;
}
RuntimeMontgomeryModint64 inv() {
return (*this).pow(md - 2);
}
};
static u64 lcg_state = 14534622846793005ull;
u32 lcg_rand(u32 l, u32 r) {
lcg_state = 6364136223846793005ULL * lcg_state + 1442695040888963407ULL;
return l + (u32)((double)(r - l) * (double)(lcg_state >> 32) / 4294967296.0);
}
int solovay_strassen_primality_test(u64 n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (!(n & 1)) return 0;
RuntimeMontgomeryModint64::set_mod(n);
RuntimeMontgomeryModint64 a{1};
RuntimeMontgomeryModint64 b = a.inv();
for (int _ = 0; _ < 15; ++_) {
u32 ra = lcg_rand(2u, ((n - 1) > ((1ull << 32) - 1)) ? 1u << 31 : n - 1);
int x = jacobi_symbol(i64(ra), n);
RuntimeMontgomeryModint64 y = (x == -1) ? b : ((x == 0) ? RuntimeMontgomeryModint64{0} : a);
RuntimeMontgomeryModint64 A{ra};
if (y == 0 || y != A.pow((n - 1) / 2)) return 0;
}
return 1;
}
int main() {
u64 Q; scanf("%lu", &Q);
while (Q--) {
u64 x; scanf("%lu", &x);
printf("%lu %d\n", x, solovay_strassen_primality_test(x));
}
}
nonamae