結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nonamaenonamae
提出日時 2021-11-21 22:39:29
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 40 ms / 9,973 ms
コード長 2,734 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 31,620 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 16:34:25
合計ジャッジ時間 1,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0