結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2022-08-29 10:51:37
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,091 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 33,140 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 02:39:42
合計ジャッジ時間 927 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 23 ms
5,376 KB
testcase_06 AC 8 ms
5,376 KB
testcase_07 AC 9 ms
5,376 KB
testcase_08 AC 8 ms
5,376 KB
testcase_09 AC 43 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:61:16: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   61 |     while (c = getchar_unlocked(), c < 48 || c > 57);
      |                ^~~~~~~~~~~~~~~~
main.c: In function 'out':
main.c:72:5: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   72 |     putchar_unlocked(x - ((((__uint128_t)x * 14757395258967641293ull) >> 3) >> 64) * 10 + 48);
      |     ^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdint.h>
#include <stdio.h>

uint64_t reduce(__uint128_t A, uint64_t n, uint64_t ninv)
{
    uint64_t y = (uint64_t)(A >> 64) - (uint64_t)(((__uint128_t)((uint64_t)A * ninv) * n) >> 64);
    return (int64_t)y < 0 ? y + n : y;
}
uint64_t mul(uint64_t a, uint64_t b, uint64_t n, uint64_t ninv)
{
    return reduce((__uint128_t)a * b, n, ninv);
}
uint64_t powmod(uint64_t a, uint64_t k, uint64_t n, uint64_t ninv, uint64_t r)
{
    uint64_t ret = r;
    while (k > 0)
    {
        if (k & 1)
            ret = mul(ret, a, n, ninv);
        a = mul(a, a, n, ninv);
        k >>= 1;
    }
    return ret;
}

int is_prime(uint64_t n)
{
    if (n < 2) return 0;
    if (n < 4) return 1;
    if (n & 1 == 0) return 0;
    
    static uint64_t base[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
    
    int s = __builtin_ctzll(n - 1);
    uint64_t d = (n - 1) >> s;

    uint64_t r = (uint64_t)-1ull % n + 1;
    uint64_t r2 = (__uint128_t)(__int128_t)-1 % n + 1;
    uint64_t ninv = n;
    for (int _ = 0; _ < 5; ++_) ninv *= 2 - ninv * n;

    for (int i = 0; i < 12; ++i)
    {
        uint64_t a = reduce((__uint128_t)r2 * base[i], n, ninv);
        if (a >= n) break;
        uint64_t c = powmod(a, d, n, ninv, r);
        if (c == r) continue;
        int f = 0;
        for (int q = 0; q < s && f == 0; q++)
        {
            f |= (c == (n - r));
            c = mul(c, c, n, ninv);
        }
        if (f == 0) return 0;
    }
    return 1;
}

uint64_t in(void) {
    uint64_t c, x = 0;
    while (c = getchar_unlocked(), c < 48 || c > 57);
    while (47 < c && c < 58)
    {
        x = x * 10 + c - 48;
        c = getchar_unlocked();
    }
    return x;
}
void out(uint64_t x)
{
    if (x >= 10) out((((__uint128_t)x * 14757395258967641293ull) >> 3) >> 64);
    putchar_unlocked(x - ((((__uint128_t)x * 14757395258967641293ull) >> 3) >> 64) * 10 + 48);
}

int main(void)
{
    uint64_t Q = in();
    while (Q--) {
        uint64_t x = in();
        out(x);
        putchar_unlocked(' ');
        out(is_prime(x));
        putchar_unlocked('\n');
    }
    return 0;
}
0