結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | nonamae |
提出日時 | 2022-07-19 15:13:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 11,230 bytes |
コンパイル時間 | 3,339 ms |
コンパイル使用メモリ | 219,680 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-01 18:09:55 |
合計ジャッジ時間 | 4,388 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
ソースコード
#pragma region opt #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma endregion opt #include <bits/stdc++.h> #pragma region type 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>>>; template<typename T> using pvec = std::pair<std::vector<T>, std::vector<T>>; #pragma endregion type #pragma region MACRO for #define FOR(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i) #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define LOOP(n) for(int _=0; _<(n); ++_) #pragma endregion MACRO for #pragma region MACRO container #define ALL(obj) (obj).begin(),(obj).end() #define SZ(obj) (static_cast<int>((obj).size())) #pragma endregion MACRO container #pragma region MACRO bits #define POPCNT32(a) __builtin_popcount((a)) #define POPCNT64(a) __builtin_popcountll((a)) #define CTZ32(a) __builtin_ctz((a)) #define CLZ32(a) __builtin_clz((a)) #define CTZ64(a) __builtin_ctzll((a)) #define CLZ64(a) __builtin_clzll((a)) #define HAS_SINGLE_BIT32(a) (__builtin_popcount((a)) == (1)) #define HAS_SINGLE_BIT64(a) (__builtin_popcountll((a)) == (1)) #define MSB32(a) ((31) - __builtin_clz((a))) #define MSB64(a) ((63) - __builtin_clzll((a))) #define BIT_WIDTH32(a) ((a) ? ((32) - __builtin_clz((a))) : (0)) #define BIT_WIDTH64(a) ((a) ? ((64) - __builtin_clzll((a))) : (0)) #define LSBit(a) ((a) & (-(a))) #define CLSBit(a) ((a) & ((a) - (1))) #define BIT_CEIL32(a) ((!(a)) ? (1) : ((POPCNT32(a)) == (1) ? ((1u) << ((31) - CLZ32((a)))) : ((1u) << ((32) - CLZ32(a))))) #define BIT_CEIL64(a) ((!(a)) ? (1) : ((POPCNT64(a)) == (1) ? ((1ull) << ((63) - CLZ64((a)))) : ((1ull) << ((64) - CLZ64(a))))) #define BIT_FLOOR32(a) ((!(a)) ? (0) : ((1u) << ((31) - CLZ32((a))))) #define BIT_FLOOR64(a) ((!(a)) ? (0) : ((1ull) << ((63) - CLZ64((a))))) #define _ROTL32(x, s) (((x) << ((s) % (32))) | (((x) >> ((32) - ((s) % (32)))))) #define _ROTR32(x, s) (((x) >> ((s) % (32))) | (((x) << ((32) - ((s) % (32)))))) #define ROTL32(x, s) (((s) == (0)) ? (x) : ((((i64)(s)) < (0)) ? (_ROTR32((x), -(s))) : (_ROTL32((x), (s))))) #define ROTR32(x, s) (((s) == (0)) ? (x) : ((((i64)(s)) < (0)) ? (_ROTL32((x), -(s))) : (_ROTR32((x), (s))))) #define _ROTL64(x, s) (((x) << ((s) % (64))) | (((x) >> ((64) - ((s) % (64)))))) #define _ROTR64(x, s) (((x) >> ((s) % (64))) | (((x) << ((64) - ((s) % (64)))))) #define ROTL64(x, s) (((s) == (0)) ? (x) : ((((i128)(s)) < (0)) ? (_ROTR64((x), -(s))) : (_ROTL64((x), (s))))) #define ROTR64(x, s) (((s) == (0)) ? (x) : ((((i128)(s)) < (0)) ? (_ROTL64((x), -(s))) : (_ROTR64((x), (s))))) #pragma endregion MACRO bits #pragma region util template<class T> inline bool chmax(T& a,T b){ if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a,T b){ if (a > b) { a = b; return 1; } return 0; } #pragma endregion util #pragma region IO // -2147483648 ~ 2147483647 (> 10 ^ 9) i32 in_i32(void) { i32 c, x = 0, f = 1; while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f; while (47 < c && c < 58) { x = x * 10 + c - 48; c = getchar_unlocked(); } return f * x; } static inline void out_i32_inner(i32 x) { if (x >= 10) out_i32_inner(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } void out_i32(i32 x) { if (x < 0) { putchar_unlocked('-'); x = -x; } out_i32_inner(x); } // -9223372036854775808 ~ 9223372036854775807 (> 10 ^ 18) i64 in_i64(void) { i64 c, x = 0, f = 1; while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f; while (47 < c && c < 58) { x = x * 10 + c - 48; c = getchar_unlocked(); } return f * x; } static inline void out_i64_inner(i64 x) { if (x >= 10) out_i64_inner(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } void out_i64(i64 x) { if (x < 0) { putchar_unlocked('-'); x = -x; } out_i64_inner(x); } // 0 ~ 4294967295 (> 10 ^ 9) u32 in_u32(void) { u32 c, x = 0; while (c = getchar_unlocked(), c < 48 || c > 57); while (47 < c && c < 58) { x = x * 10 + c - 48; c = getchar_unlocked(); } return x; } void out_u32(u32 x) { if (x >= 10) out_u32(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } // 0 ~ 18446744073709551615 (> 10 ^ 19) u64 in_u64(void) { u64 c, x = 0; while (c = getchar_unlocked(), c < 48 || c > 57); while (47 < c && c < 58) { x = x * 10 + c - 48; c = getchar_unlocked(); } return x; } void out_u64(u64 x) { if (x >= 10) out_u64(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } void NL(void) { putchar_unlocked('\n'); } void SP(void) { putchar_unlocked(' '); } #pragma endregion IO #pragma region jacobi 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; } #pragma endregion jacobi #pragma region m64 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)) { } Runtime_m64(u64 x, bool is_montgomery) : x(is_montgomery ? 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); } }; #pragma endregion m64 #pragma region Baillie_PSW primality test bool is_prime(u64 n) { { if (n == 2 || n == 3 || n == 5 || n == 7) return true; if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0) return false; if (n < 121) return n > 1; } using mint = Runtime_m64; mint::set_mod(n); { u64 d = (n - 1) << __builtin_clzll(n - 1); mint a(2); if (a.get_raw() >= n) { mint aa(a.get_raw() - n, true); a = aa; } for (d <<= 1; d; d <<= 1) { a *= a; if (d >> 63) a <<= 1; if (a.get_raw() >= n) { mint aa(a.get_raw() - n, true); a = aa; } } if (a != mint(1)) { u64 x = (n - 1) & -(n - 1); mint m = mint(n - 1); for (x >>= 1; a != m; x >>= 1) { if (x == 0) return false; a *= a; } } } { u32 k = round(sqrtl(n)); if (k * k == n) return false; } { i64 D = 5; for (int i = 0; jacobi_symbol(D, n) != -1 && i < 64; i++) { if (i & 1) D -= 2; else D += 2; D = -D; } mint Q(D < 0 ? (1 - D) / 4 % n : n - (D - 1) / 4 % n); mint u(1); mint v(1); mint Qn = Q; D %= (i64)n; mint D_mint(D < 0 ? n + D : D); u64 k = (n + 1) << __builtin_clzll(n + 1); for (k <<= 1; k; k <<= 1) { u *= v; v = v * v - (Qn + Qn); Qn *= Qn; if (k >> 63) { mint uu = u + v; if (uu.get_raw() & 1) { mint uuu(uu.get_raw() + n, true); uu = uuu; } uu >>= 1; v += D_mint * u; if (v.get_raw() & 1) { mint vv(v.get_raw() + n, true); v = vv; } v >>= 1; u = uu; Qn *= Q; } } if (u.get_val() == 0 || v.get_val() == 0) return true; u64 x = (n + 1) & ~n; for (x >>= 1; x; x >>= 1) { u *= v; v = v * v - (Qn + Qn); if (v.get_val() == 0) return true; Qn *= Qn; } } return false; } #pragma endregion Baillie_PSW primality test void Main() { // your source here int i = in_i32(); u64 x; LOOP(i) { x = in_u64(); out_u64(x); SP(); out_u64(is_prime(x) ? 1 : 0); NL(); } 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(); }