結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | nonamae |
提出日時 | 2022-07-18 19:47:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 90 ms / 9,973 ms |
コード長 | 4,860 bytes |
コンパイル時間 | 2,154 ms |
コンパイル使用メモリ | 202,848 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-16 23:50:48 |
合計ジャッジ時間 | 2,992 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 49 ms
6,816 KB |
testcase_05 | AC | 47 ms
6,816 KB |
testcase_06 | AC | 14 ms
6,816 KB |
testcase_07 | AC | 14 ms
6,816 KB |
testcase_08 | AC | 14 ms
6,816 KB |
testcase_09 | AC | 90 ms
6,816 KB |
ソースコード
#include <bits/stdc++.h> using i8 = std::int8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using i128 = __int128_t; using u128 = __uint128_t; using f32 = float; using f64 = double; using f80 = long double; template<typename T> using vec = std::vector<T>; template<typename T> using vvec = std::vector<std::vector<T>>; template<typename T> using vvvec = std::vector<std::vector<std::vector<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; } static u64 lcg_state = 14534622846793005ull; u32 lcg_rand() { return lcg_state = 6364136223846793005ULL * lcg_state + 1442695040888963407ULL; } u32 lcg_range(u32 l, u32 r) { return l + lcg_rand() % (r - l + 1); } struct Runtime_m64 { private: using m64 = u64; public: inline static m64 one, r2, n, md; m64 x; 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; } Runtime_m64() : x(0) { } Runtime_m64(u64 x) : x(reduce(u128(x) * r2)) { } u64 get_val() const { return reduce(u128(x)); } u64 get_raw() const { return x; } Runtime_m64 &operator+=(Runtime_m64 y) { x += y.x - md; if (i64(x) < 0) x += md; return *this; } Runtime_m64 &operator-=(Runtime_m64 y) { if (i64(x -= y.x) < 0) x += 2 * md; return *this; } Runtime_m64 &operator*=(Runtime_m64 y) { x = reduce(u128(x) * y.x); return *this; } Runtime_m64 &operator/=(Runtime_m64 y) { return *this *= y.inv(); } Runtime_m64 &operator<<=(u64 y) { x <<= y; return *this; } Runtime_m64 &operator>>=(u64 y) { x >>= y; return *this; } Runtime_m64 operator+(Runtime_m64 y) const { return Runtime_m64(*this) += y; } Runtime_m64 operator-(Runtime_m64 y) const { return Runtime_m64(*this) -= y; } Runtime_m64 operator*(Runtime_m64 y) const { return Runtime_m64(*this) *= y; } Runtime_m64 operator/(Runtime_m64 y) const { return Runtime_m64(*this) /= y; } Runtime_m64 operator-() const { return Runtime_m64() - Runtime_m64(*this); } Runtime_m64 operator<<(u64 y) const { return Runtime_m64(*this) <<= y; } Runtime_m64 operator>>(u64 y) const { return Runtime_m64(*this) >>= y; } bool operator==(Runtime_m64 y) const { return (x >= md ? x - md : x) == (y.x >= md ? y.x - md : y.x); } bool operator!=(Runtime_m64 y) const { return not operator==(y); } bool operator<(const Runtime_m64& other) { return (*this).get_val() < other.get_val(); } bool operator<=(const Runtime_m64& other) { return (*this).get_val() <= other.get_val(); } bool operator>(const Runtime_m64& other) { return (*this).get_val() > other.get_val(); } bool operator>=(const Runtime_m64& other) { return (*this).get_val() >= other.get_val(); } Runtime_m64 pow(u64 k) { Runtime_m64 y = 1, z = *this; for ( ; k; k >>= 1, z *= z) if (k & 1) y *= z; return y; } Runtime_m64 inv() { return (*this).pow(md - 2); } }; int solovay_strassen_primality_test(u64 n) { if (n <= 1) return 0; if (n <= 3) return 1; if (!(n & 1)) return 0; Runtime_m64::set_mod(n); Runtime_m64 a{1}; Runtime_m64 b{n - 1}; for (int _ = 0; _ < 15; ++_) { u32 ra = lcg_range(2u, ((n - 1) > ((1ull << 32) - 1)) ? 1u << 31 : n - 1); int x = jacobi_symbol(i64(ra), n); Runtime_m64 y = (x == -1) ? b : ((x == 0) ? Runtime_m64{0} : a); Runtime_m64 A{ra}; if (y == 0 || y != A.pow((n - 1) / 2)) return 0; } return 1; } void Main() { // your source here u64 Q; scanf("%lu", &Q); while (Q--) { u64 x; scanf("%lu", &x); printf("%lu %d\n", x, solovay_strassen_primality_test(x)); } return; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::cout << std::fixed << std::setprecision(13); std::cerr << std::fixed << std::setprecision(3); Main(); }