結果
| 問題 | No.8030 ミラー・ラビン素数判定法のテスト |
| ユーザー |
nonamae
|
| 提出日時 | 2021-11-21 22:39:29 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 43 ms / 9,973 ms |
| コード長 | 2,734 bytes |
| コンパイル時間 | 218 ms |
| コンパイル使用メモリ | 32,256 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 23:45:02 |
| 合計ジャッジ時間 | 945 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
main.c: In function 'in':
main.c:89:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
89 | while (c = getchar_unlocked(), c < 48 || c > 57);
| ^~~~~~~~~~~~~~~~
main.c: In function 'out':
main.c:98:3: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
98 | putchar_unlocked(x - x / 10 * 10 + 48);
| ^~~~~~~~~~~~~~~~
ソースコード
#include <stdint.h>
#include <stdio.h>
uint64_t oneM(uint64_t m) { return (uint64_t)-1ull % m + 1; }
uint64_t r2M(uint64_t m) { return (__uint128_t)(__int128_t)-1 % m + 1; }
uint64_t nM(uint64_t m) {
uint64_t N = m;
for (int _ = 0; _ < 5; ++_) N *= 2 - N * m;
return N;
}
uint64_t reduceM(__uint128_t a, uint64_t N, uint64_t m) {
uint64_t y = (uint64_t)(a >> 64) - (uint64_t)(((__uint128_t)((uint64_t)a * N) * m) >> 64);
return (int64_t)y < 0 ? y + m : y;
}
uint64_t powM(uint64_t a, int64_t n, uint64_t one, uint64_t N, uint64_t m) {
uint64_t ret = one;
while (n > 0) {
if (n & 1) ret = reduceM((__uint128_t)ret * a, N, m);
a = reduceM((__uint128_t)a * a, N, m);
n >>= 1;
}
return ret;
}
static uint64_t x = 0;
static uint64_t w = 0;
static uint64_t s = 0xb5ad4eceda1ce2a9;
inline static uint32_t next_msws(void) {
x *= x;
x += (w += s);
return x = (x >> 32) | (x << 32);
}
uint32_t range_msws(uint32_t l, uint32_t r) {
return next_msws() % (r - l + 1) + l;
}
uint64_t bin_gcd(uint64_t a, uint64_t b) {
if (!a || !b) return a | b;
uint64_t s = __builtin_ctzll(a | b);
uint64_t t;
a >>= __builtin_ctzll(a);
do {
b >>= __builtin_ctzll(b);
if (a > b) t = a, a = b, b = a;
b -= a;
} while (b);
return a << s;
}
int jacobi(int64_t a, uint64_t n) {
uint64_t t;
int j = 1;
while (a) {
if (a < 0) {
a = -a;
if ((n & 3) == 3) j = -j;
}
int s = __builtin_ctzll(a);
a >>= s;
if (((n & 7) == 3 || (n & 7) == 5) && (s & 1)) j = -j;
if ((a & n & 3) == 3) j = -j;
t = a, a = n, n = t;
a %= n;
if (a > n / 2) a -= n;
}
return n == 1 ? j : 0;
}
int is_prime(uint64_t n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (!(n & 1)) return 0;
const uint64_t mod = n;
const uint64_t r2 = r2M(n);
const uint64_t N = nM(n);
const uint64_t one = oneM(n);
const uint64_t rev = reduceM((__uint128_t)(n - 1) * r2, N, n);
for (int _ = 0; _ < 7; ++_) {
uint32_t a = range_msws(2u, ((n - 1) > ((1ull << 32) - 1)) ? 1u << 31 : n - 1);
int x = jacobi(a, n);
uint64_t y = (x == -1) ? rev : ((x == 0) ? 0 : one);
if (y == 0 || y != powM(reduceM((__uint128_t)a * r2, N, mod), (mod - 1) / 2, one, N, mod)) 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(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
int main(void) {
uint64_t T = in();
while (T--) {
uint64_t x = in();
out(x);
putchar_unlocked(' ');
out(is_prime(x));
putchar_unlocked('\n');
}
return 0;
}
nonamae