結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー lightninglightning
提出日時 2019-05-31 18:26:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 6,561 ms / 9,973 ms
コード長 2,790 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 169,632 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:23:15
合計ジャッジ時間 19,893 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3,577 ms
5,376 KB
testcase_05 AC 3,443 ms
5,376 KB
testcase_06 AC 1,328 ms
5,376 KB
testcase_07 AC 1,301 ms
5,376 KB
testcase_08 AC 1,302 ms
5,376 KB
testcase_09 AC 6,561 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define put(a) cout<<a<<"\n"
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){if(a>b){a=b;return 1;}return 0;}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){if(a<b){a=b;return 1;}return 0;}

using u32 = unsigned int;
using u64 = unsigned long long;
//using u128 = __uint128_t;

template <class Uint, class BinOp>
bool is_prime_impl(const Uint &n, const Uint *witness, BinOp modmul) {
    if (n == 2) return true;
    if (n < 2 || n % 2 == 0) return false;
    const Uint m = n - 1, d = m / (m & -m);
    auto modpow = [&](Uint a, Uint b) {
        Uint res = 1;
        for (; b; b /= 2) {
            if (b & 1) res = modmul(res, a);
            a = modmul(a, a);
        }
        return res;
    };
    auto suspect = [&](Uint a, Uint t) {
        a = modpow(a, t);
        while (t != n - 1 && a != 1 && a != n - 1) {
            a = modmul(a, a);
            t = modmul(t, 2);
        }
        return a == n - 1 || t % 2 == 1;
    };
    for (const Uint *w = witness; *w; w++) {
        if (*w % n != 0 && !suspect(*w, d)) return false;
    }
    return true;
}

bool is_prime(const u64 &n) {
    assert(n < 1ULL << 63);
    if (n < 1ULL << 32) {
        // n < 2^32
        constexpr u64 witness[] = {2, 7, 61, 0};
        auto modmul = [&](u64 a, u64 b) { return a * b % n; };
        return is_prime_impl<u64>(n, witness, modmul);
    } else {
        // n < 2^63
        constexpr u64 witness[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022, 0};
        // if u128 is available
        //auto modmul = [&](u128 a, u128 b) { return a * b % n; };
        // otherwise
        auto modmul = [&](u64 a, u64 b) {
            u64 res = 0;
            for (; b; b /= 2) {
                if (b & 1) res = (res + a) % n;
                a = (a + a) % n;
            }
            return res;
        };
        return is_prime_impl<u64>(n, witness, modmul);
    }
}

int main(){
    int n;
    cin >> n;
    vector<ll> x(n);
    REP(i,n){
        cin >> x[i];
    }
    REP(i,n){
        cout << x[i] << " ";
        if(is_prime(x[i])){
            put(1);
        }else{
            put(0);
        }
    }
    return 0;
}
0