結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー kaoru muratakaoru murata
提出日時 2021-09-12 02:22:48
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 50 ms / 9,973 ms
コード長 4,511 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 33,012 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 16:32:07
合計ジャッジ時間 1,113 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 0 ms
4,376 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 30 ms
4,380 KB
testcase_05 AC 30 ms
4,376 KB
testcase_06 AC 15 ms
4,376 KB
testcase_07 AC 15 ms
4,376 KB
testcase_08 AC 15 ms
4,376 KB
testcase_09 AC 50 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:34:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   34 |   while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
      |              ^~~~~~~~~~~~~~~~
main.c: 関数 ‘out’ 内:
main.c:43:5: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   43 |     putchar_unlocked('-');
      |     ^~~~~~~~~~~~~~~~

ソースコード

diff #

#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 ex_gcd(u64 y) {
  int i;
  u64 u = 1;
  u64 v = 0;
  u64 x = 1ULL << 63;
  for (i = 0; i < 64; i++) {
    if (u & 1){
      u = (u + y) / 2;
      v = v / 2 + x;
    } else {
      u >>= 1;
      v >>= 1;
    }
  }
  return -v;
}
static inline u64 _m(u64 mod) { return ex_gcd(mod); }
static inline u64 _r2(u64 mod) { return (u128) (i128) -1 % mod + 1; }
static inline u64 _one(u64 mod) { return -1ULL % mod + 1; }
int jacobi(i64 a, u64 n) {
  u64 t;
  int j = 1;
  while (a) {
    if (a < 0) {
      a = -a ;
      if ((n & 3) == 3) j = -j;
    }
    int ba = __builtin_ctzll(a);
    a >>= ba;
    if (((n & 7) == 3 || (n & 7) == 5) && (ba & 1)) j = -j;
    if ((a & n & 3) == 3) j = -j;
    t = a;
    a = n;
    n = t;
    a %= n;
    if (a > (i64)(n >> 1)) a -= n;
  }
  return n == 1 ? j : 0;
}
static inline u64 addmod64(u64 x, u64 y, u64 n) { return x + y >= n ? x + y - n : x + y; }
static inline u64 submod64(u64 x, u64 y, u64 n) { return x >= y  ? x - y : x - y + n; }
static inline u64 MR(u128 x, u64 m, u64 n) {
    i64 z = (x >> 64) - ((((u64)x * m) * (u128)n) >> 64);
    return z < 0 ? z + n : (u64)z;
}
static inline u64 RM(u64 x, u64 r2, u64 m, u64 n) { return MR((u128)r2 * x, m, n); }
static inline u64 mulmod64(u64 x, u64 y, u64 r2, u64 m, u64 mod) {
  return RM(MR((u128)x * y, m, mod), r2, m, mod);
}
static inline u64 powmod64(u64 a, u64 n, u64 r2, u64 m, u64 mod) {
  u64 res = 1;
  while (n > 0) {
    if (n & 1) res = mulmod64(a, res, r2, m, mod);
    a = mulmod64(a, a, r2, m, mod);
    n >>= 1;
  }
  return res;
}
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;
}

int main() {
  i64 n = in();
  for (i64 i = 0; i < n; i++) {
    i64 x = in();
    out(x);sp();out(is_prime(x));nl();
  }
  return 0;
}
0