結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | nonamae |
提出日時 | 2022-07-15 19:34:04 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 91 ms / 9,973 ms |
コード長 | 3,982 bytes |
コンパイル時間 | 2,126 ms |
コンパイル使用メモリ | 201,496 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 23:48:22 |
合計ジャッジ時間 | 2,986 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 50 ms
5,248 KB |
testcase_05 | AC | 47 ms
5,248 KB |
testcase_06 | AC | 15 ms
5,248 KB |
testcase_07 | AC | 14 ms
5,248 KB |
testcase_08 | AC | 15 ms
5,248 KB |
testcase_09 | AC | 91 ms
5,248 KB |
コンパイルメッセージ
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; } | ^
ソースコード
#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{n - 1}; 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)); } }