結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー akakimidoriakakimidori
提出日時 2019-04-24 02:52:41
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2,923 ms / 9,973 ms
コード長 1,318 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 30,060 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 16:08:11
合計ジャッジ時間 7,844 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1,513 ms
4,380 KB
testcase_05 AC 1,412 ms
4,384 KB
testcase_06 AC 359 ms
4,380 KB
testcase_07 AC 349 ms
4,380 KB
testcase_08 AC 354 ms
4,380 KB
testcase_09 AC 2,923 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef uint32_t u32;
typedef uint64_t u64;

u64 mul (u64 a, u64 b, u64 mod) {
  u64 ans = 0;
  while (b > 0) {
    if (b & 1) {
      ans += a;
      if (ans >= mod) ans -= mod;
    }
    a += a;
    if (a >= mod) a -= mod;
    b >>= 1;
  }
  return ans;
}

u64 mod_pow (u64 a, u64 n, u64 mod) {
  u64 t = 1;
  while (n > 0) {
    if (n & 1) t = mul (t, a, mod);
    a = mul (a, a, mod);
    n >>= 1;
  }
  return t;
}

u64 xor_shift (void) {
  static u64 x = (u64)88172645463325252;
  x ^= x << 7;
  return x ^= x >> 9;
}

u32 is_prime_miller (const u64 p) {
  if (p <= 1) return 0;
  if (p <= 3) return 1;
  if (p % 2 == 0) return 0;
  u64 d = p - 1;
  u32 s = 0;
  while (d % 2 == 0) {
    d /= 2;
    s++;
  }
  const u32 b[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
  for (u32 k = 0; k < 12 && b[k] < p; ++k) {
    u64 a = b[k];
    a = mod_pow (a, d, p);
    if (a == 1) continue;
    u32 i = 0;
    for (; i < s && a != p - 1; ++i, a = mul (a, a, p));    
    if (i >= s) return 0;
  }
  return 1;
}

void run (void) {
  u32 n;
  scanf ("%" SCNu32, &n);
  while (n--) {
    u64 x;
    scanf ("%" SCNu64, &x);
    printf ("%" PRIu64 " %" PRIu32 "\n", x, is_prime_miller (x));
  }
}

int main (void) {
  run();
  return 0;
}
0