結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー sekiya9311sekiya9311
提出日時 2017-10-22 19:47:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,698 ms / 9,973 ms
コード長 2,144 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 167,824 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:11:55
合計ジャッジ時間 9,659 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 1,789 ms
5,376 KB
testcase_05 AC 1,597 ms
5,376 KB
testcase_06 AC 63 ms
5,376 KB
testcase_07 AC 56 ms
5,376 KB
testcase_08 AC 56 ms
5,376 KB
testcase_09 AC 3,698 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using value_type = __uint128_t;

class XorShift {
private:
    uint32_t x = 123456789;
    uint32_t y = 362436069;
    uint32_t z = 521288629;
    uint32_t w;
public:
    XorShift(uint32_t seed = 0)
        : w(seed ? seed : 88675123) {this->random_loop();}
    void random_loop() {
        int loop_num = this->next_int() % 50;
        while (loop_num--) this->next_int();
    }
    uint32_t next_int(bool heavy_ok = false) {
        if (heavy_ok) this->random_loop();
        uint32_t t = this->x ^ (this->x << 11);
        this->x = this->y;
        this->y = this->z;
        this->z = this->w;
        return this->w = (this->w ^ (this->w >> 19))
                            ^ (t ^ (t >> 8));
    }
    uint64_t next_long(bool heavy_ok = false) {
        return ((((uint64_t)this->next_int(heavy_ok)) << 32)
                    | this->next_int(heavy_ok));
    }
    uint32_t operator()(bool heavy_ok = false) {
        return this->next_int(heavy_ok);
    }
};

value_type pow_mod(value_type a, value_type p, value_type mod) {
    if (p == 0) {
        return 1;
    } else if (p & 1) {
        return a * pow_mod(a, p - 1, mod) % mod;
    } else {
        const value_type t = pow_mod(a, p >> 1, mod);
        return t * t % mod;
    }
}

XorShift rnd;
//random_device rnd;
bool miller_labin(value_type n, int loopNum = 100) {
    if (n == 2) {
        return true;
    }
    if (n < 2 || (!(n & 1))) {
        return false;
    }
    value_type d = n - 1;
    while (!(d & 1)) {
        d >>= 1;
    }
    while (loopNum--) {
        value_type a = (((((value_type) rnd()) << 32) | (value_type) rnd()) % (n - 2)) + 1;
        value_type t = d;
        value_type y = pow_mod(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = (y * y) % n;
            t <<= 1;
        }
        if (y != n - 1 && (!(t & 1))) {
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    scanf("%d", &n);
    while (n--) {
        long long x;
        scanf("%lld", &x);
        printf("%lld %d\n", x, (int) miller_labin(x));
    }
    return 0;
}
0