結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 👑 MizarMizar
提出日時 2022-08-31 19:02:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 9,973 ms
コード長 2,487 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 33,172 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 16:41:27
合計ジャッジ時間 1,561 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 130 ms
4,376 KB
testcase_05 AC 125 ms
4,376 KB
testcase_06 AC 55 ms
4,380 KB
testcase_07 AC 53 ms
4,376 KB
testcase_08 AC 54 ms
4,376 KB
testcase_09 AC 230 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,bmi2,lzcnt,tune=native")
//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC target ("sse4")
#pragma GCC optimize("O3")
//#pragma GCC optimize ("tree-vectorize")
//#pragma GCC optimize("unroll-loops")
#ifndef NDEBUG
#define NDEBUG
#endif
#include <cassert>
#include <ctime>
#include <cstdio>
#include <cstdbool>
#include <cstdint>
uint64_t modmul(uint64_t a, uint64_t b, uint64_t n) { return (uint64_t)(((__uint128_t)a) * ((__uint128_t)b) % ((__uint128_t)n)); }
uint64_t modpow(uint64_t a, uint64_t b, uint64_t n) {
    uint64_t t = ((b & 1) == 0) ? 1 : a;
    for (b >>= 1; b != 0; b >>= 1) { a = modmul(a, a, n); if ((b & 1) == 1) { t = modmul(t, a, n); } }
    return t;
}
const uint64_t bases[] = {2,325,9375,28178,450775,9780504,1795265022};
bool miller_rabin(uint64_t n) {
    if (n == 2) { return true; }
    if (n < 2 || (n & 1) == 0) { return false; }
    uint64_t n1 = n - 1, d = n - 1;
    uint32_t s = 0;
    for (; (d & 1) == 0; d >>= 1) { s += 1; }
    for (const auto& base : bases) {
        uint64_t a = base;
        if (a >= n) { a %= n; if (a == 0) { continue; } }
        uint64_t t = modpow(a, d, n);
        if (t == 1) { continue; }
        for (uint32_t j = 1; t != n1; ++j) {
            if (j >= s) { return false; }
            t = modmul(t, t, n);
        }
    }
    return true;
}
int main(int, char**) {
    struct timespec time_monotonic_start, time_process_start, time_monotonic_end, time_process_end;
    clock_gettime(CLOCK_MONOTONIC, &time_monotonic_start);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_process_start);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        unsigned long long x;
        scanf("%llu", &x);
        printf("%llu %d\n", x, miller_rabin((uint64_t)x) ? 1 : 0);
    }
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_process_end);
    clock_gettime(CLOCK_MONOTONIC, &time_monotonic_end);
    double d_sec_monotonic =
        (double)(time_monotonic_end.tv_sec - time_monotonic_start.tv_sec) +
        (double)(time_monotonic_end.tv_nsec - time_monotonic_start.tv_nsec) / (1000 * 1000 * 1000);
    double d_sec_process =
        (double)(time_process_end.tv_sec - time_process_start.tv_sec) +
        (double)(time_process_end.tv_nsec - time_process_start.tv_nsec) / (1000 * 1000 * 1000);
    fprintf(stderr, "time_monotonic:%f, time_process:%f\n", d_sec_monotonic, d_sec_process);
}
0