結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2022-08-29 12:16:19 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,104 bytes |
| コンパイル時間 | 953 ms |
| コンパイル使用メモリ | 32,000 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-06 10:25:55 |
| 合計ジャッジ時間 | 1,668 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 2 |
コンパイルメッセージ
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);
| ^~~~~~~~~~~~~~~~
ソースコード
#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, 325, 9375, 28178, 450775, 9780504, 1795265022 };
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 < 7; ++i)
{
if (base[i] >= n) break;
uint64_t a = reduce((__uint128_t)r2 * base[i], n, ninv);
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;
}