結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | nonamae |
提出日時 | 2022-08-02 03:01:11 |
言語 | C (gcc 12.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 12,451 bytes |
コンパイル時間 | 919 ms |
コンパイル使用メモリ | 45,488 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-22 19:00:12 |
合計ジャッジ時間 | 1,200 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
ソースコード
#pragma region template #pragma GCC target("avx2") #pragma GCC optimize("O3") #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> typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef __int128_t i128; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef __uint128_t u128; typedef float f32; typedef double f64; typedef long double f80; #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))) static inline u64 get_bit_floor(u64 n) { if (n) return 1ull << (63 - __builtin_clzll(n)); return 0; } static inline u64 get_bit_ceil(u64 n) { assert(n > 0); --n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n |= n >> 32; ++n; return n; } static inline u32 get_log2(u64 n) { assert(HAS_SINGLE_BIT64(n)); const u32 deBruijn[64] = {0,1,2,53,3,7,54,27,4,38,41,8,34,55,48,28,62,5,39,46,44,42,22,9,24,35,59,56,49,18,29,11,63,52,6,26,37,40,33,47,61,45,43,21,23,58,17,10,51,25,36,32,60,20,57,16,50,31,19,15,30,14,13,12}; return deBruijn[n * 0x022fdd63cc95386dull >> 58]; } static inline u32 rotr32(u32 x, i64 r) { if (r < 0) { u64 l = ((u64)(-r)) % 32; return x << l | x >> (-l & 31); } r %= 32; return x >> r | x << (-r & 31); } static inline u64 rotr64(u64 x, i64 r) { if (r < 0) { u64 l = ((u64)(-r)) % 64; return x << l | x >> (-l & 63); } r %= 64; return x >> r | x << (-r & 63); } static inline u32 rotl32(u32 x, i64 r) { return rotr32(x, -r); } static inline u64 rotl64(u64 x, i64 r) { return rotr64(x, -r); } static inline int deBruijn_log2(u64 n) { static const u64 deBruijn = 0x022fdd63cc95386d; static const int convert[64] = {0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12}; return convert[n * deBruijn >> 58]; } static inline int bsf(u64 n) { return deBruijn_log2(n & ~(n - 1)); } 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; } 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); } 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; } 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); } 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; } void out_u32(u32 x) { if (x >= 10) out_u32(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } 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; } 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 dump_int(int x) { fprintf(stderr, "\033[1;36m%d\033[0m\n", x); } void dump_i64(i64 x) { fprintf(stderr, "\033[1;36m%ld\033[0m\n", x); } void dump_u32(u32 x) { fprintf(stderr, "\033[1;36m%u\033[0m\n", x); } void dump_u64(u64 x) { fprintf(stderr, "\033[1;36m%lu\033[0m\n", x); } void dump_int_array(int *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) { fprintf(stderr, "\033[1;36m%d\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%d\033[0m ", a[i]); } } } void dump_i64_array(i64 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) { fprintf(stderr, "\033[1;36m%ld\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%ld\033[0m ", a[i]); } } } void dump_u32_array(u32 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) { fprintf(stderr, "\033[1;36m%u\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%u\033[0m ", a[i]); } } } void dump_u64_array(u64 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) { fprintf(stderr, "\033[1;36m%lu\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%lu\033[0m ", a[i]); } } } void dump_int_array_range(int *a, int a_len, int l, int r) { if (a_len <= r) { r = a_len - 1; } if (l > r) { return; } for (int i = l; i <= r; i++) { if (i == r) { fprintf(stderr, "\033[1;36m%d\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%d\033[0m ", a[i]); } } } void dump_i64_array_range(i64 *a, int a_len, int l, int r) { if (a_len <= r) { r = a_len - 1; } if (l > r) { return; } for (int i = l; i <= r; i++) { if (i == r) { fprintf(stderr, "\033[1;36m%ld\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%ld\033[0m ", a[i]); } } } void dump_u32_array_range(u32 *a, int a_len, int l, int r) { if (a_len <= r) { r = a_len - 1; } if (l > r) { return; } for (int i = l; i <= r; i++) { if (i == r) { fprintf(stderr, "\033[1;36m%u\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%u\033[0m ", a[i]); } } } void dump_u64_array_range(u64 *a, int a_len, int l, int r) { if (a_len <= r) { r = a_len - 1; } if (l > r) { return; } for (int i = l; i <= r; i++) { if (i == r) { fprintf(stderr, "\033[1;36m%lu\033[0m\n", a[i]); } else { fprintf(stderr, "\033[1;36m%lu\033[0m ", a[i]); } } } void printb_32bit(u32 v) { u32 mask = (u32)1 << (sizeof(v) * CHAR_BIT - 1); do { putchar_unlocked(mask & v ? '1' : '0'); } while (mask >>= 1); } void printb_64bit(u64 v) { u64 mask = (u64)1 << (sizeof(v) * CHAR_BIT - 1); do { putchar_unlocked(mask & v ? '1' : '0'); } while (mask >>= 1); } #pragma endregion template /////////////////////////////////////////////////////////////////////////////// /// jacobi_symbol.c /// /////////////////////////////////////////////////////////////////////////////// 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; } /////////////////////////////////////////////////////////////////////////////// /// m64.c /// /////////////////////////////////////////////////////////////////////////////// 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 N_m64(u64 mod) { m64 N = mod; for (int i = 0; i < 5; i++) { N *= 2 - N * mod; } return N; } m64 reduce_m64(u128 a, m64 N, u64 mod) { u64 y = (u64)(a >> 64) - (u64)(((u128)((u64)a * N) * mod) >> 64); return (i64)y < 0 ? y + mod : y; } m64 to_m64(u64 a, m64 r2, m64 N, u64 mod) { return reduce_m64((u128)a * r2, N, mod); } u64 from_m64(m64 A, m64 N, u64 mod) { return reduce_m64((u128)A, N, mod); } m64 add_m64(m64 A, m64 B, u64 mod) { A += B - mod; if ((i64)A < 0) A += mod; return A; } m64 sub_m64(m64 A, m64 B, u64 mod) { if ((i64)(A -= B) < 0) A += 2 * mod; return A; } m64 min_m64(m64 A, u64 mod) { return sub_m64(0ull, A, mod); } m64 mul_m64(m64 A, m64 B, m64 N, u64 mod) { return reduce_m64((u128)A * B, N, mod); } m64 pow_m64(m64 A, i64 n, m64 one, m64 N, u64 mod) { m64 ret = one; while (n > 0) { if (n & 1) ret = mul_m64(ret, A, N, mod); A = mul_m64(A, A, N, mod); n >>= 1; } return ret; } m64 inv_m64(m64 A, m64 one, m64 N, u64 mod) { return pow_m64(A, (i64)mod - 2, one, N, mod); } m64 div_m64(m64 A, m64 B, m64 one, m64 N, u64 mod) { /* assert(is_prime(mod)); */ return mul_m64(A, inv_m64(B, one, N, mod), N, mod); } bool eq_m64(m64 A, m64 B, m64 N, u64 mod) { return from_m64(A, N, mod) == from_m64(B, N, mod); } bool neq_m64(m64 A, m64 B, m64 N, u64 mod) { return from_m64(A, N, mod) != from_m64(B, N, mod); } m64 in_m64(m64 r2, m64 N, u64 mod) { u64 c = 0; u64 a = 0; while (c = getchar(), c < 48 || c > 57); while (47 < c && c < 58) { a = a * 10 + c - 48; c = getchar(); } return to_m64(a, r2, N, mod); } void out_m64(m64 A, m64 N, u64 mod) { u64 a = from_m64(A, N, mod); out_u64(a); } /////////////////////////////////////////////////////////////////////////////// /// Baillie-PSW.c /// /////////////////////////////////////////////////////////////////////////////// bool baillie_psw(u64 n) { { if (n <= 1) return false; if (n <= 3) return true; if (!(n & 1)) return false; } const u64 mod = n; const m64 one = one_m64(n); const m64 r2 = r2_m64(n); const m64 N = N_m64(n); { u64 d = (mod - 1) << __builtin_clzll(mod - 1); m64 t = one << 1; if (t >= mod) t -= mod; for (d <<= 1; d; d <<= 1) { t = mul_m64(t, t, N, mod); if (d >> 63) { t <<= 1; if (t >= mod) t -= mod; } } if (t != one) { u64 x = (n - 1) & -(n - 1); m64 rev = mod - one; for (x >>= 1; t != rev; x >>= 1) { if (x == 0) return false; t = mul_m64(t, t, N, mod); } } } { i64 D = 5; for (int i = 0; jacobi_symbol(D, n) != -1 && i < 64; ++i) { if (i == 32) { u32 k = round(sqrtl(n)); if (k * k == n) return false; } if (i & 1) D -= 2; else D += 2; D = -D; } m64 Q = to_m64(D < 0 ? (1 - D) / 4 % mod : mod - (D - 1) / 4 % mod, r2, N, mod); m64 u = one, v = one, Qn = Q; u64 k = (n + 1) << __builtin_clzll(n + 1); D %= (i64)mod; D = to_m64(D < 0 ? mod + D : D, r2, N, mod); for (k <<= 1; k; k <<= 1) { u = mul_m64(u, v, N, mod); v = sub_m64(mul_m64(v, v, N, mod), add_m64(Qn, Qn, mod), mod); Qn = mul_m64(Qn, Qn, N, mod); if (k >> 63) { u64 uu = add_m64(u, v, mod); if (uu & 1) uu += mod; uu >>= 1; v = add_m64(mul_m64(D, u, N, mod), v, mod); if (v & 1) v += mod; v >>= 1; u = uu; Qn = mul_m64(Qn, Q, N, mod); } } if (u == 0 || v == 0) return true; u64 x = (n + 1) & ~n; for (x >>= 1; x; x >>= 1) { u = mul_m64(u, v, N, mod); v = sub_m64(mul_m64(v, v, N, mod), add_m64(Qn, Qn, mod), mod); if (v == 0) return true; Qn = mul_m64(Qn, Qn, N, mod); } } return false; } int main(void) { u64 Q = in_u64(); while (Q--) { u64 x = in_u64(); out_u64(x); SP(); out_u32(baillie_psw(x) ? 1u : 0u); NL(); } return 0; }