結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2021-09-14 03:49:03 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 29 ms / 9,973 ms |
| コード長 | 4,279 bytes |
| コンパイル時間 | 283 ms |
| コンパイル使用メモリ | 34,432 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 23:40:16 |
| 合計ジャッジ時間 | 894 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
main.c: In function 'in':
main.c:34:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
34 | while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
| ^~~~~~~~~~~~~~~~
main.c: In function 'out':
main.c:43:5: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
43 | putchar_unlocked('-');
| ^~~~~~~~~~~~~~~~
ソースコード
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* signed integer */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef __int128_t i128;
/* unsigned integer */
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef __uint128_t u128;
/* floating point number */
typedef float f32;
typedef double f64;
typedef long double f80;
/* input / output */
static inline i64 in(void) {
i64 x = 0;
i64 f = 1;
i64 c;
while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
while (47 < c && c < 58) {
x = x * 10 + c - 48;
c = getchar_unlocked();
}
return f * x;
}
static inline void out(i64 x) {
if (x < 0) {
putchar_unlocked('-');
x = -x;
}
if (x >= 10) out(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
static inline void nl(void) { putchar_unlocked('\n'); }
static inline void sp(void) { putchar_unlocked(' '); }
/* bits */
static inline u64 ctz(u64 n) { return __builtin_ctzll(n); }
static inline u64 clz(u64 n) { return __builtin_clzll(n); }
static inline u64 popcnt(u64 n) { return __builtin_popcountll(n); }
static inline u64 _gcd_(u64 a, u64 b) {
i32 shift = 0;
while (a && b && a != b) {
bool c = !(a & 1);
bool d = !(b & 1);
if (c && d) {
++shift;
a >>= 1;
b >>= 1;
}
else if (c && !d) a >>= 1;
else if (!c && d) b >>= 1;
else if (a >= b) a = (a - b) >> 1;
else {
u64 tmp = a;
a = (b - a) >> 1;
b = tmp;
}
}
return !a ? b << shift : a << shift;
}
static inline u64 _lcm_(u64 a, u64 b) {
return a / _gcd_(a, b) * b;
}
static inline i32 ceil_pow2_32(i32 n) {
return n <= 1 ? 0 : 32 - __builtin_clz(n - 1);
}
static inline i64 ceil_pow2_64(i64 n) {
return n <= 1 ? 0 : 64 - __builtin_clzl(n - 1);
}
/* xorshift */
static u64 _rng = 88172645463325252ULL;
u64 next_rand(void) {
_rng = _rng ^ (_rng << 7);
return _rng = _rng ^ (_rng >> 9);
}
/* is_prime */
static inline u64 _m(u64 mod) {
int i;
u64 u = 1;
u64 v = 0;
u64 x = 1ULL << 63;
for (int i = 0; i < 64; i++) {
if (u & 1){
u = (u + mod) / 2;
v = v / 2 + x;
} else {
u >>= 1;
v >>= 1;
}
}
return -v;
}
static inline u64 _r2(u64 mod) { return (u128) (i128) -1 % mod + 1; }
static inline u64 _one(u64 mod) { return -1ULL % mod + 1; }
static inline u64 _MR(u128 x, u64 m, u64 mod) {
i64 z = (x >> 64) - ((((u64)x * m) * (u128)mod) >> 64);
return z < 0 ? z + mod : z;
}
static inline u64 to_montgomery_form(u64 a, u64 r2, u64 m, u64 mod) {
return _MR((u128)a * r2, m, mod);
}
static inline u64 from_montgomery_form(u64 a, u64 m, u64 mod) {
return _MR((u128)a, m, mod);
}
static inline u64 addmod64(u64 x, u64 y, u64 mod) { return x + y >= mod ? x + y - mod : x + y; }
static inline u64 submod64(u64 x, u64 y, u64 mod) { return x >= y ? x - y : x - y + mod; }
static inline u64 mulmod64(u64 x, u64 y, u64 r2, u64 m, u64 mod) { return _MR((u128)r2 * _MR((u128)x * y, m, mod), m, mod); }
static inline u64 powmod64(u64 a, u64 n, u64 r2, u64 m, u64 mod) {
u64 res = _one(mod);
u64 A = to_montgomery_form(a, r2, m, mod);
while (n > 0) {
if (n & 1) res = _MR((u128)res * A, m, mod);
A = _MR((u128)A * A, m, mod);
n >>= 1;
}
return from_montgomery_form(res, m, mod);
}
bool is_prime(u64 n) {
if (n < 2 || ((n % 6 != 1) && (n % 6 != 5))) return (n == 2) || (n == 3);
u64 r2 = _r2(n);
u64 m = _m(n);
u64 s = ctz(n - 1);
u64 d = (n - 1) >> s;
u64 as[] = {2,325,9375,28178,450775,9780504,1795265022};
for (int i = 0; i < 7; i++) {
u64 a = as[i];
if (_MR(a, m, n) == 0) return true;
u64 res = powmod64(a, d, r2, m, n);
if (res == 1) continue;
bool ok = true;
for (u64 r = 0; r < s; r++) {
if (res == n - 1) {
ok = false;
break;
}
res = mulmod64(res, res, r2, m, n);
}
if (ok) return false;
}
return true;
}
void Main() {
i64 n = in();
for (i64 i = 0; i < n; i++) {
i64 x = in();
out(x);sp();out(is_prime(x));nl();
}
}
int main() {
Main();
return 0;
}