結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | nonamae |
提出日時 | 2021-11-07 11:35:25 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 14 ms / 9,973 ms |
コード長 | 10,466 bytes |
コンパイル時間 | 765 ms |
コンパイル使用メモリ | 42,824 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 23:43:54 |
合計ジャッジ時間 | 1,126 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 9 ms
5,248 KB |
testcase_05 | AC | 11 ms
5,248 KB |
testcase_06 | AC | 9 ms
5,248 KB |
testcase_07 | AC | 9 ms
5,248 KB |
testcase_08 | AC | 9 ms
5,248 KB |
testcase_09 | AC | 14 ms
5,248 KB |
ソースコード
#pragma region header #define _GNU_SOURCE #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <math.h> #include <string.h> #include <time.h> #pragma endregion header #pragma region type /* signed integer */ typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef __int128_t i128; /* unsigned integer */ typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef __uint128_t u128; /* floating point number */ typedef float f32; typedef double f64; typedef long double f80; #pragma endregion type #pragma region macro #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) #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 #pragma region io int read_int(void) { // -2147483648 ~ 2147483647 (> 10 ^ 9) int 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; } i32 in_i32(void) { // -2147483648 ~ 2147483647 (> 10 ^ 9) 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; } u32 in_u32(void) { // 0 ~ 4294967295 (> 10 ^ 9) 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; } i64 in_i64(void) { // -9223372036854775808 ~ 9223372036854775807 (> 10 ^ 18) 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; } u64 in_u64(void) { // 0 ~ 18446744073709551615 (> 10 ^ 19) 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; } static inline void write_int_inner(int x) { if (x >= 10) write_int_inner(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } void write_int(int x) { if (x < 0) { putchar_unlocked('-'); x = -x; } write_int_inner(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); } void out_u32(u32 x) { if (x >= 10) out_u32(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } 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); } 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(' '); } void write_int_array(int *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i) SP(); write_int(a[i]); } NL(); } void out_i32_array(i32 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i) SP(); out_i32(a[i]); } NL(); } void out_u32_array(u32 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i) SP(); out_u32(a[i]); } NL(); } void out_i64_array(i64 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i) SP(); out_i64(a[i]); } NL(); } void out_u64_array(u64 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i) SP(); out_u64(a[i]); } NL(); } #pragma endregion io #pragma region jacobi symbol 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 (a > n / 2) a -= n; } return n == 1 ? j : 0; } #pragma endregion jacobi symbol #pragma region binary gcd u32 bin_gcd_u32(u32 a, u32 b) { if (!a || !b) return a | b; u32 shift = CTZ32(a | b); a >>= CTZ32(a); do { b >>= CTZ32(b); if (a > b) SWAP(a, b); b -= a; } while (b); return a << shift; } u64 bin_gcd_u64(u64 a, u64 b) { if (!a || !b) return a | b; u64 shift = CTZ64(a | b); a >>= CTZ64(a); do { b >>= CTZ64(b); if (a > b) SWAP(a, b); b -= a; } while (b); return a << shift; } u64 bin_lcm_u64(u32 a, u32 b) { return (u64)a / bin_gcd_u32(a, b) * b; } #pragma endregion binary gcd #pragma region m64 typedef uint64_t m64; m64 _one_m64(u64 mod) { return (u64)-1ull % mod + 1; } m64 _r2_m64(u64 mod) { return (u128)(i128)-1 % mod + 1; } m64 _inv_m64(u64 mod) { m64 inv = mod; for (int i = 0; i < 5; i++) inv *= 2 - inv * mod; return inv; } m64 _reduce_m64(u128 a, m64 inv, u64 mod) { u64 y = (u64)(a >> 64) - (u64)(((u128)((u64)a * inv) * mod) >> 64); return (i64)y < 0 ? y + mod : y; } m64 to_m64(u64 a, m64 r2, m64 inv, u64 mod) { return _reduce_m64((u128)a * r2, inv, mod); } u64 from_m64(m64 A, m64 inv, u64 mod) { return _reduce_m64(A, inv, mod); } m64 add_m64(m64 A, m64 B, u64 mod) { return A + B >= mod ? A + B - mod: A + B; } m64 sub_m64(m64 A, m64 B, u64 mod) { return A >= B ? A - B : mod + A - B; } m64 min_m64(m64 A, u64 mod) { return sub_m64(0ull, A, mod); } m64 mul_m64(m64 A, m64 B, m64 inv, u64 mod) { return _reduce_m64((u128)A * B, inv, mod); } m64 pow_m64(m64 A, i64 n, m64 inv, u64 mod) { m64 ret = _one_m64(mod); while (n > 0) { if (n & 1) ret = mul_m64(ret, A, inv, mod); A = mul_m64(A, A, inv, mod); n >>= 1; } return ret; } m64 inv_m64(m64 A, m64 inv, u64 mod) { return pow_m64(A, (i64)mod - 2, inv, mod); } m64 div_m64(m64 A, m64 B, m64 inv, u64 mod) { /* assert(is_prime(mod)); */ return mul_m64(A, inv_m64(B, inv, mod), inv, mod); } m64 in_m64(m64 r2, m64 inv, u64 mod) { u64 c, a = 0; while (c = getchar_unlocked(), c < 48 || c > 57); while (47 < c && c < 58) { a = a * 10 + c - 48; c = getchar_unlocked(); } return to_m64(a, r2, inv, mod); } void out_m64(m64 A, m64 inv, u64 mod) { u64 a = from_m64(A, inv, mod); out_u64(a); } #pragma endregion m64 #pragma region Baillie-PSW primality test bool is_prime(u64 n) { { if (n <= 1ull) return false; if (n <= 3ull) return true; if (!(n & 1)) return false; } const u64 mod = n; const m64 one = _one_m64(mod); const m64 r2 = _r2_m64(mod); const m64 inv = _inv_m64(mod); { u64 d = (n - 1) << __builtin_clzll(n - 1); m64 a = one << 1; if (a >= n) a -= n; for (d <<= 1; d; d <<= 1) { a = mul_m64(a, a, inv, mod); if (d >> 63) { a <<= 1; if (a >= n) a -= n; } } if (a != one) { u64 x = (n - 1) & -(n - 1); m64 m = n - one; for (x >>= 1; a != m; x >>= 1) { if (x == 0) return false; a = mul_m64(a, a, inv, mod); } } } { 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; } m64 Q = to_m64(D < 0 ? (1 - D) / 4 % n : n - (D - 1) / 4 % n, r2, inv, mod); u64 u = one; u64 v = one; u64 Qn = Q; D %= (i64)n; D = to_m64(D < 0 ? n + D : D, r2, inv, mod); u64 k = (n + 1) << __builtin_clzll(n + 1); for (k <<= 1; k; k<<= 1) { u = mul_m64(u, v, inv, mod); v = sub_m64(mul_m64(v, v, inv, mod), add_m64(Qn, Qn, mod), mod); Qn = mul_m64(Qn, Qn, inv, mod); if (k >> 63) { u64 uu = add_m64(u, v, mod); if (uu & 1) uu += n; uu >>= 1; v = add_m64(mul_m64(D, u, inv, mod), v, mod); if (v & 1) v += n; v >>= 1; u = uu; Qn = mul_m64(Qn, Q, inv, mod); } } if (u == 0 || v == 0) return true; u64 x = (n + 1) & ~n; for (x >>= 1; x; x >>= 1) { u = mul_m64(u, v, inv, mod); v = sub_m64(mul_m64(v, v, inv, mod), add_m64(Qn, Qn, mod), mod); if (v == 0) return true; Qn = mul_m64(Qn, Qn, inv, mod); } } return false; } #pragma endregion Baillie-PSW primality test void Main(void) { int T = read_int(); while (T--) { u64 x = in_u64(); out_u64(x); SP(); write_int(is_prime(x)); NL(); } } int main(void) { Main(); return 0; }