結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2022-08-23 06:58:05
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 248 ms / 9,973 ms
コード長 11,264 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 39,676 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:55:03
合計ジャッジ時間 1,842 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 132 ms
5,376 KB
testcase_05 AC 119 ms
5,376 KB
testcase_06 AC 26 ms
5,376 KB
testcase_07 AC 26 ms
5,376 KB
testcase_08 AC 25 ms
5,376 KB
testcase_09 AC 248 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in_i32':
main.c:104:94: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
  104 | 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; }
      |                                                                                              ^~~~~~~~~~~~~~~~
main.c: In function 'out_i32_inner':
main.c:105:79: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
  105 | static inline void out_i32_inner(i32 x) { if (x >= 10) out_i32_inner(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); }
      |                                                                               ^~~~~~~~~~~~~~~~

ソースコード

diff #

// clang-format off
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("omit-frame-pointer")
#pragma GCC optimize("inline")
#pragma GCC option("arch=native")
#pragma GCC option("no-zero-upper")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#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;

/// (3, 7) -> 3
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
/// (4, 5) -> 5
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
/// (1, 2) -> (2, 1)
#define SWAP_REF(a, b)      \
            do {            \
                (a) ^= (b); \
                (b) ^= (a); \
                (a) ^= (b); \
            }               \
            while(0);

/// {0, 1, 2, 3, 4, ...} -> {0, 1, 1, 2, 1, ...}
#define POPCNT32(a) ((a) ? __builtin_popcount((a)) : (0))
/// {0, 1, 2, 4, 8, ...} -> {32, 0, 1, 2, 3, ...}
#define CTZ32(a) ((a) ? __builtin_ctz((a)) : 32)
/// {0, 1, 2, 4, 8, ...} -> {32, 31, 30, 29, 28, ...}
#define CLZ32(a) ((a) ? __builtin_clz((a)) : 32)
/// {0, 1, 2, 3, 4, ...} -> {0, 1, 1, 2, 1, ...}
#define POPCNT64(a) ((a) ? __builtin_popcountll((a)) : (0))
/// {0, 1, 2, 4, 8, ...} -> {64, 0, 1, 2, 3, ...}
#define CTZ64(a) ((a) ? __builtin_ctzll((a)) : 64)
/// {0, 1, 2, 4, 8, ...} -> {64, 63, 62, 61, 60, ...}
#define CLZ64(a) ((a) ? __builtin_clzll((a)) : 64)
/// {0, 1, 2, 4, 8, ...} -> {-1, 0, 1, 2, 3, ...}
#define MSB32(a) ((a) ? ((31) - __builtin_clz((a))) : (-1))
/// {0, 1, 2, 4, 8, ...} -> {-1, 0, 1, 2, 3, ...}
#define MSB64(a) ((a) ? ((63) - __builtin_clzll((a))) : (-1))
/// {1, 2, 3, 4, 5, ...} -> {1, 2, 1, 4, 1, ...}
#define LSBit(a) ((a) & (-(a)))
/// {1, 2, 3, 4, 5, ...} -> {0, 0, 2, 0, 4, ...}
#define CLSBit(a) ((a) & ((a) - (1)))
/// {1, 2, 3, 4, 5, ...} -> {1, 2, 2, 4, 4, ...}
#define BIT_FLOOR32(a) ((a) ? (1u) << MSB32((a)) : (0))
/// {6, 7, 8, 9, 10, ...} -> {4, 4, 8, 8, 8, ...}
#define BIT_FLOOR64(a) ((a) ? (1ull) << MSB64((a)) : (0))
/// {1, 2, 3, 4, 5, ...} -> {1, 2, 4, 4, 8, ...}
#define BIT_CEIL32_REF(a)           \
            do {                    \
                --(a);              \
                (a) |= (a) >> (1);  \
                (a) |= (a) >> (2);  \
                (a) |= (a) >> (4);  \
                (a) |= (a) >> (8);  \
                (a) |= (a) >> (16); \
                (a)++;              \
            } while(0);
/// {8, 9, 10, 11, 12, ...} -> {8, 16, 16, 16, 16, ...}
#define BIT_CEIL64_REF(a)           \
            do {                    \
                --(a);              \
                (a) |= (a) >> (1);  \
                (a) |= (a) >> (2);  \
                (a) |= (a) >> (4);  \
                (a) |= (a) >> (8);  \
                (a) |= (a) >> (16); \
                (a) |= (a) >> (32); \
                (a)++;              \
            } while(0);
