結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2022-07-18 03:59:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 9,973 ms
コード長 10,723 bytes
コンパイル時間 2,888 ms
コンパイル使用メモリ 220,556 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 09:50:49
合計ジャッジ時間 3,507 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 16 ms
6,940 KB
testcase_05 AC 15 ms
6,944 KB
testcase_06 AC 8 ms
6,944 KB
testcase_07 AC 9 ms
6,944 KB
testcase_08 AC 8 ms
6,944 KB
testcase_09 AC 25 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region opt

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

#pragma endregion opt

#pragma region header

#include <bits/stdc++.h>

#pragma endregion header

#pragma region type

using i8 = std::int8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using u128 = __uint128_t;

template<typename T> using vec = std::vector<T>;
template<typename T> using vvec = std::vector<std::vector<T>>;
template<typename T> using vvvec = std::vector<std::vector<std::vector<T>>>;

#pragma endregion type

#pragma region MACRO

#define FOR(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define LOOP(n) for(int _=0; _<(n); ++_)

#define ALL(obj) (obj).begin(),(obj).end()
#define SZ(obj) (static_cast<int>((obj).size()))

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

template<class T> inline bool chmax(T& a,T b){ if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a,T b){ if (a > b) { a = b; return 1; } return 0; }

#pragma endregion util

#pragma region runtime montgomery modint

struct Runtime_Montgomery_Modint32 {

private:
    using m32 = u32;

public:
    inline static m32 one, r2, n, md;

    static void set_mod(u32 m) {
        md = m;
        one = u32(-1u) % m + 1;
        r2 = u64(i64(-1)) % m + 1;
        u32 nn = m;
        for (int _ = 0; _ < 4; ++_) nn *= 2 - nn * m;
        n = nn;
    }

    static m32 reduce(u64 a) {
        u32 y = (u32(a >> 32)) - (u32((u64(u32(a) * n) * md) >> 32));
        return i32(y) < 0 ? y + md : y;
    }

    Runtime_Montgomery_Modint32() : x(0) { }
    Runtime_Montgomery_Modint32(u32 x) : x(reduce(u64(x) * r2)) { }

    m32 x;

    Runtime_Montgomery_Modint32 &operator+=(Runtime_Montgomery_Modint32 y) {
        x += y.x - md;
        if (i32(x) < 0) x += md;
        return *this;
    }
    Runtime_Montgomery_Modint32 &operator-=(Runtime_Montgomery_Modint32 y) {
        if (i32(x -= y.x) < 0) x += 2 * md;
        return *this;
    }
    Runtime_Montgomery_Modint32 &operator*=(Runtime_Montgomery_Modint32 y) {
        x = reduce(u64(x) * y.x);
        return *this;
    }
    Runtime_Montgomery_Modint32 operator+(Runtime_Montgomery_Modint32 y) const { return Runtime_Montgomery_Modint32(*this) += y; }
    Runtime_Montgomery_Modint32 operator-(Runtime_Montgomery_Modint32 y) const { return Runtime_Montgomery_Modint32(*this) -= y; }
    Runtime_Montgomery_Modint32 operator*(Runtime_Montgomery_Modint32 y) const { return Runtime_Montgomery_Modint32(*this) *= y; }
    bool operator==(Runtime_Montgomery_Modint32 y) const { return (x >= md ? x - md : x) == (y.x >= md ? y.x - md : y.x); }
    bool operator!=(Runtime_Montgomery_Modint32 y) const { return not operator==(y); }
    Runtime_Montgomery_Modint32 pow(u64 k) {
        Runtime_Montgomery_Modint32 y = 1, z = *this;
        for ( ; k; k >>= 1, z *= z) if (k & 1) y *= z;
        return y;
    }
    Runtime_Montgomery_Modint32 inv() {
        return (*this).pow(md - 2);
    }

    m32 get_raw() { return x; }
    u32 get_val() { return reduce(u64(x)); }
};

struct Runtime_Montgomery_Modint64 {

private:
    using m64 = u64;

public:
    inline static m64 one, r2, n, md;

    static void set_mod(u64 m) {
        md = m;
        one = u64(-1ull) % m + 1;
        r2 = u128(i128(-1)) % m + 1;
        u64 nn = m;
        for (int _ = 0; _ < 5; ++_) nn *= 2 - nn * m;
        n = nn;
    }

