結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー AC2KAC2K
提出日時 2023-03-29 13:39:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 561 ms / 9,973 ms
コード長 3,119 bytes
コンパイル時間 3,014 ms
コンパイル使用メモリ 244,740 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 05:33:28
合計ジャッジ時間 5,785 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 2 "template.hpp"
#include<bits/stdc++.h>
using namespace std;
#define rep(i, N)  for(int i=0;i<(N);i++)
#define all(x) (x).begin(),(x).end()
#define popcount(x) __builtin_popcount(x)
using i128=__int128_t;
using ll = long long;
using ld = long double;
using graph = vector<vector<int>>;
using P = pair<int, int>;
constexpr int inf = 1e9;
constexpr ll infl = 1e18;
constexpr ld eps = 1e-6;
const long double pi = acos(-1);
constexpr uint64_t MOD = 1e9 + 7;
constexpr uint64_t MOD2 = 998244353;
constexpr int dx[] = { 1,0,-1,0 };
constexpr int dy[] = { 0,1,0,-1 };
template<class T>static constexpr inline void chmax(T&x,T y){if(x<y)x=y;}
template<class T>static constexpr inline void chmin(T&x,T y){if(x>y)x=y;}
#line 2 "math/mod_pow.hpp"
template <class T, class U = T>
U mod_pow(T base, T exp, T mod){
    T ans = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) {
            ans *= base;
            ans %= mod;
        }
        base *= base;
        base %= mod;
        exp >>= 1;
    }
    return ans;
}
///@brief mod pow(バイナリ法)
#line 3 "math/miller.hpp"
namespace prime {
    namespace miller{
        using i128 = __int128_t;
        using u128 = __uint128_t;
        using u64 = __uint64_t;
        bool miller_rabin(u64 n,const u64 bases[],int siz) {
            u64 d = n - 1;
            u64 q = __builtin_ctz(d);
            d >>= q;

            for (int i = 0; i < siz; i++) {
                u64 a = bases[i];
                if (a == n) {
                    return true;
                } else if (n % a == 0) {
                    return false;
                }
                if (mod_pow<u128>(a, d, n) != 1) {
                    bool flag = true;
                    for (u64 r = 0; r < q; r++) {
                        u64 pow = mod_pow<u128>(a, d * (1ll << r), n);
                        if (pow == n - 1) {
                            flag = false;
                            break;
                        }
                    }

                    if (flag) {
                        return false;
                    }
                }
            }
            return true;
        }


        bool is_prime(u64 n){
            static constexpr u64 bases_int[3] = {2, 7, 61};  // intだと、2,7,61で十分
            static constexpr u64 bases_ll[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
            if (n < 2) {
                return false;
            } else if (n == 2) {
                return true;
            } else if (~n & 1) {
                return false;
            }
            if (n < (1ul << 31)) {
                return miller_rabin(n, bases_int, 3);
            } else {
                return miller_rabin(n, bases_ll, 7);
            }
        }
    };
};
///@brief fast prime check(MillerRabinの素数判定)
#line 3 "main.cpp"
int main(){
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++){
        uint64_t xi;
        scanf("%lld", &xi);
        printf("%lld ", xi);
        if (prime::miller::is_prime(xi)) {
            puts("1");
        } else {
            puts("0");
        }
    }
}
0