結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2021-10-14 09:43:22
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 6,773 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 38,144 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-17 16:27:57
合計ジャッジ時間 1,324 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 18 ms
6,944 KB
testcase_05 AC 17 ms
6,940 KB
testcase_06 AC 10 ms
6,940 KB
testcase_07 AC 9 ms
6,940 KB
testcase_08 AC 9 ms
6,940 KB
testcase_09 AC 28 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'read_i32':
main.c:40:16: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   40 |     while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
      |                ^~~~~~~~~~~~~~~~
main.c: In function '_write_i32':
main.c:49:5: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   49 |     putchar_unlocked(x - x / 10 * 10 + 48);
      |     ^~~~~~~~~~~~~~~~

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")


#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <time.h>

/* 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;

/* io */
// -2147483648 ~ 2147483647 (> 10 ^ 9)
int read_i32(void) {
    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;
}
static inline void _write_i32(int x) {
    if (x >= 10) _write_i32(x / 10);
    putchar_unlocked(x - x / 10 * 10 + 48);
}
void write_i32(int x) {
    if (x < 0) {
        putchar_unlocked('-');
        x = -x;
    }
    _write_i32(x);
}
// -9223372036854775808 ~ 9223372036854775807 (> 10 ^ 18)
i64 read_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 _write_i64(i64 x) {
    if (x >= 10) _write_i64(x / 10);
    putchar_unlocked(x - x / 10 * 10 + 48);
}
void write_i64(i64 x) {
    if (x < 0) {
        putchar_unlocked('-');
        x = -x;
    }
    _write_i64(x);
}
// 0 ~ 4294967295 (> 10 ^ 9)
u32 in(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 a) {
    if (a >= 10) out(a / 10);
    putchar_unlocked(a - a / 10 * 10 + 48);
}
// 0 ~ 18446744073709551615 (> 10 ^ 19)
u64 in64(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 out64(u64 a) {
    if (a >= 10) out64(a / 10);
    putchar_unlocked(a - a / 10 * 10 + 48);
}
static inline void NL(void) { putchar_unlocked('\n'); }
static inline void SP(void) { putchar_unlocked(' '); }
#ifdef DEBUG
void out_bit(u64 n) {
    u64 mask = (u64)1ull << (sizeof(n) * CHAR_BIT - 1);
    putchar_unlocked('0');
    putchar_unlocked('b');
    do {
        putchar_unlocked(mask & n ? '1' : '0');
    }
    while (mask >>= 1);
}
#endif

/* macro */
#define SWAP(a, b) (((a)^=(b)),((b)^=(a)),((a)^=(b)))
#define MAX(a, b) ((a)>(b)?(a):(b))
#define MIN(a, b) ((a)<(b)?(a):(b))
#define REP(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;++i)
#define FOR(i,n) REP(i,0,(n))
#define LOOP(n) FOR(i,n)

/* bit macro */
#define POPCNT(a) __builtin_popcountll((a))
#define CTZ(a) __builtin_ctzll((a))
#define CLZ(a) __builtin_clzll((a))
#define LSBit(a) ((a)&(-(a)))
#define CLSBit(a) ((a)&((a)-(1)))
#define HAS_SINGLE_BIT(a) (POPCNT((a))==1)
#define BIT_CEIL(a) ((!(a))?(1):((POPCNT(a))==(1)?((1ull)<<((63)-CLZ((a)))):((1ull)<<((64)-CLZ(a)))))
#define BIT_FLOOR(a) ((!(a))?(0):((1ull)<<((63)-CLZ((a)))))
#define BIT_WIDTH(a) ((a)?((64)-CLZ((a))):(0))
#define _ROTL(x, s) (((x)<<((s)%(64)))|(((x)>>((64)-((s)%(64))))))
#define _ROTR(x, s) (((x)>>((s)%(64)))|(((x)<<((64)-((s)%(64))))))
#define ROTL(x, s) (((s)==(0))?(0):((((i128)(s))<(0))?(_ROTR((x),-(s))):(_ROTL((x),(s)))))
#define ROTR(x, s) (((s)==(0))?(0):((((i128)(s))<(0))?(_ROTL((x),-(s))):(_ROTR((x),(s)))))

/* montgomery modular multiplication 64-bit */
typedef uint64_t m64;

m64 _one_64(u64 mod) {
    return -1ull % mod + 1;
}
m64 _r2_64(u64 mod) {
    return (u128)(i128)-1 % mod + 1;
}
m64 _inv_64(u64 mod) {
    u64 inv = mod;
    LOOP(__builtin_ctz(sizeof(u64) * 8) - 1) inv *= 2 - mod * inv;
    return inv;
}
m64 _reduce(u128 a, m64 inv, u64 mod) {
    i64 A = (a >> 64) - ((((u64)a * inv) * (u128)mod) >> 64);
    return A < 0 ? A + mod : (u64)A;
}
m64 to_m64(u64 a, m64 r2, m64 inv, u64 mod) {
    return _reduce((u128)a * r2, inv, mod);
}
u64 from_m64(m64 A, m64 inv, u64 mod) {
    m64 t = _reduce((u128)A, inv, mod) - mod;
    return t + (mod & -(t >> 63u));
}
m64 add_MR_64(m64 A, m64 B, u64 mod) {
    A += B - (mod << 1u);
    A += (mod << 1u) & -(A >> 63u);
    return A;
}
m64 sub_MR_64(m64 A, m64 B, u64 mod) {
    A -= B;
    A += (mod << 1u) & -(A >> 63u);
    return A;
}
m64 min_MR_64(m64 A, u64 mod) {
    return sub_MR_64(0, A, mod);
}
m64 mul_MR_64(m64 A, m64 B, m64 inv, u64 mod) {
    return _reduce((u128)A * B, inv, mod);
}
m64 pow_MR_64(m64 A, i64 n, m64 inv, u64 mod) {
    m64 ret = _one_64(mod);
    while (n > 0) {
        if (n & 1) ret = mul_MR_64(ret, A, inv, mod);
        A = mul_MR_64(A, A, inv, mod);
        n >>= 1;
    }
    return ret;
}
m64 inv_MR_64(m64 A, m64 inv, u64 mod) {
    return pow_MR_64(A, (i64)mod - 2, inv, mod);
}
m64 div_MR_64(m64 A, m64 B, m64 inv, u64 mod) {
    /* assert(is_prime(mod)); */
    return mul_MR_64(A, inv_MR_64(B, inv, mod), inv, mod);
}
m64 in_MR_64(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_MR_64(m64 A, m64 inv, u64 mod) {
    u64 a = from_m64(A, inv, mod);
    out(a);
}

bool miller_rabin(u64 n, u64 d, const u64 *bases, int bases_len, m64 r2, m64 inv, m64 one, m64 rev) {
    for (int i = 0; i < bases_len; i++) {
        if (n <= bases[i]) break;
        m64 a = to_m64(bases[i], r2, inv, n);
        u64 t = d;
        m64 y = pow_MR_64(a, t, inv, n);
        while (t != n - 1 && y != one && y != rev) {
            y = mul_MR_64(y, y, inv, n);
            t <<= 1;
        }
        if (y != rev && (!(t & 1))) return false;
    }
    return true;
}

bool is_prime(u64 n) {
    if (n <= 3u) return n == 2u || n == 3u;
    if (!(n & 1)) return false;
    u64 m = n - 1;
    m64 r2 = _r2_64(n);
    m64 inv = _inv_64(n);
    m64 one = _one_64(n);
    m64 rev = to_m64(m, r2, inv, n);
    u64 d = m >> CTZ(m);
    const u64 bases[] = { 2ull, 325ull, 9375ull, 28178ull, 450775ull, 9780504ull, 1795265022ull };
    return miller_rabin(n, d, bases, 7, r2, inv, one, rev);
}

void Main(void) {
    int q = read_i32();
    LOOP(q) {
        u64 x = in64();
        out64(x); SP(); out(is_prime(x));
        NL();
    }
}

int main() {
    Main();
    return 0;
}
0