    static m64 reduce(u128 a) {
        u64 y = (u64(a >> 64)) - (u64((u128(u64(a) * n) * md) >> 64));
        return i64(y) < 0 ? y + md : y;
    }

    Runtime_Montgomery_Modint64() : x(0) { }
    Runtime_Montgomery_Modint64(u64 x) : x(reduce(u128(x) * r2)) { }

    m64 x;

    Runtime_Montgomery_Modint64 &operator+=(Runtime_Montgomery_Modint64 y) {
        x += y.x - md;
        if (i64(x) < 0) x += md;
        return *this;
    }
    Runtime_Montgomery_Modint64 &operator-=(Runtime_Montgomery_Modint64 y) {
        if (i64(x -= y.x) < 0) x += 2 * md;
        return *this;
    }
    Runtime_Montgomery_Modint64 &operator*=(Runtime_Montgomery_Modint64 y) {
        x = reduce(u128(x) * y.x);
        return *this;
    }
    Runtime_Montgomery_Modint64 operator+(Runtime_Montgomery_Modint64 y) const { return Runtime_Montgomery_Modint64(*this) += y; }
    Runtime_Montgomery_Modint64 operator-(Runtime_Montgomery_Modint64 y) const { return Runtime_Montgomery_Modint64(*this) -= y; }
    Runtime_Montgomery_Modint64 operator*(Runtime_Montgomery_Modint64 y) const { return Runtime_Montgomery_Modint64(*this) *= y; }
    bool operator==(Runtime_Montgomery_Modint64 y) const { return (x >= md ? x - md : x) == (y.x >= md ? y.x - md : y.x); }
    bool operator!=(Runtime_Montgomery_Modint64 y) const { return not operator==(y); }
    Runtime_Montgomery_Modint64 pow(u64 k) {
        Runtime_Montgomery_Modint64 y = 1, z = *this;
        for ( ; k; k >>= 1, z *= z) if (k & 1) y *= z;
        return y;
    }
    Runtime_Montgomery_Modint64 inv() {
        return (*this).pow(md - 2);
    }

    m64 get_raw() { return x; }
    u64 get_val() { return reduce(u128(x)); }
};

#pragma endregion runtime montgomery modint

#pragma region mr

u32 is_prime32(u32 n) {
    using m32 = Runtime_Montgomery_Modint32;
    m32::set_mod(n);
    m32 o{1};
    m32 r{n - 1};
    u32 base[] = { 2u, 7u, 61u };
    u32 d = (n - 1) >> __builtin_ctz(n - 1);
    for (int i = 0; i < 3; i++) {
        if (n <= base[i]) break;
        m32 a(base[i]);
        u32 t = d;
        m32 y = a.pow(t);
        while (t != (n - 1) && y != o && y != r) {
            y *= y;
            t <<= 1;
        }
        if (y != r && (!(t & 1))) return 0;
    }
    return 1;
}

u64 is_prime64(u64 n) {
    using m64 = Runtime_Montgomery_Modint64;
    m64::set_mod(n);
    m64 o{1};
    m64 r{n - 1};
    u64 base[] = { 2ul, 325ul, 9375ul, 28178ul, 450775ul, 9780504ul, 1795265022ul };
    u64 d = (n - 1) >> __builtin_ctzll(n - 1);
    for (int i = 0; i < 7; i++) {
        if (n <= base[i]) break;
        m64 a(base[i]);
        u64 t = d;
        m64 y = a.pow(t);
        while (t != (n - 1) && y != o && y != r) {
            y *= y;
            t <<= 1;
        }
        if (y != r && (!(t & 1))) return 0;
    }
    return 1;
}

u32 is_prime(u64 n) {
    if (n <= 1) return 0;
    if (n <= 3) return 1;
    if (!(n & 1)) return 0;
    if (n < (1ull << 30)) return is_prime32(u32(n));
    return is_prime64(n);
}

#pragma endregion mr

void Main() {
    // your source here
    u64 Q = in_u64();
    while (Q--) {
        u64 x = in_u64();
        out_u64(x);
        SP();
        out_u32(is_prime(x));
        NL();
    }
    return;
}

int main() {
    
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::cout << std::fixed << std::setprecision(13);
    std::cerr << std::fixed << std::setprecision(3);

    Main();
}
0