結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2022-07-09 22:20:19
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 25 ms / 9,973 ms
コード長 11,274 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 45,416 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:51:22
合計ジャッジ時間 1,211 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 0 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 15 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 25 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region opt

#pragma GCC target("avx2")
#pragma GCC optimize("O3")

#pragma endregion opt


#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

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;

#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

// -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 xorshift

const f64 _R_ = 1.0 / 0xffffffffffffffff;

static u64 _xorshift_state_ = 88172645463325252ULL;

u64 next_rand_xorshift(void) {
    _xorshift_state_ = _xorshift_state_ ^ (_xorshift_state_ << 7);
    return _xorshift_state_ = _xorshift_state_ ^ (_xorshift_state_ >> 9);
}

void rand_init_xorshift(u64 seed) {
    _xorshift_state_ += seed;
    (void)next_rand_xorshift();
}

u64 random_range_xorshift(u64 l, u64 r) {
    return next_rand_xorshift() % (r - l + 1) + l;
}

f64 probability_xorshift(void) {
    return _R_ * next_rand_xorshift();
}

#pragma endregion xorshift


#pragma region binary gcd

u64 bin_gcd(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;
}

#pragma endregion binary gcd


#pragma region jacobi symbol

int jacobi(i64 a, u64 n) {
    u64 t;
    int j = 1;
    while (a) {
        if (a < 0) {
            a = -a;
            if ((n & 3) == 3) j = -j;
        }
        int s = CTZ64(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 montgomery_32bit

typedef uint32_t m32;

m32 one_m32(u32 mod) {
    return -1u % mod + 1;
}

m32 r2_m32(u32 mod) {
    return (u64)(i64)-1 % mod + 1;
}

m32 N_m32(u32 mod) {
    m32 N = mod;
    for (int _ = 0; _ < __builtin_ctz(sizeof(u32) * 8); _++) N *= 2 - mod * N; 
    return N;
}

m32 reduce_m32(u64 a, m32 N, u32 mod) {
    i32 z = (a >> 32) - ((((u32)a * N) * (u64)mod) >> 32);
    return z < 0 ? z + mod : (u64)z;
}

m32 to_m32(u32 a, m32 r2, m32 N, u32 mod) {
    return reduce_m32((u64)a * r2, N, mod);
}

u32 from_m32(m32 A, m32 N, u32 mod) {
    m32 t = reduce_m32((u64)A, N, mod) - mod;
    return t + (mod & -(t >> 31u));
}

m32 add_m32(m32 A, m32 B, u32 mod) {
    A += B - (mod << 1u);
    A += (mod << 1u) & -(A >> 31u);
    return A;
}

m32 sub_m32(m32 A, m32 B, u32 mod) {
    A -= B;
    A += (mod << 1u) & -(A >> 31u);
    return A;
}

m32 min_m32(m32 A, u32 mod) {
    return sub_m32(0, A, mod);
}

m32 mul_m32(m32 A, m32 B, m32 N, u32 mod) {
    return reduce_m32((u64)A * B, N, mod);
}

m32 pow_m32(m32 A, i64 n, m32 one, m32 N, u32 mod) {
    m32 ret = one;
    while (n > 0) {
        if (n & 1) ret = mul_m32(ret, A, N, mod);
        A = mul_m32(A, A, N, mod);
        n >>= 1;
    }
    return ret;
}

m32 inv_m32(m32 A, m32 one, m32 N, u32 mod) {
    return pow_m32(A, (i64)mod - 2, one, N, mod);
}

m32 div_m32(m32 A, m32 B, m32 one, m32 N, u32 mod) {
    /* assert(is_prime(mod)); */
    return mul_m32(A, inv_m32(B, one, N, mod), N, mod);
}

m32 in_m32(m32 r2, m32 N, u32 mod) {
    u32 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_m32(a, r2, N, mod);
}

void out_m32(m32 A, m32 N, u32 mod) {
    u32 a = from_m32(A, N, mod);
    out_u32(a);
}

#pragma endregion montgomery_32bit


#pragma region montgomery_64bit

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

m64 in_m64(m64 r2, m64 N, 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, N, mod);
}

void out_m64(m64 A, m64 N, u64 mod) {
    u64 a = from_m64(A, N, mod);
    out_u64(a);
}

#pragma endregion montgomery_64bit


#pragma region miller_rabin_primality_test

bool is_prime32(u32 n) {
    u32 m = n - 1;
    m32 one = one_m32(n);
    m32 r2 = r2_m32(n);
    m32 N = N_m32(n);
    m32 rev = to_m32(m, r2, N, n);
    u32 d = m >> CTZ32(m);
    u32 base[] = { 2u, 7u, 61u };
    for (int i = 0; i < 3; i++) {
        if (n <= base[i]) break;
        u32 t = d;
        m32 y = pow_m32(to_m32(base[i], r2, N, n), t, one, N, n);
        while (t != m && y != one && y != rev) {
            y = mul_m32(y, y, N, n);
            t <<= 1;
        }
        if (y != rev && (!(t & 1))) return false;
    }
    return true;
}

bool is_prime64(u64 n) {
    u64 m = n - 1;
    m64 one = one_m64(n);
    m64 r2 = r2_m64(n);
    m64 N = N_m64(n);
    m64 rev = to_m64(m, r2, N, n);
    u64 d = m >> CTZ64(m);
    u64 base[] = { 2ul, 325ul, 9375ul, 28178ul, 450775ul, 9780504ul, 1795265022ul };
    for (int i = 0; i < 7; i++) {
    if (n <= base[i]) break;
    u64 t = d;
    m64 y = pow_m64(to_m64(base[i], r2, N, n), t, one, N, n);
    while (t != m && y != one && y != rev) {
        y = mul_m64(y, y, N, n);
        t <<= 1;
    }
    if (y != rev && (!(t & 1))) return false;
  }
  return true;
}

u32 is_prime(u64 n) {
    if (n <= 1ul) return 0;
    if (n <= 3ul) return 1;
    if (!(n & 1)) return 0;
    if (n < ((u32)1u << 31)) return is_prime32((u32)n) ? 1 : 0;
    return is_prime64(n) ? 1 : 0;
}

#pragma endregion miller_rabin_primality_test


int main(void) {

    u64 Q = in_u64();
    while (Q--) {
        u64 x = in_u64();
        out_u64(x);
        SP();
        out_u32(is_prime(x));
        NL();
    }
    return 0;
}
0