結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2022-07-19 17:26:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 9,973 ms
コード長 8,933 bytes
コンパイル時間 3,292 ms
コンパイル使用メモリ 219,104 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-16 23:50:52
合計ジャッジ時間 4,000 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma region opt
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma endregion opt

#include <bits/stdc++.h>

#pragma region type
using i8 = std::int8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t;
using i128 = __int128_t; using u128 = __uint128_t;
using f32 = float; using f64 = double; using f80 = long double;

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>>>;
template<typename T> using pvec = std::pair<std::vector<T>, std::vector<T>>;
#pragma endregion type

#pragma region MACRO for
#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); ++_)
#pragma endregion MACRO for

#pragma region MACRO container
#define ALL(obj) (obj).begin(),(obj).end()
#define SZ(obj) (static_cast<int>((obj).size()))
#pragma endregion MACRO container

#pragma region MACRO bits
#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 bits

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

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

#pragma endregion jacobi

#pragma region Baillie_PSW primality test

int is_prime(u64 n) {
    
    using m64 = u64;

    {
        if (n <= 1) return 0;
        if (n <= 3) return 1;
        if (!(n & 1)) return 0;
    }
    
    const m64 one = (u64)-1ull % n + 1;
    const m64 r2 = (u128)(i128)-1 % n + 1;
    m64 N_ = n;
    for (int i = 0; i < 5; i++) N_ *= 2 - N_ * n;
    const m64 N = N_;

    auto reduce = [](u128 a, m64 x, u64 mod) {
        u64 y = (u64)(a >> 64) - (u64)(((u128)((u64)a * x) * mod) >> 64);
        return (i64)y < 0 ? y + mod : y;
    };
    auto to = [&reduce](u64 a, m64 x, m64 y, u64 mod) {
        return reduce((u128)a * x, y, mod);
    };
    auto add = [](m64 x, m64 y, u64 mod) {
        return x + y >= mod ? x + y - mod:  x + y;
    };
    auto sub = [](m64 x, m64 y, u64 mod) {
        return x >= y ? x - y : mod + x - y;
    };
    auto mul = [&reduce](m64 x, m64 y, m64 z, u64 mod) {
        return reduce(u128(x) * y, z, mod);
    };

    {
        u64 d = (n - 1) << __builtin_clzll(n - 1);
        m64 t = one << 1;
        if (t >= n) t -= n;
        for (d <<= 1; d; d <<= 1) {
            t = mul(t, t, N, n);
            if (d >> 63) {
                t <<= 1;
                if (t >= n) t -= n;
            }
        }
        if (t != one) {
            u64 x = (n - 1) & -(n - 1);
            m64 mone = n - one;
            for (x >>= 1; t != mone; x >>= 1) {
                if (x == 0) return 0;
                t = mul(t, t, N, n);
            }
        }
    }

    {
        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 0;
            }
            if (i & 1) D -= 2;
            else D += 2;
            D = -D;
        }

        m64 Q = to(D < 0 ? (1 - D) / 4 % n : n - (D - 1) / 4 % n, r2, N, n);

        m64 u, v, Qn;
        u64 k = (n + 1) << __builtin_clzll(n + 1);
        u = one;
        v = one;
        Qn = Q;
        D %= (i64)n;
        D = to(D < 0 ? n + D : D, r2, N, n);
        for (k <<= 1; k; k <<= 1) {
            u = mul(u, v, N, n);
            v = sub(mul(v, v, N, n), add(Qn, Qn, n), n);
            Qn = mul(Qn, Qn, N, n);
            if (k >> 63) {
                u64 uu = add(u, v, n);
                if (uu & 1) uu += n;
                uu >>= 1;
                v = add(mul(D, u, N, n), v, n);
                if (v & 1) v += n;
                v >>= 1;
                u = uu;
                Qn = mul(Qn, Q, N, n);
            }
        }

        if (u == 0 || v == 0) return 1;
        u64 x = (n + 1) & ~n;
        for (x >>= 1; x; x >>= 1) {
            u = mul(u, v, N, n);
            v = sub(mul(v, v, N, n), add(Qn, Qn, n), n);
            if (v == 0) return 1;
            Qn = mul(Qn, Qn, N, n);
        }
    }

    return 0;
}

#pragma endregion Baillie_PSW primality test

void Main() {
    // your source here
    int T; scanf("%d", &T);
    while (T--) {
        unsigned long long int x;
        scanf("%llu", &x);
        printf("%llu %d\n", x, is_prime(x));
    }
    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