#define ROTL32_INNER(x, l) (((x) << (l)) | ((x) >> ((-l) & (31))))
#define ROTR32_INNER(x, r) (((x) >> (r)) | ((x) << ((-r) & (31))))
#define ROTR32(x, r) (((r) < (0)) ? (ROTL32_INNER((x), ((u64)(-r) % (32)))) : (ROTR32_INNER((x), ((r) % (32)))))
#define ROTL32(x, l) ROTR32((x), (-l))
#define ROTL64_INNER(x, l) (((x) << (l)) | ((x) >> ((-l) & (63))))
#define ROTR64_INNER(x, r) (((x) >> (r)) | ((x) << ((-r) & (63))))
#define ROTR64(x, r) (((r) < (0)) ? (ROTL64_INNER((x), ((u64)(-r) % (64)))) : (ROTR64_INNER((x), ((r) % (64)))))
#define ROTL64(x, l) ROTR64((x), (-l))

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); }
// clang-format on

u64 mu_b32(u32 mod) { return ((u64)(-1) / mod) + 1; }
u32 mul_b32(u32 a, u32 b, u64 mu, u32 mod)
{
    u64 z = a;
    z *= b;
    u64 x = (u64)(((u128)(z) * (u128)(mu)) >> 64);
    u32 v = (u32)(z - x * mod);
    if (mod <= v)
        v += mod;
    return v;
}
u32 add_b32(u32 a, u32 b, u32 mod) { return a + b >= mod ? a + b - mod : a + b; }
u32 sub_b32(u32 a, u32 b, u32 mod) { return a >= b ? a - b : mod + a - b; }
u32 pow_b32(u32 a, u32 k, u64 mu, u32 mod)
{
    u32 ret = 1;
    while (k > 0) {
        if (k & 1)
            ret = mul_b32(ret, a, mu, mod);
        a = mul_b32(a, a, mu, mod);
        k >>= 1;
    }
    return ret;
}

typedef struct Tuple_t {
    u64 n;
    u64 r;
    u64 s;
    u64 t;
    u128 n2;
} Tuple;

Tuple mu_b64(u64 mod)
{
    assert(mod >= 3);
    assert(CLSBit(mod) != 0);
    Tuple ret;
    ret.n = mod;
    ret.r = UINT64_MAX / mod;
    ret.s = (~(u128)0ull) / mod;
    ret.t = (UINT64_MAX - ret.r * mod) + 1;
    ret.n2 = (u128)mod * (u128)mod;
    return ret;
}
u64 mul_b64(u64 a, u64 b, Tuple tu)
{
    u128 x = (u128)a * (u128)b;
    assert(x < tu.n2);
    const u128 aa = x >> 64;
    const u64 bb = (u64)x;
    const u128 qa = (aa * tu.s) >> 64;
    const u64 qb = (u64)(((u128)bb * tu.r) >> 64);
    u128 a1 = aa * tu.t - qa * tu.n;
    if (a1 >= tu.n)
        a1 -= tu.n;
    u64 b1 = bb - qb * tu.n;
    if (b1 >= tu.n)
        b1 -= tu.n;
    u128 x1 = a1 + b1;
    if (x1 >= tu.n)
        x1 -= tu.n;
    return x1;
}
u64 add_b64(u64 a, u64 b, u64 mod) { return a + b >= mod ? a + b - mod : a + b; }
u64 sub_b64(u64 a, u64 b, u64 mod) { return a >= b ? a - b : mod + a - b; }
u64 pow_b64(u64 a, u64 k, Tuple tu)
{
    u64 ret = 1;
    while (k > 0) {
        if (k & 1)
            ret = mul_b64(ret, a, tu);
        a = mul_b64(a, a, tu);
        k >>= 1;
    }
    return ret;
}

// clang-format off
static u64 lcg_state = 14534622846793005ull;
u32 lcg_rand(void) { return lcg_state = 6364136223846793005ull * lcg_state + 1442695040888963407ull; }
u32 lcg_range(u32 l, u32 r) { return l + lcg_rand() % (r - l + 1); }
// clang-format on

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;
}

bool is_prime(u64 n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (!(n & 1))
        return false;
    Tuple mu = mu_b64(n);
    for (int _ = 0; _ < 15; ++_) {
        u32 a = lcg_range(2u, ((n - 1) > ((1ull << 32) - 1)) ? 1u << 31 : n - 1);
        int x = jacobi_symbol(a, n);
        u64 y = (x == -1) ? n - 1 : ((x == 0) ? 0 : 1);
        if (y == 0 || y != pow_b64(a, (n - 1) / 2, mu))
            return false;
    }
    return true;
}

int main(void)
{
    i32 Q = in_i32();
    while (Q--) {
        u64 x = in_u64();
        out_u64(x);
        SP();
        out_u32((u32)(is_prime(x)));
        NL();
    }
    return 0;
}
0