結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー tarakojo1019tarakojo1019
提出日時 2021-03-26 20:30:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 9,973 ms
コード長 1,542 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 65,280 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:42:43
合計ジャッジ時間 1,306 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

unsigned long long
mod_mul(unsigned long long a, unsigned long long b, unsigned long long mod) {
    long long ret = a * b - mod * (unsigned long long)((long double)(a) * (long double)(b) / (long double)(mod));
    return ret + mod * (ret < 0) - mod * (ret >= (long long)mod);
}

unsigned long long mod_pow(unsigned long long x, unsigned long long k, unsigned long long mod) {
    unsigned long long res = 1;
    while (k) {
        if (k & 1) res = mod_mul(res, x, mod);
        x = mod_mul(x, x, mod);
        k >>= 1;
    }
    return res;
}
const unsigned long long numset[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022ULL};
bool check(unsigned long long n) {
    if (n < 2 || ((n % 6 != 1) && (n % 6 != 5))) return (n == 2) || (n == 3);
    unsigned long long d = n - 1, s = 0;
    while (d % 2 == 0) {
        d /= 2;
        s++;
    }
    for (unsigned long long a : numset) {
        if (a % n == 0) return true;
        unsigned long long res = mod_pow(a, d, n);
        if (res == 1) continue;
        bool ok = true;
        for (unsigned int r = 0; r < s; r++) {
            if (res == n - 1) {
                ok = false;
                break;
            }
            res = mod_mul(res, res, n);
        }
        if (ok) return false;
    }
    return true;
}

using ull = unsigned long long;
int main() {
    ull n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        ull x;
        cin >> x;
        cout << x << " " << (check(x) ? 1 : 0) << endl;
    }
    return 0;
}
0